툴팁
툴팁 (Tooltip)
요소 위에 마우스를 올렸을 때 정보를 드러내는 팝업 컴포넌트예요. 버튼 등 어떤 요소든 감싸서 추가 설명을 보여줄 수 있습니다.
출처: Nuxt UI
본문
Tooltip의 기본 슬롯에 Button 또는 다른 컴포넌트를 넣어 사용해요.
<template>
<UTooltip text="Open on GitHub">
<UButton label="Open" color="neutral" variant="subtle" />
</UTooltip>
</template>
[!WARNING]
Reka UI의
TooltipProvider컴포넌트를 사용하는App컴포넌트로 앱을 감싸 주세요.
[!TIP] See: /docs/components/app#props
App컴포넌트의tooltipprop을 확인해 Tooltip을 전역으로 구성하는 방법을 알아볼 수 있어요.
텍스트 (Text)
text prop을 사용해 Tooltip의 콘텐츠를 설정해요.
<template>
<UTooltip text="Open on GitHub">
<UButton label="Open" color="neutral" variant="subtle" />
</UTooltip>
</template>
Kbds
kbds prop을 사용해 Tooltip 안에 Kbd 컴포넌트를 렌더링해요.
<template>
<UTooltip text="Open on GitHub" :kbds="[
'meta',
'G'
]">
<UButton label="Open" color="neutral" variant="subtle" />
</UTooltip>
</template>
[!TIP]
meta같은 특수 키를 사용할 수 있어요. macOS에서는⌘로, 다른 플랫폼에서는Ctrl로 표시됩니다.
지연 (Delay)
delay-duration prop을 사용해 Tooltip이 나타나기 전의 지연 시간을 바꿔요. 예를 들어 0으로 설정하면 즉시 나타나게 할 수 있습니다.
<template>
<UTooltip :delay-duration="0" text="Open on GitHub">
<UButton label="Open" color="neutral" variant="subtle" />
</UTooltip>
</template>
[!TIP]
App컴포넌트의tooltip.delayDuration옵션으로 전역 구성할 수 있어요.
콘텐츠 (Content)
content prop을 사용해 Tooltip 콘텐츠가 렌더링되는 방식을 제어할 수 있어요(예: align 또는 side).
[!TIP]
App컴포넌트의tooltip.content옵션으로 전역 구성할 수 있어요.
<template>
<UTooltip :content="{
align: 'center',
side: 'bottom',
sideOffset: 8
}" text="Open on GitHub">
<UButton label="Open" color="neutral" variant="subtle" />
</UTooltip>
</template>
화살표 (Arrow)
arrow prop을 사용해 Tooltip에 화살표를 표시해요.
<template>
<UTooltip arrow text="Open on GitHub">
<UButton label="Open" color="neutral" variant="subtle" />
</UTooltip>
</template>
비활성화 (Disabled)
disabled prop을 사용해 Tooltip을 비활성화해요.
<template>
<UTooltip disabled text="Open on GitHub">
<UButton label="Open" color="neutral" variant="subtle" />
</UTooltip>
</template>
예시 (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>
<UTooltip v-model:open="open" text="Open on GitHub">
<UButton label="Open" color="neutral" variant="subtle" />
</UTooltip>
</template>
[!NOTE]
이 예시에서는
defineShortcuts를 활용해O키를 누르면 Tooltip을 켜고 끌 수 있어요.
커서 따라가기 (With following cursor)
reference prop을 사용해 요소 위에 마우스를 올렸을 때 Tooltip이 커서를 따라가도록 만들 수 있어요:
<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>
<UTooltip
: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>
{{ anchor.x.toFixed(0) }} - {{ anchor.y.toFixed(0) }}
</template>
</UTooltip>
</template>
API
Props
/**
* Props for the Tooltip component
*/
interface TooltipProps {
/**
* The text content of the tooltip.
*/
text?: string | undefined;
/**
* The keyboard keys to display in the tooltip.
*/
kbds?: (string | undefined)[] | KbdProps[] | undefined;
/**
* The content of the tooltip.
*
* Inherits from the `tooltip.content` of the {@link AppProps } component when not provided.
* @default { side: 'bottom', sideOffset: 8, collisionPadding: 8 }
*/
content?: Omit<TooltipContentProps, "as" | "asChild"> & Partial<EmitsToProps<TooltipContentImplEmits>> | undefined;
/**
* Display an arrow alongside the tooltip.
* `{ rounded: true }`{lang="ts-type"}
* @default false
*/
arrow?: boolean | Omit<TooltipArrowProps, "as" | "asChild"> | undefined;
/**
* Render the tooltip in a portal.
* @default true
*/
portal?: string | false | true | HTMLElement | undefined;
/**
* The reference (or anchor) element that is being referred to for positioning.
*
* If not provided will use the current component as anchor.
*/
reference?: Element | VirtualElement | undefined;
ui?: { content?: SlotClass; arrow?: SlotClass; text?: SlotClass; kbds?: SlotClass; kbdsSize?: SlotClass; } | undefined;
/**
* The open state of the tooltip 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 tooltip.
*/
open?: boolean | undefined;
/**
* Override the duration given to the `Provider` to customise
* the open delay for a specific tooltip.
* @default 700
*/
delayDuration?: number | undefined;
/**
* Prevents Tooltip.Content from remaining open when hovering.
* Disabling this has accessibility consequences. Inherits
* from Tooltip.Provider.
*/
disableHoverableContent?: boolean | undefined;
/**
* When `true`, clicking on trigger will not close the content.
* @default false
*/
disableClosingTrigger?: boolean | undefined;
/**
* When `true`, disable tooltip
* @default false
*/
disabled?: boolean | undefined;
/**
* Prevent the tooltip from opening if the focus did not come from
* the keyboard by matching against the `:focus-visible` selector.
* This is useful if you want to avoid opening it when switching
* browser tabs or closing a dialog.
* @default false
*/
ignoreNonKeyboardFocus?: boolean | undefined;
}
Slots
/**
* Slots for the Tooltip component
*/
interface TooltipSlots {
default(): any;
content(): any;
}
Emits
/**
* Emitted events for the Tooltip component
*/
interface TooltipEmits {
update:open: (payload: [value: boolean]) => void;
}
테마 (Theme)
export default defineAppConfig({
ui: {
tooltip: {
slots: {
content: 'flex items-center gap-1 bg-default text-highlighted shadow-sm rounded-sm ring ring-default h-6 px-2.5 py-1 text-xs select-none data-[state=delayed-open]:animate-[scale-in_100ms_var(--ease-out)] data-[state=closed]:animate-[scale-out_100ms_var(--ease-out)] origin-(--reka-tooltip-content-transform-origin) pointer-events-auto',
arrow: 'fill-bg stroke-default',
text: 'truncate',
kbds: "hidden lg:inline-flex items-center shrink-0 gap-0.5 not-first-of-type:before:content-['·'] not-first-of-type:before:me-0.5",
kbdsSize: 'sm'
}
}
}
})