@behivetech/atoms.tooltip 1.0.0 → 1.1.1
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/LICENSE +21 -0
- package/README.md +8 -0
- package/dist/index.cjs +71 -17
- package/dist/index.css +40 -36
- package/dist/index.d.ts +106 -1
- package/dist/index.js +71 -17
- package/dist/index.native.js +263 -0
- package/dist/native/index.d.ts +2 -0
- package/dist/native/tooltip.block.d.ts +100 -0
- package/dist/native/tooltip.block.native.d.ts +2 -0
- package/dist/native/tooltip.d.ts +30 -0
- package/dist/native/tooltip.native.d.ts +35 -0
- package/docs.json +8 -1
- package/index.ts +2 -2
- package/package.json +30 -9
- package/tooltip.block.css +57 -0
- package/tooltip.block.native.ts +112 -0
- package/tooltip.block.ts +64 -0
- package/tooltip.native.tsx +262 -0
- package/tooltip.tsx +13 -11
- package/tooltip.module.scss +0 -55
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// GENERATED from tooltip.block.ts by @behivetech/blocks.builder. Do not edit by hand: run `pnpm block:build`.
|
|
2
|
+
import { createNativeBlock as __bhtCreateNativeBlock } from "@behivetech/blocks.core/native";
|
|
3
|
+
import { StyleSheet as __bhtStyleSheet } from "react-native";
|
|
4
|
+
import type { tooltip as __bht0_source } from "./tooltip.block";
|
|
5
|
+
|
|
6
|
+
// Entries that read no color token are one object in both schemes.
|
|
7
|
+
const __bht0_shared = __bhtStyleSheet.create({
|
|
8
|
+
__disabledTrigger: { display: "flex", flexDirection: "row" },
|
|
9
|
+
"--sideTop__content": { marginBottom: 8 },
|
|
10
|
+
"--sideBottom__content": { marginTop: 8 },
|
|
11
|
+
"--sideLeft__content": { marginRight: 8 },
|
|
12
|
+
"--sideRight__content": { marginLeft: 8 },
|
|
13
|
+
});
|
|
14
|
+
const __bht0_light = __bhtStyleSheet.create({
|
|
15
|
+
__content: {
|
|
16
|
+
paddingTop: 4,
|
|
17
|
+
paddingRight: 8,
|
|
18
|
+
paddingBottom: 4,
|
|
19
|
+
paddingLeft: 8,
|
|
20
|
+
backgroundColor: "#303030",
|
|
21
|
+
color: "#f1f1f1",
|
|
22
|
+
borderTopLeftRadius: 4,
|
|
23
|
+
borderTopRightRadius: 4,
|
|
24
|
+
borderBottomRightRadius: 4,
|
|
25
|
+
borderBottomLeftRadius: 4,
|
|
26
|
+
fontFamily: "Inter",
|
|
27
|
+
fontSize: 12,
|
|
28
|
+
fontWeight: "400",
|
|
29
|
+
lineHeight: 16,
|
|
30
|
+
letterSpacing: 0.3996,
|
|
31
|
+
maxWidth: 256,
|
|
32
|
+
elevation: 8,
|
|
33
|
+
minWidth: 96,
|
|
34
|
+
textAlign: "center",
|
|
35
|
+
},
|
|
36
|
+
"__disabledTrigger:focusVisible": {
|
|
37
|
+
outlineWidth: 3,
|
|
38
|
+
outlineStyle: "solid",
|
|
39
|
+
outlineColor: "#64558f",
|
|
40
|
+
outlineOffset: 2,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
const __bht0_dark = __bhtStyleSheet.create({
|
|
44
|
+
__content: {
|
|
45
|
+
paddingTop: 4,
|
|
46
|
+
paddingRight: 8,
|
|
47
|
+
paddingBottom: 4,
|
|
48
|
+
paddingLeft: 8,
|
|
49
|
+
backgroundColor: "#e2e2e2",
|
|
50
|
+
color: "#303030",
|
|
51
|
+
borderTopLeftRadius: 4,
|
|
52
|
+
borderTopRightRadius: 4,
|
|
53
|
+
borderBottomRightRadius: 4,
|
|
54
|
+
borderBottomLeftRadius: 4,
|
|
55
|
+
fontFamily: "Inter",
|
|
56
|
+
fontSize: 12,
|
|
57
|
+
fontWeight: "400",
|
|
58
|
+
lineHeight: 16,
|
|
59
|
+
letterSpacing: 0.3996,
|
|
60
|
+
maxWidth: 256,
|
|
61
|
+
elevation: 8,
|
|
62
|
+
minWidth: 96,
|
|
63
|
+
textAlign: "center",
|
|
64
|
+
},
|
|
65
|
+
"__disabledTrigger:focusVisible": {
|
|
66
|
+
outlineWidth: 3,
|
|
67
|
+
outlineStyle: "solid",
|
|
68
|
+
outlineColor: "#cebdfe",
|
|
69
|
+
outlineOffset: 2,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
export const tooltip: typeof __bht0_source = __bhtCreateNativeBlock<
|
|
73
|
+
typeof __bht0_source
|
|
74
|
+
>("BHT__Tooltip", {
|
|
75
|
+
structure: {
|
|
76
|
+
children: {
|
|
77
|
+
content: {},
|
|
78
|
+
arrow: {},
|
|
79
|
+
disabledTrigger: { a11y: { role: "group" } },
|
|
80
|
+
},
|
|
81
|
+
variants: {
|
|
82
|
+
sideTop: { children: { content: {} } },
|
|
83
|
+
sideBottom: { children: { content: {} } },
|
|
84
|
+
sideLeft: { children: { content: {} } },
|
|
85
|
+
sideRight: { children: { content: {} } },
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
sheets: {
|
|
89
|
+
light: { ...__bht0_shared, ...__bht0_light },
|
|
90
|
+
dark: { ...__bht0_shared, ...__bht0_dark },
|
|
91
|
+
},
|
|
92
|
+
plan: {
|
|
93
|
+
root: [],
|
|
94
|
+
children: {
|
|
95
|
+
content: [
|
|
96
|
+
{ key: "__content" },
|
|
97
|
+
{ key: "--sideTop__content", when: { variant: "sideTop" } },
|
|
98
|
+
{ key: "--sideBottom__content", when: { variant: "sideBottom" } },
|
|
99
|
+
{ key: "--sideLeft__content", when: { variant: "sideLeft" } },
|
|
100
|
+
{ key: "--sideRight__content", when: { variant: "sideRight" } },
|
|
101
|
+
],
|
|
102
|
+
arrow: [],
|
|
103
|
+
disabledTrigger: [
|
|
104
|
+
{ key: "__disabledTrigger" },
|
|
105
|
+
{
|
|
106
|
+
key: "__disabledTrigger:focusVisible",
|
|
107
|
+
when: { state: "focusVisible" },
|
|
108
|
+
},
|
|
109
|
+
],
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
});
|
package/tooltip.block.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Converted from tooltip.module.scss by `block convert`; this file is the source from here on.
|
|
2
|
+
import { css, defineBlock } from "@behivetech/blocks.core";
|
|
3
|
+
import { tokens } from "@behivetech/blocks.core/tokens";
|
|
4
|
+
const { color, motion, shape, spacing, type } = tokens;
|
|
5
|
+
export const tooltip = defineBlock("BHT__Tooltip", {
|
|
6
|
+
children: {
|
|
7
|
+
content: {
|
|
8
|
+
padding: [spacing[1], spacing[2]],
|
|
9
|
+
backgroundColor: color.inverseSurface,
|
|
10
|
+
color: color.inverseOnSurface,
|
|
11
|
+
borderRadius: shape.cornerExtraSmall,
|
|
12
|
+
fontFamily: type.bodySmall.font,
|
|
13
|
+
fontSize: type.bodySmall.size,
|
|
14
|
+
fontWeight: type.bodySmall.weight,
|
|
15
|
+
lineHeight: type.bodySmall.lineHeight,
|
|
16
|
+
letterSpacing: type.bodySmall.tracking,
|
|
17
|
+
maxWidth: "16rem",
|
|
18
|
+
web: {
|
|
19
|
+
zIndex: "var(--z-tooltip, 600)",
|
|
20
|
+
userSelect: "none",
|
|
21
|
+
"&[data-state=delayed-open]": {
|
|
22
|
+
animation: css`tooltip-in ${motion.durationShort3} ${motion.easingStandardDecelerate}`,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
// Native: the bubble is drawn in the overlay layer, which places it against the trigger.
|
|
26
|
+
native: { elevation: 8, minWidth: "6rem", textAlign: "center" },
|
|
27
|
+
},
|
|
28
|
+
arrow: { web: { fill: color.inverseSurface } },
|
|
29
|
+
// A focusable wrapper around a disabled control: a group, so a screen reader announces it as the
|
|
30
|
+
// container of the control it explains (the span's markup carries the role on the web).
|
|
31
|
+
disabledTrigger: {
|
|
32
|
+
a11y: { role: "group" },
|
|
33
|
+
display: "inline-flex",
|
|
34
|
+
states: {
|
|
35
|
+
focusVisible: {
|
|
36
|
+
outlineWidth: 3,
|
|
37
|
+
outlineStyle: "solid",
|
|
38
|
+
outlineColor: color.primary,
|
|
39
|
+
outlineOffset: 2,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
web: { "& > *": { pointerEvents: "none" } },
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
// Native only: the gap between the trigger and the tooltip, on the side the tooltip sits on (the overlay
|
|
46
|
+
// layer places the tooltip against that edge of the trigger).
|
|
47
|
+
variants: {
|
|
48
|
+
sideTop: {
|
|
49
|
+
children: { content: { native: { marginBottom: spacing[2] } } },
|
|
50
|
+
},
|
|
51
|
+
sideBottom: {
|
|
52
|
+
children: { content: { native: { marginTop: spacing[2] } } },
|
|
53
|
+
},
|
|
54
|
+
sideLeft: {
|
|
55
|
+
children: { content: { native: { marginRight: spacing[2] } } },
|
|
56
|
+
},
|
|
57
|
+
sideRight: {
|
|
58
|
+
children: { content: { native: { marginLeft: spacing[2] } } },
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
web: {
|
|
62
|
+
"@keyframes tooltip-in": { from: { opacity: 0 }, to: { opacity: 1 } },
|
|
63
|
+
},
|
|
64
|
+
});
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assignRef,
|
|
3
|
+
OverlayPortal,
|
|
4
|
+
useBlock,
|
|
5
|
+
type OverlayRect,
|
|
6
|
+
type StylesFor,
|
|
7
|
+
} from "@behivetech/blocks.core/native";
|
|
8
|
+
import {
|
|
9
|
+
isValidElement,
|
|
10
|
+
useCallback,
|
|
11
|
+
useEffect,
|
|
12
|
+
useRef,
|
|
13
|
+
useState,
|
|
14
|
+
type ElementType,
|
|
15
|
+
type ReactElement,
|
|
16
|
+
type ReactNode,
|
|
17
|
+
type Ref,
|
|
18
|
+
} from "react";
|
|
19
|
+
import {
|
|
20
|
+
AccessibilityInfo,
|
|
21
|
+
Platform,
|
|
22
|
+
Pressable,
|
|
23
|
+
Text,
|
|
24
|
+
type GestureResponderEvent,
|
|
25
|
+
} from "react-native";
|
|
26
|
+
import { tooltip } from "./tooltip.block";
|
|
27
|
+
import type { TooltipSide } from "./tooltip";
|
|
28
|
+
|
|
29
|
+
// The type the web renderer owns, re-exported so the native entry declares the same name. A native file's
|
|
30
|
+
// import of its own stem means the web file in the native declarations (`emitNativeDeclarations`).
|
|
31
|
+
export type { TooltipSide } from "./tooltip";
|
|
32
|
+
|
|
33
|
+
/** How long a tooltip stays on screen after the long press that showed it, in ms. */
|
|
34
|
+
const VISIBLE_MS = 3000;
|
|
35
|
+
/** How long an element that takes no `onLongPress` is held before the tooltip shows, in ms: React Native's own long press. */
|
|
36
|
+
const HOLD_MS = 500;
|
|
37
|
+
|
|
38
|
+
const SIDE_KEYS = {
|
|
39
|
+
top: "sideTop",
|
|
40
|
+
right: "sideRight",
|
|
41
|
+
bottom: "sideBottom",
|
|
42
|
+
left: "sideLeft",
|
|
43
|
+
} as const;
|
|
44
|
+
|
|
45
|
+
export interface TooltipProps {
|
|
46
|
+
/** Text shown in the tooltip. It is also announced as the trigger's hint, so an icon-only control still needs its own `accessibilityLabel` */
|
|
47
|
+
label: string;
|
|
48
|
+
/** Which side of the trigger to place the tooltip on; defaults to "top" */
|
|
49
|
+
side?: TooltipSide;
|
|
50
|
+
/** The element the tooltip describes, rendered as it is: it is given `onLongPress`, which shows the tooltip after its own, and a `ref`, which is how the tooltip finds it on screen */
|
|
51
|
+
children: ReactElement;
|
|
52
|
+
/** An override block made with `defineBlock`, typed to this component's children and variants; it wins over the block's own styles. */
|
|
53
|
+
styles?: StylesFor<typeof tooltip>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** What the trigger gives through its ref, as far as the tooltip reads it. */
|
|
57
|
+
interface Measurable {
|
|
58
|
+
measureInWindow?: (
|
|
59
|
+
callback: (x: number, y: number, width: number, height: number) => void,
|
|
60
|
+
) => void;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
type PressHandler = (event: GestureResponderEvent) => void;
|
|
64
|
+
|
|
65
|
+
interface TriggerProps {
|
|
66
|
+
disabled?: boolean;
|
|
67
|
+
accessibilityHint?: string;
|
|
68
|
+
ref?: Ref<Measurable>;
|
|
69
|
+
onLongPress?: PressHandler;
|
|
70
|
+
onStartShouldSetResponder?: (event: GestureResponderEvent) => boolean;
|
|
71
|
+
onResponderGrant?: PressHandler;
|
|
72
|
+
onResponderRelease?: PressHandler;
|
|
73
|
+
onResponderTerminate?: PressHandler;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Where a press landed in the window: a place with no size, for a trigger that cannot be measured. A press
|
|
78
|
+
* that comes with no event (a test that fires `longPress` bare) lands at the window's corner.
|
|
79
|
+
*/
|
|
80
|
+
const pointOf = (event?: GestureResponderEvent): OverlayRect => ({
|
|
81
|
+
x: event?.nativeEvent?.pageX ?? 0,
|
|
82
|
+
y: event?.nativeEvent?.pageY ?? 0,
|
|
83
|
+
width: 0,
|
|
84
|
+
height: 0,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The React Native renderer of Tooltip: the same block, with the same `label` and `side` props. There is no
|
|
89
|
+
* hover on a phone, so the tooltip shows on a long press of the trigger, for a few seconds, and a screen
|
|
90
|
+
* reader hears it as the trigger's hint and when it appears. There is no `className`, so restyle it with
|
|
91
|
+
* `styles`; it has no arrow and no animation.
|
|
92
|
+
*
|
|
93
|
+
* The trigger is your own element, with no wrapper around it, so its layout is what it is without the
|
|
94
|
+
* tooltip: it is given `onLongPress` (a `Pressable`, a `Text` and the registry's controls take it), the
|
|
95
|
+
* responder handlers that time the hold for an element that does not (a `View`, an `Avatar`), and a `ref`.
|
|
96
|
+
* A disabled trigger is the one exception: it is wrapped in a group that takes the long press for it.
|
|
97
|
+
*
|
|
98
|
+
* The tooltip is drawn in the app's `OverlayHost` (`@behivetech/blocks.core/native`), above everything else in
|
|
99
|
+
* the window, against the side of the trigger: no ancestor clips it and no sibling paints over it. Above and
|
|
100
|
+
* below, it is level with the trigger's start edge, or with its end edge when that leaves it more room (a
|
|
101
|
+
* control in the far corner of the screen), and a long label wraps before it reaches the edge of the screen;
|
|
102
|
+
* it does not move to another side when the one asked for has no room. Inside a dialog it is drawn in the
|
|
103
|
+
* dialog's own layer. An app that mounts no `OverlayHost` shows no tooltip (development says so); the hint and
|
|
104
|
+
* the announcement still reach a screen reader.
|
|
105
|
+
*/
|
|
106
|
+
export const Tooltip = ({
|
|
107
|
+
label,
|
|
108
|
+
side = "top",
|
|
109
|
+
children,
|
|
110
|
+
styles,
|
|
111
|
+
}: TooltipProps) => {
|
|
112
|
+
// where the trigger is in the window while the tooltip shows; a new place each time it is shown
|
|
113
|
+
const [anchor, setAnchor] = useState<OverlayRect>();
|
|
114
|
+
const trigger = useRef<Measurable | null>(null);
|
|
115
|
+
// the hold of an element that takes no `onLongPress`, while it is being timed
|
|
116
|
+
const hold = useRef<ReturnType<typeof setTimeout>>(undefined);
|
|
117
|
+
// when the tooltip was last shown, so one hold that is reported twice shows it once
|
|
118
|
+
const shownAt = useRef(Number.NEGATIVE_INFINITY);
|
|
119
|
+
const [, childProps] = useBlock(
|
|
120
|
+
tooltip,
|
|
121
|
+
{ [SIDE_KEYS[side]]: true },
|
|
122
|
+
{ styles },
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
let hide: ReturnType<typeof setTimeout> | undefined;
|
|
127
|
+
|
|
128
|
+
if (anchor) {
|
|
129
|
+
hide = setTimeout(() => setAnchor(undefined), VISIBLE_MS);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return () => clearTimeout(hide);
|
|
133
|
+
}, [anchor]);
|
|
134
|
+
|
|
135
|
+
// a hold that is still being timed when the tooltip unmounts has nothing left to show
|
|
136
|
+
useEffect(() => () => clearTimeout(hold.current), []);
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Shows the tooltip. An element that takes both `onLongPress` and the responder handlers (a `Text`) reports
|
|
140
|
+
* one hold twice, a moment apart: the second report is left out. Two long presses are never that close,
|
|
141
|
+
* because each takes as long as the gap that is asked for.
|
|
142
|
+
*/
|
|
143
|
+
const show = (touch: OverlayRect) => {
|
|
144
|
+
const now = Date.now();
|
|
145
|
+
const node = trigger.current;
|
|
146
|
+
|
|
147
|
+
if (now - shownAt.current >= HOLD_MS) {
|
|
148
|
+
shownAt.current = now;
|
|
149
|
+
// a live region speaks on Android, and an announcement on top of it would speak twice; iOS has no live
|
|
150
|
+
// regions, so it is told
|
|
151
|
+
if (Platform.OS === "ios") {
|
|
152
|
+
AccessibilityInfo.announceForAccessibility(label);
|
|
153
|
+
}
|
|
154
|
+
// Where the press landed, until the trigger says where it is. A device answers at once, in the same
|
|
155
|
+
// update; an element that passes its ref to no view, and a test renderer (which lays nothing out), never do.
|
|
156
|
+
setAnchor(touch);
|
|
157
|
+
node?.measureInWindow?.((x, y, width, height) => {
|
|
158
|
+
// a view that is not laid out reports no size: the press is the better place
|
|
159
|
+
if (width > 0 && height > 0) {
|
|
160
|
+
setAnchor({ x, y, width, height });
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
const release = () => clearTimeout(hold.current);
|
|
166
|
+
|
|
167
|
+
// the trigger, when it is an element: its type is rendered again below, with its own props and the handlers
|
|
168
|
+
// that show the tooltip
|
|
169
|
+
const child = isValidElement<TriggerProps>(children) ? children : undefined;
|
|
170
|
+
const own = child?.props;
|
|
171
|
+
const Trigger = child?.type as ElementType<TriggerProps> | undefined;
|
|
172
|
+
const ownRef = own?.ref;
|
|
173
|
+
// one callback while the trigger's own ref is the same, so a render does not detach and attach the ref again
|
|
174
|
+
const setTrigger = useCallback(
|
|
175
|
+
(node: Measurable | null) => {
|
|
176
|
+
trigger.current = node;
|
|
177
|
+
|
|
178
|
+
const cleanup = assignRef(ownRef, node);
|
|
179
|
+
|
|
180
|
+
// a cleanup of its own, so the trigger's runs: React calls a ref that returns one with no `null`
|
|
181
|
+
return () => {
|
|
182
|
+
trigger.current = null;
|
|
183
|
+
|
|
184
|
+
if (cleanup) {
|
|
185
|
+
cleanup();
|
|
186
|
+
} else {
|
|
187
|
+
assignRef(ownRef, null);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
},
|
|
191
|
+
[ownRef],
|
|
192
|
+
);
|
|
193
|
+
const startHold = (event: GestureResponderEvent) => {
|
|
194
|
+
const touch = pointOf(event);
|
|
195
|
+
|
|
196
|
+
own?.onResponderGrant?.(event);
|
|
197
|
+
clearTimeout(hold.current);
|
|
198
|
+
hold.current = setTimeout(() => show(touch), HOLD_MS);
|
|
199
|
+
};
|
|
200
|
+
// what is rendered in the trigger's place: the child itself, unless it is an element the tooltip can show on
|
|
201
|
+
let element: ReactNode = children;
|
|
202
|
+
|
|
203
|
+
if (own?.disabled === true) {
|
|
204
|
+
// a disabled trigger takes no press, so a group around it takes the long press and carries the hint
|
|
205
|
+
element = (
|
|
206
|
+
<Pressable
|
|
207
|
+
{...childProps("disabledTrigger")}
|
|
208
|
+
ref={(node: Measurable | null) => {
|
|
209
|
+
trigger.current = node;
|
|
210
|
+
}}
|
|
211
|
+
accessibilityHint={label}
|
|
212
|
+
onLongPress={(event) => show(pointOf(event))}
|
|
213
|
+
>
|
|
214
|
+
{children}
|
|
215
|
+
</Pressable>
|
|
216
|
+
);
|
|
217
|
+
} else if (own !== undefined && Trigger !== undefined) {
|
|
218
|
+
// an enabled trigger is rendered as it is, with the handlers that show the tooltip added to its own
|
|
219
|
+
element = (
|
|
220
|
+
<Trigger
|
|
221
|
+
{...own}
|
|
222
|
+
ref={setTrigger}
|
|
223
|
+
accessibilityHint={own.accessibilityHint ?? label}
|
|
224
|
+
onLongPress={(event) => {
|
|
225
|
+
own.onLongPress?.(event);
|
|
226
|
+
show(pointOf(event));
|
|
227
|
+
}}
|
|
228
|
+
// An element that takes no `onLongPress` (a `View`, an `Avatar`) is held through the responder system;
|
|
229
|
+
// a `Pressable` sets these four itself, so there they do nothing.
|
|
230
|
+
onStartShouldSetResponder={(event) =>
|
|
231
|
+
own.onStartShouldSetResponder?.(event) ?? true
|
|
232
|
+
}
|
|
233
|
+
onResponderGrant={startHold}
|
|
234
|
+
onResponderRelease={(event) => {
|
|
235
|
+
own.onResponderRelease?.(event);
|
|
236
|
+
release();
|
|
237
|
+
}}
|
|
238
|
+
onResponderTerminate={(event) => {
|
|
239
|
+
own.onResponderTerminate?.(event);
|
|
240
|
+
release();
|
|
241
|
+
}}
|
|
242
|
+
/>
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return (
|
|
247
|
+
<>
|
|
248
|
+
{element}
|
|
249
|
+
{anchor && (
|
|
250
|
+
<OverlayPortal anchor={{ ...anchor, side }}>
|
|
251
|
+
<Text
|
|
252
|
+
{...childProps("content")}
|
|
253
|
+
role="tooltip"
|
|
254
|
+
accessibilityLiveRegion="polite"
|
|
255
|
+
>
|
|
256
|
+
{label}
|
|
257
|
+
</Text>
|
|
258
|
+
</OverlayPortal>
|
|
259
|
+
)}
|
|
260
|
+
</>
|
|
261
|
+
);
|
|
262
|
+
};
|
package/tooltip.tsx
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { isValidElement, type ReactElement } from "react";
|
|
2
2
|
import * as RadixTooltip from "@radix-ui/react-tooltip";
|
|
3
|
-
|
|
4
|
-
import
|
|
3
|
+
|
|
4
|
+
import "./tooltip.block.css";
|
|
5
|
+
import { useBlock, type StylesFor } from "@behivetech/blocks.core";
|
|
6
|
+
import { tooltip } from "./tooltip.block";
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Shared open delay. Lives here rather than on each caller so tooltips feel the
|
|
@@ -21,6 +23,9 @@ export interface TooltipProps {
|
|
|
21
23
|
children: ReactElement;
|
|
22
24
|
/** Additional class names to merge with the tooltip content element */
|
|
23
25
|
className?: string;
|
|
26
|
+
|
|
27
|
+
/** An override block made with `defineBlock`, typed to this component's children and variants; it wins over the block's own styles. */
|
|
28
|
+
styles?: StylesFor<typeof tooltip>;
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
function isDisabled(element: ReactElement): boolean {
|
|
@@ -47,20 +52,17 @@ export const Tooltip = ({
|
|
|
47
52
|
side = "top",
|
|
48
53
|
children,
|
|
49
54
|
className,
|
|
55
|
+
styles,
|
|
50
56
|
}: TooltipProps) => {
|
|
51
|
-
const [,
|
|
52
|
-
className,
|
|
53
|
-
rootClass: "BHT__Tooltip",
|
|
54
|
-
styles,
|
|
55
|
-
});
|
|
57
|
+
const [, childProps] = useBlock(tooltip, {}, { styles });
|
|
56
58
|
|
|
57
59
|
const disabled = isDisabled(children);
|
|
58
60
|
|
|
59
|
-
// The wrapper is deliberately left
|
|
61
|
+
// The wrapper is deliberately left unlabeled: the disabled control inside
|
|
60
62
|
// keeps its own accessible name, and Radix points the wrapper's
|
|
61
63
|
// `aria-describedby` at the tooltip, so focusing it announces both.
|
|
62
64
|
const trigger = disabled ? (
|
|
63
|
-
<span
|
|
65
|
+
<span {...childProps("disabledTrigger")} role="group" tabIndex={0}>
|
|
64
66
|
{children}
|
|
65
67
|
</span>
|
|
66
68
|
) : (
|
|
@@ -73,12 +75,12 @@ export const Tooltip = ({
|
|
|
73
75
|
<RadixTooltip.Trigger asChild>{trigger}</RadixTooltip.Trigger>
|
|
74
76
|
<RadixTooltip.Portal>
|
|
75
77
|
<RadixTooltip.Content
|
|
76
|
-
|
|
78
|
+
{...childProps("content", { className })}
|
|
77
79
|
side={side}
|
|
78
80
|
sideOffset={6}
|
|
79
81
|
>
|
|
80
82
|
{label}
|
|
81
|
-
<RadixTooltip.Arrow
|
|
83
|
+
<RadixTooltip.Arrow {...childProps("arrow")} />
|
|
82
84
|
</RadixTooltip.Content>
|
|
83
85
|
</RadixTooltip.Portal>
|
|
84
86
|
</RadixTooltip.Root>
|
package/tooltip.module.scss
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
// Material Design 3 plain tooltip. `inverse-surface` has no pre-MD3 name, so
|
|
2
|
-
// its fallback is the old on-surface/surface pair swapped — which is exactly
|
|
3
|
-
// what "inverse" means.
|
|
4
|
-
|
|
5
|
-
.BHT__Tooltip {
|
|
6
|
-
&__content {
|
|
7
|
-
padding: var(--spacing-1, 0.25rem) var(--spacing-2, 0.5rem);
|
|
8
|
-
background: var(--md-sys-color-inverse-surface, var(--color-on-surface));
|
|
9
|
-
color: var(--md-sys-color-inverse-on-surface, var(--color-surface));
|
|
10
|
-
border-radius: var(--md-sys-shape-corner-extra-small, 4px);
|
|
11
|
-
font-family: var(--md-sys-typescale-body-small-font, var(--font-body));
|
|
12
|
-
font-size: var(--md-sys-typescale-body-small-size, 0.75rem);
|
|
13
|
-
font-weight: var(--md-sys-typescale-body-small-weight, 400);
|
|
14
|
-
line-height: var(--md-sys-typescale-body-small-line-height, 1rem);
|
|
15
|
-
letter-spacing: var(--md-sys-typescale-body-small-tracking, 0.0333em);
|
|
16
|
-
max-width: 16rem;
|
|
17
|
-
z-index: var(--z-tooltip, 600);
|
|
18
|
-
user-select: none;
|
|
19
|
-
|
|
20
|
-
&[data-state="delayed-open"] {
|
|
21
|
-
animation: tooltip-in var(--md-sys-motion-duration-short-3, 150ms)
|
|
22
|
-
var(--md-sys-motion-easing-standard-decelerate, ease-out);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
&__arrow {
|
|
27
|
-
fill: var(--md-sys-color-inverse-surface, var(--color-on-surface));
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// A disabled control fires no pointer events of its own, so the wrapper takes
|
|
31
|
-
// the listeners and the control is made transparent to the pointer.
|
|
32
|
-
&__disabledTrigger {
|
|
33
|
-
display: inline-flex;
|
|
34
|
-
|
|
35
|
-
> * {
|
|
36
|
-
pointer-events: none;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
&:focus-visible {
|
|
40
|
-
outline: 3px solid
|
|
41
|
-
var(--md-sys-color-secondary, var(--color-border-focus));
|
|
42
|
-
outline-offset: 2px;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
@keyframes tooltip-in {
|
|
48
|
-
from {
|
|
49
|
-
opacity: 0;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
to {
|
|
53
|
-
opacity: 1;
|
|
54
|
-
}
|
|
55
|
-
}
|