๐ข Tooltip
- Provider to control display delay globally.
- Opens when the trigger is focused or hovered.
- Closes when the trigger is activated or when pressing escape.
- Supports custom timings.
Basic
use NTooltip
to provide information for a component.
Prop | Default | Type | Description |
---|---|---|---|
content | - | string | Set the tooltip content. |
disabled | - | boolean | Set to disable the tooltip. |
Color
tooltip="{color}"
is used to set the color of the tooltip.
Prop | Default | Type | Description |
---|---|---|---|
tooltip | black | string | Set the tooltip color. |
You can use any color provided by the Tailwind CSS color palette. You can also add your own colors to the palette through the Configuration section.
Size
Prop | Default | Type | Description |
---|---|---|---|
size | xs | string | Set the tooltip general size. |
You can adjust the tooltip size using any value, including breakpoints (e.g.,
sm:sm
, xs:lg
) and states (e.g., hover:lg
, focus:3xl
).Provider
Configure the tooltip provider using the _tooltipProvider
prop.
Prop | Default | Type | Description |
---|---|---|---|
delayDuration | 600 | number | Set the delay duration of the tooltip. |
disableClosingTrigger | - | boolean | When true , clicking on trigger will not close the content. |
disableHoverableContent | false | boolean | When true , trying to hover the content will result in the tooltip closing as the pointer leaves the trigger. |
disabled | - | boolean | Set to disable the tooltip. |
For more props and information, please refer to the Radix Tooltip Provider documentation.
Root
Configure the tooltip root using the _tooltipRoot
prop.
Prop | Default | Type | Description |
---|---|---|---|
defaultOpen | false | boolean | Set the default open state of the tooltip. |
open | false | boolean | Set the open state of the tooltip. |
onUpdate:open | - | void | Event handler called when the open state of the tooltip changes. |
For more props and information, please refer to the Radix Tooltip Root documentation.
Content
Configure the tooltip content using the _tooltipContent
prop.
Prop | Default | Type | Description |
---|---|---|---|
align | center | start end center | Set the alignment of the tooltip content. |
side | top | top right bottom left | Set the side of the tooltip content. |
sideOffset | - | number | Set the offset of the tooltip content. |
alignOffset | - | number | Set the offset of the tooltip content. |
For more props and information, please refer to the Radix Tooltip Content documentation.
Slots
You can use the following slots to customize the tooltip.
Name | Description |
---|---|
default | The trigger slot. |
content | The content slot. |
Props
import type { TooltipContentProps, TooltipProviderProps, TooltipRootProps, TooltipTriggerProps } from 'radix-vue'
import type { HTMLAttributes } from 'vue'
import type { NButtonProps } from './button'
interface BaseExtensions {
class?: HTMLAttributes['class']
size?: HTMLAttributes['class']
}
type TriggerExtensions = NButtonProps & TooltipTriggerProps
type ContentExtensions = BaseExtensions & TooltipContentProps
export interface NTooltipProps extends BaseExtensions {
/**
* Disable the tooltip.
*/
disabled?: boolean
/**
* Add a content of the tooltip.
*/
content?: string
/**
* Allows you to add `UnaUI` tooltip preset properties,
* Think of it as a shortcut for adding options or variants to the preset if available.
*
* @see https://github.com/una-ui/una-ui/blob/main/packages/preset/src/_shortcuts/tooltip.ts
* @example
* tooltip="green"
*/
tooltip?: HTMLAttributes['class']
// subcomponents
_tooltipProvider?: Partial<NTooltipProviderProps>
_tooltipRoot?: Partial<NTooltipRootProps>
_tooltipTrigger?: Partial<NTooltipTriggerProps>
_tooltipContent?: Partial<NTooltipContentProps>
una?: NTooltipUnaProps
}
export interface NTooltipRootProps extends TooltipRootProps {
'onUpdate:open'?: (value: boolean) => void
}
export interface NTooltipProviderProps extends TooltipProviderProps {
}
export interface NTooltipTriggerProps extends TriggerExtensions {
}
export interface NTooltipContentProps extends ContentExtensions {
/**
* Allows you to add `UnaUI` tooltip preset properties,
* Think of it as a shortcut for adding options or variants to the preset if available.
*
* @see https://github.com/una-ui/una-ui/blob/main/packages/preset/src/_shortcuts/tooltip.ts
* @example
* tooltip="green"
*/
tooltip?: HTMLAttributes['class']
/**
* `UnaUI` preset configuration
*
* @see https://github.com/una-ui/una-ui/blob/main/packages/preset/src/_shortcuts/tooltip.ts
*/
una?: NTooltipUnaProps['tooltipContent']
}
interface NTooltipUnaProps {
/** CSS class for the tooltip content */
tooltipContent?: HTMLAttributes['class']
}
Presets
import type { RuleContext } from '@unocss/core'
import type { Theme } from '@unocss/preset-uno'
import { parseColor } from '@unocss/preset-mini/utils'
type TooltipPrefix = 'tooltip'
export const staticTooltip: Record<`${TooltipPrefix}-${string}`, string> = {
'tooltip-content': 'z-50 overflow-hidden rounded-md px-0.75em py-0.375em text-xs shadow-md animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
'tooltip-white': 'border border-base bg-popover text-popover',
'tooltip-black': 'border border-foreground bg-popover-foreground text-popover-foreground',
}
export const dynamicTooltip = [
[/^tooltip-(.*)$/, ([, c]: string[], { theme }: RuleContext<Theme>) => {
const color = parseColor(c, theme)
if ((color?.cssColor?.type === 'rgb' || color?.cssColor?.type === 'rgba') && color.cssColor.components)
return `bg-${c}-100 border-${c}-50 text-${c}-800 dark:bg-${c}-800 dark:border-${c}-800 dark:text-${c}-100`
}],
]
export const tooltip = [
...dynamicTooltip,
staticTooltip,
]
Components
You can use any sub-components of NTooltip
, such as NTooltipTrigger
, NTooltipContent
, which are defined in the Radix Tooltip documentation. For more information, please refer to the Radix Tooltip documentation.
<script setup lang="ts">
import type { TooltipRootEmits } from 'radix-vue'
import type { NTooltipProps } from '../../../types'
import { useForwardPropsEmits } from 'radix-vue'
import TooltipContent from './TooltipContent.vue'
import TooltipProvider from './TooltipProvider.vue'
import TooltipRoot from './TooltipRoot.vue'
import TooltipTrigger from './TooltipTrigger.vue'
defineOptions({
inheritAttrs: false,
})
const props = defineProps<NTooltipProps>()
const emits = defineEmits<TooltipRootEmits>()
const forwarded = useForwardPropsEmits(props, emits)
</script>
<template>
<TooltipProvider
v-bind="_tooltipProvider"
:disabled
>
<TooltipRoot
v-bind="_tooltipRoot"
>
<TooltipTrigger
as-child
v-bind="_tooltipTrigger"
>
<slot />
</TooltipTrigger>
<TooltipContent
v-if="$slots.content || content"
v-bind="forwarded._tooltipContent"
:size
:tooltip
:disabled
:una="forwarded.una?.tooltipContent"
>
<slot name="content">
{{ content }}
</slot>
</TooltipContent>
</TooltipRoot>
</TooltipProvider>
</template>