슬라이드오버
슬라이드오버 (Slideover)
Slideover는 화면의 어느 한쪽에서 슬라이드되어 들어오는 다이얼로그(dialog) 컴포넌트예요. 기본 slot의 Button이나 다른 컴포넌트로 열 수 있고, #content, #header, #body, #footer slot으로 내용을 구성해요.
출처: Nuxt UI
본문
사용법 (Usage)
Slideover의 기본 slot에 Button이나 다른 컴포넌트를 넣어서 사용해요.
그런 다음 #content slot을 사용해 Slideover가 열렸을 때 표시될 콘텐츠를 추가해요.
<template>
<USlideover>
<UButton label="Open" color="neutral" variant="subtle" />
<template #content>
<Placeholder class="h-full m-4" />
</template></USlideover>
</template>
#header, #body, #footer slot을 사용해서 Slideover의 콘텐츠를 커스터마이즈할 수도 있어요.
제목 (Title)
title prop을 사용해 Slideover 헤더의 제목을 설정할 수 있어요.
<template>
<USlideover title="Slideover with title">
<UButton label="Open" color="neutral" variant="subtle" />
<template #body>
<Placeholder class="h-full" />
</template></USlideover>
</template>
설명 (Description)
description prop을 사용해 Slideover 헤더의 설명을 설정할 수 있어요.
<template>
<USlideover title="Slideover with description" description="Lorem ipsum dolor sit amet, consectetur adipiscing elit.">
<UButton label="Open" color="neutral" variant="subtle" />
<template #body>
<Placeholder class="h-full" />
</template></USlideover>
</template>
닫기 (Close)
close prop을 사용해 Slideover 헤더에 표시되는 닫기 버튼을 커스터마이즈하거나 숨길 수 있어요(false 값).
Button 컴포넌트의 속성을 그대로 전달해 커스터마이즈할 수 있어요.
<template>
<USlideover title="Slideover with close button" :close="{
color: 'primary',
variant: 'outline',
class: 'rounded-full'
}">
<UButton label="Open" color="neutral" variant="subtle" />
<template #body>
<Placeholder class="h-full" />
</template></USlideover>
</template>
[!NOTE]
#contentslot을 사용하면 닫기 버튼은 헤더의 일부이므로 표시되지 않아요.
닫기 아이콘 (Close Icon)
close-icon prop을 사용해 닫기 버튼의 Icon을 커스터마이즈할 수 있어요. 기본값은 i-lucide-x예요.
<template>
<USlideover title="Slideover with close button" close-icon="i-lucide-arrow-right">
<UButton label="Open" color="neutral" variant="subtle" />
<template #body>
<Placeholder class="h-full" />
</template></USlideover>
</template>
Nuxt:
[!TIP] See: /docs/getting-started/integrations/icons/nuxt#theme
app.config.ts안의ui.icons.close키에서 이 아이콘을 전역으로 커스터마이즈할 수 있어요.
Vue:
[!TIP] See: /docs/getting-started/integrations/icons/vue#theme
vite.config.ts안의ui.icons.close키에서 이 아이콘을 전역으로 커스터마이즈할 수 있어요.
측면 (Side)
side prop을 사용해 Slideover가 슬라이드되어 들어올 화면의 측면을 설정할 수 있어요. 기본값은 right예요.
<template>
<USlideover side="left" title="Slideover with side">
<UButton label="Open" color="neutral" variant="subtle" />
<template #body>
<Placeholder class="h-full min-h-48" />
</template></USlideover>
</template>
인셋 (Inset) 4.3+
inset prop을 사용해 Slideover를 가장자리에서 안쪽으로 들여쓸 수 있어요.
<template>
<USlideover side="right" inset title="Slideover with inset">
<UButton label="Open" color="neutral" variant="subtle" />
<template #body>
<Placeholder class="min-w-96 min-h-96 size-full" />
</template></USlideover>
</template>
전환 (Transition)
transition prop을 사용해 Slideover를 애니메이션할지 여부를 제어할 수 있어요. 기본값은 true예요.
<template>
<USlideover :transition="false" title="Slideover without transition">
<UButton label="Open" color="neutral" variant="subtle" />
<template #body>
<Placeholder class="h-full" />
</template></USlideover>
</template>
오버레이 (Overlay)
overlay prop을 사용해 Slideover에 오버레이가 있는지 여부를 제어할 수 있어요. 기본값은 true예요.
<template>
<USlideover :overlay="false" title="Slideover without overlay">
<UButton label="Open" color="neutral" variant="subtle" />
<template #body>
<Placeholder class="h-full" />
</template></USlideover>
</template>
모달 (Modal)
modal prop을 사용해 Slideover가 바깥 콘텐츠와의 상호작용을 차단할지 여부를 제어할 수 있어요. 기본값은 true예요.
[!NOTE]
modal을false로 설정하면 오버레이가 자동으로 비활성화되고 바깥 콘텐츠가 상호작용 가능해져요.
<template>
<USlideover :modal="false" title="Slideover interactive">
<UButton label="Open" color="neutral" variant="subtle" />
<template #body>
<Placeholder class="h-full" />
</template></USlideover>
</template>
닫기 가능 여부 (Dismissible)
dismissible prop을 사용해 Slideover가 바깥을 클릭하거나 escape를 눌렀을 때 닫힐 수 있는지 여부를 제어할 수 있어요. 기본값은 true예요.
[!NOTE]
사용자가 닫으려고 시도하면
close:prevent이벤트가 발생해요.
[!TIP]
modal: false와dismissible: false를 함께 사용하면 Slideover를 닫지 않고도 배경을 상호작용할 수 있게 만들 수 있어요.
<template>
<USlideover :dismissible="false" modal title="Slideover non-dismissible">
<UButton label="Open" color="neutral" variant="subtle" />
<template #body>
<Placeholder class="h-full" />
</template></USlideover>
</template>
마운트 해제 (Unmount) 4.10+
unmount-on-hide prop을 사용해 Slideover가 닫혔을 때 콘텐츠가 마운트 해제되는 것을 방지할 수 있어요. 기본값은 true예요.
<template>
<USlideover :unmount-on-hide="false" title="Slideover">
<UButton label="Open" color="neutral" variant="subtle" />
<template #body>
<Placeholder class="h-full" />
</template></USlideover>
</template>
[!NOTE]
DOM을 검사하면 Slideover가 닫혀 있는 동안에도 콘텐츠가 렌더링되어 있는 것을 볼 수 있어요.
[!TIP]
portalprop을false로 설정하면 콘텐츠가 서버에서도 렌더링돼요. 이는 SSR 중에 열린 Slideover를 페이지 로드 시 깜빡임 없이 렌더링하거나, 콘텐츠를 SEO에 노출시키는 데 유용해요.
예시 (Examples)
열림 상태 제어 (Control open state)
default-open prop이나 v-model:open 디렉티브로 열림 상태를 제어할 수 있어요.
<script setup lang="ts">
const open = ref(false)
defineShortcuts({
o: () => open.value = !open.value
})
</script>
<template>
<USlideover v-model:open="open">
<UButton label="Open" color="neutral" variant="subtle" />
<template #content>
<Placeholder class="h-full m-4" />
</template>
</USlideover>
</template>
[!NOTE]
이 예시에서는
defineShortcuts를 활용해O키를 눌러 Slideover를 토글할 수 있어요.
[!TIP]
이렇게 하면 트리거를 Slideover 밖으로 옮기거나 완전히 제거할 수 있어요.
프로그래매틱 사용 (Programmatic usage)
useOverlay 컴포저블을 사용해 Slideover를 프로그래매틱하게 열 수 있어요.
[!WARNING]
앱을
App컴포넌트로 감싸야 해요. 이 컴포넌트는OverlayProvider컴포넌트를 사용해요.
먼저 프로그래매틱하게 열릴 슬라이드오버 컴포넌트를 만들어요.
<script setup lang="ts">
defineProps<{
count: number
}>()
const emit = defineEmits<{ close: [boolean] }>()
</script>
<template>
<USlideover :close="{ onClick: () => emit('close', false) }" :description="`This slideover was opened programmatically ${count} times`">
<template #body>
<Placeholder class="h-full" />
</template>
<template #footer>
<div class="flex gap-2">
<UButton color="neutral" label="Dismiss" @click="emit('close', false)" />
<UButton label="Success" @click="emit('close', true)" />
</div>
</template>
</USlideover>
</template>
[!NOTE]
여기서는 슬라이드오버가 닫히거나 해제될 때
close이벤트를 발생시키고 있어요.close이벤트를 통해 어떤 데이터든 내보낼 수 있고, 그 데이터가open()의 해결 값(resolved value)이 돼요. 프로미스가 해결되려면 이벤트가 반드시 발생해야 해요.
그런 다음 앱에서 사용해요.
<script setup lang="ts">
import { LazySlideoverExample } from '#components'
const count = ref(0)
const toast = useToast()
const overlay = useOverlay()
const slideover = overlay.create(LazySlideoverExample)
async function open() {
const instance = slideover.open({
count: count.value
})
const shouldIncrement = await instance.result
if (shouldIncrement) {
count.value++
toast.add({
title: `Success: ${shouldIncrement}`,
color: 'success',
id: 'slideover-success'
})
// Update the count
slideover.patch({
count: count.value
})
return
}
toast.add({
title: `Dismissed: ${shouldIncrement}`,
color: 'error',
id: 'slideover-dismiss'
})
}
</script>
<template>
<UButton label="Open" color="neutral" variant="subtle" @click="open" />
</template>
[!TIP]
슬라이드오버 컴포넌트 안에서
emit('close')를 발생시켜 슬라이드오버를 닫을 수 있어요.
중첩 슬라이드오버 (Nested slideovers)
슬라이드오버를 서로 중첩할 수 있어요.
<script setup lang="ts">
const first = ref(false)
const second = ref(false)
</script>
<template>
<USlideover v-model:open="first" title="First slideover" :ui="{ footer: 'justify-end' }">
<UButton color="neutral" variant="subtle" label="Open" />
<template #body>
<Placeholder class="h-full" />
</template>
<template #footer>
<UButton label="Close" color="neutral" variant="outline" @click="first = false" />
<USlideover v-model:open="second" title="Second slideover" :ui="{ footer: 'justify-end' }">
<UButton label="Open second" color="neutral" />
<template #body>
<Placeholder class="h-full" />
</template>
<template #footer>
<UButton label="Close" color="neutral" variant="outline" @click="second = false" />
</template>
</USlideover>
</template>
</USlideover>
</template>
footer slot과 함께
#footer slot을 사용해 Slideover의 body 뒤에 콘텐츠를 추가할 수 있어요.
<script setup lang="ts">
const open = ref(false)
</script>
<template>
<USlideover v-model:open="open" title="Slideover with footer" description="This is useful when you want a form in a Slideover." :ui="{ footer: 'justify-end' }">
<UButton label="Open" color="neutral" variant="subtle" />
<template #body>
<Placeholder class="h-full" />
</template>
<template #footer="{ close }">
<UButton label="Cancel" color="neutral" variant="outline" @click="close" />
<UButton label="Submit" color="neutral" />
</template>
</USlideover>
</template>
API
Props
/**
* Props for the Slideover component
*/
interface SlideoverProps {
title?: string | undefined;
description?: string | undefined;
/**
* The content of the slideover.
*/
content?: Omit<DialogContentProps, "as" | "asChild" | "forceMount"> & Partial<EmitsToProps<DialogContentImplEmits>> | undefined;
/**
* Render an overlay behind the slideover.
* @default true
*/
overlay?: boolean | undefined;
/**
* Animate the slideover when opening or closing.
* @default true
*/
transition?: boolean | undefined;
/**
* The side of the slideover.
* @default 'right'
*/
side?: "right" | "top" | "bottom" | "left" | undefined;
/**
* Whether to inset the slideover from the edges.
* @default false
*/
inset?: boolean | undefined;
/**
* Render the slideover in a portal.
* @default true
*/
portal?: string | false | true | HTMLElement | undefined;
/**
* Display a close button to dismiss the slideover.
* `{ size: 'md', color: 'neutral', variant: 'ghost' }`{lang="ts-type"}
* @default true
*/
close?: boolean | Omit<ButtonProps, LinkPropsKeys> | undefined;
/**
* The icon displayed in the close button.
* @default appConfig.ui.icons.close
*/
closeIcon?: any;
/**
* When `false`, the slideover will not close when clicking outside or pressing escape.
* @default true
*/
dismissible?: boolean | undefined;
ui?: { overlay?: SlotClass; content?: SlotClass; header?: SlotClass; wrapper?: SlotClass; body?: SlotClass; footer?: SlotClass; title?: SlotClass; description?: SlotClass; close?: SlotClass; } | undefined;
/**
* The controlled open state of the dialog. Can be binded as `v-model:open`.
*/
open?: boolean | undefined;
/**
* The open state of the dialog when it is initially rendered. Use when you do not need to control its open state.
*/
defaultOpen?: boolean | undefined;
/**
* The modality of the dialog When set to `true`, <br>
* interaction with outside elements will be disabled and only dialog content will be visible to screen readers.
* @default true
*/
modal?: boolean | undefined;
/**
* When set to `false`, the dialog content will not be unmounted when closed, but instead hidden with CSS. <br>
* Useful for SEO or when you want to improve performance by not remounting the component on every open.
* @default true
*/
unmountOnHide?: boolean | undefined;
}
Slots
/**
* Slots for the Slideover component
*/
interface SlideoverSlots {
default(): any;
content(): any;
header(): any;
title(): any;
description(): any;
actions(): any;
close(): any;
body(): any;
footer(): any;
}
Emits
/**
* Emitted events for the Slideover component
*/
interface SlideoverEmits {
leave: (payload: []) => void;
after:leave: (payload: []) => void;
enter: (payload: []) => void;
after:enter: (payload: []) => void;
close:prevent: (payload: []) => void;
update:open: (payload: [value: boolean]) => void;
}
테마 (Theme)
export default defineAppConfig({
ui: {
slideover: {
slots: {
overlay: 'fixed inset-0 bg-elevated/75',
content: 'fixed bg-default divide-y divide-default sm:ring ring-default sm:shadow-lg flex flex-col focus:outline-none',
header: 'flex items-center gap-1.5 p-4 sm:px-6 min-h-(--ui-header-height)',
wrapper: '',
body: 'flex-1 overflow-y-auto p-4 sm:p-6',
footer: 'flex items-center gap-1.5 p-4 sm:px-6',
title: 'text-highlighted font-semibold',
description: 'mt-1 text-muted text-sm',
close: 'absolute top-4 end-4'
},
variants: {
side: {
top: {
content: ''
},
right: {
content: 'max-w-md'
},
bottom: {
content: ''
},
left: {
content: 'max-w-md'
}
},
inset: {
true: {
content: 'rounded-lg'
}
},
transition: {
true: {
overlay: 'data-[state=open]:animate-[fade-in_200ms_var(--ease-out)] data-[state=closed]:animate-[fade-out_200ms_var(--ease-out)]'
}
}
},
compoundVariants: [
{
side: 'top',
inset: true,
class: {
content: 'max-h-[calc(100%-2rem)] inset-x-4 top-4'
}
},
{
side: 'top',
inset: false,
class: {
content: 'max-h-full inset-x-0 top-0'
}
},
{
side: 'right',
inset: true,
class: {
content: 'w-[calc(100%-2rem)] inset-y-4 right-4'
}
},
{
side: 'right',
inset: false,
class: {
content: 'w-full inset-y-0 right-0'
}
},
{
side: 'bottom',
inset: true,
class: {
content: 'max-h-[calc(100%-2rem)] inset-x-4 bottom-4'
}
},
{
side: 'bottom',
inset: false,
class: {
content: 'max-h-full inset-x-0 bottom-0'
}
},
{
side: 'left',
inset: true,
class: {
content: 'w-[calc(100%-2rem)] inset-y-4 left-4'
}
},
{
side: 'left',
inset: false,
class: {
content: 'w-full inset-y-0 left-0'
}
},
{
transition: true,
side: 'top',
class: {
content: 'data-[state=open]:animate-[slide-in-from-top_200ms_var(--ease-out)] data-[state=closed]:animate-[slide-out-to-top_200ms_var(--ease-out)]'
}
},
{
transition: true,
side: 'right',
class: {
content: 'data-[state=open]:animate-[slide-in-from-right_200ms_var(--ease-out)] data-[state=closed]:animate-[slide-out-to-right_200ms_var(--ease-out)]'
}
},
{
transition: true,
side: 'bottom',
class: {
content: 'data-[state=open]:animate-[slide-in-from-bottom_200ms_var(--ease-out)] data-[state=closed]:animate-[slide-out-to-bottom_200ms_var(--ease-out)]'
}
},
{
transition: true,
side: 'left',
class: {
content: 'data-[state=open]:animate-[slide-in-from-left_200ms_var(--ease-out)] data-[state=closed]:animate-[slide-out-to-left_200ms_var(--ease-out)]'
}
}
]
}
}
})