Scroll Area
Scroll Area (스크롤 영역)
스타일이 적용된 스크롤바를 가진 커스텀 스크롤 가능 영역 컴포넌트예요. 네이티브 스크롤바를 숨기고 원하는 스타일의 스크롤바를 렌더링하도록 설계됐어요.
출처: 문서
본문
구조 (Anatomy)
스크롤 영역의 기본 구조는 다음과 같아요. Root 안에 Viewport와 Content, 그리고 스크롤바와 모서리를 배치해요.
<ScrollArea.Root>
<ScrollArea.Viewport>
<ScrollArea.Content />
</ScrollArea.Viewport>
<ScrollArea.Scrollbar>
<ScrollArea.Thumb />
</ScrollArea.Scrollbar>
<ScrollArea.Corner />
</ScrollArea.Root>
필수 스타일 (Required style)
스크롤 영역이 네이티브 스크롤바를 숨기려면 ScrollArea.Viewport 요소에 다음 스타일이 필요하다는 점에 유의해야 해요:
[data-scope='scroll-area'][data-part='viewport'] {
scrollbar-width: none;
&::-webkit-scrollbar {
display: none;
}
}
예제 (Examples)
기본 (Basic)
커스텀 스크롤바를 가진 기본 스크롤 가능 영역을 만들어요.
import { ScrollArea } from '@ark-ui/react/scroll-area'
import styles from 'styles/scroll-area.module.css'
export const Basic = () => (
<ScrollArea.Root className={styles.Root}>
<ScrollArea.Viewport className={styles.Viewport}>
<ScrollArea.Content className={styles.Content}>
<p className={styles.Paragraph}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium,
totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt
explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur
magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia
dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et
dolore magnam aliquam quaerat voluptatem.
</p>
</ScrollArea.Content>
</ScrollArea.Viewport>
<ScrollArea.Scrollbar className={styles.Scrollbar}>
<ScrollArea.Thumb className={styles.Thumb} />
</ScrollArea.Scrollbar>
<ScrollArea.Corner className={styles.Corner} />
</ScrollArea.Root>
)
가로 스크롤 (Horizontal)
스크롤 영역을 가로 방향 전용으로 구성해요.
import { ScrollArea } from '@ark-ui/react/scroll-area'
import styles from 'styles/scroll-area.module.css'
export const Horizontal = () => (
<ScrollArea.Root className={styles.Root} style={{ height: 'auto' }}>
<ScrollArea.Viewport className={styles.Viewport}>
<ScrollArea.Content className={styles.Content}>
<p className={styles.Paragraph} style={{ width: '50vw' }}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
</p>
</ScrollArea.Content>
</ScrollArea.Viewport>
<ScrollArea.Scrollbar orientation="horizontal" className={styles.Scrollbar}>
<ScrollArea.Thumb className={styles.Thumb} />
</ScrollArea.Scrollbar>
<ScrollArea.Corner className={styles.Corner} />
</ScrollArea.Root>
)
양방향 (Both Directions)
가로와 세로 양방향 스크롤을 활성화해요.
import { ScrollArea } from '@ark-ui/react/scroll-area'
import styles from 'styles/scroll-area.module.css'
export const BothDirections = () => (
<ScrollArea.Root className={styles.Root} style={{ height: '12rem' }}>
<ScrollArea.Viewport className={styles.Viewport}>
<ScrollArea.Content className={styles.Content}>
<p className={styles.Paragraph} style={{ width: '50vw' }}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
</p>
<p className={styles.Paragraph} style={{ width: '50vw' }}>
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem
aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.
Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni
dolores eos qui ratione voluptatem sequi nesciunt.
</p>
<p className={styles.Paragraph} style={{ width: '50vw' }}>
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti
atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique
sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
</p>
</ScrollArea.Content>
</ScrollArea.Viewport>
<ScrollArea.Scrollbar orientation="vertical" className={styles.Scrollbar}>
<ScrollArea.Thumb className={styles.Thumb} />
</ScrollArea.Scrollbar>
<ScrollArea.Scrollbar orientation="horizontal" className={styles.Scrollbar}>
<ScrollArea.Thumb className={styles.Thumb} />
</ScrollArea.Scrollbar>
<ScrollArea.Corner className={styles.Corner} />
</ScrollArea.Root>
)
중첩 (Nested)
복잡한 레이아웃을 위해 스크롤 영역은 서로 중첩될 수 있어요.
import { ScrollArea } from '@ark-ui/react/scroll-area'
import styles from 'styles/scroll-area.module.css'
export const Nested = () => (
<ScrollArea.Root className={styles.Root} style={{ height: '12rem' }}>
<ScrollArea.Viewport className={styles.Viewport}>
<ScrollArea.Content className={styles.Content}>
<p className={styles.Paragraph}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.
</p>
<ScrollArea.Root className={styles.Root} style={{ height: '8rem', width: '100%' }}>
<ScrollArea.Viewport className={styles.Viewport}>
<ScrollArea.Content className={styles.Content}>
<p className={styles.Paragraph}>
This is a nested scroll area. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit
voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore
veritatis et quasi architecto beatae vitae dicta sunt explicabo.
</p>
</ScrollArea.Content>
</ScrollArea.Viewport>
<ScrollArea.Scrollbar orientation="vertical" className={styles.Scrollbar}>
<ScrollArea.Thumb className={styles.Thumb} />
</ScrollArea.Scrollbar>
<ScrollArea.Corner className={styles.Corner} />
</ScrollArea.Root>
</ScrollArea.Content>
</ScrollArea.Viewport>
<ScrollArea.Scrollbar orientation="vertical" className={styles.Scrollbar}>
<ScrollArea.Thumb className={styles.Thumb} />
</ScrollArea.Scrollbar>
<ScrollArea.Corner className={styles.Corner} />
</ScrollArea.Root>
)
API 레퍼런스 (API Reference)
속성 (Props)
Root
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
ids
Type: Partial<{ root: string; viewport: string; content: string; scrollbar: string; thumb: string }>
Required: false
Default Value: undefined
Description: The ids of the scroll area elements
데이터 속성 (Data Attributes)
data-scope: scroll-area
data-part: root
data-overflow-x: Present when the content overflows the x-axis
data-overflow-y: Present when the content overflows the y-axis
Content
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: scroll-area
data-part: content
data-overflow-x: Present when the content overflows the x-axis
data-overflow-y: Present when the content overflows the y-axis
Corner
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: scroll-area
data-part: corner
data-hover: Present when hovered
data-state: "hidden" | "visible"
data-overflow-x: Present when the content overflows the x-axis
data-overflow-y: Present when the content overflows the y-axis
RootProvider
Props
value
Type: UseScrollAreaReturn
Required: true
Default Value: undefined
Description: undefined
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
Scrollbar
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
orientation
Type: Orientation
Required: false
Default Value: undefined
Description: undefined
데이터 속성 (Data Attributes)
data-scope: scroll-area
data-part: scrollbar
data-orientation: The orientation of the scrollbar
data-scrolling: Present when scrolling
data-hover: Present when hovered
data-dragging: Present when in the dragging state
data-overflow-x: Present when the content overflows the x-axis
data-overflow-y: Present when the content overflows the y-axis
Thumb
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: scroll-area
data-part: thumb
data-orientation: The orientation of the thumb
data-hover: Present when hovered
data-dragging: Present when in the dragging state
Viewport
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: scroll-area
data-part: viewport
data-at-top: Present when scrolled to the top edge
data-at-bottom: Present when scrolled to the bottom edge
data-at-left: Present when scrolled to the left edge
data-at-right: Present when scrolled to the right edge
data-overflow-x: Present when the content overflows the x-axis
data-overflow-y: Present when the content overflows the y-axis
컨텍스트 (Context)
API:
| Property | Type | Description |
|---|---|---|
isAtTop |
boolean |
Whether the scroll area is at the top |
isAtBottom |
boolean |
Whether the scroll area is at the bottom |
isAtLeft |
boolean |
Whether the scroll area is at the left |
isAtRight |
boolean |
Whether the scroll area is at the right |
hasOverflowX |
boolean |
Whether the scroll area has horizontal overflow |
hasOverflowY |
boolean |
Whether the scroll area has vertical overflow |
getScrollProgress |
() => Point |
Get the scroll progress as values between 0 and 1 |
scrollToEdge |
(details: ScrollToEdgeDetails) => void |
Scroll to the edge of the scroll area |
scrollTo |
(details: ScrollToDetails) => void |
Scroll to specific coordinates |
getScrollbarState |
(props: ScrollbarProps) => ScrollbarState |
Returns the state of the scrollbar |