ScrollArea
Features
- Scrollbar sits on top of the scrollable content, taking up no space.
- Scrolling is native; no underlying position movements via CSS transformations.
- Shims pointer behaviors only when interacting with the controls, so keyboard controls are unaffected.
- Supports Right to Left direction.
Installation
Install the component from your command line.
$ npm add reka-uiAnatomy
Import all parts and piece them together.
<script setup>
import { ScrollAreaRoot, ScrollAreaScrollbar, ScrollAreaThumb, ScrollAreaViewport } from 'reka-ui'
</script>
<template>
<ScrollAreaRoot>
<ScrollAreaViewport />
<ScrollAreaScrollbar orientation="horizontal">
<ScrollAreaThumb />
</ScrollAreaScrollbar>
<ScrollAreaScrollbar orientation="vertical">
<ScrollAreaThumb />
</ScrollAreaScrollbar>
<ScrollAreaCorner />
</ScrollAreaRoot>
</template>API Reference
Root
Contains all the parts of a scroll area.
Viewport
The viewport area of the scroll area.
Scrollbar
The vertical scrollbar. Add a second Scrollbar with an orientation prop to enable horizontal scrolling.
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
[data-orientation] | "vertical" | "horizontal" |
Thumb
The thumb to be used in ScrollAreaScrollbar.
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
Corner
The corner where both vertical and horizontal scrollbars meet.
Examples
Custom Scroll
Use the exposed viewport to modify / or set the scroll position outside default methods. $el on the root's template ref is the root element; it is a reactive ref under the hood, so it always points at the element currently rendered (see the element-first ref contract).
<script setup lang="ts">
import { ScrollAreaRoot, ScrollAreaScrollbar, ScrollAreaThumb, ScrollAreaViewport } from 'reka-ui'
const scrollArea = useTemplateRef('scrollArea')
function scrollToBottom() {
const viewport = scrollArea.value?.viewport
if (viewport) {
const top = scrollArea.value?.$el.scrollHeight
viewport.scrollTo({
top,
behavior: 'smooth'
})
}
}
</script>
<template>
<ScrollAreaRoot ref="scrollArea">
<ScrollAreaViewport />
<ScrollAreaScrollbar orientation="horizontal">
<ScrollAreaThumb />
</ScrollAreaScrollbar>
<ScrollAreaScrollbar orientation="vertical">
<ScrollAreaThumb />
</ScrollAreaScrollbar>
<ScrollAreaCorner />
</ScrollAreaRoot>
</template>Headless composable
useScrollArea returns the root registry (context), scrollTop / scrollTopLeft, and the root, viewport and content surfaces. Bind each surface's attrs.value to its element and register the viewport through context.
<script setup lang="ts">
import { useScrollArea } from 'reka-ui'
import { onMounted, ref } from 'vue'
const viewportEl = ref<HTMLElement>()
const scrollArea = useScrollArea()
onMounted(() => {
scrollArea.context.onViewportChange(viewportEl.value!)
// Lets the viewport scroll vertically.
scrollArea.context.onScrollbarYEnabledChange(true)
})
</script>
<template>
<div v-bind="scrollArea.root.attrs.value">
<div
ref="viewportEl"
v-bind="scrollArea.viewport.attrs.value"
style="height: 200px"
>
<div v-bind="scrollArea.content.attrs.value">
…
</div>
</div>
</div>
</template>The custom scrollbars stay in the components: their ResizeObserver sizing, pointer capture, thumb positioning and the auto / scroll / hover / glimpse visibility timers are not part of the composable, so type and scrollHideDelay only matter when the scrollbar parts render. Hide the native scrollbars yourself when you render your own. The composable is experimental and callable outside component setup.
Real-world examples
- Data Table: Compose Checkbox, DropdownMenu, ContextMenu, ScrollArea and Pagination into a sortable, selectable table.
Accessibility
In most cases, it's best to rely on native scrolling and work with the customization options available in CSS. When that isn't enough, ScrollArea provides additional customizability while maintaining the browser's native scroll behavior (as well as accessibility features, like keyboard scrolling).
Keyboard Interactions
Scrolling via keyboard is supported by default because the component relies on native scrolling. Specific keyboard interactions may differ between platforms, so we do not specify them here or add specific event listeners to handle scrolling via key events.
