Scroll Area 문서
Scroll Area는 플랫폼과 관계없이 일관된 스크롤 영역을 제공하는 컴포넌트예요. 스크롤바가 언제 나타나고 숨는지를 세밀하게 조절해서, 어떤 환경에서도 같은 모양과 동작을 보장해 줘요.
출처: 문서
본문
아래 예시는 가로·세로 스크롤바를 모두 가진 Scroll Area예요. ScrollArea.Root가 영역을 잡고, Viewport가 내용을 스크롤하며, Scrollbar와 Thumb, Corner가 스크롤 UI를 담당해요.
<script lang="ts">
import { ScrollArea } from "bits-ui";
</script>
<ScrollArea.Root
class="border-dark-10 bg-background-alt shadow-card relative overflow-hidden rounded-[10px] border px-4 py-4"
>
<ScrollArea.Viewport class="h-full max-h-[200px] w-full max-w-[200px]">
<h4
class="text-foreground mb-4 mt-2 text-xl font-semibold leading-none tracking-[-0.01em]"
>
Scroll Area
</h4>
<p class="text-foreground-alt text-wrap text-sm leading-5">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Dignissimos
impedit rem, repellat deserunt ducimus quasi nisi voluptatem cumque
aliquid esse ea deleniti eveniet incidunt! Deserunt minus laborum
accusamus iusto dolorum. Lorem ipsum dolor sit, amet consectetur
adipisicing elit. Blanditiis officiis error minima eos fugit voluptate
excepturi eveniet dolore et, ratione impedit consequuntur dolorem hic quae
corrupti autem? Dolorem, sit voluptatum.
</p>
</ScrollArea.Viewport>
<ScrollArea.Scrollbar
orientation="vertical"
class="bg-muted hover:bg-dark-10 data-[state=visible]:animate-in data-[state=hidden]:animate-out data-[state=hidden]:fade-out-0 data-[state=visible]:fade-in-0 flex w-2.5 touch-none select-none rounded-full border-l border-l-transparent p-px transition-all duration-200 hover:w-3"
>
<ScrollArea.Thumb class="bg-muted-foreground flex-1 rounded-full" />
</ScrollArea.Scrollbar>
<ScrollArea.Scrollbar
orientation="horizontal"
class="bg-muted hover:bg-dark-10 flex h-2.5 touch-none select-none rounded-full border-t border-t-transparent p-px transition-all duration-200 hover:h-3 "
>
<ScrollArea.Thumb class="bg-muted-foreground rounded-full" />
</ScrollArea.Scrollbar>
<ScrollArea.Corner />
</ScrollArea.Root>
구조 (Structure)
Scroll Area의 기본 구조는 ScrollArea.Root가 전체를 감싸고, 그 안에 Viewport, 세로·가로 Scrollbar(+ Thumb), 그리고 Corner를 조합하는 방식이에요.
<script lang="ts">
import { ScrollArea } from "bits-ui";
</script>
<ScrollArea.Root>
<ScrollArea.Viewport>
<!-- Scrollable content here -->
</ScrollArea.Viewport>
<ScrollArea.Scrollbar orientation="vertical">
<ScrollArea.Thumb />
</ScrollArea.Scrollbar>
<ScrollArea.Scrollbar orientation="horizontal">
<ScrollArea.Thumb />
</ScrollArea.Scrollbar>
<ScrollArea.Corner />
</ScrollArea.Root>
재사용 컴포넌트 (Reusable Components)
Scroll Area를 애플리케이션 곳곳에서 쓸 계획이라면, 매번 반복해서 코드를 작성하지 않도록 재사용 가능한 컴포넌트를 만드는 걸 권장해요.
아래 예시는 몇 가지 커스텀 prop을 받아 더 유연하게 만들어진 Scroll Area 컴포넌트를 보여 줘요.
MyScrollArea.svelte
<script lang="ts">
import { ScrollArea, type WithoutChild } from "bits-ui";
type Props = WithoutChild<ScrollArea.RootProps> & {
orientation: "vertical" | "horizontal" | "both";
viewportClasses?: string;
};
let {
ref = $bindable(null),
orientation = "vertical",
viewportClasses,
children,
...restProps
}: Props = $props();
</script>
{#snippet Scrollbar({
orientation,
}: {
orientation: "vertical" | "horizontal";
})}
<ScrollArea.Scrollbar {orientation}>
<ScrollArea.Thumb />
</ScrollArea.Scrollbar>
{/snippet}
<ScrollArea.Root bind:ref {...restProps}>
<ScrollArea.Viewport class={viewportClasses}>
{@render children?.()}
</ScrollArea.Viewport>
{#if orientation === "vertical" || orientation === "both"}
{@render Scrollbar({ orientation: "vertical" })}
{/if}
{#if orientation === "horizontal" || orientation === "both"}
{@render Scrollbar({ orientation: "horizontal" })}
{/if}
<ScrollArea.Corner />
</ScrollArea.Root>
이 커스텀 컴포넌트를 다음 예시들에서 사용해서 Scroll Area의 동작을 어떻게 커스터마이즈하는지 보여 드릴게요.
Scroll Area 타입 (Scroll Area Types)
Hover
hover 타입은 Scroll Area의 기본 타입이에요. 앞서 살펴본 대표 예시가 바로 이 타입이죠. 사용자가 Scroll Area 위에 마우스를 올리고(hover), 내용이 뷰포트보다 클 때만 스크롤바를 보여 줘요.
<MyScrollArea type="hover">
<!-- ... -->
</MyScrollArea>
Scroll
scroll 타입은 사용자가 내용을 스크롤할 때 스크롤바를 보여 줘요. macOS의 동작과 비슷하죠.
<MyScrollArea type="scroll">
<!-- ... -->
</MyScrollArea>
Auto
auto 타입은 일반적인 브라우저 스크롤바처럼 동작해요. 내용이 뷰포트보다 클 때 스크롤바가 나타나며, 항상 보이게 유지돼요.
<MyScrollArea type="auto">
<!-- ... -->
</MyScrollArea>
Always
always 타입은 Scroll Area에 overflow: scroll을 설정한 것처럼 동작해요. 내용이 뷰포트보다 작아도 스크롤바가 항상 보여요. 아래 예시에서는 두 스크롤바가 모두 렌더링되도록 MyScrollArea의 orientation prop을 'both'로 설정했어요.
<MyScrollArea type="always" orientation="both">
<!-- ... -->
</MyScrollArea>
숨김 지연 커스터마이즈 (Customizing the Hide Delay)
스크롤바가 숨는 지연 시간은 scrollHideDelay prop으로 조절할 수 있어요.
<MyScrollArea scrollHideDelay={10}>
<!-- ... -->
</MyScrollArea>
API Reference
ScrollArea.Root
The container of all scroll area components. Overflow is hidden on this element to prevent double scrollbars.
| Property | Type | Description | Details |
|---|---|---|---|
type |
enum - 'hover' | 'scroll' | 'auto' | 'always' |
The type of scroll area.Default: 'hover' |
|
scrollHideDelay |
number |
The delay in milliseconds before the scroll area hides itself when using 'hover' or 'scroll' type.Default: 600 |
|
dir |
enum - 'ltr' | 'rtl' |
The reading direction of the app.Default: 'ltr' |
|
ref $bindable |
HTMLDivElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
| Data Attribute | Value | Description | Details |
|---|---|---|---|
data-scroll-area-root |
'' |
Present on the root element. |
ScrollArea.Viewport
The component which wraps the content and is responsible for computing the scroll area size.
| Property | Type | Description | Details |
|---|---|---|---|
ref $bindable |
HTMLDivElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
| Data Attribute | Value | Description | Details |
|---|---|---|---|
data-scroll-area-viewport |
'' |
Present on the viewport element. |
ScrollArea.Scrollbar
A scrollbar of the scroll area.
| Property | Type | Description | Details |
|---|---|---|---|
orientation required |
enum - 'horizontal' | 'vertical' |
The orientation of the scrollbar.Default: —— undefined |
|
forceMount |
boolean |
Whether or not to forcefully mount the content. This is useful if you want to use Svelte transitions or another animation library for the content.Default: false |
|
ref $bindable |
HTMLDivElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
| Data Attribute | Value | Description | Details |
|---|---|---|---|
data-state |
enum - 'visible' | 'hidden' |
The visibility state of the scrollbar | |
data-scroll-area-scrollbar-x |
'' |
Present on the 'horizontal' scrollbar element. |
|
data-scroll-area-scrollbar-y |
'' |
Present on the 'vertical' scrollbar element. |
ScrollArea.Thumb
A thumb of a scrollbar in the scroll area.
| Property | Type | Description | Details |
|---|---|---|---|
forceMount |
boolean |
Whether or not to forcefully mount the content. This is useful if you want to use Svelte transitions or another animation library for the content.Default: false |
|
ref $bindable |
HTMLDivElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
| Data Attribute | Value | Description | Details |
|---|---|---|---|
data-state |
enum - 'visible' | 'hidden' |
The visibility state of the scrollbar | |
data-scroll-area-thumb-x |
'' |
Present on the 'horizontal' thumb element. |
|
data-scroll-area-thumb-y |
'' |
Present on the 'vertical' thumb element. |
ScrollArea.Corner
The corner element between the X and Y scrollbars.
| Property | Type | Description | Details |
|---|---|---|---|
ref $bindable |
HTMLDivElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
| Data Attribute | Value | Description | Details |
|---|---|---|---|
data-scroll-area-corner |
'' |
Present on the corner element. |
더 알아보기 (Learn more)
Scroll Area는 어떤 플랫폼에서도 일관된 스크롤 동작을 만들고 싶을 때 사용해요. 스크롤바의 type과 scrollHideDelay로 보이는 타이밍을 조절할 수 있으니, 콘텐츠가 자주 넘치는 목록이나 대화상자(Dialog), Popover 같은 오버레이 영역에도 자연스럽게 녹여낼 수 있어요.