Tooltip
The Tooltip is a popup that displays clarifying text related to an element that has been focused or hovered on.
- Status
- alpha
- Version
- 4.0.2
- Import from
@twilio-paste/core/tooltip— or —@twilio-paste/tooltip
Using alpha components in your product
Alpha components are ready for you to use in production. Components such as Tooltip are likely to not see any API changes. This is only an alpha due to the lack of documentation and a matching Sketch symbol. Feel free to share your experience using this component by starting a discussion on GitHub. We are also looking for a contributor to symbolize this component in Sketch. Interested? Reach out!
Guidelines#
About Tooltip#
The Tooltip component is a popup that displays text related to an element when the element is focused via the keyboard or on mouse hover. Tooltip follows the WAI-ARIA Tooltip Pattern.
Tooltip compared to Popover#
Accessibility#
A tooltip must only be placed on a natively focusable HTML element. Good candidates include a Paste Button or a Paste Anchor if the tip also links to a help article.
Do not place tooltips on non-focusable elements, like an icon.
If your tooltip wraps a natively focusable HTML element that includes only an icon, make sure you set the prop decorative={false}, and
give the icon a title. The title of the icon should be the accessible name for the button action, like "Delete phone number". The tooltip provides the additional context, like "You can delete phone numbers every 7 days".
This ensures the icon and tooltip are accessible to screen readers. Refer to the focusable element example for implementation.
Examples#
Basic Tooltip#
Controlling placement#
The placement of the tooltip is configurable via the placement prop. The available placement options are
available in the props description here. No matter the placement, the tooltip will always adjust
to remain in the screen so you don't have to worry about it going off the page, or being covered by another element.
Focusable element#
A tooltip must be placed on a focusable element, such as an anchor or button.
Do not place tooltips on non-focusable elements, like an icon. Wrap them in a focusable element and place the tooltip on that.
Tooltips require their children to forwardRefs#
If you want to add a Tooltip around a React component, you must make sure that component forwards refs. All Paste components do this by default, so this is only a concern when placing Tooltips around other types of components.
Using state hooks#
🚨 Power user move! 🚨
Only use this property if you are a power user. It's very easy to break your implementation and unfortunately the Paste team will not be able to debug this for you. Proceed with extreme caution.
The tooltip comes with the option of "hooking" into the internal state by using the state hook originally provided by Reakit.
Rather than the state be internal to the component, you can use the useTooltipState hook and pass the returned state
to Tooltip as the state prop.
This allows you to use certain returned props from the state hook, including functions like hide and show.
An example usecase of this might be programmatically providing the user a tooltip when a certain user action is taken.
In the example below we are showing a tooltip when another button is pressed. When pressed the button uses the show
function from the hook. Another button using the hide function from the hook is also provided to hide the tooltip
when pressed.
It should be noted that when doing so, the state prop takes precident over the other properties that affect the state or initial state of the Tooltip. They will be ignored in favour of them being provided as arguments to the useTooltipState hook.
For full details on how to use the state hook, and what props to provide it, follow the Tooltip Primitive documentation. It's the same hook, just renamed.
Anatomy#
Tooltip#
| Property | Default token | Modifiable? |
|---|---|---|
| background-color | color-background-body-inverse | No |
| border-width | border-width-10 | No |
| border-color | color-border-dark | No |
| border-radius | border-radius-20 | No |
| box-shadow | shadow | No |
| max-width | size-30 | No |
| padding | space-30 | No |
| padding-top | space-20 | No |
| padding-bottom | space-20 | No |
| z-index | z-index-90 | No |
Usage Guide#
API#
Installation#
yarn add @twilio-paste/tooltip - or - yarn add @twilio-paste/coreUsage#
import {Tooltip} from '@twilio-paste/tooltip';const PopoverExample: React.FC = () => { return ( <Tooltip baseId="tooltip-example" text="Tooltip text..."> <Button variant="primary">Open Tooltip</Button> </Tooltip> );};Tooltip Props#
baseId?: string
Sets the ID that will serve as a base for all the items IDs.
children: ReactElement
The tooltip reference, meaning the element where the tooltip originates. Must be a focusable element.
gutter?: string
Sets the offset between the reference and the tooltip on the main axis.
placement?: "auto-start" | "auto" | "auto-end" | "top-start...
Sets the placement of tooltip in relation to the child element. Available options include:
- auto-start
- auto-end
- auto
- top-start
- top-end
- top
- bottom-start
- bottom-end
- bottom
- right-start
- right-end
- right
- left-start
- left-end
- left
text: string
The text content of the Tooltip.