Popover
트리거 요소 주위에 떠 있는 비모달(non-modal) 다이얼로그 컴포넌트예요.
출처: Nuxt UI
본문
사용법
Popover의 기본 슬롯에 Button이나 다른 어떤 컴포넌트든 넣어 주세요.
그다음 #content 슬롯에 Popover가 열렸을 때 표시할 콘텐츠를 추가해 주세요.
<template>
<UPopover>
<UButton label="Open" color="neutral" variant="subtle" />
<template #content>
<Placeholder class="size-48 m-4 inline-flex" />
</template></UPopover>
</template>
Mode
mode prop으로 Popover의 모드를 바꿀 수 있어요. 기본값은 click이에요.
[!TIP]
hover모드에서는enable-touchprop을 설정하면 터치 기기에서 트리거를 탭해 Popover를 열고 닫을 수 있어요. 탭하도록 만든 트리거에는click모드를 사용해 주세요.
<template>
<UPopover mode="hover" enable-touch>
<UButton label="Open" color="neutral" variant="subtle" />
<template #content>
<Placeholder class="size-48 m-4 inline-flex" />
</template></UPopover>
</template>
[!NOTE]
Delay
hover 모드를 사용할 때 open-delay와 close-delay prop으로 Popover가 열리고 닫히기 전의 지연 시간을 조절할 수 있어요.
<template>
<UPopover mode="hover" :open-delay="500" :close-delay="300">
<UButton label="Open" color="neutral" variant="subtle" />
<template #content>
<Placeholder class="size-48 m-4 inline-flex" />
</template></UPopover>
</template>
Content
content prop으로 Popover 콘텐츠가 렌더링되는 방식을 제어할 수 있어요. 예를 들어 align이나 side 같은 옵션을 설정할 수 있어요.
<template>
<UPopover :content="{
align: 'center',
side: 'bottom',
sideOffset: 8
}">
<UButton label="Open" color="neutral" variant="subtle" />
<template #content>
<Placeholder class="size-48 m-4 inline-flex" />
</template></UPopover>
</template>
Arrow
arrow prop으로 Popover에 화살표를 표시할 수 있어요.
<template>
<UPopover arrow>
<UButton label="Open" color="neutral" variant="subtle" />
<template #content>
<Placeholder class="size-48 m-4 inline-flex" />
</template></UPopover>
</template>
Modal
modal prop으로 Popover가 외부 콘텐츠와의 상호작용을 막을지 제어할 수 있어요. 기본값은 false예요.
<template>
<UPopover modal>
<UButton label="Open" color="neutral" variant="subtle" />
<template #content>
<Placeholder class="size-48 m-4 inline-flex" />
</template></UPopover>
</template>
Dismissible
dismissible prop으로 Popover를 외부를 클릭하거나 esc 키를 눌렀을 때 닫을 수 있게 할지 제어할 수 있어요. 기본값은 true예요.
[!NOTE]
사용자가 닫으려고 하면
close:prevent이벤트가 발생해요.
<template>
<UPopover :dismissible="false" :ui="{ content: 'p-4' }">
<UButton label="Open" color="neutral" variant="subtle" />
<template #content="{ close }">
<div class="flex items-center gap-4 mb-4">
<h2 class="text-highlighted font-semibold">
Popover non-dismissible
</h2>
<UButton color="neutral" variant="ghost" icon="i-lucide-x" @click="close" />
</div>
<Placeholder class="size-full min-h-48" />
</template>
</UPopover>
</template>
예시
열림 상태 제어하기
default-open prop이나 v-model:open 디렉티브를 사용해 열림 상태를 제어할 수 있어요.
<script setup lang="ts">
const open = ref(false)
defineShortcuts({
o: () => open.value = !open.value
})
</script>
<template>
<UPopover v-model:open="open">
<UButton label="Open" color="neutral" variant="subtle" />
<template #content>
<Placeholder class="size-48 m-4 inline-flex" />
</template>
</UPopover>
</template>
[!NOTE]
이 예시에서는
defineShortcuts를 활용해O키를 눌러 Popover를 열고 닫을 수 있어요.
커맨드 팔레트와 함께 사용하기
Popover의 콘텐츠 안에 CommandPalette 컴포넌트를 사용할 수 있어요.
<script setup lang="ts">
import type { CommandPaletteItem } from '@nuxt/ui'
const items = ref([
{
label: 'bug',
value: 'bug',
chip: {
color: 'error'
}
},
{
label: 'feature',
value: 'feature',
chip: {
color: 'success'
}
},
{
label: 'enhancement',
value: 'enhancement',
chip: {
color: 'info'
}
}
] satisfies CommandPaletteItem[])
const label = ref([])
</script>
<template>
<UPopover :content="{ side: 'right', align: 'start' }">
<UButton
icon="i-lucide-tag"
label="Select labels"
color="neutral"
variant="subtle"
/>
<template #content>
<UCommandPalette
v-model="label"
multiple
placeholder="Search labels..."
:groups="[{ id: 'labels', items }]"
:ui="{ input: '[&>input]:h-8 [&>input]:text-sm' }"
/>
</template>
</UPopover>
</template>
커서 따라가기
reference prop을 사용하면 요소 위에 마우스를 올렸을 때 Popover가 커서를 따라오게 할 수 있어요:
<script setup lang="ts">
const open = ref(false)
const anchor = ref({ x: 0, y: 0 })
const reference = computed(() => ({
getBoundingClientRect: () =>
({
width: 0,
height: 0,
left: anchor.value.x,
right: anchor.value.x,
top: anchor.value.y,
bottom: anchor.value.y,
...anchor.value
} as DOMRect)
}))
</script>
<template>
<UPopover
:open="open"
:reference="reference"
:content="{ side: 'top', sideOffset: 16, updatePositionStrategy: 'always' }"
>
<div
class="flex items-center justify-center rounded-md border border-dashed border-accented text-sm aspect-video w-72"
@pointerenter="open = true"
@pointerleave="open = false"
@pointermove="(ev: PointerEvent) => {
anchor.x = ev.clientX
anchor.y = ev.clientY
}"
>
Hover me
</div>
<template #content>
<div class="p-4">
{{ anchor.x.toFixed(0) }} - {{ anchor.y.toFixed(0) }}
</div>
</template>
</UPopover>
</template>
앵커 슬롯 사용하기
#anchor 슬롯을 사용하면 커스텀 요소를 기준으로 Popover를 배치할 수 있어요.
[!WARNING]
이 슬롯은
mode가click일 때만 동작해요.
<script lang="ts" setup>
const open = ref(false)
</script>
<template>
<UPopover
v-model:open="open"
:dismissible="false"
:ui="{ content: 'w-(--reka-popover-trigger-width) p-4' }"
>
<template #anchor>
<UInput placeholder="Focus to open" @focus="open = true" @blur="open = false" />
</template>
<template #content>
<Placeholder class="w-full aspect-square" />
</template>
</UPopover>
</template>
API
Props
/**
* Props for the Popover component
*/
interface PopoverProps {
/**
* The display mode of the popover.
* @default 'click'
*/
mode?: M | undefined;
/**
* The content of the popover.
* @default { side: 'bottom', sideOffset: 8, collisionPadding: 8 }
*/
content?: Omit<PopoverContentProps, "as" | "asChild" | "forceMount"> & Partial<EmitsToProps<PopoverContentImplEmits>> | undefined;
/**
* Display an arrow alongside the popover.
* `{ rounded: true }`{lang="ts-type"}
* @default false
*/
arrow?: boolean | Omit<PopoverArrowProps, "as" | "asChild"> | undefined;
/**
* Render the popover in a portal.
* @default true
*/
portal?: string | false | true | HTMLElement | undefined;
/**
* The reference (or anchor) element that is being referred to for positioning.
*
* Accepts an element or a virtual element (anything with `getBoundingClientRect`),
* and can be changed reactively to re-anchor the popover (e.g. for a guided tour).
* If not provided will use the current component as anchor.
*/
reference?: Element | VirtualElement | undefined;
/**
* When `false`, the popover will not close when clicking outside or pressing escape.
* @default true
*/
dismissible?: boolean | undefined;
ui?: { content?: SlotClass; arrow?: SlotClass; } | undefined;
/**
* The open state of the popover when it is initially rendered. Use when you do not need to control its open state.
*/
defaultOpen?: boolean | undefined;
/**
* The controlled open state of the popover.
*/
open?: boolean | undefined;
/**
* The modality of the popover. When set to true, interaction with outside elements will be disabled and only popover content will be visible to screen readers.
* @default false
*/
modal?: boolean | undefined;
/**
* The duration from when the mouse enters the trigger until the hover card opens.
* @default 0
*/
openDelay?: number | undefined;
/**
* The duration from when the mouse leaves the trigger or content until the hover card closes.
* @default 0
*/
closeDelay?: number | undefined;
/**
* When `true`, tapping the trigger on touch devices toggles the hover card open/closed. By default touch interactions are ignored to match pointer hover semantics.
*/
enableTouch?: boolean | undefined;
}
Slots
/**
* Slots for the Popover component
*/
interface PopoverSlots {
default(): any;
content(): any;
anchor(): any;
}
[!NOTE]
close함수는mode가click일 때만 사용할 수 있어요. Reka UI가Popover에는 이 함수를 노출하지만HoverCard에는 노출하지 않기 때문이에요.
Emits
/**
* Emitted events for the Popover component
*/
interface PopoverEmits {
close:prevent: (payload: []) => void;
update:open: (payload: [value: boolean]) => void;
}
테마
export default defineAppConfig({
ui: {
popover: {
slots: {
content: 'bg-default shadow-lg rounded-md ring ring-default data-[state=open]:animate-[scale-in_100ms_var(--ease-out)] data-[state=closed]:animate-[scale-out_100ms_var(--ease-out)] origin-(--reka-popover-content-transform-origin) focus:outline-none pointer-events-auto',
arrow: 'fill-bg stroke-default'
}
}
}
})
변경 기록
사이트맵
전체 페이지 목록은 사이트맵에서 확인할 수 있어요.