본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Splitter (스플리터)

원문 보기 위키 갱신

인터페이스를 크기 조절 가능한 섹션으로 나눠주는 컴포넌트예요. 사용자가 패널 사이의 분할 핸들을 드래그해서 각 패널의 크기를 조절할 수 있어요.

출처: 문서

본문

구조 (Anatomy)

스플리터의 기본 구조는 다음과 같아요. Root 안에 여러 Panel과 그 사이를 잇는 ResizeTrigger를 배치해요.

<Splitter.Root>
  <Splitter.Panel />
  <Splitter.ResizeTrigger>
    <Splitter.ResizeTriggerIndicator />
  </Splitter.ResizeTrigger>
  <Splitter.Panel />
</Splitter.Root>

예제 (Examples)

기본 (Basic)

panels prop으로 패널 목록을 지정해요. 각 Splitter.Panel은 id로 식별되고, ResizeTrigger의 id로 인접한 패널 쌍을 연결해요.

import { Splitter } from '@ark-ui/react/splitter'
import styles from 'styles/splitter.module.css'

export const Basic = () => (
  <Splitter.Root className={styles.Root} panels={[{ id: 'a' }, { id: 'b' }]}>
    <Splitter.Panel className={styles.Panel} id="a">
      A
    </Splitter.Panel>
    <Splitter.ResizeTrigger className={styles.ResizeTrigger} id="a:b" aria-label="Resize">
      <Splitter.ResizeTriggerIndicator className={styles.ResizeTriggerIndicator} />
    </Splitter.ResizeTrigger>
    <Splitter.Panel className={styles.Panel} id="b">
      B
    </Splitter.Panel>
  </Splitter.Root>
)

컨텍스트 (Context)

Splitter.Context 또는 useSplitterContext 훅으로 스플리터의 API에 접근할 수 있어요. 이를 통해 패널을 프로그래밍 방식으로 크기 조절할 수 있어요:

import { Splitter } from '@ark-ui/react/splitter'
import button from 'styles/button.module.css'
import styles from 'styles/splitter.module.css'

export const Context = () => (
  <Splitter.Root className={styles.Root} panels={[{ id: 'a' }, { id: 'b' }]}>
    <Splitter.Context>
      {(splitter) => (
        <>
          <Splitter.Panel className={styles.Panel} id="a">
            <button className={button.Root} type="button" onClick={() => splitter.resizePanel('a', 10)}>
              Set to 10%
            </button>
          </Splitter.Panel>
          <Splitter.ResizeTrigger className={styles.ResizeTrigger} id="a:b" aria-label="Resize">
            <Splitter.ResizeTriggerIndicator className={styles.ResizeTriggerIndicator} />
          </Splitter.ResizeTrigger>
          <Splitter.Panel className={styles.Panel} id="b">
            <button className={button.Root} type="button" onClick={() => splitter.resizePanel('b', 10)}>
              Set to 10%
            </button>
          </Splitter.Panel>
        </>
      )}
    </Splitter.Context>
  </Splitter.Root>
)

세로 방향 (Vertical)

기본적으로 Splitter 컴포넌트는 가로 방향이에요. 세로 스플리터가 필요하다면 orientation prop을 사용해요:

import { Splitter } from '@ark-ui/react/splitter'
import styles from 'styles/splitter.module.css'

export const Vertical = () => (
  <Splitter.Root className={styles.Root} orientation="vertical" panels={[{ id: 'a' }, { id: 'b' }]}>
    <Splitter.Panel className={styles.Panel} id="a">
      A
    </Splitter.Panel>
    <Splitter.ResizeTrigger className={styles.ResizeTrigger} id="a:b" aria-label="Resize">
      <Splitter.ResizeTriggerIndicator className={styles.ResizeTriggerIndicator} />
    </Splitter.ResizeTrigger>
    <Splitter.Panel className={styles.Panel} id="b">
      B
    </Splitter.Panel>
  </Splitter.Root>
)

접을 수 있는 패널 (Collapsible Panels)

패널을 접을 수 있게 만들려면 접고 싶은 패널에 collapsible prop을 true로 설정해요. 또한 collapsedSize prop으로 패널이 접힌 상태의 크기를 설정할 수 있어요.

사이드바 레이아웃을 만들 때 유용할 수 있어요.

import { Splitter } from '@ark-ui/react/splitter'
import styles from 'styles/splitter.module.css'

