-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.tsx
143 lines (127 loc) · 3.45 KB
/
index.tsx
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* External dependencies
*/
// eslint-disable-next-line no-restricted-imports
import * as Ariakit from '@ariakit/react';
/**
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import {
Children,
useContext,
createContext,
forwardRef,
} from '@wordpress/element';
import deprecated from '@wordpress/deprecated';
/**
* Internal dependencies
*/
import type {
TooltipProps,
TooltipInternalContext as TooltipInternalContextType,
} from './types';
import Shortcut from '../shortcut';
import { positionToPlacement } from '../popover/utils';
const TooltipInternalContext = createContext< TooltipInternalContextType >( {
isNestedInTooltip: false,
} );
/**
* Time over anchor to wait before showing tooltip
*/
export const TOOLTIP_DELAY = 700;
const CONTEXT_VALUE = {
isNestedInTooltip: true,
};
function UnforwardedTooltip(
props: TooltipProps,
ref: React.ForwardedRef< any >
) {
const {
children,
delay = TOOLTIP_DELAY,
hideOnClick = true,
placement,
position,
shortcut,
text,
...restProps
} = props;
const { isNestedInTooltip } = useContext( TooltipInternalContext );
const baseId = useInstanceId( Tooltip, 'tooltip' );
const describedById = text || shortcut ? baseId : undefined;
const isOnlyChild = Children.count( children ) === 1;
// console error if more than one child element is added
if ( ! isOnlyChild ) {
if ( 'development' === process.env.NODE_ENV ) {
// eslint-disable-next-line no-console
console.error(
'wp-components.Tooltip should be called with only a single child element.'
);
}
}
// Compute tooltip's placement:
// - give priority to `placement` prop, if defined
// - otherwise, compute it from the legacy `position` prop (if defined)
// - finally, fallback to the default placement: 'bottom'
let computedPlacement;
if ( placement !== undefined ) {
computedPlacement = placement;
} else if ( position !== undefined ) {
computedPlacement = positionToPlacement( position );
deprecated( '`position` prop in wp.components.tooltip', {
since: '6.4',
alternative: '`placement` prop',
} );
}
computedPlacement = computedPlacement || 'bottom';
// Removing the `Ariakit` namespace from the hook name allows ESLint to
// properly identify the hook, and apply the correct linting rules.
const useAriakitTooltipStore = Ariakit.useTooltipStore;
const tooltipStore = useAriakitTooltipStore( {
placement: computedPlacement,
showTimeout: delay,
} );
if ( isNestedInTooltip ) {
return isOnlyChild ? (
<Ariakit.Role { ...restProps } render={ children } />
) : (
children
);
}
return (
<TooltipInternalContext.Provider value={ CONTEXT_VALUE }>
<Ariakit.TooltipAnchor
onClick={ hideOnClick ? tooltipStore.hide : undefined }
store={ tooltipStore }
render={ isOnlyChild ? children : undefined }
ref={ ref }
>
{ isOnlyChild ? undefined : children }
</Ariakit.TooltipAnchor>
{ isOnlyChild && ( text || shortcut ) && (
<Ariakit.Tooltip
{ ...restProps }
className="components-tooltip"
unmountOnHide
gutter={ 4 }
id={ describedById }
overflowPadding={ 0.5 }
store={ tooltipStore }
>
{ text }
{ shortcut && (
<Shortcut
className={
text ? 'components-tooltip__shortcut' : ''
}
shortcut={ shortcut }
/>
) }
</Ariakit.Tooltip>
) }
</TooltipInternalContext.Provider>
);
}
export const Tooltip = forwardRef( UnforwardedTooltip );
export default Tooltip;