A scrolling box with thin scrollbars that look the same on every platform. The native scrollbars are hidden, so scrolling with the wheel, touch, and keyboard still works natively, and the themed scrollbars follow it.
<script lang="ts">
import { ScrollArea } from "#lib/components/ui/scroll-area/index.js";
</script>
<ScrollArea style="height: 18rem; width: 12rem" rounded="md">
{#each tags as tag (tag)}
<div>{tag}</div>
{/each}
</ScrollArea>pnx remote0 add https://plainsv.com/r/scroll-area.jsonGive the scroll area a height or max-height through class or style, so the content can overflow it. A scrollbar shows only when its direction is enabled and the content overflows. Drag the thumb to scroll, or press the track to jump there.
Set orientation to horizontal or both to scroll sideways. Content wider than the box scrolls instead of wrapping.
<ScrollArea orientation="horizontal" style="max-width: 24rem">
<div style="display: flex; gap: 1rem">...</div>
</ScrollArea>Set type to hover, the default, to show the scrollbars while the box is hovered or scrolling, scroll to show them only while scrolling, or always to keep them shown.
<ScrollArea type="always" style="height: 10rem">...</ScrollArea>Use bind:viewport to get the scrolling element, such as to read scrollTop or call scrollTo.
<script lang="ts">
let viewport: HTMLDivElement | undefined = $state();
</script>
<ScrollArea bind:viewport style="height: 18rem">...</ScrollArea>
<button onclick={() => viewport?.scrollTo({ top: 0, behavior: "smooth" })}>Back to top</button>Set size to xs, sm, md, or lg for a 6, 8, 10, or 12 pixel scrollbar. The default is md.
<ScrollArea size="lg" type="always" style="height: 8rem">...</ScrollArea>The box is square-cornered by default; set rounded to xs through 4xl or pill to round it.
<ScrollArea rounded="lg" style="height: 8rem">...</ScrollArea>The ScrollArea props extend the native div attributes of the box.
interface Props extends CompPropsWithChild<"div"> {
/** Directions the content scrolls in; defaults to `vertical` */
orientation?: "vertical" | "horizontal" | "both";
/** `hover` shows the scrollbars while hovered or scrolling; `scroll` only while scrolling; `always` keeps them shown; defaults to `hover` */
type?: "hover" | "scroll" | "always";
/** Scrolling element, for reading or setting the scroll position; supports `bind:viewport` */
viewport?: HTMLDivElement;
/** Scrollbar thickness on the shared scale; defaults to `md` */
size?: UiSize;
/** Corner radius of the box; square when omitted. `pill` is a true pill that can clip the content, so use `3xl` or `4xl` to stay round */
rounded?: UiRounded;
}