Scroll Area
A styleable scroll region.
Anatomy
<script>
import { ScrollArea } from '@shardsui/svelte/scroll-area'
</script>
<ScrollArea.Root>
<ScrollArea.Viewport>
<ScrollArea.Content />
</ScrollArea.Viewport>
<ScrollArea.Scrollbar>
<ScrollArea.Thumb />
</ScrollArea.Scrollbar>
<ScrollArea.Corner />
</ScrollArea.Root>Examples
Both scrollbars
When both scrollbars are visible, add <ScrollArea.Corner> to fill the gap where they meet so they never overlap.
Gradient scroll fade
Feed the viewport's overflow CSS variables into a mask-image to fade content near the edges. The fade deepens the further the user scrolls away from each edge.
.viewport {
mask-image: linear-gradient(
to bottom,
transparent 0,
black min(40px, var(--scroll-area-overflow-y-start)),
black calc(100% - min(40px, var(--scroll-area-overflow-y-end, 40px))),
transparent 100%
);
mask-repeat: no-repeat;
}The variables are written on <ScrollArea.Viewport> and inherit from there, so the viewport itself or any element inside it can read them.
They are only set once the scroll area has measured itself after mount. Until then a var() without a fallback makes the whole mask-image invalid and nothing is masked, so give each call a fallback if the fade has to render on the first paint.
var(--scroll-area-overflow-y-start, 0px);
var(--scroll-area-overflow-y-end, 40px);Combining with Tabs
When a tab list overflows, wrap <Tabs.List> in a <ScrollArea.Viewport> so the list scrolls horizontally inside the scroll area. Nest the tabs as the viewport's content and add the scrollbar parts alongside it.
<Tabs.Root value="overview">
<ScrollArea.Root>
<ScrollArea.Viewport>
<Tabs.List>
<Tabs.Tab value="overview">Overview</Tabs.Tab>
<Tabs.Indicator />
</Tabs.List>
</ScrollArea.Viewport>
<ScrollArea.Scrollbar orientation="horizontal">
<ScrollArea.Thumb />
</ScrollArea.Scrollbar>
</ScrollArea.Root>
<Tabs.Panel value="overview">...</Tabs.Panel>
</Tabs.Root>Because the overflow variables inherit, the tab list can drive its own mask fade from --scroll-area-overflow-x-start and --scroll-area-overflow-x-end.
API reference
Root
Groups all parts of the scroll area.
Renders a <div> element.
Viewport
The element that scrolls. Hides the browser's scrollbars and carries the overflow CSS variables.
Renders a <div> element.
Content
A container for the content of the scroll area. Sized to fit its children so horizontal overflow measures correctly.
Renders a <div> element.
Scrollbar
A vertical or horizontal scrollbar for the scroll area. Rendered only while its axis overflows, unless keepMounted is set. Clicking the track jumps to that position; dragging the thumb scrolls.
Renders a <div> element.
Thumb
The draggable part of the scrollbar that indicates the current scroll position. Sized from the scrollbar's thumb CSS variable, so it needs a cross-axis size of its own.
Renders a <div> element.
Corner
Fills the gap where the horizontal and vertical scrollbars meet. Rendered only while both are visible.
Renders a <div> element.