Tooltip
A short label for a control on hover or keyboard focus
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@nyte-ai/ui/tooltip";A visible label and optional shortcut for a control with an accessible name. For longer content, use Preview card.
Usage
- Pass the control to
TooltipTriggerthroughrender. Give the control its ownaria-label, because the tooltip does not name it. - An icon-only Button also sets its label as the native
title. Passtitle={undefined}so only one tooltip appears. TooltipContentkeeps line breaks in its text.- Wrap the app once in
TooltipProvider. A trigger's owndelayorcloseDelayoverrides it. - Keep instructions and disabled reasons visible outside the tooltip.
Anatomy
<TooltipProvider>
<Tooltip>
<TooltipTrigger />
<TooltipContent />
</Tooltip>
</TooltipProvider>Examples
import { Button } from "@nyte-ai/ui/button";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@nyte-ai/ui/tooltip";
<TooltipProvider>
<Tooltip>
<TooltipTrigger
render={<Button iconOnly icon="copy" aria-label="Copy output" title={undefined} />}
/>
<TooltipContent>Copy output</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger
render={<Button iconOnly icon="trash" aria-label="Delete session" title={undefined} />}
/>
<TooltipContent>Delete session ⌘⌫</TooltipContent>
</Tooltip>
</TooltipProvider>;Props
TooltipContent takes xstyle, className, and style. Its className and style cannot be
functions of state. See Theming. Tooltip and TooltipTrigger are
Base UI's.
The reference tables below come from Base UI, MIT, © Material-UI SAS.
Tooltip
Groups all parts of the tooltip. Doesn't render its own HTML element.
Root Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| defaultOpen | boolean | false | Whether the tooltip is initially open. To render a controlled tooltip, use the open prop instead. |
| open | boolean | - | Whether the tooltip is currently open. |
| onOpenChange | ((open: boolean, eventDetails: Tooltip.Root.ChangeEventDetails) => void) | - | Event handler called when the tooltip is opened or closed. |
| actionsRef | React.RefObject<Tooltip.Root.Actions | null> | - | A ref to imperative actions. unmount: Unmounts the tooltip popup.close: Closes the tooltip imperatively when called. |
| defaultTriggerId | string | null | - | ID of the trigger that the tooltip is associated with.
This is useful in conjunction with the defaultOpen prop to create an initially open tooltip. |
| handle | Tooltip.Handle<Payload> | - | A handle to associate the tooltip with a trigger. If specified, allows external triggers to control the tooltip's open state. Can be created with the Tooltip.createHandle() method. |
| onOpenChangeComplete | ((open: boolean) => void) | - | Event handler called after any animations complete when the tooltip is opened or closed. |
| triggerId | string | null | - | ID of the trigger that the tooltip is associated with.
This is useful in conjunction with the open prop to create a controlled tooltip.
There's no need to specify this prop when the tooltip is uncontrolled (that is, when the open prop is not set). |
| trackCursorAxis | 'none' | 'x' | 'y' | 'both' | 'none' | Determines which axis the tooltip should track the cursor on. |
| disabled | boolean | false | Whether the tooltip is disabled. |
| disableHoverablePopup | boolean | false | Whether the tooltip contents can be hovered without closing the tooltip. |
| children | React.ReactNode | PayloadChildRenderFunction<Payload> | - | The content of the tooltip.
This can be a regular React node or a render function that receives the payload of the active trigger. |
TooltipTrigger
An element to attach the tooltip to.
Renders a <button> element.
Trigger Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| closeOnClick | boolean | true | Whether the tooltip should close when this trigger is clicked. |
| handle | Tooltip.Handle<Payload> | - | A handle to associate the trigger with a tooltip. |
| payload | Payload | - | A payload to pass to the tooltip when it is opened. |
| disabled | boolean | false | If true, the tooltip will not open when interacting with this trigger.
Note that this doesn't apply the disabled attribute to the trigger element.
If you want to disable the trigger element itself, you can pass the disabled prop to the trigger element via the render prop. |
| delay | number | 600 | How long to wait before opening the tooltip on hover. Specified in milliseconds. |
| closeDelay | number | 0 | How long to wait before closing the tooltip. Specified in milliseconds. |
| className | string | ((state: Tooltip.Trigger.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: Tooltip.Trigger.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: Tooltip.Trigger.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Trigger Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| data-popup-open | - | Present when the corresponding tooltip is open. |
| data-trigger-disabled | - | Present when the trigger is disabled, either by the disabled prop or by a parent <Tooltip.Root> component. |
TooltipContent
Renders the portal, positioner, and popup. Positioning props go to the positioner and the rest to the popup.
| Prop | Type | Default |
|---|---|---|
side | "top" | "bottom" | "left" | "right" | "inline-start" | "inline-end" | "bottom" |
align | "start" | "center" | "end" | "center" |
sideOffset | number | OffsetFunction | 6 |
alignOffset | number | OffsetFunction | |
tint | Tint |
Positioner
Positions the tooltip against the trigger.
Renders a <div> element.
Positioner Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| disableAnchorTracking | boolean | false | Whether to disable the popup from tracking any layout shift of its positioning anchor. |
| align | Align | 'center' | How to align the popup relative to the specified side. |
| alignOffset | number | OffsetFunction | 0 | Additional offset along the alignment axis in pixels.
Also accepts a function that returns the offset to read the dimensions of the anchor
and positioner elements, along with its side and alignment. The function takes a data object parameter with the following properties: data.anchor: the dimensions of the anchor element with properties width and height.data.positioner: the dimensions of the positioner element with properties width and height.data.side: which side of the anchor element the positioner is aligned against.data.align: how the positioner is aligned relative to the specified side. |
| side | Side | 'top' | Which side of the anchor element to align the popup against. May automatically change to avoid collisions. |
| sideOffset | number | OffsetFunction | 0 | Distance between the anchor and the popup in pixels.
Also accepts a function that returns the distance to read the dimensions of the anchor
and positioner elements, along with its side and alignment. The function takes a data object parameter with the following properties: data.anchor: the dimensions of the anchor element with properties width and height.data.positioner: the dimensions of the positioner element with properties width and height.data.side: which side of the anchor element the positioner is aligned against.data.align: how the positioner is aligned relative to the specified side. |
| arrowPadding | number | 5 | Minimum distance to maintain between the arrow and the edges of the popup. Use it to prevent the arrow element from hanging out of the rounded corners of a popup. |
| anchor | Element | VirtualElement | React.RefObject<Element | null> | (() => Element | VirtualElement | null) | null | - | An element to position the popup against. By default, the popup will be positioned against the trigger. |
| collisionAvoidance | CollisionAvoidance | - | Determines how to handle collisions when positioning the popup. side controls overflow on the preferred placement axis (top/bottom or left/right): 'flip': keep the requested side when it fits; otherwise try the opposite side
(top and bottom, or left and right).'shift': never change side; keep the requested side and move the popup within
the clipping boundary so it stays visible.'none': do not correct side-axis overflow. align controls overflow on the alignment axis (start/center/end): 'flip': keep side, but swap start and end when the requested alignment overflows.'shift': keep side and requested alignment, then nudge the popup along the
alignment axis to fit.'none': do not correct alignment-axis overflow. fallbackAxisSide controls fallback behavior on the perpendicular axis when the
preferred axis cannot fit: 'start': allow perpendicular fallback and try the logical start side first
(top before bottom, or left before right in LTR).'end': allow perpendicular fallback and try the logical end side first
(bottom before top, or right before left in LTR).'none': do not fallback to the perpendicular axis. When side is 'shift', explicitly setting align only supports 'shift' or 'none'.
If align is omitted, it defaults to 'flip'. |
| collisionBoundary | Boundary | 'clipping-ancestors' | An element or a rectangle that delimits the area that the popup is confined to. |
| collisionPadding | Padding | 5 | Additional space to maintain from the edge of the collision boundary. |
| sticky | boolean | false | Whether to maintain the popup in the viewport after the anchor element was scrolled out of view. |
| positionMethod | 'absolute' | 'fixed' | 'absolute' | Determines which CSS position property to use. |
| className | string | ((state: Tooltip.Positioner.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: Tooltip.Positioner.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: Tooltip.Positioner.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
alignOffset Prop Example:
<Positioner
alignOffset={({ side, align, anchor, positioner }) => {
return side === 'top' || side === 'bottom' ? anchor.width : anchor.height;
}}
/>sideOffset Prop Example:
<Positioner
sideOffset={({ side, align, anchor, positioner }) => {
return side === 'top' || side === 'bottom' ? anchor.height : anchor.width;
}}
/>collisionAvoidance Prop Example:
<Positioner
collisionAvoidance={{
side: 'shift',
align: 'shift',
fallbackAxisSide: 'none',
}}
/>Positioner Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| data-open | - | Present when the tooltip is open. |
| data-closed | - | Present when the tooltip is closed. |
| data-anchor-hidden | - | Present when the anchor is hidden. |
| data-align | 'start' | 'center' | 'end' | Indicates how the popup is aligned relative to specified side. |
| data-side | 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start' | Indicates which side the popup is positioned relative to the trigger. |
Positioner CSS Variables:
| Variable | Type | Description |
|---|---|---|
--anchor-height | number | The anchor's height. |
--anchor-width | number | The anchor's width. |
--available-height | number | The available height between the trigger and the edge of the viewport. |
--available-width | number | The available width between the trigger and the edge of the viewport. |
--positioner-height | number | The height of the tooltip's positioner.
It is important to set height to this value when using CSS to animate size changes. |
--positioner-width | number | The width of the tooltip's positioner.
It is important to set width to this value when using CSS to animate size changes. |
--transform-origin | string | The coordinates that this element is anchored to. Used for animations and transitions. |
Popup
A container for the tooltip contents.
Renders a <div> element.
Popup Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| className | string | ((state: Tooltip.Popup.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: Tooltip.Popup.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: Tooltip.Popup.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Popup Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| data-open | - | Present when the tooltip is open. |
| data-closed | - | Present when the tooltip is closed. |
| data-align | 'start' | 'center' | 'end' | Indicates how the popup is aligned relative to specified side. |
| data-instant | 'delay' | 'dismiss' | 'focus' | Present if animations should be instant. |
| data-side | 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start' | Indicates which side the popup is positioned relative to the trigger. |
| data-starting-style | - | Present when the tooltip begins animating in. |
| data-ending-style | - | Present when the tooltip is animating out. |
TooltipProvider
| Prop | Default |
|---|---|
delay | 300 |
closeDelay | 100 |
timeout | 200 |
Provides a shared delay for multiple tooltips. The grouping logic ensures that once a tooltip becomes visible, the adjacent tooltips will be shown instantly.
Provider Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| delay | number | - | How long to wait before opening the tooltip on hover. Specified in milliseconds. |
| closeDelay | number | - | How long to wait before closing a tooltip. Specified in milliseconds. |
| timeout | number | 400 | Another tooltip will open instantly if the previous tooltip is closed within this timeout. Specified in milliseconds. |
| children | React.ReactNode | - | - |
Accessibility
- Hover opens after
delay. Keyboard focus opens at once. - Escape, an outside press, and a trigger click close it.
- The popup appears without motion under
prefers-reduced-motion: reduce.