Collapsible
Collapsible (접이식 패널)
Collapsible은 콘텐츠 표시 여부를 토글할 수 있는 접이식 요소예요. 버튼 하나로 내용을 펼치고 접을 수 있어서 공간을 효율적으로 관리할 때 유용해요.
출처: Nuxt UI
본문
Collapsible의 기본 슬롯(default slot)에 Button이나 다른 컴포넌트를 넣어 사용해요.
그다음 #content 슬롯을 사용해 Collapsible이 열렸을 때 표시할 내용을 추가하면 돼요.
<template>
<UCollapsible class="flex flex-col gap-2 w-48">
<UButton label="Open" color="neutral" variant="subtle" trailing-icon="i-lucide-chevron-down" block />
<template #content>
<Placeholder class="h-48" />
</template></UCollapsible>
</template>
언마운트 (Unmount)
접힐 때 내용을 언마운트하지 않으려면 unmount-on-hide prop을 사용하면 돼요. 기본값은 true예요.
<template>
<UCollapsible :unmount-on-hide="false" class="flex flex-col gap-2 w-48">
<UButton label="Open" color="neutral" variant="subtle" trailing-icon="i-lucide-chevron-down" block />
<template #content>
<Placeholder class="h-48" />
</template></UCollapsible>
</template>
[!NOTE]
DOM을 직접 검사해 콘텐츠가 렌더링되는지 확인해 볼 수 있어요.
비활성화 (Disabled)
disabled prop을 사용해 Collapsible을 비활성화할 수 있어요.
<template>
<UCollapsible class="flex flex-col gap-2 w-48" disabled>
<UButton label="Open" color="neutral" variant="subtle" trailing-icon="i-lucide-chevron-down" block />
<template #content>
<Placeholder class="h-48" />
</template></UCollapsible>
</template>
예제 (Examples)
열림 상태 제어하기
default-open prop이나 v-model:open 디렉티브를 사용해 열림 상태를 제어할 수 있어요.
<script setup lang="ts">
const open = ref(true)
defineShortcuts({
o: () => open.value = !open.value
})
</script>
<template>
<UCollapsible v-model:open="open" class="flex flex-col gap-2 w-48">
<UButton
label="Open"
color="neutral"
variant="subtle"
trailing-icon="i-lucide-chevron-down"
block
/>
<template #content>
<Placeholder class="h-48" />
</template>
</UCollapsible>
</template>
[!NOTE]
이 예제에서는
defineShortcuts를 활용해O키를 누르면 Collapsible을 토글할 수 있어요.
[!TIP]
이렇게 하면 트리거를 Collapsible 밖으로 옮기거나 완전히 제거할 수도 있어요.
회전 아이콘 사용하기
다음은 Button 안의 아이콘이 Collapsible의 열림 상태를 나타내며 회전하는 예제예요.
<template>
<UCollapsible class="flex flex-col gap-2 w-48">
<UButton
class="group"
label="Open"
color="neutral"
variant="subtle"
trailing-icon="i-lucide-chevron-down"
:ui="{
trailingIcon: 'group-data-[state=open]:rotate-180 transition-transform duration-200'
}"
block
/>
<template #content>
<Placeholder class="h-48" />
</template>
</UCollapsible>
</template>
API
Props
/**
* Props for the Collapsible component
*/
interface CollapsibleProps {
/**
* The element or component this component should render as.
* @default 'div'
*/
as?: any;
ui?: { root?: SlotClass; content?: SlotClass; } | undefined;
/**
* When `true`, prevents the user from interacting with the collapsible.
*/
disabled?: boolean | undefined;
/**
* The open state of the collapsible when it is initially rendered. <br> Use when you do not need to control its open state.
*/
defaultOpen?: boolean | undefined;
/**
* The controlled open state of the collapsible. Can be binded with `v-model`.
*/
open?: boolean | undefined;
/**
* When `true`, the element will be unmounted on closed state.
* @default true
*/
unmountOnHide?: boolean | undefined;
}
Slots
/**
* Slots for the Collapsible component
*/
interface CollapsibleSlots {
default(): any;
content(): any;
}
Emits
/**
* Emitted events for the Collapsible component
*/
interface CollapsibleEmits {
update:open: (payload: [value: boolean]) => void;
}
테마 (Theme)
export default defineAppConfig({
ui: {
collapsible: {
slots: {
root: '',
content: 'data-[state=open]:animate-[collapsible-down_200ms_var(--ease-out)] data-[state=closed]:animate-[collapsible-up_200ms_var(--ease-out)] data-[state=closed]:overflow-hidden'
}
}
}
})