@tallyui/primitives 3.20.1 → 4.0.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/dist/index.d.ts +4 -0
- package/dist/index.js +515 -328
- package/package.json +1 -1
- package/src/combobox/combobox.tsx +105 -10
- package/src/combobox/types.ts +4 -1
- package/src/context-menu/context-menu.tsx +3 -6
- package/src/dialog/dialog.tsx +4 -6
- package/src/dropdown-menu/dropdown-menu.tsx +20 -8
- package/src/hooks/measure-trigger.ts +14 -0
- package/src/hooks/use-escape-dismiss.ts +35 -0
- package/src/hooks/use-relative-position.ts +4 -1
- package/src/hover-card/hover-card.tsx +3 -6
- package/src/hover-card/types.ts +2 -0
- package/src/layer-stack.ts +19 -0
- package/src/popover/popover.tsx +92 -13
- package/src/select/select.tsx +18 -8
- package/src/tooltip/tooltip.tsx +3 -6
- package/src/hooks/use-relative-position.web.ts +0 -9
package/src/popover/popover.tsx
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import {
|
|
3
|
+
Platform,
|
|
3
4
|
Pressable,
|
|
4
5
|
View,
|
|
5
6
|
type GestureResponderEvent,
|
|
@@ -7,8 +8,10 @@ import {
|
|
|
7
8
|
type LayoutRectangle,
|
|
8
9
|
} from 'react-native';
|
|
9
10
|
import { useAugmentedRef, useControllableState, useRelativePosition, type LayoutPosition } from '../hooks';
|
|
11
|
+
import { measureTrigger } from '../hooks/measure-trigger';
|
|
12
|
+
import { isTopLayer, pushLayer, removeLayer } from '../layer-stack';
|
|
10
13
|
import { Portal as RNPortal } from '../portal';
|
|
11
|
-
import { Slot } from '../slot';
|
|
14
|
+
import { Slot, composeRefs } from '../slot';
|
|
12
15
|
import type {
|
|
13
16
|
CloseProps,
|
|
14
17
|
ContentProps,
|
|
@@ -23,7 +26,13 @@ import type {
|
|
|
23
26
|
// Internal context (nativeID for aria linkage)
|
|
24
27
|
// ---------------------------------------------------------------------------
|
|
25
28
|
|
|
26
|
-
|
|
29
|
+
interface InternalContextValue {
|
|
30
|
+
nativeID: string;
|
|
31
|
+
triggerRef: React.MutableRefObject<View | null>;
|
|
32
|
+
overlayCount: React.MutableRefObject<number>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const InternalContext = React.createContext<InternalContextValue | null>(null);
|
|
27
36
|
|
|
28
37
|
function useInternalContext() {
|
|
29
38
|
const context = React.useContext(InternalContext);
|
|
@@ -64,9 +73,11 @@ function Root({ open: openProp, defaultOpen, onOpenChange: onOpenChangeProp, chi
|
|
|
64
73
|
});
|
|
65
74
|
const [triggerPosition, setTriggerPosition] = React.useState<LayoutPosition | null>(null);
|
|
66
75
|
const [contentLayout, setContentLayout] = React.useState<LayoutRectangle | null>(null);
|
|
76
|
+
const triggerRef = React.useRef<View | null>(null);
|
|
77
|
+
const overlayCount = React.useRef(0);
|
|
67
78
|
|
|
68
79
|
return (
|
|
69
|
-
<InternalContext.Provider value={{ nativeID }}>
|
|
80
|
+
<InternalContext.Provider value={{ nativeID, triggerRef, overlayCount }}>
|
|
70
81
|
<RootContext.Provider
|
|
71
82
|
value={{
|
|
72
83
|
open,
|
|
@@ -94,15 +105,14 @@ const Trigger = React.forwardRef<View, TriggerProps>(function Trigger(
|
|
|
94
105
|
ref
|
|
95
106
|
) {
|
|
96
107
|
const { open, onOpenChange, setTriggerPosition } = useRootContext();
|
|
108
|
+
const { triggerRef } = useInternalContext();
|
|
97
109
|
|
|
98
110
|
const augmentedRef = useAugmentedRef<View>({
|
|
99
111
|
ref,
|
|
100
112
|
methods: {
|
|
101
113
|
open: () => {
|
|
102
114
|
onOpenChange(true);
|
|
103
|
-
augmentedRef.current
|
|
104
|
-
setTriggerPosition({ width, pageX, pageY, height });
|
|
105
|
-
});
|
|
115
|
+
measureTrigger(augmentedRef.current, setTriggerPosition);
|
|
106
116
|
},
|
|
107
117
|
close: () => {
|
|
108
118
|
setTriggerPosition(null);
|
|
@@ -111,12 +121,14 @@ const Trigger = React.forwardRef<View, TriggerProps>(function Trigger(
|
|
|
111
121
|
},
|
|
112
122
|
});
|
|
113
123
|
|
|
124
|
+
React.useLayoutEffect(() => {
|
|
125
|
+
triggerRef.current = augmentedRef.current;
|
|
126
|
+
});
|
|
127
|
+
|
|
114
128
|
const onPress = React.useCallback(
|
|
115
129
|
(ev: GestureResponderEvent) => {
|
|
116
130
|
if (disabled) return;
|
|
117
|
-
augmentedRef.current
|
|
118
|
-
setTriggerPosition({ width, pageX, pageY, height });
|
|
119
|
-
});
|
|
131
|
+
measureTrigger(augmentedRef.current, setTriggerPosition);
|
|
120
132
|
onOpenChange(!open);
|
|
121
133
|
onPressProp?.(ev);
|
|
122
134
|
},
|
|
@@ -171,6 +183,16 @@ const Overlay = React.forwardRef<View, OverlayProps>(function Overlay(
|
|
|
171
183
|
ref
|
|
172
184
|
) {
|
|
173
185
|
const { open, onOpenChange, setTriggerPosition, setContentLayout } = useRootContext();
|
|
186
|
+
const { overlayCount } = useInternalContext();
|
|
187
|
+
|
|
188
|
+
// While an Overlay renders, an outside press is the Overlay's to handle.
|
|
189
|
+
React.useLayoutEffect(() => {
|
|
190
|
+
if (!forceMount && !open) return;
|
|
191
|
+
overlayCount.current += 1;
|
|
192
|
+
return () => {
|
|
193
|
+
overlayCount.current -= 1;
|
|
194
|
+
};
|
|
195
|
+
}, [open, forceMount, overlayCount]);
|
|
174
196
|
|
|
175
197
|
const onPress = React.useCallback(
|
|
176
198
|
(ev: GestureResponderEvent) => {
|
|
@@ -212,12 +234,67 @@ const Content = React.forwardRef<View, ContentProps>(function Content(
|
|
|
212
234
|
style,
|
|
213
235
|
disablePositioningStyle,
|
|
214
236
|
onLayout: onLayoutProp,
|
|
237
|
+
onEscapeKeyDown,
|
|
238
|
+
onPointerDownOutside,
|
|
239
|
+
onInteractOutside,
|
|
215
240
|
...props
|
|
216
241
|
},
|
|
217
242
|
ref
|
|
218
243
|
) {
|
|
219
|
-
const { open, triggerPosition, contentLayout, setContentLayout } =
|
|
220
|
-
|
|
244
|
+
const { open, onOpenChange, triggerPosition, setTriggerPosition, contentLayout, setContentLayout } =
|
|
245
|
+
useRootContext();
|
|
246
|
+
const { nativeID, triggerRef, overlayCount } = useInternalContext();
|
|
247
|
+
|
|
248
|
+
const contentRef = React.useRef<View | null>(null);
|
|
249
|
+
const composedRef = React.useMemo(() => composeRefs(ref, contentRef), [ref]);
|
|
250
|
+
|
|
251
|
+
const onEscapeKeyDownRef = React.useRef(onEscapeKeyDown);
|
|
252
|
+
const onPointerDownOutsideRef = React.useRef(onPointerDownOutside);
|
|
253
|
+
const onInteractOutsideRef = React.useRef(onInteractOutside);
|
|
254
|
+
const closeRef = React.useRef(() => {});
|
|
255
|
+
onEscapeKeyDownRef.current = onEscapeKeyDown;
|
|
256
|
+
onPointerDownOutsideRef.current = onPointerDownOutside;
|
|
257
|
+
onInteractOutsideRef.current = onInteractOutside;
|
|
258
|
+
closeRef.current = () => {
|
|
259
|
+
setTriggerPosition(null);
|
|
260
|
+
setContentLayout(null);
|
|
261
|
+
onOpenChange(false);
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
// Web only: Escape and a pointerdown outside content and trigger dismiss the
|
|
265
|
+
// topmost open layer (see layer-stack.ts). Native is untouched.
|
|
266
|
+
React.useEffect(() => {
|
|
267
|
+
if (!open || Platform.OS !== 'web' || typeof document === 'undefined') return;
|
|
268
|
+
pushLayer(nativeID);
|
|
269
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
270
|
+
if (event.key !== 'Escape' || !isTopLayer(nativeID)) return;
|
|
271
|
+
onEscapeKeyDownRef.current?.(event);
|
|
272
|
+
if (!event.defaultPrevented) closeRef.current();
|
|
273
|
+
};
|
|
274
|
+
const onPointerDown = (event: PointerEvent) => {
|
|
275
|
+
if (!isTopLayer(nativeID) || overlayCount.current !== 0) return;
|
|
276
|
+
const target = event.target;
|
|
277
|
+
const contentNode = contentRef.current as unknown as Node | null;
|
|
278
|
+
const triggerNode = triggerRef.current as unknown as Node | null;
|
|
279
|
+
if (target instanceof Node && (contentNode?.contains(target) || triggerNode?.contains(target))) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
const outside = new CustomEvent('popover.pointerDownOutside', {
|
|
283
|
+
cancelable: true,
|
|
284
|
+
detail: { originalEvent: event },
|
|
285
|
+
});
|
|
286
|
+
onPointerDownOutsideRef.current?.(outside);
|
|
287
|
+
onInteractOutsideRef.current?.(outside);
|
|
288
|
+
if (!outside.defaultPrevented) closeRef.current();
|
|
289
|
+
};
|
|
290
|
+
document.addEventListener('keydown', onKeyDown);
|
|
291
|
+
document.addEventListener('pointerdown', onPointerDown, { capture: true });
|
|
292
|
+
return () => {
|
|
293
|
+
document.removeEventListener('keydown', onKeyDown);
|
|
294
|
+
document.removeEventListener('pointerdown', onPointerDown, { capture: true });
|
|
295
|
+
removeLayer(nativeID);
|
|
296
|
+
};
|
|
297
|
+
}, [open, nativeID, triggerRef, overlayCount]);
|
|
221
298
|
|
|
222
299
|
const positionStyle = useRelativePosition({
|
|
223
300
|
align,
|
|
@@ -230,6 +307,8 @@ const Content = React.forwardRef<View, ContentProps>(function Content(
|
|
|
230
307
|
side,
|
|
231
308
|
disablePositioningStyle,
|
|
232
309
|
});
|
|
310
|
+
const lastPositionStyle = React.useRef<typeof positionStyle | undefined>(undefined);
|
|
311
|
+
if (open) lastPositionStyle.current = positionStyle;
|
|
233
312
|
|
|
234
313
|
const onLayout = React.useCallback(
|
|
235
314
|
(event: LayoutChangeEvent) => {
|
|
@@ -247,13 +326,13 @@ const Content = React.forwardRef<View, ContentProps>(function Content(
|
|
|
247
326
|
|
|
248
327
|
return (
|
|
249
328
|
<Component
|
|
250
|
-
ref={
|
|
329
|
+
ref={composedRef}
|
|
251
330
|
role="dialog"
|
|
252
331
|
nativeID={nativeID}
|
|
253
332
|
aria-labelledby={`${nativeID}_title`}
|
|
254
333
|
aria-describedby={`${nativeID}_description`}
|
|
255
334
|
aria-modal={true}
|
|
256
|
-
style={[positionStyle, style]}
|
|
335
|
+
style={[!open && forceMount ? lastPositionStyle.current ?? positionStyle : positionStyle, style]}
|
|
257
336
|
onLayout={onLayout}
|
|
258
337
|
{...props}
|
|
259
338
|
/>
|
package/src/select/select.tsx
CHANGED
|
@@ -8,6 +8,8 @@ import {
|
|
|
8
8
|
type LayoutRectangle,
|
|
9
9
|
} from 'react-native';
|
|
10
10
|
import { useAugmentedRef, useControllableState, useRelativePosition, type LayoutPosition } from '../hooks';
|
|
11
|
+
import { measureTrigger } from '../hooks/measure-trigger';
|
|
12
|
+
import { useEscapeDismiss } from '../hooks/use-escape-dismiss';
|
|
11
13
|
import { Portal as RNPortal } from '../portal';
|
|
12
14
|
import { Slot } from '../slot';
|
|
13
15
|
import type {
|
|
@@ -128,9 +130,7 @@ const Trigger = React.forwardRef<View, TriggerProps>(function Trigger(
|
|
|
128
130
|
methods: {
|
|
129
131
|
open: () => {
|
|
130
132
|
onOpenChange(true);
|
|
131
|
-
augmentedRef.current
|
|
132
|
-
setTriggerPosition({ width, pageX, pageY, height });
|
|
133
|
-
});
|
|
133
|
+
measureTrigger(augmentedRef.current, setTriggerPosition);
|
|
134
134
|
},
|
|
135
135
|
close: () => {
|
|
136
136
|
setTriggerPosition(null);
|
|
@@ -142,9 +142,7 @@ const Trigger = React.forwardRef<View, TriggerProps>(function Trigger(
|
|
|
142
142
|
const onPress = React.useCallback(
|
|
143
143
|
(ev: GestureResponderEvent) => {
|
|
144
144
|
if (disabled || disabledRoot) return;
|
|
145
|
-
augmentedRef.current
|
|
146
|
-
setTriggerPosition({ width, pageX, pageY, height });
|
|
147
|
-
});
|
|
145
|
+
measureTrigger(augmentedRef.current, setTriggerPosition);
|
|
148
146
|
onOpenChange(!open);
|
|
149
147
|
onPressProp?.(ev);
|
|
150
148
|
},
|
|
@@ -258,11 +256,23 @@ const Content = React.forwardRef<View, ContentProps>(function Content(
|
|
|
258
256
|
style,
|
|
259
257
|
disablePositioningStyle,
|
|
260
258
|
onLayout: onLayoutProp,
|
|
259
|
+
onEscapeKeyDown,
|
|
261
260
|
...props
|
|
262
261
|
},
|
|
263
262
|
ref
|
|
264
263
|
) {
|
|
265
|
-
const { open, triggerPosition, contentLayout, setContentLayout } =
|
|
264
|
+
const { open, onOpenChange, triggerPosition, contentLayout, setTriggerPosition, setContentLayout } =
|
|
265
|
+
useRootContext();
|
|
266
|
+
|
|
267
|
+
useEscapeDismiss(
|
|
268
|
+
open,
|
|
269
|
+
() => {
|
|
270
|
+
setTriggerPosition(null);
|
|
271
|
+
setContentLayout(null);
|
|
272
|
+
onOpenChange(false);
|
|
273
|
+
},
|
|
274
|
+
onEscapeKeyDown
|
|
275
|
+
);
|
|
266
276
|
|
|
267
277
|
const positionStyle = useRelativePosition({
|
|
268
278
|
align,
|
|
@@ -295,7 +305,7 @@ const Content = React.forwardRef<View, ContentProps>(function Content(
|
|
|
295
305
|
ref={ref}
|
|
296
306
|
role="list"
|
|
297
307
|
aria-modal={true}
|
|
298
|
-
style={[positionStyle, style]}
|
|
308
|
+
style={[positionStyle, triggerPosition ? { minWidth: triggerPosition.width } : null, style]}
|
|
299
309
|
onLayout={onLayout}
|
|
300
310
|
{...props}
|
|
301
311
|
/>
|
package/src/tooltip/tooltip.tsx
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
type LayoutRectangle,
|
|
8
8
|
} from 'react-native';
|
|
9
9
|
import { useAugmentedRef, useControllableState, useRelativePosition, type LayoutPosition } from '../hooks';
|
|
10
|
+
import { measureTrigger } from '../hooks/measure-trigger';
|
|
10
11
|
import { Portal as RNPortal } from '../portal';
|
|
11
12
|
import { Slot } from '../slot';
|
|
12
13
|
import type {
|
|
@@ -136,9 +137,7 @@ const Trigger = React.forwardRef<View, TriggerProps>(function Trigger(
|
|
|
136
137
|
methods: {
|
|
137
138
|
open: () => {
|
|
138
139
|
onOpenChange(true);
|
|
139
|
-
augmentedRef.current
|
|
140
|
-
setTriggerPosition({ width, pageX, pageY, height });
|
|
141
|
-
});
|
|
140
|
+
measureTrigger(augmentedRef.current, setTriggerPosition);
|
|
142
141
|
},
|
|
143
142
|
close: () => {
|
|
144
143
|
setTriggerPosition(null);
|
|
@@ -150,9 +149,7 @@ const Trigger = React.forwardRef<View, TriggerProps>(function Trigger(
|
|
|
150
149
|
const show = React.useCallback(() => {
|
|
151
150
|
clearDelay();
|
|
152
151
|
if (disabled) return;
|
|
153
|
-
augmentedRef.current
|
|
154
|
-
setTriggerPosition({ width, pageX, pageY, height });
|
|
155
|
-
});
|
|
152
|
+
measureTrigger(augmentedRef.current, setTriggerPosition);
|
|
156
153
|
onOpenChange(true);
|
|
157
154
|
}, [augmentedRef, clearDelay, disabled, onOpenChange, setTriggerPosition]);
|
|
158
155
|
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
// Web stub — on web we use Radix positioning, so this returns an empty object.
|
|
2
|
-
|
|
3
|
-
import type { LayoutPosition } from './use-relative-position';
|
|
4
|
-
|
|
5
|
-
export function useRelativePosition(_args: Record<string, unknown>) {
|
|
6
|
-
return {};
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export type { LayoutPosition };
|