Components
Tooltip
Tooltip displays brief information when the user hovers over or focuses on an element. It is used for secondary information, such as explaining the meaning of an icon.
React
const PreviewEn = () => { return ( <Tooltip content='Copy' placement='top'> <Button icon aria-label='Copy'> <FilesIcon aria-hidden /> </Button> </Tooltip> ); }; render(<PreviewEn />)
- children
- Description
The element or string that triggers the tooltip. @note If it is a string, it will be wrapped in a span. @note If it is an element, it needs to be able to receive a ref.
- Type
string | (ReactElement<unknown, string | JSXElementConstructor<any>> & RefAttributes<HTMLElement>)
- content
- Description
Content of the tooltip
- Type
string
- placement
- Description
Placement of the tooltip on the trigger.
- Type
"top" | "right" | "bottom" | "left"- Default
top
- autoPlacement
- Description
Whether to enable auto placement.
- Type
boolean- Default
true
- open
- Description
Whether the tooltip is open or not. This overrides the internal state of the tooltip.
- Type
boolean
- type
- Description
Override if `aria-describedby` or `aria-labelledby` is used. By default, if the trigger element has no inner text, `aria-labelledby` is used.
- Type
"describedby" | "labelledby"
| Name | Type | Default | Description |
|---|---|---|---|
| children | string | (ReactElement<unknown, string | JSXElementConstructor<any>> & RefAttributes<HTMLElement>) | - | The element or string that triggers the tooltip. @note If it is a string, it will be wrapped in a span. @note If it is an element, it needs to be able to receive a ref. |
| content | string | - | Content of the tooltip |
| placement | "top" | "right" | "bottom" | "left" | top | Placement of the tooltip on the trigger. |
| autoPlacement | boolean | true | Whether to enable auto placement. |
| open | boolean | - | Whether the tooltip is open or not. This overrides the internal state of the tooltip. |
| type | "describedby" | "labelledby" | - | Override if `aria-describedby` or `aria-labelledby` is used. By default, if the trigger element has no inner text, `aria-labelledby` is used. |
Usage
Examples
With text
React
const WithStringEn = () => { return <Tooltip content='Organisation number'>Org. no.</Tooltip>; }; render(<WithStringEn />)
Placement
Use the placement prop to adjust the placement of the Tooltip.
React
const PlacementEn = () => { return ( <Tooltip content='Copy' placement='bottom'> <Button icon aria-label='Copy'> <FilesIcon aria-hidden /> </Button> </Tooltip> ); }; render(<PlacementEn />)
HTML
Tooltip uses a manual popover (mozilla.org).
You must enable hover to open, focus to open, and close again on loss of focus, hover, or Esc.
In React we use floating-ui (floating-ui.com) for placement, and this is a library that can also be used in pure JavaScript applications.
To style the popover as a Tooltip, the ds-tooltip class is used.
data-placement is used to position the arrow correctly.
This attribute is related to a plugin we have created for floating-ui.
It is portable to others using floating-ui, and you can find the code on github.com.
CSS variables and data attributes
| Name | Value |
|---|---|
| --dsc-tooltip-background | var(--ds-color-neutral-text-default) |
| --dsc-tooltip-color | var(--ds-color-neutral-background-default) |
| --dsc-tooltip-border-radius | var(--ds-border-radius-default) |
| --dsc-tooltip-padding | var(--ds-size-1) var(--ds-size-2) |
| --dsc-tooltip-arrow-size | var(--ds-size-2) |
| --dsc-tooltip-transition-duration | 0.2s |
| --dsc-tooltip-transition-delay | 150ms |
No relevant data attributes found.
Edit this page on github.com (opens in a new tab)