useTooltipTrigger
Provides the behavior and accessibility implementation for a tooltip trigger, e.g. a button that shows a description when focused or hovered.
| install | yarn add @react-aria/tooltip |
|---|---|
| version | 3.1.1 |
| usage | import {useTooltipTrigger, useTooltip} from '@react-aria/tooltip' |
API#
useTooltipTrigger(
props: TooltipTriggerProps,
state: TooltipTriggerState,
ref: RefObject<HTMLElement>
): TooltipTriggerAriauseTooltip(
(props: AriaTooltipProps,
, state: TooltipTriggerState
)): TooltipAriaFeatures#
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. useTooltipTrigger and useTooltip help build
fully accessible tooltips that can be styled as needed.
- Keyboard focus management and cross browser normalization
- Hover management and cross browser normalization
- Labeling support for screen readers (aria-describedby)
- Exposed as a tooltip to assistive technology via ARIA
- Matches native tooltip behavior with delay on hover of first tooltip and no delay on subsequent tooltips.
Anatomy#
A tooltip consists of two parts: the trigger element and the tooltip itself. Users may reveal the tooltip by hovering or focusing the trigger.
useTooltipTrigger returns props to be spread onto its target trigger and the tooltip:
| Name | Type | Description |
triggerProps | HTMLAttributes<HTMLElement>
& PressProps
& HoverProps
& FocusEvents | Props for the trigger element. |
tooltipProps | HTMLAttributes<HTMLElement> | Props for the overlay container element. |
useTooltip returns props to be spread onto the tooltip:
| Name | Type | Description |
tooltipProps | HTMLAttributes<HTMLElement> | Props for the tooltip element. |
Tooltip state is managed by the useTooltipTriggerState
hook in @react-stately/tooltip. The state object should be passed as an option to useTooltipTrigger and useTooltip.
Example#
This example implements a Tooltip that renders in an absolute position next to its target. The useTooltip hook applies the correct ARIA attributes to the tooltip, and useTooltipTrigger associates it to the trigger element.
Two instances of the example are rendered to demonstrate the behavior of the delay on hover. If you hover over the first button, the tooltip will be shown after a delay. Hovering over the second button shows the tooltip immediately. If you wait for a delay before hovering another element, the delay restarts.
import {useTooltipTriggerState} from '@react-stately/tooltip';
import {mergeProps} from '@react-aria/utils';
function Tooltip({state ...props}) {
let {tooltipProps} = useTooltip(props state);
return (
<span
style={
position: 'absolute'
left: '5px'
top: '100%'
marginTop: '10px'
backgroundColor: 'white'
color: 'black'
padding: '5px'
}
...mergeProps(props tooltipProps)>
propschildren
</span>
);
}
function Example(props) {
let state = useTooltipTriggerState(props);
let ref = ReactuseRef();
// Get props for the trigger and its tooltip
let {triggerProps tooltipProps} = useTooltipTrigger(props state ref);
return (
<span style={position: 'relative'}>
<button ref=ref ...triggerProps>
I have a tooltip
</button>
stateisOpen && (
<Tooltip state=state ...tooltipProps>
And the tooltip tells you more information.
</Tooltip>
)
</span>
);
}
<Example />
<Example />import {useTooltipTriggerState} from '@react-stately/tooltip';
import {mergeProps} from '@react-aria/utils';
function Tooltip({state ...props}) {
let {tooltipProps} = useTooltip(props state);
return (
<span
style={
position: 'absolute'
left: '5px'
top: '100%'
marginTop: '10px'
backgroundColor: 'white'
color: 'black'
padding: '5px'
}
...mergeProps(props tooltipProps)>
propschildren
</span>
);
}
function Example(props) {
let state = useTooltipTriggerState(props);
let ref = ReactuseRef();
// Get props for the trigger and its tooltip
let {triggerProps tooltipProps} = useTooltipTrigger(
props
state
ref
);
return (
<span style={position: 'relative'}>
<button ref=ref ...triggerProps>
I have a tooltip
</button>
stateisOpen && (
<Tooltip state=state ...tooltipProps>
And the tooltip tells you more information.
</Tooltip>
)
</span>
);
}
<Example />
<Example />import {useTooltipTriggerState} from '@react-stately/tooltip';
import {mergeProps} from '@react-aria/utils';
function Tooltip({
state
...props
}) {
let {
tooltipProps
} = useTooltip(
props
state
);
return (
<span
style={
position:
'absolute'
left: '5px'
top: '100%'
marginTop:
'10px'
backgroundColor:
'white'
color: 'black'
padding: '5px'
}
...mergeProps(
props
tooltipProps
)>
propschildren
</span>
);
}
function Example(props) {
let state = useTooltipTriggerState(
props
);
let ref = ReactuseRef();
// Get props for the trigger and its tooltip
let {
triggerProps
tooltipProps
} = useTooltipTrigger(
props
state
ref
);
return (
<span
style={
position:
'relative'
}>
<button
ref=ref
...triggerProps>
I have a tooltip
</button>
stateisOpen && (
<Tooltip
state=state
...tooltipProps>
And the tooltip
tells you more
information.
</Tooltip>
)
</span>
);
}
<Example />
<Example />Internationalization#
RTL#
In right-to-left languages, tooltips should be mirrored across trigger. Ensure that your CSS and overlay positioning (if any) accounts for this.
| Name | Type | Default | Description |
isDisabled | boolean | — | Whether the tooltip should be disabled, independent from the trigger. |
delay | number | 1500 | The delay time for the tooltip to show up. See guidelines. |
trigger | 'focus' | — | By default, opens for both focus and hover. Can be made to open only for focus. |
isOpen | boolean | — | Whether the overlay is open by default (controlled). |
defaultOpen | boolean | — | Whether the overlay is open by default (uncontrolled). |
onOpenChange | (
(isOpen: boolean
)) => void | — | Handler that is called when the overlay's open state changes. |
Properties
| Name | Type | Description |
isOpen | boolean | Whether the tooltip is currently showing. |
Methods
| Method | Description |
open(
(immediate: boolean
)): void | Shows the tooltip. By default, the tooltip becomes visible after a delay
depending on a global warmup timer. The immediate option shows the
tooltip immediately instead. |
close(
(immediate: boolean
)): void | Hides the tooltip. |
| Name | Type | Description |
triggerProps | HTMLAttributes<HTMLElement>
& PressProps
& HoverProps
& FocusEvents | Props for the trigger element. |
tooltipProps | HTMLAttributes<HTMLElement> | Props for the overlay container element. |
| Name | Type | Description |
isPressed | boolean | Whether the target is in a controlled press state (e.g. an overlay it triggers is open). |
isDisabled | boolean | Whether the press events should be disabled. |
preventFocusOnPress | boolean | Whether the target should not receive focus on press. |
onPress | (
(e: PressEvent
)) => void | Handler that is called when the press is released over the target. |
onPressStart | (
(e: PressEvent
)) => void | Handler that is called when a press interaction starts. |
onPressEnd | (
(e: PressEvent
)) => void | Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target. |
onPressChange | (
(isPressed: boolean
)) => void | Handler that is called when the press state changes. |
onPressUp | (
(e: PressEvent
)) => void | Handler that is called when a press is released over the target, regardless of whether it started on the target or not. |
| Name | Type | Description |
isDisabled | boolean | Whether the hover events should be disabled. |
onHoverStart | (
(e: HoverEvent
)) => void | Handler that is called when a hover interaction starts. |
onHoverEnd | (
(e: HoverEvent
)) => void | Handler that is called when a hover interaction ends. |
onHoverChange | (
(isHovering: boolean
)) => void | Handler that is called when the hover state changes. |
| Name | Type | Description |
isOpen | boolean | |
id | string | The element's unique identifier. See MDN. |
aria-label | string | Defines a string value that labels the current element. |
aria-labelledby | string | Identifies the element (or elements) that labels the current element. |
aria-describedby | string | Identifies the element (or elements) that describes the object. |
aria-details | string | Identifies the element (or elements) that provide a detailed, extended description for the object. |
| Name | Type | Description |
tooltipProps | HTMLAttributes<HTMLElement> | Props for the tooltip element. |
Manages state for a tooltip trigger. Tracks whether the tooltip is open, and provides methods to toggle this state. Ensures only one tooltip is open at a time and controls the delay for showing a tooltip.
useTooltipTriggerState(
(props: TooltipTriggerProps
)): TooltipTriggerStateProvides the accessibility implementation for a Tooltip component.
useTooltip(
(props: AriaTooltipProps,
, state: TooltipTriggerState
)): TooltipAria