Scrollbar
Wrap scrollable content to replace the browser's native scrollbars with consistently styled thin, rounded, translucent bars. Colors come from theme variables and automatically become lighter in the dark theme.
Import
import { HuiScrollbar } from '@nscode/handy-ui'
Basic Usage
height sets the area's height. Scrollbars appear when content overflows. They are hidden by default, appear on hover, scrolling, or dragging, and fade out one second after the pointer leaves or activity stops. Drag the thumb, or click the track to move it to that position; keep holding to continue dragging.
<script setup lang="ts">
import { HuiScrollbar } from '@nscode/handy-ui'
const items = Array.from({ length: 30 }, (_, i) => `第 ${i + 1} 行内容`)
</script>
<template>
<HuiScrollbar :height="200" class="box">
<p v-for="item in items" :key="item" class="row">
{{ 'Row ' + item.split(' ')[1] + ' content' }}
</p>
</HuiScrollbar>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.row {
margin: 0;
padding: var(--hui-spacing-2) var(--hui-spacing-3);
border-bottom: 1px solid var(--hui-border-color-lighter);
}
</style>
Horizontal Scrolling
A horizontal scrollbar appears when content exceeds the area's width. The content container is a block element with the area's width by default. For horizontally arranged content, use view-style to set width: max-content, allowing the thumb to update when the content grows wider.
<script setup lang="ts">
import { HuiScrollbar } from '@nscode/handy-ui'
const cards = Array.from({ length: 20 }, (_, i) => i + 1)
const viewStyle = {
display: 'flex',
gap: '12px',
width: 'max-content',
padding: '12px 12px 16px'
}
</script>
<template>
<HuiScrollbar :view-style="viewStyle" class="box">
<div v-for="card in cards" :key="card" class="card">Card {{ card }}</div>
</HuiScrollbar>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.card {
display: flex;
align-items: center;
justify-content: center;
width: 120px;
height: 80px;
border-radius: var(--hui-border-radius);
background: var(--hui-fill-color);
color: var(--hui-text-color-secondary);
}
</style>
Maximum Height
max-height sets an upper height limit. Short content uses only the height it needs; overflowing content becomes scrollable. The thumb updates automatically as content is added or removed.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiScrollbar, HuiSpace } from '@nscode/handy-ui'
const count = ref(3)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton type="primary" icon="plus" @click="count++">Add a row</HuiButton>
<HuiButton icon="minus" :disabled="count === 0" @click="count--">One fewer row</HuiButton>
<span>Total {{ count }} rows; scrollbars appear above 200px</span>
</HuiSpace>
<HuiScrollbar :max-height="200" class="box">
<p v-for="i in count" :key="i" class="row">Row {{ i }} content</p>
</HuiScrollbar>
</HuiSpace>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.row {
margin: 0;
padding: var(--hui-spacing-2) var(--hui-spacing-3);
border-bottom: 1px solid var(--hui-border-color-lighter);
}
</style>
Filling the Parent
Set height to '100%' to fill a parent with a defined height. The panel below is 240px tall, with the area below its header used for scrolling:
<script setup lang="ts">
import { HuiScrollbar } from '@nscode/handy-ui'
const items = Array.from({ length: 30 }, (_, i) => `第 ${i + 1} 条通知`)
</script>
<template>
<div class="panel">
<div class="panel__header">Notifications</div>
<div class="panel__body">
<HuiScrollbar height="100%">
<p v-for="item in items" :key="item" class="row">
{{ 'Notification ' + item.split(' ')[1] }}
</p>
</HuiScrollbar>
</div>
</div>
</template>
<style scoped>
.panel {
display: flex;
flex-direction: column;
height: 240px;
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.panel__header {
padding: var(--hui-spacing-2) var(--hui-spacing-3);
border-bottom: 1px solid var(--hui-border-color-light);
font-weight: 600;
}
.panel__body {
flex: 1;
min-height: 0;
}
.row {
margin: 0;
padding: var(--hui-spacing-2) var(--hui-spacing-3);
}
</style>
Always Visible
always keeps scrollbars visible without fading out. A scrollbar is still omitted in any direction where the content fits.
<script setup lang="ts">
import { HuiScrollbar } from '@nscode/handy-ui'
const items = Array.from({ length: 20 }, (_, i) => `第 ${i + 1} 行`)
</script>
<template>
<HuiScrollbar always :height="200" class="box">
<div class="content">
<p v-for="item in items" :key="item" class="row">
{{ 'Row ' + item.split(' ')[1] }}: this row is wider than the area, enabling horizontal
scrolling
</p>
</div>
</HuiScrollbar>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.content {
width: 720px;
padding: var(--hui-spacing-3) var(--hui-spacing-3) var(--hui-spacing-4);
}
.row {
margin: 0 0 var(--hui-spacing-2);
}
</style>
Minimum Thumb Size
With very long content, a proportional thumb can become difficult to drag. min-size sets its minimum length in pixels, defaulting to 20. Each area below contains 1000 rows:
<script setup lang="ts">
import { HuiScrollbar, HuiSpace } from '@nscode/handy-ui'
const items = Array.from({ length: 1000 }, (_, i) => `第 ${i + 1} 行内容`)
</script>
<template>
<HuiSpace fill align="start">
<div>
<p class="label">Default (20px)</p>
<HuiScrollbar always :height="200" class="box">
<p v-for="item in items" :key="item" class="row">
{{ 'Row ' + item.split(' ')[1] + ' content' }}
</p>
</HuiScrollbar>
</div>
<div>
<p class="label">min-size="60"</p>
<HuiScrollbar always :height="200" :min-size="60" class="box">
<p v-for="item in items" :key="item" class="row">
{{ 'Row ' + item.split(' ')[1] + ' content' }}
</p>
</HuiScrollbar>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin: 0 0 var(--hui-spacing-2);
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.row {
margin: 0;
padding: var(--hui-spacing-1) var(--hui-spacing-3);
}
</style>
Scrolling to a Position
Call methods through the ref:
scrollTo(options): pass{ top, left, behavior }.behavior: 'smooth'scrolls smoothly. Providing onlytoporleftleaves the other axis unchanged.scrollTo(x, y): scroll directly to horizontalxand verticaly.setScrollTop(value)andsetScrollLeft(value): change a single axis.wrapRefis the scrollable element; read dimensions such asscrollHeightfrom it.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiScrollbar, HuiSpace } from '@nscode/handy-ui'
import type { ScrollbarExpose } from '@nscode/handy-ui'
const scrollbarRef = ref<ScrollbarExpose>()
const items = Array.from({ length: 40 }, (_, i) => `第 ${i + 1} 行`)
const toTopLeft = () => scrollbarRef.value?.scrollTo({ top: 0, left: 0, behavior: 'smooth' })
const toBottom = () => {
const wrap = scrollbarRef.value?.wrapRef
if (wrap) scrollbarRef.value?.scrollTo({ top: wrap.scrollHeight, behavior: 'smooth' })
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton icon="top" @click="toTopLeft">Scroll smoothly to the top-left</HuiButton>
<HuiButton icon="arrow-down" @click="toBottom">Scroll smoothly to the bottom</HuiButton>
<HuiButton @click="scrollbarRef?.scrollTo(200, 300)">scrollTo(200, 300)</HuiButton>
<HuiButton @click="scrollbarRef?.setScrollTop(400)">setScrollTop(400)</HuiButton>
<HuiButton @click="scrollbarRef?.setScrollLeft(150)">setScrollLeft(150)</HuiButton>
</HuiSpace>
<HuiScrollbar ref="scrollbarRef" :height="220" class="box">
<div class="content">
<p v-for="item in items" :key="item" class="row">
{{ 'Row ' + item.split(' ')[1] }}: this row is wider than the area, allowing the
horizontal methods to be demonstrated
</p>
</div>
</HuiScrollbar>
</HuiSpace>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.content {
width: 900px;
padding: var(--hui-spacing-3) var(--hui-spacing-3) var(--hui-spacing-4);
}
.row {
margin: 0 0 var(--hui-spacing-2);
}
</style>
Scroll Events
The scroll event receives { scrollTop, scrollLeft }. Scrolling by wheel, thumb dragging, track clicks, or method calls all emits the event.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiScrollbar, HuiSpace } from '@nscode/handy-ui'
import type { ScrollbarScrollEvent } from '@nscode/handy-ui'
const scrollTop = ref(0)
const scrollLeft = ref(0)
const items = Array.from({ length: 40 }, (_, i) => `第 ${i + 1} 行`)
const handleScroll = (event: ScrollbarScrollEvent) => {
scrollTop.value = Math.round(event.scrollTop)
scrollLeft.value = Math.round(event.scrollLeft)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<span>scrollTop:{{ scrollTop }}, scrollLeft:{{ scrollLeft }}</span>
<HuiScrollbar :height="200" class="box" @scroll="handleScroll">
<div class="content">
<p v-for="item in items" :key="item" class="row">
{{ 'Row ' + item.split(' ')[1] }}: try scrolling down and to the right
</p>
</div>
</HuiScrollbar>
</HuiSpace>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.content {
width: 800px;
padding: var(--hui-spacing-3) var(--hui-spacing-3) var(--hui-spacing-4);
}
.row {
margin: 0 0 var(--hui-spacing-2);
}
</style>
Combine scroll, setScrollTop, and wrapRef to synchronize a slider with the scroll position in both directions:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiScrollbar, HuiSlider, HuiSpace } from '@nscode/handy-ui'
import type { ScrollbarExpose, ScrollbarScrollEvent } from '@nscode/handy-ui'
const scrollbarRef = ref<ScrollbarExpose>()
const percent = ref(0)
const items = Array.from({ length: 50 }, (_, i) => `第 ${i + 1} 行内容`)
const maxScrollTop = () => {
const wrap = scrollbarRef.value?.wrapRef
return wrap ? wrap.scrollHeight - wrap.clientHeight : 0
}
// Scroll the content when the slider is dragged
const handleSlider = (value: number | number[]) => {
percent.value = Array.isArray(value) ? value[0] : value
scrollbarRef.value?.setScrollTop((maxScrollTop() * percent.value) / 100)
}
// Synchronize the slider when the content scrolls
const handleScroll = ({ scrollTop }: ScrollbarScrollEvent) => {
const max = maxScrollTop()
percent.value = max > 0 ? Math.round((scrollTop / max) * 100) : 0
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSlider :model-value="percent" @update:model-value="handleSlider" />
<HuiScrollbar ref="scrollbarRef" :height="200" class="box" @scroll="handleScroll">
<p v-for="item in items" :key="item" class="row">
{{ 'Row ' + item.split(' ')[1] + ' content' }}
</p>
</HuiScrollbar>
</HuiSpace>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.row {
margin: 0;
padding: var(--hui-spacing-2) var(--hui-spacing-3);
border-bottom: 1px solid var(--hui-border-color-lighter);
}
</style>
Native Scrollbars
native uses browser scrollbars instead of the custom bars, standardizing only their color and thickness. Native scrollbars remain visible and take up content space. From left to right, the examples show custom scrollbars (the default), native, and browser defaults without Scrollbar.
<script setup lang="ts">
import { HuiScrollbar, HuiSpace } from '@nscode/handy-ui'
const items = Array.from({ length: 30 }, (_, i) => `第 ${i + 1} 行内容`)
</script>
<template>
<HuiSpace fill align="start">
<div>
<p class="label">Custom (default)</p>
<HuiScrollbar :height="200" class="box">
<p v-for="item in items" :key="item" class="row">
{{ 'Row ' + item.split(' ')[1] + ' content' }}
</p>
</HuiScrollbar>
</div>
<div>
<p class="label">native</p>
<HuiScrollbar native :height="200" class="box">
<p v-for="item in items" :key="item" class="row">
{{ 'Row ' + item.split(' ')[1] + ' content' }}
</p>
</HuiScrollbar>
</div>
<div>
<p class="label">Browser default</p>
<div class="box plain">
<p v-for="item in items" :key="item" class="row">
{{ 'Row ' + item.split(' ')[1] + ' content' }}
</p>
</div>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin: 0 0 var(--hui-spacing-2);
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.plain {
height: 200px;
overflow: auto;
}
.row {
margin: 0;
padding: var(--hui-spacing-2) var(--hui-spacing-3);
border-bottom: 1px solid var(--hui-border-color-lighter);
}
</style>
Content Container
The component has two layers: the scrollable area (hui-scrollbar__wrap) and its content container (hui-scrollbar__view). wrap-class and wrap-style apply to the scrollable area; view-class and view-style apply to the content container. tag changes the content container's tag. Below it is rendered as a <ul>, with list items in the default slot:
<script setup lang="ts">
import { HuiScrollbar } from '@nscode/handy-ui'
const fruits = ['苹果', '香蕉', '橙子', '葡萄', '西瓜', '草莓', '芒果', '菠萝', '樱桃', '桃子']
const wrapStyle = { background: 'var(--hui-fill-color-light)' }
const viewStyle = { margin: '0', padding: '8px 12px 8px 32px' }
</script>
<template>
<HuiScrollbar :height="160" tag="ul" :wrap-style="wrapStyle" :view-style="viewStyle" class="box">
<li v-for="fruit in fruits" :key="fruit" class="item">
{{
{
苹果: 'Apple',
香蕉: 'Banana',
橙子: 'Orange',
葡萄: 'Grape',
西瓜: 'Watermelon',
草莓: 'Strawberry',
芒果: 'Mango',
菠萝: 'Pineapple',
樱桃: 'Cherry',
桃子: 'Peach'
}[fruit]
}}
</li>
</HuiScrollbar>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.item {
padding: var(--hui-spacing-1) 0;
}
</style>
Keyboard Scrolling
The bars themselves have aria-hidden="true" and cannot receive focus, keeping them out of screen-reader navigation. For keyboard scrolling, set tabindex="0" on the scrollable area. After Tab focus, use arrow keys, PageUp, PageDown, Home, End, or Space to scroll. Focus displays the bars and a focus outline. aria-label names the area and adds role="region" to it.
<script setup lang="ts">
import { HuiScrollbar } from '@nscode/handy-ui'
const items = Array.from({ length: 30 }, (_, i) => `第 ${i + 1} 条公告`)
</script>
<template>
<HuiScrollbar :height="200" tabindex="0" aria-label="Announcements" class="box">
<p v-for="item in items" :key="item" class="row">{{ 'Announcement ' + item.split(' ')[1] }}</p>
</HuiScrollbar>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.row {
margin: 0;
padding: var(--hui-spacing-2) var(--hui-spacing-3);
border-bottom: 1px solid var(--hui-border-color-lighter);
}
</style>
Custom Appearance
The following CSS variables control thickness, edge spacing, and colors. Override them through the component's style or an ancestor class. Native scrollbars also use these variables. Public variables are not redefined on the component root; the example deliberately sets them on an ancestor so nested scrollbars inherit the same settings. The hover variable is named --hui-scrollbar-thumb-color-hover; no fallback for the old name is retained.
| Variable | Default | Description |
|---|---|---|
--hui-scrollbar-size | 6px | Scrollbar thickness |
--hui-scrollbar-gap | 2px | Gap from the area's edge |
--hui-scrollbar-thumb-color | var(--hui-text-color-quaternary) | Thumb color |
--hui-scrollbar-thumb-color-hover | var(--hui-text-color-tertiary) | Thumb color on hover or drag |
<script setup lang="ts">
import { HuiScrollbar } from '@nscode/handy-ui'
const items = Array.from({ length: 30 }, (_, i) => `第 ${i + 1} 行内容`)
</script>
<template>
<div class="primary">
<HuiScrollbar always :height="200" class="box">
<p v-for="item in items" :key="item" class="row">
{{ 'Row ' + item.split(' ')[1] + ' content' }}
</p>
</HuiScrollbar>
</div>
</template>
<style scoped>
.box {
border: 1px solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
.primary {
--hui-scrollbar-size: 10px;
--hui-scrollbar-thumb-color: var(--hui-color-primary-light-5);
--hui-scrollbar-thumb-color-hover: var(--hui-color-primary);
}
.row {
margin: 0;
padding: var(--hui-spacing-2) var(--hui-spacing-3);
border-bottom: 1px solid var(--hui-border-color-lighter);
}
</style>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| height | number | string | — | Height; numbers use pixels and strings are used as provided, such as '50vh' or '100%'. Percentages require a parent with a defined height |
| maxHeight | number | string | — | Maximum height in pixels for numeric values. Short content uses its own height; overflow becomes scrollable |
| always | boolean | false | Keep scrollbars visible. By default, they appear only on hover, scrolling, dragging, or keyboard focus |
| minSize | number | 20 | Minimum thumb length in pixels. Non-numeric or negative values fall back to 20 |
| native | boolean | false | Use native browser scrollbars with standardized color and thickness, without the custom bars |
| wrapClass | ClassValue | — | Classes for the scrollable area (hui-scrollbar__wrap) |
| wrapStyle | StyleValue | — | Styles for the scrollable area |
| viewClass | ClassValue | — | Classes for the content container (hui-scrollbar__view) |
| viewStyle | StyleValue | — | Styles for the content container |
| tag | string | 'div' | Tag for the content container |
| tabindex | number | string | — | Scrollable area's tabindex. Set to 0 for Tab focus and scrolling with arrow keys and other scrolling keys |
| ariaLabel | string | — | Accessible name for the scrollable area; adds role="region" when provided |
Events
| Event | Callback arguments | Description |
|---|---|---|
| scroll | (event: ScrollbarScrollEvent) | Emitted on scrolling with { scrollTop, scrollLeft }, including scrolling caused by dragging, track clicks, or method calls |
Slots
| Slot | Description |
|---|---|
| default | Scrollable content, placed in the content container |
Methods
| Method | Parameters | Description |
|---|---|---|
| scrollTo | (options: ScrollToOptions) or (x, y) | Scroll to a position. Options accept behavior: 'smooth'; providing one axis leaves the other unchanged |
| setScrollTop | (value: number) | Set the vertical scroll position in pixels |
| setScrollLeft | (value: number) | Set the horizontal scroll position in pixels |
| update | — | Recalculate thumb size and position. Size changes are normally handled automatically |
| wrapRef | — | An HTMLElement reference rather than a method; read dimensions such as scrollHeight from it |
Types
import type {
ScrollbarProps,
ScrollbarEmits,
ScrollbarScrollEvent,
ScrollbarScrollToOptions,
ScrollbarExpose
} from '@nscode/handy-ui'
interface ScrollbarScrollEvent {
scrollTop: number
scrollLeft: number
}
// Same as DOM ScrollToOptions: { left?: number; top?: number; behavior?: 'auto' | 'smooth' | 'instant' }
type ScrollbarScrollToOptions = ScrollToOptions
interface ScrollbarExpose {
wrapRef: HTMLElement | undefined
scrollTo: {
(options: ScrollbarScrollToOptions): void
(x: number, y: number): void
}
setScrollTop: (value: number) => void
setScrollLeft: (value: number) => void
update: () => void
}