export const Collapsible = () => (
  <Splitter.Root
    className={styles.Root}
    defaultSize={[15, 20]}
    panels={[
      { id: 'a', collapsible: true, collapsedSize: 5, minSize: 10, maxSize: 20 },
      { id: 'b', minSize: 50 },
    ]}
  >
    <Splitter.Panel className={styles.Panel} id="a">
      A
    </Splitter.Panel>
    <Splitter.ResizeTrigger className={styles.ResizeTrigger} id="a:b" aria-label="Resize">
      <Splitter.ResizeTriggerIndicator className={styles.ResizeTriggerIndicator} />
    </Splitter.ResizeTrigger>
    <Splitter.Panel className={styles.Panel} id="b">
      B
    </Splitter.Panel>
  </Splitter.Root>
)

여러 개의 패널 (Multiple Panels)

Splitter 컴포넌트를 여러 패널과 함께 사용하는 예제예요.

import { Splitter } from '@ark-ui/react/splitter'
import styles from 'styles/splitter.module.css'

export const MultiplePanels = () => (
  <Splitter.Root
    className={styles.Root}
    panels={[
      { id: 'a', minSize: 20 },
      { id: 'b', minSize: 40 },
      { id: 'c', minSize: 20 },
    ]}
    defaultSize={[20, 60, 20]}
  >
    <Splitter.Panel className={styles.Panel} id="a">
      A
    </Splitter.Panel>
    <Splitter.ResizeTrigger className={styles.ResizeTrigger} id="a:b" aria-label="Resize">
      <Splitter.ResizeTriggerIndicator className={styles.ResizeTriggerIndicator} />
    </Splitter.ResizeTrigger>
    <Splitter.Panel className={styles.Panel} id="b">
      B
    </Splitter.Panel>
    <Splitter.ResizeTrigger className={styles.ResizeTrigger} id="b:c" aria-label="Resize">
      <Splitter.ResizeTriggerIndicator className={styles.ResizeTriggerIndicator} />
    </Splitter.ResizeTrigger>
    <Splitter.Panel className={styles.Panel} id="c">
      C
    </Splitter.Panel>
  </Splitter.Root>
)

루트 프로바이더 (Root Provider)

스플리터를 제어하는 또 다른 방법은 RootProvider 컴포넌트와 useSplitter 훅을 사용하는 거예요. 이렇게 하면 컴포넌트 밖에서 상태와 메서드에 접근할 수 있어요.

import { Splitter, useSplitter } from '@ark-ui/react/splitter'
import styles from 'styles/splitter.module.css'

export const RootProvider = () => {
  const splitter = useSplitter({
    defaultSize: [50, 50],
    panels: [{ id: 'a' }, { id: 'b' }],
  })

  return (
    <div className="stack">
      <output>current size: {JSON.stringify(splitter.getSizes())}</output>

      <Splitter.RootProvider className={styles.Root} value={splitter}>
        <Splitter.Panel className={styles.Panel} id="a">
          A
        </Splitter.Panel>
        <Splitter.ResizeTrigger className={styles.ResizeTrigger} id="a:b" aria-label="Resize">
          <Splitter.ResizeTriggerIndicator className={styles.ResizeTriggerIndicator} />
        </Splitter.ResizeTrigger>
        <Splitter.Panel className={styles.Panel} id="b">
          B
        </Splitter.Panel>
      </Splitter.RootProvider>
    </div>
  )
}

크기 조절 표시기 (Resize Indicator)

Splitter.ResizeTriggerIndicator 컴포넌트를 사용해서 크기 조절 핸들에 시각적 표시기를 표시해요.

import { Splitter } from '@ark-ui/react/splitter'
import styles from 'styles/splitter.module.css'

export const ResizeIndicator = () => (
  <Splitter.Root className={styles.Root} panels={[{ id: 'a' }, { id: 'b' }]}>
    <Splitter.Panel className={styles.Panel} id="a">
      A
    </Splitter.Panel>
    <Splitter.ResizeTrigger className={styles.ResizeTrigger} id="a:b" aria-label="Resize">
      <Splitter.ResizeTriggerIndicator className={styles.ResizeTriggerIndicator} />
    </Splitter.ResizeTrigger>
    <Splitter.Panel className={styles.Panel} id="b">
      B
    </Splitter.Panel>
  </Splitter.Root>
)

