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.
<button
class="ds-button"
data-icon="true"
type="button"
aria-label="Copy"
data-tooltip="Copy"
data-placement="top"
data-autoplacement="true"
>
<svg aria-hidden="true" role="img"></svg>
</button>const PreviewEn = () => { return ( <Tooltip content='Copy' placement='top'> <Button icon aria-label='Copy'> <FilesIcon aria-hidden /> </Button> </Tooltip> ); }; render(<PreviewEn />)
Usage
Use the @digdir/designsystemet-web package to implement Tooltip.
You only need to add the data-tooltip attribute to the element that will trigger the tooltip.
For accessibility, the element will automatically receive either aria-label or aria-description, depending on whether it contains text content.
<button data-placement="left" data-tooltip="left" class="ds-button">
left
</button>
Custom element
Use the setTooltipElement function to set a custom tooltip element.
The element will auto automatically be added do document.body if it is not already in the document.
import { setTooltipElement } from @digdir/designsystemet-web
const myTooltip = document.createElement('div');
myTooltip.className = 'ds-tooltip my-custom-tooltip';
setTooltipElement(myTooltip);
Text from another HTML element
You can use the data-tooltip attribute with a CSS ID selector to point to another element that contains tooltip text.
<button data-tooltip="#tooltip-content" class="ds-button">Button</button>
<div id="tooltip-content" class="ds-sr-only">This is the tooltip content.</div>
Tooltip inside Shadow DOM / Web component
When using data-tooltip inside a Shadow DOM, you must manually provide an accessible name or description:
- Use
aria-labelwhen the element contains visuals only (for example, an icon button). - Use
aria-descriptionwhen the element already contains visible text and the tooltip provides additional information.
This is required because the tooltip MutationObserver cannot detect or initialize tooltips inside a Shadow DOM.
Alternatively, you can manually initialize tooltips inside your Shadow DOM by using the initTooltips function:
import { initTooltips } from "@digdir/designsystemet-web";
class MyComponent extends HTMLElement {
connectedCallback() {
setTimeout(initTooltips, 0, this.shadowRoot); // Using setTimeout to let children mount before initializing
}
}
CSS variables and data attributes
Sizes are controlled with data-size and colors with data-color. The component will inherit from the nearest parent with these set.
| Name | Value |
|---|---|
| --dsc-tooltip-arrow-size | var(--ds-size-2) |
| --dsc-tooltip-background | var(--ds-color-neutral-text-default) |
| --dsc-tooltip-border-radius | var(--ds-border-radius-default) |
| --dsc-tooltip-color | var(--ds-color-neutral-background-default) |
| --dsc-tooltip-padding | var(--ds-size-1) var(--ds-size-2) |
| --dsc-tooltip-placement | top |
| --dsc-tooltip-transition-delay | 150ms |
| --dsc-tooltip-transition-duration | 0.2s |
| Name | Value |
|---|---|
| data-overscroll | contain |
| data-tooltip |
React
import { Tooltip, Button } from "@digdir/designsystemet-react";
<Tooltip content="Help text">
<Button>Hover me</Button>
</Tooltip>;
Tooltip
- 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
Placement- 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. @deprecated This should not be used on Tooltip. Use a Popover instead @TODO Look at this prop
- Type
boolean
- type
- Description
@deprecated This prop has no effect. The tooltip will be set as `aria-description` if the component already contains accessible text, and `aria-label` otherwise.
- 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 | Placement | 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. @deprecated This should not be used on Tooltip. Use a Popover instead @TODO Look at this prop |
| type | "describedby" | "labelledby" | - | @deprecated This prop has no effect. The tooltip will be set as `aria-description` if the component already contains accessible text, and `aria-label` otherwise. |