Splitter
Splitter (스플리터)
Splitter는 드래그 가능한 핸들로 분리된 크기 조절 가능한 패널 집합이에요. 코드 에디터의 레이아웃처럼 패널을 자유롭게 나누고 크기를 조절할 수 있어요.
출처: Nuxt UI
본문
Splitter 컴포넌트를 사용해 드래그 가능한 핸들로 분리된 크기 조절 가능한 패널 목록을 표시할 수 있어요.
<script setup lang="ts">
import type { SplitterItem } from '@nuxt/ui'
const card = 'bg-elevated/50 border border-default rounded-xl items-center justify-center text-muted font-medium'
const items: SplitterItem[] = [
{ slot: 'left', minSize: 15, defaultSize: 25, class: card },
{ slot: 'main', minSize: 30, defaultSize: 50, class: card },
{ slot: 'right', minSize: 15, defaultSize: 25, class: card }
]
</script>
<template>
<div class="w-full h-96">
<USplitter id="splitter-example" :items="items">
<template #left>
Left
</template>
<template #main>
Main
</template>
<template #right>
Right
</template>
</USplitter>
</div>
</template>
[!NOTE]
Splitter는 컨테이너의 높이를 가득 채우므로, 부모 요소에 높이가 정의되어 있는지 확인하세요.
항목 (Items)
items prop에 다음 속성들을 가진 객체 배열을 전달할 수 있어요:
defaultSize?: numberminSize?: numbermaxSize?: numbercollapsible?: booleancollapsedSize?: numbersizeUnit?: '%' | 'px'order?: numberid?: stringslot?: stringclass?: anyui?: { panel?: ClassNameValue }
slot 키로 패널의 내용을 채우고 class 키로 스타일을 지정해요. slot 키가 없는 항목은 panel-{index} 슬롯으로 대체돼요. 크기는 기본적으로 백분율이며, 픽셀 값을 사용하려면 항목에 sizeUnit: 'px'를 설정하세요.
[!CAUTION]
서버에서 렌더링할 때는
idprop을 설정하고 모든 항목에defaultSize를 주거나 아예 주지 않아야 해요. 그렇지 않으면 id가 자동 생성되어 서버와 클라이언트가 달라질 수 있고, 이는 하이드레이션 시 레이아웃을 깨뜨릴 수 있어요.defaultSize가 없는 항목은 서버에서 균등한 비율로 대체되므로, 두 방식을 섞으면 하이드레이션 후 패널이 점프해요. 픽셀 크기는 클라이언트에서 측정되므로 항상 조금씩 달라질 수 있어요.
<script setup lang="ts">
import type { SplitterItem } from '@nuxt/ui'
const items = ref<SplitterItem[]>([
{
slot: "sidebar",
minSize: 15,
maxSize: 40,
defaultSize: 25,
class: "bg-elevated/50 border border-default rounded-xl items-center justify-center text-muted font-medium"
},
{
slot: "main",
defaultSize: 75,
class: "bg-elevated/50 border border-default rounded-xl items-center justify-center text-muted font-medium"
}
])
</script>
<template>
<USplitter id="splitter-items" :items="items">
<template #sidebar>
Sidebar
</template>
<template #main>
Main
</template></USplitter>
</template>
방향 (Orientation)
orientation prop을 사용해 스플리터의 방향을 변경할 수 있어요. 기본값은 horizontal이에요.
<script setup lang="ts">
import type { SplitterItem } from '@nuxt/ui'
const items = ref<SplitterItem[]>([
{
slot: "first",
class: "bg-elevated/50 border border-default rounded-xl items-center justify-center text-muted font-medium"
},
{
slot: "second",
class: "bg-elevated/50 border border-default rounded-xl items-center justify-center text-muted font-medium"
}
])
</script>
<template>
<USplitter id="splitter-orientation" orientation="vertical" :items="items">
<template #first>
First
</template>
<template #second>
Second
</template></USplitter>
</template>
예제 (Examples)
접을 수 있는 패널 (With collapsible panel)
항목에 collapsible: true를 설정하면 minSize보다 더 접을 수 있어요. collapsedSize를 사용하면 접혔을 때 패널 일부를 계속 표시할 수 있어요. 패널 슬롯은 collapsed, collapse, expand를 노출하므로 프로그래밍 방식으로 제어할 수 있고, collapse, expand, resize 이벤트는 패널 인덱스와 함께 발생해요.
<script setup lang="ts">
import type { SplitterItem } from '@nuxt/ui'
const items: SplitterItem[] = [
{ slot: 'sidebar', sizeUnit: 'px', minSize: 150, defaultSize: 250, collapsible: true, collapsedSize: 48, class: 'bg-elevated/50 border border-default rounded-xl' },
{ slot: 'main', class: 'bg-elevated/50 border border-default rounded-xl items-center justify-center text-muted font-medium' }
]
</script>
<template>
<div class="w-full h-96">
<USplitter id="splitter-collapsible-example" :items="items">
<template #sidebar="{ collapsed, collapse, expand }">
<div class="flex-1 flex items-center justify-center p-2">
<UButton
:icon="collapsed ? 'i-lucide-panel-left-open' : 'i-lucide-panel-left-close'"
:label="collapsed ? undefined : 'Collapse'"
:aria-label="collapsed ? 'Expand' : undefined"
color="neutral"
variant="subtle"
@click="collapsed ? expand() : collapse()"
/>
</div>
</template>
<template #main>
Main
</template>
</USplitter>
</div>
</template>
중첩된 스플리터 (With nested splitters)
패널 안에 Splitter를 중첩하면 IDE 스타일의 2차원 레이아웃을 만들 수 있어요.
<script setup lang="ts">
import type { SplitterItem } from '@nuxt/ui'
const card = 'bg-elevated/50 border border-default rounded-xl items-center justify-center text-muted font-medium'
const items: SplitterItem[] = [
{ slot: 'left', minSize: 20, class: card },
{ slot: 'right', minSize: 20 }
]
const nested: SplitterItem[] = [
{ slot: 'top', minSize: 20, class: card },
{ slot: 'bottom', minSize: 20, class: card }
]
</script>
<template>
<div class="w-full h-96">
<USplitter id="splitter-nested-example" :items="items">
<template #left>
Left
</template>
<template #right>
<USplitter id="splitter-nested-example-inner" orientation="vertical" :items="nested">
<template #top>
Top
</template>
<template #bottom>
Bottom
</template>
</USplitter>
</template>
</USplitter>
</div>
</template>
커스텀 핸들 (With custom handle)
핸들은 기본적으로 보이지 않아요. ui prop으로 스타일을 다시 지정해(예: 나란히 붙은 레이아웃에서 보이는 구분선으로) 재스타일링할 수 있고, resize-handle 슬롯을 사용해 그립(grip) 같은 내용을 안에 렌더링할 수 있어요.
<script setup lang="ts">
import type { SplitterItem } from '@nuxt/ui'
const items: SplitterItem[] = [
{ slot: 'left', minSize: 20, defaultSize: 30, class: 'items-center justify-center text-muted font-medium' },
{ slot: 'right', defaultSize: 70, class: 'items-center justify-center text-muted font-medium' }
]
</script>
<template>
<div class="w-full h-96">
<USplitter id="splitter-custom-handle-example" :items="items" :ui="{ handle: 'data-[orientation=horizontal]:w-px data-[orientation=vertical]:h-px bg-border transition-colors data-[state=hover]:bg-primary data-[state=drag]:bg-primary' }" class="rounded-lg border border-default overflow-hidden">
<template #left>
Left
</template>
<template #right>
Right
</template>
</USplitter>
</div>
</template>
상태 유지 (With persistence)
auto-save-id를 제공하면 레이아웃을 localStorage에 저장하고 새로고침 시 복원해요.
<template>
<USplitter id="my-layout" auto-save-id="my-layout" :items="items">
<!-- ... -->
</USplitter>
</template>
API
Props
/**
* Props for the Splitter component
*/
interface SplitterProps {
/**
* The element or component this component should render as.
* @default 'div'
*/
as?: any;
/**
* A unique id for the group, also used to derive the ids of its panels and handles.
* Set it when rendering on the server, auto-generated ids can differ between the server and the client and break resizing on hydration.
*/
id?: string | undefined;
/**
* The orientation of the splitter.
* @default 'horizontal'
*/
orientation?: "horizontal" | "vertical" | undefined;
items?: T[] | undefined;
/**
* Whether the resize handles are disabled, locking the current layout.
* @default false
*/
disabled?: boolean | undefined;
ui?: { root?: SlotClass; panel?: SlotClass; handle?: SlotClass; } | undefined;
/**
* Unique id used to auto-save group arrangement via `localStorage`.
*/
autoSaveId?: null | string | undefined;
/**
* Step size when arrow key was pressed.
*/
keyboardResizeBy?: null | number | undefined;
/**
* Custom storage API; defaults to localStorage
*/
storage?: PanelGroupStorage | undefined;
/**
* Allow this much margin when determining resizable handle hit detection
*/
hitAreaMargins?: PointerHitAreaMargins | undefined;
}
Slots
/**
* Slots for the Splitter component
*/
interface SplitterSlots {
resize-handle(): any;
}
Emits
/**
* Emitted events for the Splitter component
*/
interface SplitterEmits {
layout: (payload: [val: number[]]) => void;
collapse: (payload: [index: number]) => void;
expand: (payload: [index: number]) => void;
resize: (payload: [index: number, size: number, prevSize?: number | undefined]) => void;
dragging: (payload: [index: number, dragging: boolean]) => void;
}
테마 (Theme)
export default defineAppConfig({
ui: {
splitter: {
slots: {
root: '',
panel: 'flex',
handle: 'group relative shrink-0 focus-visible:outline-2 focus-visible:outline-primary data-[panel-resize-handle-enabled=false]:cursor-default'
},
variants: {
orientation: {
horizontal: {
handle: 'w-2 cursor-col-resize'
},
vertical: {
handle: 'h-2 cursor-row-resize'
}
}
}
}
}
})