동적 접기 (Dynamic Collapsible)

collapsePanel()과 expandPanel() 메서드를 사용해서 뷰포트 크기에 따라 패널 접기를 프로그래밍 방식으로 제어해요. 작은 화면에서 접히는 반응형 사이드바 레이아웃에 유용해요.

/** biome-ignore-all lint/correctness/useExhaustiveDependencies: intentional */

import { Splitter, useSplitter } from '@ark-ui/react/splitter'
import { useLayoutEffect, useRef, useState } from 'react'
import styles from 'styles/splitter.module.css'

export const DynamicCollapsible = () => {
  const [rootSize, setRootSize] = useState<number | null>(null)
  const ref = useRef<HTMLDivElement>(null)

  useLayoutEffect(() => {
    const handleResize = () => setRootSize(ref.current?.offsetWidth ?? null)
    handleResize()
    window.addEventListener('resize', handleResize)
    return () => window.removeEventListener('resize', handleResize)
  }, [])

  const isBelowMd = rootSize != null && rootSize < 600

  useLayoutEffect(() => {
    if (isBelowMd) splitter.collapsePanel('a')
    else splitter.expandPanel('a')
  }, [isBelowMd])

  const splitter = useSplitter({
    panels: [{ id: 'a', collapsible: isBelowMd, collapsedSize: 5, minSize: 20, maxSize: 40 }, { id: 'b' }],
    defaultSize: [15, 85],
  })

  return (
    <Splitter.RootProvider className={styles.Root} value={splitter} ref={ref}>
      <Splitter.Panel className={styles.Panel} id="a">
        A
      </Splitter.Panel>
      <Splitter.ResizeTrigger className={styles.ResizeTrigger} id="a:b" aria-label="Resize panels">
        <Splitter.ResizeTriggerIndicator className={styles.ResizeTriggerIndicator} />
      </Splitter.ResizeTrigger>
      <Splitter.Panel className={styles.Panel} id="b">
        B
      </Splitter.Panel>
    </Splitter.RootProvider>
  )
}

중첩 (Nested)

그리드 형태의 레이아웃을 만들려면 스플리터를 중첩해요. Splitter.createRegistry()를 사용해 스플리터 인스턴스 사이에 공유 레지스트리를 만들면, 가로와 세로 스플리터가 만나는 지점에서 여러 곳을 동시에 드래그할 수 있어요.

import { Splitter } from '@ark-ui/react/splitter'
import styles from 'styles/splitter.module.css'

const registry = Splitter.createRegistry()

export const Nested = () => (
  <Splitter.Root
    className={styles.Root}
    orientation="horizontal"
    panels={[{ id: 'left' }, { id: 'center' }, { id: 'right' }]}
    registry={registry}
  >
    <Splitter.Panel className={styles.Panel} id="left">
      Left
    </Splitter.Panel>
    <Splitter.ResizeTrigger className={styles.ResizeTrigger} id="left:center" aria-label="Resize">
      <Splitter.ResizeTriggerIndicator className={styles.ResizeTriggerIndicator} />
    </Splitter.ResizeTrigger>
    <Splitter.Panel className={styles.Panel} id="center">
      <Splitter.Root orientation="vertical" panels={[{ id: 'top' }, { id: 'bottom' }]} registry={registry}>
        <Splitter.Panel className={styles.Panel} id="top">
          Top
        </Splitter.Panel>
        <Splitter.ResizeTrigger className={styles.ResizeTrigger} id="top:bottom" aria-label="Resize">
          <Splitter.ResizeTriggerIndicator className={styles.ResizeTriggerIndicator} />
        </Splitter.ResizeTrigger>
        <Splitter.Panel className={styles.Panel} id="bottom">
          Bottom
        </Splitter.Panel>
      </Splitter.Root>
    </Splitter.Panel>
    <Splitter.ResizeTrigger className={styles.ResizeTrigger} id="center:right" aria-label="Resize">
      <Splitter.ResizeTriggerIndicator className={styles.ResizeTriggerIndicator} />
    </Splitter.ResizeTrigger>
    <Splitter.Panel className={styles.Panel} id="right">
      Right
    </Splitter.Panel>
  </Splitter.Root>
)

API 레퍼런스 (API Reference)

속성 (Props)

Root

Props

panels Type: PanelData[] Required: true Default Value: undefined Description: The size constraints of the panels.

