-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathtooltip.tsx
More file actions
82 lines (79 loc) · 2.39 KB
/
tooltip.tsx
File metadata and controls
82 lines (79 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Slot } from '@radix-ui/react-slot';
import '@digdir/designsystemet-web'; // Import _ds-floating functionality
import type { HTMLAttributes, ReactElement, RefAttributes } from 'react';
import { forwardRef } from 'react';
import type { DefaultProps, Placement } from '../../types';
import type { MergeRight } from '../../utilities';
export type TooltipProps = MergeRight<
Omit<DefaultProps, 'data-color'> & HTMLAttributes<HTMLDivElement>,
{
/**
* 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.
*/
children: (ReactElement & RefAttributes<HTMLElement>) | string;
/**
* Content of the tooltip
**/
content: string;
/**
* Placement of the tooltip on the trigger.
* @default 'top'
*/
placement?: Placement;
/**
* Whether to enable auto placement.
* @default true
*/
autoPlacement?: 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
*/
open?: boolean;
/**
* @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';
}
>;
/**
* Tooltip component that displays a small piece of information when hovering or focusing on an element.
*
* @example
* <Tooltip content='This is a tooltip'>
* <button>Hover me</button>
* </Tooltip>
*
* @example
* <Tooltip content='This is a tooltip'>
* Hover me
* </Tooltip>
*/
export const Tooltip = forwardRef<HTMLDivElement, TooltipProps>(
function Tooltip(
{ content, placement = 'top', autoPlacement = true, ...rest },
ref,
) {
/* check if children is a string */
const isString = typeof rest.children === 'string';
return (
<Slot
data-tooltip={content}
data-placement={placement}
data-autoplacement={autoPlacement}
suppressHydrationWarning // Since data-tooltip adds aria-label/aria-description
ref={ref}
{...rest}
>
{isString ? <span tabIndex={0}>{rest.children}</span> : rest.children}
</Slot>
);
},
);