OnThisPage
OnThisPage links to headings and shows the visible span of a long article. the reading-position rail also works as a range control, so someone can move through the content without aiming at the scrollbar.
Choose an article
Pick one article you have time to finish today.
Keep a note
Write down the detail you want to remember.
usage
<script lang="ts">
import { OnThisPage } from '@ciabi/loafkit-ui';
let scrollContainer = $state<HTMLDivElement>();
let content = $state<HTMLDivElement>();
const headings = [
{ id: 'reading-plan-start', text: 'Choose an article' },
{ id: 'reading-plan-notes', text: 'Keep a note' }
];
</script>
<OnThisPage {headings} {scrollContainer} {content} title="Reading plan" />
<div bind:this={scrollContainer} style="max-height: 14rem; overflow: auto;">
<div bind:this={content}>
<h2 id="reading-plan-start">Choose an article</h2>
<p style="min-height: 12rem;">Pick one article you have time to finish today.</p>
<h2 id="reading-plan-notes">Keep a note</h2>
<p style="min-height: 12rem;">Write down the detail you want to remember.</p>
</div>
</div>the small example uses an explicit scroll container. in a full Loaf shell, look up #loaf after mounting and pass that element instead. the default is the document element, which is only right when the document itself scrolls.
connect the reading area
the IDs in your headings data must already exist in the rendered document. OnThisPage doesn’t create headings for you.
navigation
selecting a heading updates the hash, scrolls it into view and focuses it. the range control supports arrows, Page Up and Page Down, Home and End. it is disabled when there is no scroll range.
when the heading list is height-limited, OnThisPage keeps the visible section in view as you scroll the reading area. the list still scrolls independently when you explore it.
API reference
OnThisPage
headings-
headings to link to; their IDs must already exist in the document.
scrollContainer-
the element that scrolls; when unset, the document element is used.
content-
content element to observe for size changes.
title- Default:
'On this page'the visible heading.
viewport-
returns visible top/bottom bounds when chrome reduces the reading area.
class- Default:
''extra classes on the component root.
style-
inline styles on the component root.
PageHeading
one heading supplied to OnThisPage.