Exported components
AnnotatedScrollBar AI
A vertical scroll bar with labels for the content of an existing scrolling element, such as the years of a photo gallery. Click or drag the rail, hover it to preview a position, or use the arrows, the wheel and the keyboard.
Usage
Place the bar next to the scrolling element, in a flex or grid row it fills the height of, and pass the element as scrollRef. By default the labels come from the elements with data-scroll-label.
<script lang="ts">
import { AnnotatedScrollBar } from 'fluentui-svelte';
let gallery = $state<HTMLDivElement | null>(null);
const years = Array.from({ length: 50 }, (_, index) => 2024 - index);
</script>
<div class="collection">
<div id="photos" class="gallery" bind:this={gallery}>
{#each years as year (year)}
<section data-scroll-label={year}><h3>{year}</h3></section>
{/each}
</div>
<AnnotatedScrollBar scrollRef={gallery} aria-label="Photo timeline" />
</div>
<style>
.collection { display: flex; gap: 16px; height: 320px; }
.gallery { flex: 1; overflow: auto; scrollbar-width: none; }
section { height: 240px; }
</style> Keyboard and accessibility
The bar is one tab stop. Name it with aria-label, aria-labelledby or Label. Arrow keys scroll by arrowStep, Page Up and Page Down by a viewport, and Home and End to the ends. Escape closes the detail. Keep the headings in the content: the labels do not replace them. Give the scrolling element an id: the bar points to it with aria-controls.
Labels from the content
A label starts at each element selector matches, [data-scroll-label] by default. It takes the name of its data-scroll-label, its aria-label, its first heading or its text: here each section is named after its heading. The bar follows the content as it changes.
Labels from an array
Pass labels to set the text of each label yourself, here the short form of each year. A label starts at the element its selector matches, or at its offset in pixels from the top of the content. A selector that matches nothing leaves its label out until the element appears.
Groups and subgroups
Mark the parts of a label with data-scroll-subgroup, here the months of each year. The rail shows them under their label, and the detail and screen readers name the label and the subgroups at a position, such as 2024: June, July. Months side by side share a position. hideSubgroups keeps only the labels on the rail.
Virtualized collection
A virtualized list renders only the rows in view, and the bar cannot measure the others. Give each label and subgroup an offset in pixels: with rows of 48 pixels, the number of rows before it times 48. Here 10 years hold 50 photos a month. hideSubgroups keeps the years on the rail while the detail names the month.
Custom detail
Hover the rail to preview a position without scrolling. By default the detail shows the label and the subgroups there. The detail snippet replaces it and receives the label, the subgroups and the offset in pixels; here it adds the number of photos of each year. On a touch screen, the detail shows while dragging.
Crowded labels
Forty years do not fit on the rail: the bar hides the labels that collide, keeps the first one and favors the later ones. labelSpacing sets the minimum space between labels, 4 pixels by default.
Position
Set position to left for a bar on the left of the content.
Arrows
hideArrows hides the arrow buttons. arrowStep sets the pixels scrolled by each arrow press and arrow key, 48 by default; the virtualized collection sets it to the height of a row.
Cancel a scroll
onScrolling receives the offset and the kind of each scroll the bar asks for: click, drag, arrow, wheel or keyboard. Return false to cancel it. The bar follows any other scroll of the content.
Disabled
disabled turns the bar off, and it still follows the content. The bar is also off when the content fits.
AnnotatedScrollBar
| Name | Type | Default | Description |
|---|---|---|---|
scrollRef | HTMLElement | null | The element to scroll. | |
labels | readonly AnnotatedScrollBarLabel[] | The labels. Without them, the bar reads them from the content with selector. | |
selector | string | '[data-scroll-label]' | A CSS selector of the elements in the content that start a label. |
position | 'left' | 'right' | 'right' | The side of the content the bar is on. |
arrowStep | number | 48 | Pixels scrolled per arrow press. |
labelSpacing | number | 4 | The minimum space in pixels between labels. |
hideArrows | boolean | Hides the arrow buttons. | |
hideSubgroups | boolean | Hides the subgroup labels on the rail. | |
disabled | boolean | Disables the bar. | |
detail | Snippet | The content of the tooltip that previews a position. | |
onScrolling | (offset: number, kind: 'click' | 'drag' | 'arrow' | 'wheel' | 'keyboard') => boolean | void | Called before the bar scrolls the content. Return false to cancel. | |
ref bindable | HTMLDivElement | null | The root element. | |
HTMLAttributes<HTMLDivElement> without children |

