@telus-uds/components-base 1.24.1 → 1.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -2
- package/lib/Box/Box.js +3 -1
- package/lib/Divider/Divider.js +3 -0
- package/lib/FlexGrid/FlexGrid.js +3 -1
- package/lib/Link/InlinePressable.js +8 -1
- package/lib/Link/LinkBase.js +6 -8
- package/lib/StackView/StackView.js +3 -1
- package/lib/StackView/StackWrapBox.js +3 -1
- package/lib/StackView/StackWrapGap.js +3 -1
- package/lib/ThemeProvider/utils/styles.js +1 -3
- package/lib/Tooltip/Tooltip.js +85 -158
- package/lib/Tooltip/Tooltip.native.js +357 -0
- package/lib/Tooltip/index.js +6 -1
- package/lib/Tooltip/shared.js +39 -0
- package/lib/Typography/Typography.js +30 -3
- package/lib/utils/floating-ui/index.js +43 -0
- package/lib/utils/floating-ui/index.native.js +43 -0
- package/lib-module/Box/Box.js +3 -1
- package/lib-module/Divider/Divider.js +3 -0
- package/lib-module/FlexGrid/FlexGrid.js +3 -1
- package/lib-module/Link/InlinePressable.js +8 -1
- package/lib-module/Link/LinkBase.js +6 -8
- package/lib-module/StackView/StackView.js +3 -1
- package/lib-module/StackView/StackWrapBox.js +3 -1
- package/lib-module/StackView/StackWrapGap.js +3 -1
- package/lib-module/ThemeProvider/utils/styles.js +1 -3
- package/lib-module/Tooltip/Tooltip.js +85 -155
- package/lib-module/Tooltip/Tooltip.native.js +326 -0
- package/lib-module/Tooltip/index.js +4 -1
- package/lib-module/Tooltip/shared.js +27 -0
- package/lib-module/Typography/Typography.js +29 -3
- package/lib-module/utils/floating-ui/index.js +1 -0
- package/lib-module/utils/floating-ui/index.native.js +1 -0
- package/package.json +4 -2
- package/src/Box/Box.jsx +1 -0
- package/src/Divider/Divider.jsx +3 -0
- package/src/FlexGrid/FlexGrid.jsx +1 -0
- package/src/Link/InlinePressable.jsx +9 -3
- package/src/Link/LinkBase.jsx +17 -10
- package/src/StackView/StackView.jsx +1 -0
- package/src/StackView/StackWrapBox.jsx +1 -0
- package/src/StackView/StackWrapGap.jsx +1 -0
- package/src/ThemeProvider/utils/styles.js +1 -3
- package/src/Tooltip/Tooltip.jsx +79 -156
- package/src/Tooltip/Tooltip.native.jsx +283 -0
- package/src/Tooltip/index.js +5 -1
- package/src/Tooltip/shared.js +27 -0
- package/src/Typography/Typography.jsx +27 -2
- package/src/utils/floating-ui/index.js +1 -0
- package/src/utils/floating-ui/index.native.js +1 -0
package/src/Tooltip/Tooltip.jsx
CHANGED
|
@@ -1,18 +1,10 @@
|
|
|
1
1
|
import React, { forwardRef, useEffect, useRef, useState } from 'react'
|
|
2
|
-
import {
|
|
2
|
+
import { Platform, Pressable, StyleSheet, Text, View } from 'react-native'
|
|
3
|
+
import propTypes from './shared'
|
|
4
|
+
import { useFloating, arrow, offset, shift, flip, autoPlacement } from '../utils/floating-ui'
|
|
3
5
|
|
|
4
|
-
import PropTypes from 'prop-types'
|
|
5
6
|
import { applyShadowToken, applyTextStyles, useThemeTokens } from '../ThemeProvider'
|
|
6
|
-
import {
|
|
7
|
-
a11yProps,
|
|
8
|
-
getTokensPropType,
|
|
9
|
-
selectSystemProps,
|
|
10
|
-
selectTokens,
|
|
11
|
-
variantProp,
|
|
12
|
-
viewProps
|
|
13
|
-
} from '../utils'
|
|
14
|
-
import Backdrop from './Backdrop'
|
|
15
|
-
import getTooltipPosition from './getTooltipPosition'
|
|
7
|
+
import { a11yProps, selectSystemProps, selectTokens, viewProps } from '../utils'
|
|
16
8
|
import TooltipButton from '../TooltipButton'
|
|
17
9
|
import useCopy from '../utils/useCopy'
|
|
18
10
|
import dictionary from './dictionary'
|
|
@@ -38,53 +30,34 @@ const selectTooltipShadowStyles = ({ shadow, borderRadius }) => ({
|
|
|
38
30
|
borderRadius,
|
|
39
31
|
...applyShadowToken(shadow)
|
|
40
32
|
})
|
|
41
|
-
|
|
42
|
-
|
|
33
|
+
|
|
34
|
+
const selectMobileTooltipPositionStyles = ({ top, left, position }) => {
|
|
35
|
+
return { top, left, position }
|
|
43
36
|
}
|
|
44
|
-
|
|
37
|
+
|
|
38
|
+
const selectMobileArrowStyles = (
|
|
45
39
|
{ backgroundColor, arrowWidth, arrowBorderRadius, shadow },
|
|
46
|
-
{
|
|
40
|
+
{ placement, x: arrowX, y: arrowY }
|
|
47
41
|
) => {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const horizontalOffset = rectangleSide / 2
|
|
55
|
-
|
|
56
|
-
// percentage-based absolute positioning doesn't act well on native, so we have to
|
|
57
|
-
// calculate the pixel values
|
|
58
|
-
const directionalStyles = {
|
|
59
|
-
above: {
|
|
60
|
-
bottom: verticalOffset,
|
|
61
|
-
left: tooltipWidth / 2 - horizontalOffset,
|
|
62
|
-
transform: [{ rotateZ: '45deg' }]
|
|
63
|
-
},
|
|
64
|
-
below: {
|
|
65
|
-
top: verticalOffset,
|
|
66
|
-
left: tooltipWidth / 2 - horizontalOffset,
|
|
67
|
-
transform: [{ rotateZ: '-135deg' }]
|
|
68
|
-
},
|
|
69
|
-
left: {
|
|
70
|
-
right: verticalOffset,
|
|
71
|
-
top: tooltipHeight / 2 - horizontalOffset,
|
|
72
|
-
transform: [{ rotateZ: '-45deg' }]
|
|
73
|
-
},
|
|
74
|
-
right: {
|
|
75
|
-
left: verticalOffset,
|
|
76
|
-
top: tooltipHeight / 2 - horizontalOffset,
|
|
77
|
-
transform: [{ rotateZ: '135deg' }]
|
|
78
|
-
}
|
|
79
|
-
}
|
|
42
|
+
const staticSide = {
|
|
43
|
+
top: 'bottom',
|
|
44
|
+
right: 'left',
|
|
45
|
+
bottom: 'top',
|
|
46
|
+
left: 'right'
|
|
47
|
+
}[placement.split('-')[0]]
|
|
80
48
|
|
|
81
49
|
return {
|
|
82
50
|
backgroundColor,
|
|
83
|
-
width:
|
|
84
|
-
height:
|
|
51
|
+
width: arrowWidth,
|
|
52
|
+
height: arrowWidth,
|
|
85
53
|
borderBottomRightRadius: arrowBorderRadius, // this corner will be the arrow tip after rotation
|
|
54
|
+
transform: [{ rotateZ: '45deg' }],
|
|
86
55
|
...applyShadowToken(shadow),
|
|
87
|
-
|
|
56
|
+
left: arrowX != null && arrowX,
|
|
57
|
+
top: arrowY != null && arrowY,
|
|
58
|
+
right: '',
|
|
59
|
+
bottom: '',
|
|
60
|
+
[staticSide]: '-4px'
|
|
88
61
|
}
|
|
89
62
|
}
|
|
90
63
|
|
|
@@ -111,26 +84,37 @@ const defaultControl = (pressableState, variant) => (
|
|
|
111
84
|
const Tooltip = forwardRef(
|
|
112
85
|
({ children, content, position = 'auto', copy = 'en', tokens, variant, ...rest }, ref) => {
|
|
113
86
|
const [isOpen, setIsOpen] = useState(false)
|
|
87
|
+
const arrowRef = useRef()
|
|
114
88
|
|
|
115
|
-
const controlRef = useRef()
|
|
116
|
-
const [controlLayout, setControlLayout] = useState(null)
|
|
117
89
|
const [tooltipDimensions, setTooltipDimensions] = useState(null)
|
|
118
|
-
const [windowDimensions, setWindowDimensions] = useState(Dimensions.get('window'))
|
|
119
|
-
const [tooltipPosition, setTooltipPosition] = useState(null)
|
|
120
90
|
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
setWindowDimensions(window)
|
|
129
|
-
})
|
|
91
|
+
const positions = {
|
|
92
|
+
auto: undefined, // Auto needs to specifically bet set to undefined
|
|
93
|
+
above: 'top',
|
|
94
|
+
right: 'right',
|
|
95
|
+
below: 'bottom',
|
|
96
|
+
left: 'left'
|
|
97
|
+
}
|
|
130
98
|
|
|
131
|
-
|
|
99
|
+
// "AutoPlacement" can't be used with "flip"
|
|
100
|
+
const additionalMiddleware = position !== 'auto' ? autoPlacement : flip
|
|
101
|
+
|
|
102
|
+
const {
|
|
103
|
+
x,
|
|
104
|
+
y,
|
|
105
|
+
reference,
|
|
106
|
+
floating,
|
|
107
|
+
strategy,
|
|
108
|
+
placement,
|
|
109
|
+
middlewareData: { arrow: arrowData = {} }
|
|
110
|
+
} = useFloating({
|
|
111
|
+
placement: positions[position],
|
|
112
|
+
middleware: [shift(), additionalMiddleware(), offset(15), arrow({ element: arrowRef })]
|
|
132
113
|
})
|
|
133
114
|
|
|
115
|
+
const getCopy = useCopy({ dictionary, copy })
|
|
116
|
+
const themeTokens = useThemeTokens('Tooltip', tokens, variant)
|
|
117
|
+
|
|
134
118
|
const toggleIsOpen = () => setIsOpen(!isOpen)
|
|
135
119
|
const close = () => setIsOpen(false)
|
|
136
120
|
|
|
@@ -161,56 +145,11 @@ const Tooltip = forwardRef(
|
|
|
161
145
|
}
|
|
162
146
|
|
|
163
147
|
useEffect(() => {
|
|
164
|
-
if (isOpen) {
|
|
165
|
-
controlRef.current.measureInWindow((x, y, width, height) => {
|
|
166
|
-
setControlLayout({ x, y, width, height })
|
|
167
|
-
})
|
|
168
|
-
} else {
|
|
169
|
-
setControlLayout(null)
|
|
148
|
+
if (!isOpen) {
|
|
170
149
|
setTooltipDimensions(null)
|
|
171
|
-
setTooltipPosition(null)
|
|
172
150
|
}
|
|
173
151
|
}, [isOpen])
|
|
174
152
|
|
|
175
|
-
useEffect(() => {
|
|
176
|
-
setIsOpen(false)
|
|
177
|
-
}, [windowDimensions])
|
|
178
|
-
|
|
179
|
-
useEffect(() => {
|
|
180
|
-
if (
|
|
181
|
-
(tooltipPosition !== null && !tooltipPosition?.isNormalized) ||
|
|
182
|
-
!isOpen ||
|
|
183
|
-
controlLayout === null ||
|
|
184
|
-
tooltipDimensions == null
|
|
185
|
-
) {
|
|
186
|
-
return
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const updatedPosition = getTooltipPosition(position, {
|
|
190
|
-
controlLayout,
|
|
191
|
-
tooltipDimensions,
|
|
192
|
-
windowDimensions,
|
|
193
|
-
arrowWidth,
|
|
194
|
-
arrowOffset
|
|
195
|
-
})
|
|
196
|
-
|
|
197
|
-
// avoid ending up in an infinite normalization loop
|
|
198
|
-
if (tooltipPosition?.isNormalized && updatedPosition.isNormalized) {
|
|
199
|
-
return
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
setTooltipPosition(updatedPosition)
|
|
203
|
-
}, [
|
|
204
|
-
isOpen,
|
|
205
|
-
position,
|
|
206
|
-
tooltipDimensions,
|
|
207
|
-
controlLayout,
|
|
208
|
-
windowDimensions,
|
|
209
|
-
arrowWidth,
|
|
210
|
-
arrowOffset,
|
|
211
|
-
tooltipPosition
|
|
212
|
-
])
|
|
213
|
-
|
|
214
153
|
const control = children !== undefined ? children : defaultControl
|
|
215
154
|
const pressableStyles =
|
|
216
155
|
control === defaultControl ? Platform.select({ web: { outline: 'none' } }) : undefined
|
|
@@ -218,10 +157,10 @@ const Tooltip = forwardRef(
|
|
|
218
157
|
control === defaultControl ? { top: 10, bottom: 10, left: 10, right: 10 } : undefined
|
|
219
158
|
|
|
220
159
|
return (
|
|
221
|
-
<View style={staticStyles.container} {...selectProps(rest)}>
|
|
160
|
+
<View style={staticStyles.container} {...selectProps(rest)} ref={ref}>
|
|
222
161
|
<Pressable
|
|
223
162
|
onPress={toggleIsOpen}
|
|
224
|
-
ref={
|
|
163
|
+
ref={reference}
|
|
225
164
|
onBlur={close}
|
|
226
165
|
style={pressableStyles}
|
|
227
166
|
hitSlop={pressableHitSlop}
|
|
@@ -233,30 +172,34 @@ const Tooltip = forwardRef(
|
|
|
233
172
|
: control}
|
|
234
173
|
</Pressable>
|
|
235
174
|
{isOpen && (
|
|
236
|
-
<
|
|
175
|
+
<View
|
|
176
|
+
ref={floating}
|
|
177
|
+
style={[
|
|
178
|
+
selectTooltipShadowStyles(themeTokens),
|
|
179
|
+
staticStyles.tooltip, // applied separately so that it doesn't cover the arrow
|
|
180
|
+
selectMobileTooltipPositionStyles({
|
|
181
|
+
position: strategy,
|
|
182
|
+
top: y ?? 0,
|
|
183
|
+
left: x ?? 0
|
|
184
|
+
})
|
|
185
|
+
]}
|
|
186
|
+
onLayout={onTooltipLayout}
|
|
187
|
+
accessibilityRole="alert"
|
|
188
|
+
>
|
|
237
189
|
<View
|
|
238
|
-
ref={ref}
|
|
239
190
|
style={[
|
|
240
|
-
staticStyles.
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
191
|
+
staticStyles.arrow,
|
|
192
|
+
selectMobileArrowStyles(themeTokens, {
|
|
193
|
+
...arrowData,
|
|
194
|
+
placement
|
|
195
|
+
})
|
|
245
196
|
]}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
>
|
|
249
|
-
<
|
|
250
|
-
style={[
|
|
251
|
-
staticStyles.arrow,
|
|
252
|
-
tooltipPosition && selectArrowStyles(themeTokens, tooltipPosition)
|
|
253
|
-
]}
|
|
254
|
-
/>
|
|
255
|
-
<View style={selectTooltipStyles(themeTokens)}>
|
|
256
|
-
<Text style={selectTextStyles(themeTokens)}>{content}</Text>
|
|
257
|
-
</View>
|
|
197
|
+
ref={arrowRef}
|
|
198
|
+
/>
|
|
199
|
+
<View style={selectTooltipStyles(themeTokens)}>
|
|
200
|
+
<Text style={selectTextStyles(themeTokens)}>{content}</Text>
|
|
258
201
|
</View>
|
|
259
|
-
</
|
|
202
|
+
</View>
|
|
260
203
|
)}
|
|
261
204
|
</View>
|
|
262
205
|
)
|
|
@@ -266,32 +209,15 @@ Tooltip.displayName = 'Tooltip'
|
|
|
266
209
|
|
|
267
210
|
Tooltip.propTypes = {
|
|
268
211
|
...selectedSystemPropTypes,
|
|
269
|
-
|
|
270
|
-
* Used to render the control (i.e. tooltip trigger). If a render function is used it will receive the
|
|
271
|
-
* pressable state and tooltip variant as an argument.
|
|
272
|
-
*/
|
|
273
|
-
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
|
|
274
|
-
/**
|
|
275
|
-
* The message. Can be raw text or text components.
|
|
276
|
-
*/
|
|
277
|
-
content: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
|
|
278
|
-
/**
|
|
279
|
-
* Select English or French copy for the accessible label.
|
|
280
|
-
*/
|
|
281
|
-
copy: PropTypes.oneOf(['en', 'fr']),
|
|
282
|
-
/**
|
|
283
|
-
* Use to place the tooltip in a specific location (only if it fits within viewport).
|
|
284
|
-
*/
|
|
285
|
-
position: PropTypes.oneOf(['auto', 'above', 'right', 'below', 'left']),
|
|
286
|
-
tokens: getTokensPropType('Tooltip'),
|
|
287
|
-
variant: variantProp.propType
|
|
212
|
+
...propTypes
|
|
288
213
|
}
|
|
289
214
|
|
|
290
215
|
export default Tooltip
|
|
291
216
|
|
|
292
217
|
const staticStyles = StyleSheet.create({
|
|
293
218
|
container: {
|
|
294
|
-
alignItems: 'flex-start'
|
|
219
|
+
alignItems: 'flex-start',
|
|
220
|
+
position: 'absolute'
|
|
295
221
|
},
|
|
296
222
|
tooltip: {
|
|
297
223
|
position: 'absolute',
|
|
@@ -299,9 +225,6 @@ const staticStyles = StyleSheet.create({
|
|
|
299
225
|
top: 0,
|
|
300
226
|
left: 0
|
|
301
227
|
},
|
|
302
|
-
tooltipHidden: {
|
|
303
|
-
opacity: 0
|
|
304
|
-
},
|
|
305
228
|
arrow: {
|
|
306
229
|
position: 'absolute'
|
|
307
230
|
}
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import React, { forwardRef, useEffect, useRef, useState } from 'react'
|
|
2
|
+
import { Dimensions, Platform, Pressable, StyleSheet, Text, View } from 'react-native'
|
|
3
|
+
import propTypes from './shared'
|
|
4
|
+
|
|
5
|
+
import { applyShadowToken, applyTextStyles, useThemeTokens } from '../ThemeProvider'
|
|
6
|
+
import { a11yProps, selectSystemProps, selectTokens, viewProps } from '../utils'
|
|
7
|
+
import Backdrop from './Backdrop'
|
|
8
|
+
import getTooltipPosition from './getTooltipPosition'
|
|
9
|
+
import TooltipButton from '../TooltipButton'
|
|
10
|
+
import useCopy from '../utils/useCopy'
|
|
11
|
+
import dictionary from './dictionary'
|
|
12
|
+
|
|
13
|
+
const [selectProps, selectedSystemPropTypes] = selectSystemProps([a11yProps, viewProps])
|
|
14
|
+
|
|
15
|
+
const selectTooltipStyles = ({
|
|
16
|
+
backgroundColor,
|
|
17
|
+
paddingTop,
|
|
18
|
+
paddingBottom,
|
|
19
|
+
paddingLeft,
|
|
20
|
+
paddingRight,
|
|
21
|
+
borderRadius
|
|
22
|
+
}) => ({
|
|
23
|
+
backgroundColor,
|
|
24
|
+
paddingTop,
|
|
25
|
+
paddingBottom,
|
|
26
|
+
paddingLeft,
|
|
27
|
+
paddingRight,
|
|
28
|
+
borderRadius
|
|
29
|
+
})
|
|
30
|
+
const selectTooltipShadowStyles = ({ shadow, borderRadius }) => ({
|
|
31
|
+
borderRadius,
|
|
32
|
+
...applyShadowToken(shadow)
|
|
33
|
+
})
|
|
34
|
+
const selectTooltipPositionStyles = ({ top, left, width }) => {
|
|
35
|
+
return { top, left, width }
|
|
36
|
+
}
|
|
37
|
+
const selectArrowStyles = (
|
|
38
|
+
{ backgroundColor, arrowWidth, arrowBorderRadius, shadow },
|
|
39
|
+
{ position, width: tooltipWidth, height: tooltipHeight }
|
|
40
|
+
) => {
|
|
41
|
+
// the arrow width is actually a diagonal of the rectangle that we'll use as a tip
|
|
42
|
+
const rectangleSide = Math.sqrt((arrowWidth * arrowWidth) / 2)
|
|
43
|
+
|
|
44
|
+
// position the arrow at the side and center of the tooltip - this happens before rotation
|
|
45
|
+
// so we use the rectangle size as basis
|
|
46
|
+
const verticalOffset = (-1 * rectangleSide) / 2
|
|
47
|
+
const horizontalOffset = rectangleSide / 2
|
|
48
|
+
|
|
49
|
+
// percentage-based absolute positioning doesn't act well on native, so we have to
|
|
50
|
+
// calculate the pixel values
|
|
51
|
+
const directionalStyles = {
|
|
52
|
+
above: {
|
|
53
|
+
bottom: verticalOffset,
|
|
54
|
+
left: tooltipWidth / 2 - horizontalOffset,
|
|
55
|
+
transform: [{ rotateZ: '45deg' }]
|
|
56
|
+
},
|
|
57
|
+
below: {
|
|
58
|
+
top: verticalOffset,
|
|
59
|
+
left: tooltipWidth / 2 - horizontalOffset,
|
|
60
|
+
transform: [{ rotateZ: '-135deg' }]
|
|
61
|
+
},
|
|
62
|
+
left: {
|
|
63
|
+
right: verticalOffset,
|
|
64
|
+
top: tooltipHeight / 2 - horizontalOffset,
|
|
65
|
+
transform: [{ rotateZ: '-45deg' }]
|
|
66
|
+
},
|
|
67
|
+
right: {
|
|
68
|
+
left: verticalOffset,
|
|
69
|
+
top: tooltipHeight / 2 - horizontalOffset,
|
|
70
|
+
transform: [{ rotateZ: '135deg' }]
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
backgroundColor,
|
|
76
|
+
width: rectangleSide,
|
|
77
|
+
height: rectangleSide,
|
|
78
|
+
borderBottomRightRadius: arrowBorderRadius, // this corner will be the arrow tip after rotation
|
|
79
|
+
...applyShadowToken(shadow),
|
|
80
|
+
...directionalStyles[position]
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const selectTextStyles = (tokens) => applyTextStyles(selectTokens('Typography', tokens))
|
|
85
|
+
const defaultControl = (pressableState, variant) => (
|
|
86
|
+
<TooltipButton pressableState={pressableState} variant={variant} />
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Tooltip provides a descriptive and detailed explanation or instructions. It can be used next to an input label
|
|
91
|
+
* to help a user fill it in, or as a standalone component.
|
|
92
|
+
*
|
|
93
|
+
* By default the TooltipButton component will be used as a control for triggering the tooltip, but you may attach
|
|
94
|
+
* a tooltip to any other component. A render function can be used to adjust the control's styling on state changes (hover, focus, etc.).
|
|
95
|
+
*
|
|
96
|
+
* ### Positioning
|
|
97
|
+
* By default a Tooltip will be automatically positioned in a way that ensures it fits within the viewport.
|
|
98
|
+
* You may suggest a position with a prop - it will be used, unless the tooltip would end up outside the viewport.
|
|
99
|
+
*
|
|
100
|
+
* ### Usage criteria
|
|
101
|
+
* - You may use one when the information is useful only to a small percentage of users (ie. tech savvy people wouldn't need this info).
|
|
102
|
+
* - Tooltips may also be useful when vertical space is an issue.
|
|
103
|
+
*/
|
|
104
|
+
const Tooltip = forwardRef(
|
|
105
|
+
({ children, content, position = 'auto', copy = 'en', tokens, variant, ...rest }, ref) => {
|
|
106
|
+
const [isOpen, setIsOpen] = useState(false)
|
|
107
|
+
|
|
108
|
+
const controlRef = useRef()
|
|
109
|
+
const [controlLayout, setControlLayout] = useState(null)
|
|
110
|
+
const [tooltipDimensions, setTooltipDimensions] = useState(null)
|
|
111
|
+
const [windowDimensions, setWindowDimensions] = useState(Dimensions.get('window'))
|
|
112
|
+
const [tooltipPosition, setTooltipPosition] = useState(null)
|
|
113
|
+
|
|
114
|
+
const getCopy = useCopy({ dictionary, copy })
|
|
115
|
+
const themeTokens = useThemeTokens('Tooltip', tokens, variant)
|
|
116
|
+
|
|
117
|
+
const { arrowWidth, arrowOffset } = themeTokens
|
|
118
|
+
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
const subscription = Dimensions.addEventListener('change', ({ window }) => {
|
|
121
|
+
setWindowDimensions(window)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
return () => subscription?.remove()
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
const toggleIsOpen = () => setIsOpen(!isOpen)
|
|
128
|
+
const close = () => setIsOpen(false)
|
|
129
|
+
|
|
130
|
+
const getPressableState = ({ pressed, hovered, focused }) => ({
|
|
131
|
+
pressed,
|
|
132
|
+
hover: hovered,
|
|
133
|
+
focus: focused
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
const onTooltipLayout = ({
|
|
137
|
+
nativeEvent: {
|
|
138
|
+
layout: { width, height }
|
|
139
|
+
}
|
|
140
|
+
}) => {
|
|
141
|
+
if (
|
|
142
|
+
tooltipDimensions === null ||
|
|
143
|
+
tooltipDimensions.width !== width ||
|
|
144
|
+
tooltipDimensions.height !== height
|
|
145
|
+
) {
|
|
146
|
+
setTooltipDimensions({
|
|
147
|
+
width: Platform.select({
|
|
148
|
+
web: width + 0.3, // avoids often unnecessary line breaks due to subpixel rendering of fonts
|
|
149
|
+
native: width
|
|
150
|
+
}),
|
|
151
|
+
height
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
useEffect(() => {
|
|
157
|
+
if (isOpen) {
|
|
158
|
+
controlRef.current.measureInWindow((x, y, width, height) => {
|
|
159
|
+
setControlLayout({ x, y, width, height })
|
|
160
|
+
})
|
|
161
|
+
} else {
|
|
162
|
+
setControlLayout(null)
|
|
163
|
+
setTooltipDimensions(null)
|
|
164
|
+
setTooltipPosition(null)
|
|
165
|
+
}
|
|
166
|
+
}, [isOpen])
|
|
167
|
+
|
|
168
|
+
useEffect(() => {
|
|
169
|
+
setIsOpen(false)
|
|
170
|
+
}, [windowDimensions])
|
|
171
|
+
|
|
172
|
+
useEffect(() => {
|
|
173
|
+
if (
|
|
174
|
+
(tooltipPosition !== null && !tooltipPosition?.isNormalized) ||
|
|
175
|
+
!isOpen ||
|
|
176
|
+
controlLayout === null ||
|
|
177
|
+
tooltipDimensions == null
|
|
178
|
+
) {
|
|
179
|
+
return
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const updatedPosition = getTooltipPosition(position, {
|
|
183
|
+
controlLayout,
|
|
184
|
+
tooltipDimensions,
|
|
185
|
+
windowDimensions,
|
|
186
|
+
arrowWidth,
|
|
187
|
+
arrowOffset
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
// avoid ending up in an infinite normalization loop
|
|
191
|
+
if (tooltipPosition?.isNormalized && updatedPosition.isNormalized) {
|
|
192
|
+
return
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
setTooltipPosition(updatedPosition)
|
|
196
|
+
}, [
|
|
197
|
+
isOpen,
|
|
198
|
+
position,
|
|
199
|
+
tooltipDimensions,
|
|
200
|
+
controlLayout,
|
|
201
|
+
windowDimensions,
|
|
202
|
+
arrowWidth,
|
|
203
|
+
arrowOffset,
|
|
204
|
+
tooltipPosition
|
|
205
|
+
])
|
|
206
|
+
|
|
207
|
+
const control = children !== undefined ? children : defaultControl
|
|
208
|
+
const pressableStyles =
|
|
209
|
+
control === defaultControl ? Platform.select({ web: { outline: 'none' } }) : undefined
|
|
210
|
+
const pressableHitSlop =
|
|
211
|
+
control === defaultControl ? { top: 10, bottom: 10, left: 10, right: 10 } : undefined
|
|
212
|
+
|
|
213
|
+
return (
|
|
214
|
+
<View style={staticStyles.container} {...selectProps(rest)}>
|
|
215
|
+
<Pressable
|
|
216
|
+
onPress={toggleIsOpen}
|
|
217
|
+
ref={controlRef}
|
|
218
|
+
onBlur={close}
|
|
219
|
+
style={pressableStyles}
|
|
220
|
+
hitSlop={pressableHitSlop}
|
|
221
|
+
accessibilityLabel={getCopy('a11yText')}
|
|
222
|
+
accessibilityRole="button"
|
|
223
|
+
>
|
|
224
|
+
{typeof control === 'function'
|
|
225
|
+
? (pressableState) => control(getPressableState(pressableState), variant)
|
|
226
|
+
: control}
|
|
227
|
+
</Pressable>
|
|
228
|
+
{isOpen && (
|
|
229
|
+
<Backdrop onPress={close}>
|
|
230
|
+
<View
|
|
231
|
+
ref={ref}
|
|
232
|
+
style={[
|
|
233
|
+
staticStyles.tooltip,
|
|
234
|
+
selectTooltipShadowStyles(themeTokens), // applied separately so that it doesn't cover the arrow
|
|
235
|
+
tooltipPosition && selectTooltipPositionStyles(tooltipPosition),
|
|
236
|
+
(tooltipPosition === null || tooltipPosition?.isNormalized) &&
|
|
237
|
+
staticStyles.tooltipHidden // visually hide the tooltip until we have a final measurement
|
|
238
|
+
]}
|
|
239
|
+
onLayout={onTooltipLayout}
|
|
240
|
+
accessibilityRole="alert"
|
|
241
|
+
>
|
|
242
|
+
<View
|
|
243
|
+
style={[
|
|
244
|
+
staticStyles.arrow,
|
|
245
|
+
tooltipPosition && selectArrowStyles(themeTokens, tooltipPosition)
|
|
246
|
+
]}
|
|
247
|
+
/>
|
|
248
|
+
<View style={selectTooltipStyles(themeTokens)}>
|
|
249
|
+
<Text style={selectTextStyles(themeTokens)}>{content}</Text>
|
|
250
|
+
</View>
|
|
251
|
+
</View>
|
|
252
|
+
</Backdrop>
|
|
253
|
+
)}
|
|
254
|
+
</View>
|
|
255
|
+
)
|
|
256
|
+
}
|
|
257
|
+
)
|
|
258
|
+
Tooltip.displayName = 'NativeTooltip'
|
|
259
|
+
|
|
260
|
+
Tooltip.propTypes = {
|
|
261
|
+
...selectedSystemPropTypes,
|
|
262
|
+
...propTypes
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export default Tooltip
|
|
266
|
+
|
|
267
|
+
const staticStyles = StyleSheet.create({
|
|
268
|
+
container: {
|
|
269
|
+
alignItems: 'flex-start'
|
|
270
|
+
},
|
|
271
|
+
tooltip: {
|
|
272
|
+
position: 'absolute',
|
|
273
|
+
maxWidth: 240,
|
|
274
|
+
top: 0,
|
|
275
|
+
left: 0
|
|
276
|
+
},
|
|
277
|
+
tooltipHidden: {
|
|
278
|
+
opacity: 0
|
|
279
|
+
},
|
|
280
|
+
arrow: {
|
|
281
|
+
position: 'absolute'
|
|
282
|
+
}
|
|
283
|
+
})
|
package/src/Tooltip/index.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Dimensions } from 'react-native'
|
|
2
|
+
import NonNativeTooltip from './Tooltip'
|
|
3
|
+
import NativeToolTip from './Tooltip.native'
|
|
4
|
+
|
|
5
|
+
const Tooltip = !Dimensions.get('screen').width <= 340 ? NativeToolTip : NonNativeTooltip
|
|
2
6
|
|
|
3
7
|
export default Tooltip
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import PropTypes from 'prop-types'
|
|
2
|
+
|
|
3
|
+
import { getTokensPropType, variantProp } from '../utils'
|
|
4
|
+
|
|
5
|
+
const propTypes = {
|
|
6
|
+
/**
|
|
7
|
+
* Used to render the control (i.e. tooltip trigger). If a render function is used it will receive the
|
|
8
|
+
* pressable state and tooltip variant as an argument.
|
|
9
|
+
*/
|
|
10
|
+
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
|
|
11
|
+
/**
|
|
12
|
+
* The message. Can be raw text or text components.
|
|
13
|
+
*/
|
|
14
|
+
content: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
|
|
15
|
+
/**
|
|
16
|
+
* Select English or French copy for the accessible label.
|
|
17
|
+
*/
|
|
18
|
+
copy: PropTypes.oneOf(['en', 'fr']),
|
|
19
|
+
/**
|
|
20
|
+
* Use to place the tooltip in a specific location (only if it fits within viewport).
|
|
21
|
+
*/
|
|
22
|
+
position: PropTypes.oneOf(['auto', 'above', 'right', 'below', 'left']),
|
|
23
|
+
tokens: getTokensPropType('Tooltip'),
|
|
24
|
+
variant: variantProp.propType
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default propTypes
|
|
@@ -60,6 +60,7 @@ const Typography = forwardRef(
|
|
|
60
60
|
const viewport = useViewport()
|
|
61
61
|
const themeTokens = useThemeTokens('Typography', tokens, variant, { viewport })
|
|
62
62
|
const { themeOptions } = useTheme()
|
|
63
|
+
|
|
63
64
|
const resolvedTextProps = {
|
|
64
65
|
...selectTextProps(rest),
|
|
65
66
|
style: selectTextStyles(
|
|
@@ -71,17 +72,41 @@ const Typography = forwardRef(
|
|
|
71
72
|
}
|
|
72
73
|
|
|
73
74
|
const containerProps = {
|
|
75
|
+
accessibilityRole,
|
|
74
76
|
...getA11yPropsFromHtmlTag(tag, accessibilityRole),
|
|
75
77
|
...selectContainerProps(rest)
|
|
76
78
|
}
|
|
77
79
|
|
|
80
|
+
const resetTagStyling = (child) => {
|
|
81
|
+
if (typeof child === 'object' && (child?.type === 'sub' || child?.type === 'sup')) {
|
|
82
|
+
const sanitizedChild = React.cloneElement(child, {
|
|
83
|
+
style: {
|
|
84
|
+
...child?.props?.style,
|
|
85
|
+
lineHeight: 0
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
return sanitizedChild
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return child
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const sanitizeChildren = () => {
|
|
96
|
+
if (Array.isArray(children)) {
|
|
97
|
+
return children.map(resetTagStyling)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return resetTagStyling(children)
|
|
101
|
+
}
|
|
102
|
+
|
|
78
103
|
return block ? (
|
|
79
104
|
<View ref={ref} {...containerProps}>
|
|
80
|
-
<Text {...resolvedTextProps}>{children}</Text>
|
|
105
|
+
<Text {...resolvedTextProps}>{sanitizeChildren(children)}</Text>
|
|
81
106
|
</View>
|
|
82
107
|
) : (
|
|
83
108
|
<Text ref={ref} {...containerProps} {...resolvedTextProps}>
|
|
84
|
-
{children}
|
|
109
|
+
{sanitizeChildren(children)}
|
|
85
110
|
</Text>
|
|
86
111
|
)
|
|
87
112
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useFloating, arrow, offset, shift, flip, autoPlacement } from '@floating-ui/react-dom'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useFloating, arrow, offset, shift, flip, autoPlacement } from '@floating-ui/react-native'
|