A Tooltip is a popover that provides context-specific details to enhance user understanding. This component appears as a react-portal when the user either focuses on the trigger element using the keyboard or on hover.
Usage
import { Tooltip } from '@console/nimbus-ui/core';Features
The HTML title attribute can be used to create a tooltip, but it cannot be styled. Custom styled tooltips can be hard to build in an accessible way.
The following Tooltip is built using floating-ui, turning tooltips into fully accessible reusable components.
The tooltip has the following features:
- Dynamic anchor positioning using floating-ui. The tooltip is positioned next to its reference element, and remains anchored to it while avoiding collisions.
- The elements are given relevant role and ARIA attributes to be accessible to screen readers and follow the Tooltip Pattern.
- The tooltip will open on focus and hover events and closes using the escape key.
- Support for custom timings.
- Allows for composable components, allowing custom props for subcomponents.
- Supports ref forwardRef through the
asChildprop.
Examples
Tooltip Variants
Buttons allow for variantions in different styles. With the variation prop,<br/>
you can control the colour, background color and style of the button.
Placement
Use the placement prop to set the preferred placement of the tooltip.
Delay
By default Tooltips have a short delay when hovering the trigger, or instantly when using keyboard focus.<br />
You can control this delay using the delay prop. By default the prop is set to 750ms.
Below is a tooltip delay is set to 0ms.
Offset
The Tooltip's offset with respect to its trigger can be adjusted using the offset prop. <br />
The offset prop controls the spacing applied along the main axis between the element and its trigger.
Below is a tooltip offset by an additional 30px above the trigger.
Controlled
Tooltips can be controlled using the open prop with your own state hook.<br/>
Pass your setter to onOpenChange to allow keyboard events, blur events etc. to continue outside of the trigger.<br/>
Tooltip.Trigger allows for react-aria Events such as [PressEvents](https://react-spectrum.adobe.com/react-aria/usePress.html), [HoverEvents](https://react-spectrum.adobe.com/react-aria/useHover.html) instead of using native HTMLElement only.
Default Open
Use the defaultOpen prop to set the tooltip open on render (uncontrolled).
Allow Dismiss
Use the allowDismiss prop to dismiss the Tooltip upon pressing the reference element.<br/>
The tooltip will still work with keyboard press.
Rich Content
You can render rich content within the Tooltip.<br/> Use any HTML markup you require but try and avoid interactive content as focus doesn't persist within a Tooltip.
As Child
Use the asChild prop to render the trigger element as the trigger rather than the wrapper div.
Classes API
Use the classes API to make changes without overriding className. You can use the following psudoselectors.
API
Tooltip
Root component that takes both the Trigger and the Content as children.
<Tooltip>
<Tooltip.Trigger>...</Tooltip.Trigger>
<Tooltip.Content>...</Tooltip.Content>
<Tooltip>Tooltip container component for displaying additional information on hover or focus.
| Prop | Type | Default | Description |
|---|---|---|---|
| allowDismiss | boolean | — | If true dismisses the tooltip on 'press'. This doesn't work with keyboard press. |
| classes | { trigger?: string; tooltip?: string; } | — | Custom class names to add to underlying DOM elements for styling. @selector `trigger` controls Tooltip.Trigger element @selector `tooltip` controls Tooltip.Content element; |
| defaultOpen | boolean | false | The default open state of the Tooltip. Use this prop when you want the state to be open in an uncontrolled component. @selector [data-state="open"] |
| delay | number | Partial<{ open: number; close: number; }> | 750ms | The delay in milliseconds before the Tooltip opens. |
| offset | number | 10px | The offset in pixels for positioning the Tooltip. |
| onOpenChange | (open: boolean) => void | — | Callback function invoked when the open state changes. Must use this prop to allow escape and blur events. @param open @returns boolean |
| open | boolean | — | The controlled open state of the Tooltip. Use this prop to control whether the tooltip is open or closed using your own state hook. |
| placement | enum | top | Controls the placement of the tooltip in relation to the trigger. @selector [data-placement="top"] |
| variant | enum | dark | The variant of the Tooltip. @selector [data-variant] |
Tooltip.Trigger
This is the button that toggles the tooltip. By default, the Tooltip.Content will position itself against the trigger.
You can add any element as the trigger. The element will be default be wrapped by a <span /> tag.
<Tooltip.Trigger>Add your tooltip trigger element here</Tooltip.Trigger>Tooltip container component for displaying additional information on hover or focus.
| Prop | Type | Default | Description |
|---|---|---|---|
| allowDismiss | boolean | — | If true dismisses the tooltip on 'press'. This doesn't work with keyboard press. |
| classes | { trigger?: string; tooltip?: string; } | — | Custom class names to add to underlying DOM elements for styling. @selector `trigger` controls Tooltip.Trigger element @selector `tooltip` controls Tooltip.Content element; |
| defaultOpen | boolean | false | The default open state of the Tooltip. Use this prop when you want the state to be open in an uncontrolled component. @selector [data-state="open"] |
| delay | number | Partial<{ open: number; close: number; }> | 750ms | The delay in milliseconds before the Tooltip opens. |
| offset | number | 10px | The offset in pixels for positioning the Tooltip. |
| onOpenChange | (open: boolean) => void | — | Callback function invoked when the open state changes. Must use this prop to allow escape and blur events. @param open @returns boolean |
| open | boolean | — | The controlled open state of the Tooltip. Use this prop to control whether the tooltip is open or closed using your own state hook. |
| placement | enum | top | Controls the placement of the tooltip in relation to the trigger. @selector [data-placement="top"] |
| variant | enum | dark | The variant of the Tooltip. @selector [data-variant] |
Tooltip.Content
The component that pops out when the tooltip is open. Use this to wrap your content.
<Tooltip.Content>Add your tooltip content here</Tooltip.Content>No prop table available
There are no props available for this subcomponent.
React Aria
Figma