asChild Type: boolean Required: false Default Value: undefined Description: Use the provided child element as the default rendered element, combining their props and behavior.

defaultSize Type: PanelSize[] Required: false Default Value: undefined Description: The initial size of the panels when rendered. Use when you don't need to control the size of the panels.

id Type: string Required: false Default Value: undefined Description: The unique identifier of the machine.

ids Type: Partial<{ root: string resizeTrigger: (id: string) => string label: (id: string) => string panel: (id: string | number) => string }> Required: false Default Value: undefined Description: The ids of the elements in the splitter. Useful for composition.

keyboardResizeBy Type: number Required: false Default Value: undefined Description: The number of pixels to resize the panel by when the keyboard is used.

nonce Type: string Required: false Default Value: undefined Description: The nonce for the injected splitter cursor stylesheet.

onCollapse Type: (details: ExpandCollapseDetails) => void Required: false Default Value: undefined Description: Function called when a panel is collapsed.

onExpand Type: (details: ExpandCollapseDetails) => void Required: false Default Value: undefined Description: Function called when a panel is expanded.

onResize Type: (details: ResizeDetails) => void Required: false Default Value: undefined Description: Function called when the splitter is resized.

onResizeEnd Type: (details: ResizeEndDetails) => void Required: false Default Value: undefined Description: Function called when the splitter resize ends.

onResizeStart Type: () => void Required: false Default Value: undefined Description: Function called when the splitter resize starts.

orientation Type: 'horizontal' | 'vertical' Required: false Default Value: "horizontal" Description: The orientation of the splitter. Can be horizontal or vertical

registry Type: SplitterRegistry Required: false Default Value: undefined Description: The splitter registry to use for multi-drag support. When provided, enables dragging at the intersection of multiple splitters.

size Type: PanelSize[] Required: false Default Value: undefined Description: The controlled size data of the panels

데이터 속성 (Data Attributes)

data-scope: splitter data-part: root data-orientation: The orientation of the splitter data-dragging: Present when in the dragging state

Panel

Props

id Type: string 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.

데이터 속성 (Data Attributes)

data-scope: splitter data-part: panel data-orientation: The orientation of the panel data-dragging: Present when in the dragging state data-id: data-index: The index of the item

Registry

Props

hitAreaMargins Type: HitAreaMargins Required: false Default Value: undefined Description: The hit area margins for resize handles. Larger margins make it easier to grab handles, especially on touch devices.

nonce Type: string Required: false Default Value: undefined Description: The nonce for the injected cursor stylesheet (for CSP compliance).

ResizeTriggerIndicator

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.

ResizeTrigger

Props

id Type: ResizeTriggerId 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.

disabled Type: boolean Required: false Default Value: undefined Description: undefined

데이터 속성 (Data Attributes)

data-scope: splitter data-part: resize-trigger data-id: data-orientation: The orientation of the resizetrigger data-focus: Present when focused data-dragging: Present when in the dragging state data-disabled: Present when disabled

RootProvider

Props

value Type: UseSplitterReturn 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.

컨텍스트 (Context)

API:

Property Type Description
dragging boolean Whether the splitter is currently being resized.
orientation `"horizontal" "vertical"`
getSizes () => number[] Returns the current sizes of the panels.
setSizes (size: PanelSize[]) => void Sets the sizes of the panels.
getItems () => SplitterItem[] Returns the items of the splitter.
getPanels () => PanelData[] Returns the panels of the splitter.
getPanelById (id: PanelId) => PanelData Returns the panel with the specified id.
getPanelSize (id: PanelId) => number Returns the size of the specified panel.
isPanelCollapsed (id: PanelId) => boolean Returns whether the specified panel is collapsed.
isPanelExpanded (id: PanelId) => boolean Returns whether the specified panel is expanded.
collapsePanel (id: PanelId) => void Collapses the specified panel.
expandPanel (id: PanelId, minSize?: number) => void Expands the specified panel.
resizePanel (id: PanelId, unsafePanelSize: number) => void Resizes the specified panel.
getLayout () => string Returns the layout of the splitter.
resetSizes VoidFunction Resets the splitter to its initial state.
getResizeTriggerState (props: ResizeTriggerProps) => ResizeTriggerState Returns the state of the resize trigger.

접근성 (Accessibility)

Window Splitter WAI-ARIA design pattern을 준수해요.

더 알아보기 (Learn more)