@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/package.json
CHANGED
|
@@ -5,10 +5,14 @@ import {
|
|
|
5
5
|
TextInput,
|
|
6
6
|
View,
|
|
7
7
|
type GestureResponderEvent,
|
|
8
|
+
type LayoutChangeEvent,
|
|
9
|
+
type LayoutRectangle,
|
|
8
10
|
type NativeSyntheticEvent,
|
|
9
11
|
type TextInputChangeEventData,
|
|
10
12
|
} from 'react-native';
|
|
11
|
-
import { useControllableState } from '../hooks';
|
|
13
|
+
import { useAugmentedRef, useControllableState, useRelativePosition, type LayoutPosition } from '../hooks';
|
|
14
|
+
import { measureTrigger } from '../hooks/measure-trigger';
|
|
15
|
+
import { useEscapeDismiss } from '../hooks/use-escape-dismiss';
|
|
12
16
|
import { Portal as RNPortal } from '../portal';
|
|
13
17
|
import { Slot } from '../slot';
|
|
14
18
|
import type {
|
|
@@ -24,6 +28,31 @@ import type {
|
|
|
24
28
|
TriggerProps,
|
|
25
29
|
} from './types';
|
|
26
30
|
|
|
31
|
+
// Private anchor state: the anchor is the Trigger when one is mounted, otherwise the Input.
|
|
32
|
+
interface AnchorContextType {
|
|
33
|
+
triggerPosition: LayoutPosition | null;
|
|
34
|
+
setTriggerPosition: (position: LayoutPosition | null) => void;
|
|
35
|
+
contentLayout: LayoutRectangle | null;
|
|
36
|
+
setContentLayout: (layout: LayoutRectangle | null) => void;
|
|
37
|
+
triggerRef: React.MutableRefObject<View | null>;
|
|
38
|
+
inputRef: React.MutableRefObject<View | null>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const AnchorContext = React.createContext<AnchorContextType | null>(null);
|
|
42
|
+
|
|
43
|
+
// Every caller has already called useRootContext(), which throws outside Root.
|
|
44
|
+
const useAnchorContext = () => React.useContext(AnchorContext)!;
|
|
45
|
+
|
|
46
|
+
// Stores the mounted node in an anchor ref and clears it on unmount.
|
|
47
|
+
function useAnchorNode(anchorRef: React.MutableRefObject<View | null>, node: React.RefObject<View | null>) {
|
|
48
|
+
React.useEffect(() => {
|
|
49
|
+
anchorRef.current = node.current;
|
|
50
|
+
return () => {
|
|
51
|
+
anchorRef.current = null;
|
|
52
|
+
};
|
|
53
|
+
}, [anchorRef, node]);
|
|
54
|
+
}
|
|
55
|
+
|
|
27
56
|
// ---------------------------------------------------------------------------
|
|
28
57
|
// Root context
|
|
29
58
|
// ---------------------------------------------------------------------------
|
|
@@ -96,6 +125,11 @@ function Root({
|
|
|
96
125
|
onChange: onSearchValueChangeProp,
|
|
97
126
|
});
|
|
98
127
|
|
|
128
|
+
const [triggerPosition, setTriggerPosition] = React.useState<LayoutPosition | null>(null);
|
|
129
|
+
const [contentLayout, setContentLayout] = React.useState<LayoutRectangle | null>(null);
|
|
130
|
+
const triggerRef = React.useRef<View | null>(null);
|
|
131
|
+
const inputRef = React.useRef<View | null>(null);
|
|
132
|
+
|
|
99
133
|
return (
|
|
100
134
|
<RootContext.Provider
|
|
101
135
|
value={{
|
|
@@ -108,7 +142,11 @@ function Root({
|
|
|
108
142
|
nativeID,
|
|
109
143
|
}}
|
|
110
144
|
>
|
|
111
|
-
|
|
145
|
+
<AnchorContext.Provider
|
|
146
|
+
value={{ triggerPosition, setTriggerPosition, contentLayout, setContentLayout, triggerRef, inputRef }}
|
|
147
|
+
>
|
|
148
|
+
{children}
|
|
149
|
+
</AnchorContext.Provider>
|
|
112
150
|
</RootContext.Provider>
|
|
113
151
|
);
|
|
114
152
|
}
|
|
@@ -124,6 +162,9 @@ const Input = React.forwardRef<View, InputProps>(function Input(
|
|
|
124
162
|
ref
|
|
125
163
|
) {
|
|
126
164
|
const { open, onOpenChange, searchValue, onSearchValueChange } = useRootContext();
|
|
165
|
+
const { inputRef } = useAnchorContext();
|
|
166
|
+
const augmentedRef = useAugmentedRef<View>({ ref });
|
|
167
|
+
useAnchorNode(inputRef, augmentedRef);
|
|
127
168
|
|
|
128
169
|
const onChange = React.useCallback(
|
|
129
170
|
(ev: NativeSyntheticEvent<TextInputChangeEventData>) => {
|
|
@@ -145,7 +186,7 @@ const Input = React.forwardRef<View, InputProps>(function Input(
|
|
|
145
186
|
|
|
146
187
|
return (
|
|
147
188
|
<Component
|
|
148
|
-
ref={
|
|
189
|
+
ref={augmentedRef}
|
|
149
190
|
role="combobox"
|
|
150
191
|
aria-expanded={open}
|
|
151
192
|
aria-autocomplete="list"
|
|
@@ -169,20 +210,31 @@ const Trigger = React.forwardRef<View, TriggerProps>(function Trigger(
|
|
|
169
210
|
ref
|
|
170
211
|
) {
|
|
171
212
|
const { open, onOpenChange } = useRootContext();
|
|
213
|
+
const { triggerRef, inputRef } = useAnchorContext();
|
|
214
|
+
const augmentedRef = useAugmentedRef<View>({ ref });
|
|
215
|
+
useAnchorNode(triggerRef, augmentedRef);
|
|
172
216
|
|
|
173
217
|
const onPress = React.useCallback(
|
|
174
218
|
(ev: GestureResponderEvent) => {
|
|
175
|
-
|
|
219
|
+
// A press on an Input inside the Trigger only opens: the Input's focus has
|
|
220
|
+
// already opened the list, so a toggle would shut it again. A node without
|
|
221
|
+
// `contains` falls through to the toggle.
|
|
222
|
+
const input = inputRef.current as { contains?: (node: unknown) => boolean } | null;
|
|
223
|
+
if (typeof input?.contains === 'function' && input.contains(ev.target)) {
|
|
224
|
+
if (!open) onOpenChange(true);
|
|
225
|
+
} else {
|
|
226
|
+
onOpenChange(!open);
|
|
227
|
+
}
|
|
176
228
|
onPressProp?.(ev);
|
|
177
229
|
},
|
|
178
|
-
[onOpenChange, onPressProp, open]
|
|
230
|
+
[inputRef, onOpenChange, onPressProp, open]
|
|
179
231
|
);
|
|
180
232
|
|
|
181
233
|
const Component = asChild ? Slot : Pressable;
|
|
182
234
|
|
|
183
235
|
return (
|
|
184
236
|
<Component
|
|
185
|
-
ref={
|
|
237
|
+
ref={augmentedRef}
|
|
186
238
|
role="button"
|
|
187
239
|
aria-expanded={open}
|
|
188
240
|
onPress={onPress}
|
|
@@ -199,6 +251,7 @@ Trigger.displayName = 'ComboboxTrigger';
|
|
|
199
251
|
|
|
200
252
|
function Portal({ forceMount, hostName, children }: PortalProps) {
|
|
201
253
|
const rootValue = useRootContext();
|
|
254
|
+
const anchorValue = useAnchorContext();
|
|
202
255
|
|
|
203
256
|
if (!forceMount && !rootValue.open) {
|
|
204
257
|
return null;
|
|
@@ -206,7 +259,7 @@ function Portal({ forceMount, hostName, children }: PortalProps) {
|
|
|
206
259
|
|
|
207
260
|
return (
|
|
208
261
|
<RNPortal hostName={hostName} name={`${rootValue.nativeID}_portal`}>
|
|
209
|
-
<RootContext.Provider value={rootValue}>{children}</RootContext.Provider>
|
|
262
|
+
<RootContext.Provider value={rootValue}><AnchorContext.Provider value={anchorValue}>{children}</AnchorContext.Provider></RootContext.Provider>
|
|
210
263
|
</RNPortal>
|
|
211
264
|
);
|
|
212
265
|
}
|
|
@@ -218,10 +271,44 @@ Portal.displayName = 'ComboboxPortal';
|
|
|
218
271
|
// ---------------------------------------------------------------------------
|
|
219
272
|
|
|
220
273
|
const Content = React.forwardRef<View, ContentProps>(function Content(
|
|
221
|
-
{ asChild, forceMount, ...props },
|
|
274
|
+
{ asChild, forceMount, style, onLayout: onLayoutProp, onEscapeKeyDown, ...props },
|
|
222
275
|
ref
|
|
223
276
|
) {
|
|
224
|
-
const { open } = useRootContext();
|
|
277
|
+
const { open, onOpenChange } = useRootContext();
|
|
278
|
+
const { triggerPosition, setTriggerPosition, contentLayout, setContentLayout, triggerRef, inputRef } =
|
|
279
|
+
useAnchorContext();
|
|
280
|
+
|
|
281
|
+
React.useEffect(() => {
|
|
282
|
+
if (!open) return;
|
|
283
|
+
measureTrigger(triggerRef.current ?? inputRef.current, setTriggerPosition);
|
|
284
|
+
return () => {
|
|
285
|
+
setTriggerPosition(null);
|
|
286
|
+
setContentLayout(null);
|
|
287
|
+
};
|
|
288
|
+
}, [open, triggerRef, inputRef, setTriggerPosition, setContentLayout]);
|
|
289
|
+
|
|
290
|
+
useEscapeDismiss(open, () => onOpenChange(false), onEscapeKeyDown);
|
|
291
|
+
|
|
292
|
+
// Content opens below its anchor, left edges aligned (Select's default), with
|
|
293
|
+
// v2's 4 px gap (monorepo packages/components/src/popover/anchor.tsx:14), and
|
|
294
|
+
// is clamped into the window exactly as Select is.
|
|
295
|
+
const positionStyle = useRelativePosition({
|
|
296
|
+
align: 'start',
|
|
297
|
+
avoidCollisions: true,
|
|
298
|
+
triggerPosition,
|
|
299
|
+
contentLayout,
|
|
300
|
+
alignOffset: 0,
|
|
301
|
+
sideOffset: 4,
|
|
302
|
+
side: 'bottom',
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
const onLayout = React.useCallback(
|
|
306
|
+
(event: LayoutChangeEvent) => {
|
|
307
|
+
setContentLayout(event.nativeEvent.layout);
|
|
308
|
+
onLayoutProp?.(event);
|
|
309
|
+
},
|
|
310
|
+
[onLayoutProp, setContentLayout]
|
|
311
|
+
);
|
|
225
312
|
|
|
226
313
|
if (!forceMount && !open) {
|
|
227
314
|
return null;
|
|
@@ -229,7 +316,15 @@ const Content = React.forwardRef<View, ContentProps>(function Content(
|
|
|
229
316
|
|
|
230
317
|
const Component = asChild ? Slot : View;
|
|
231
318
|
|
|
232
|
-
return
|
|
319
|
+
return (
|
|
320
|
+
<Component
|
|
321
|
+
ref={ref}
|
|
322
|
+
role="listbox"
|
|
323
|
+
style={[positionStyle, triggerPosition ? { minWidth: triggerPosition.width } : null, style]}
|
|
324
|
+
onLayout={onLayout}
|
|
325
|
+
{...props}
|
|
326
|
+
/>
|
|
327
|
+
);
|
|
233
328
|
});
|
|
234
329
|
|
|
235
330
|
Content.displayName = 'ComboboxContent';
|
package/src/combobox/types.ts
CHANGED
|
@@ -55,7 +55,10 @@ interface PortalProps extends ForceMountable {
|
|
|
55
55
|
children?: React.ReactNode;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
interface ContentProps extends SlottableViewProps, ForceMountable {
|
|
58
|
+
interface ContentProps extends SlottableViewProps, ForceMountable {
|
|
59
|
+
/** Platform: WEB ONLY. Called on Escape before the list closes; call event.preventDefault() to keep it open. */
|
|
60
|
+
onEscapeKeyDown?: (event: KeyboardEvent) => void;
|
|
61
|
+
}
|
|
59
62
|
|
|
60
63
|
interface ItemProps extends SlottablePressableProps {
|
|
61
64
|
value: string;
|
|
@@ -8,6 +8,7 @@ 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';
|
|
11
12
|
import { Portal as RNPortal } from '../portal';
|
|
12
13
|
import { Slot } from '../slot';
|
|
13
14
|
import type {
|
|
@@ -140,9 +141,7 @@ const Trigger = React.forwardRef<View, TriggerProps>(function Trigger(
|
|
|
140
141
|
methods: {
|
|
141
142
|
open: () => {
|
|
142
143
|
onOpenChange(true);
|
|
143
|
-
augmentedRef.current
|
|
144
|
-
setTriggerPosition({ width, pageX, pageY, height });
|
|
145
|
-
});
|
|
144
|
+
measureTrigger(augmentedRef.current, setTriggerPosition);
|
|
146
145
|
},
|
|
147
146
|
close: () => {
|
|
148
147
|
setTriggerPosition(null);
|
|
@@ -154,9 +153,7 @@ const Trigger = React.forwardRef<View, TriggerProps>(function Trigger(
|
|
|
154
153
|
const onLongPress = React.useCallback(
|
|
155
154
|
(ev: GestureResponderEvent) => {
|
|
156
155
|
if (disabled) return;
|
|
157
|
-
augmentedRef.current
|
|
158
|
-
setTriggerPosition({ width, pageX, pageY, height });
|
|
159
|
-
});
|
|
156
|
+
measureTrigger(augmentedRef.current, setTriggerPosition);
|
|
160
157
|
onOpenChange(!open);
|
|
161
158
|
onLongPressProp?.(ev);
|
|
162
159
|
},
|
package/src/dialog/dialog.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import * as React from 'react';
|
|
|
2
2
|
import { Platform, Pressable, Text, View, type GestureResponderEvent } from 'react-native';
|
|
3
3
|
import { useControllableState } from '../hooks';
|
|
4
4
|
import { Portal as RNPortal } from '../portal';
|
|
5
|
+
import { isTopLayer, pushLayer, removeLayer } from '../layer-stack';
|
|
5
6
|
import { Slot } from '../slot';
|
|
6
7
|
import type {
|
|
7
8
|
CloseProps,
|
|
@@ -15,8 +16,6 @@ import type {
|
|
|
15
16
|
TriggerProps,
|
|
16
17
|
} from './types';
|
|
17
18
|
|
|
18
|
-
const openContentStack: string[] = [];
|
|
19
|
-
|
|
20
19
|
// ---------------------------------------------------------------------------
|
|
21
20
|
// Contexts
|
|
22
21
|
// ---------------------------------------------------------------------------
|
|
@@ -173,17 +172,16 @@ const Content = React.forwardRef<View, ContentProps>(function Content(
|
|
|
173
172
|
onOpenChangeRef.current = onOpenChange;
|
|
174
173
|
React.useEffect(() => {
|
|
175
174
|
if ((!open && !forceMount) || Platform.OS !== 'web' || typeof document === 'undefined') return;
|
|
176
|
-
|
|
175
|
+
pushLayer(nativeID);
|
|
177
176
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
178
|
-
if (event.key !== 'Escape' ||
|
|
177
|
+
if (event.key !== 'Escape' || !isTopLayer(nativeID)) return;
|
|
179
178
|
onEscapeKeyDownRef.current?.(event);
|
|
180
179
|
if (!event.defaultPrevented) onOpenChangeRef.current(false);
|
|
181
180
|
};
|
|
182
181
|
document.addEventListener('keydown', onKeyDown);
|
|
183
182
|
return () => {
|
|
184
183
|
document.removeEventListener('keydown', onKeyDown);
|
|
185
|
-
|
|
186
|
-
if (index !== -1) openContentStack.splice(index, 1);
|
|
184
|
+
removeLayer(nativeID);
|
|
187
185
|
};
|
|
188
186
|
}, [open, forceMount, nativeID]);
|
|
189
187
|
|
|
@@ -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 {
|
|
@@ -140,9 +142,7 @@ const Trigger = React.forwardRef<View, TriggerProps>(function Trigger(
|
|
|
140
142
|
methods: {
|
|
141
143
|
open: () => {
|
|
142
144
|
onOpenChange(true);
|
|
143
|
-
augmentedRef.current
|
|
144
|
-
setTriggerPosition({ width, pageX, pageY, height });
|
|
145
|
-
});
|
|
145
|
+
measureTrigger(augmentedRef.current, setTriggerPosition);
|
|
146
146
|
},
|
|
147
147
|
close: () => {
|
|
148
148
|
setTriggerPosition(null);
|
|
@@ -154,9 +154,7 @@ const Trigger = React.forwardRef<View, TriggerProps>(function Trigger(
|
|
|
154
154
|
const onPress = React.useCallback(
|
|
155
155
|
(ev: GestureResponderEvent) => {
|
|
156
156
|
if (disabled) return;
|
|
157
|
-
augmentedRef.current
|
|
158
|
-
setTriggerPosition({ width, pageX, pageY, height });
|
|
159
|
-
});
|
|
157
|
+
measureTrigger(augmentedRef.current, setTriggerPosition);
|
|
160
158
|
onOpenChange(!open);
|
|
161
159
|
onPressProp?.(ev);
|
|
162
160
|
},
|
|
@@ -250,11 +248,23 @@ const Content = React.forwardRef<View, ContentProps>(function Content(
|
|
|
250
248
|
style,
|
|
251
249
|
disablePositioningStyle,
|
|
252
250
|
onLayout: onLayoutProp,
|
|
251
|
+
onEscapeKeyDown,
|
|
253
252
|
...props
|
|
254
253
|
},
|
|
255
254
|
ref
|
|
256
255
|
) {
|
|
257
|
-
const { open, triggerPosition, contentLayout, setContentLayout } =
|
|
256
|
+
const { open, onOpenChange, triggerPosition, contentLayout, setTriggerPosition, setContentLayout } =
|
|
257
|
+
useRootContext();
|
|
258
|
+
|
|
259
|
+
useEscapeDismiss(
|
|
260
|
+
open,
|
|
261
|
+
() => {
|
|
262
|
+
setTriggerPosition(null);
|
|
263
|
+
setContentLayout(null);
|
|
264
|
+
onOpenChange(false);
|
|
265
|
+
},
|
|
266
|
+
onEscapeKeyDown
|
|
267
|
+
);
|
|
258
268
|
|
|
259
269
|
const positionStyle = useRelativePosition({
|
|
260
270
|
align,
|
|
@@ -615,7 +625,9 @@ const SubContent = React.forwardRef<View, SubContentProps>(function SubContent(
|
|
|
615
625
|
{ asChild, forceMount, ...props },
|
|
616
626
|
ref
|
|
617
627
|
) {
|
|
618
|
-
const { open, nativeID } = useSubContext();
|
|
628
|
+
const { open, nativeID, onOpenChange } = useSubContext();
|
|
629
|
+
|
|
630
|
+
useEscapeDismiss(open, () => onOpenChange(false));
|
|
619
631
|
|
|
620
632
|
if (!forceMount && !open) {
|
|
621
633
|
return null;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Platform, type View } from 'react-native';
|
|
2
|
+
import type { LayoutPosition } from './use-relative-position';
|
|
3
|
+
|
|
4
|
+
// On web, react-native-web's `measure()` sums offsetLeft/offsetTop along the
|
|
5
|
+
// offsetParent chain, which leave out CSS transforms (a centred Dialog's -50%
|
|
6
|
+
// translate), while `measureInWindow()` uses getBoundingClientRect
|
|
7
|
+
// (react-native-web/dist/exports/UIManager/index.js).
|
|
8
|
+
export function measureTrigger(node: View | null, onMeasure: (position: LayoutPosition) => void): void {
|
|
9
|
+
if (Platform.OS === 'web') {
|
|
10
|
+
node?.measureInWindow?.((x, y, width, height) => onMeasure({ pageX: x, pageY: y, width, height }));
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
node?.measure?.((_x, _y, width, height, pageX, pageY) => onMeasure({ pageX, pageY, width, height }));
|
|
14
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { Platform } from 'react-native';
|
|
3
|
+
import { isTopLayer, pushLayer, removeLayer } from '../layer-stack';
|
|
4
|
+
|
|
5
|
+
// Web only: while `active`, this content is a layer in layer-stack.ts, and Escape
|
|
6
|
+
// in the capture phase (a focused input cannot swallow it) closes it when top, then is stopped (one Escape, one layer).
|
|
7
|
+
// `onEscapeKeyDown` runs first and may call preventDefault() to keep it open.
|
|
8
|
+
export function useEscapeDismiss(
|
|
9
|
+
active: boolean,
|
|
10
|
+
close: () => void,
|
|
11
|
+
onEscapeKeyDown?: (event: KeyboardEvent) => void
|
|
12
|
+
): void {
|
|
13
|
+
const id = React.useId();
|
|
14
|
+
const closeRef = React.useRef(close);
|
|
15
|
+
const onEscapeKeyDownRef = React.useRef(onEscapeKeyDown);
|
|
16
|
+
closeRef.current = close;
|
|
17
|
+
onEscapeKeyDownRef.current = onEscapeKeyDown;
|
|
18
|
+
|
|
19
|
+
React.useEffect(() => {
|
|
20
|
+
if (!active || Platform.OS !== 'web' || typeof document === 'undefined') return;
|
|
21
|
+
pushLayer(id);
|
|
22
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
23
|
+
if (event.key !== 'Escape' || !isTopLayer(id)) return;
|
|
24
|
+
onEscapeKeyDownRef.current?.(event);
|
|
25
|
+
if (event.defaultPrevented) return;
|
|
26
|
+
closeRef.current();
|
|
27
|
+
event.stopPropagation();
|
|
28
|
+
};
|
|
29
|
+
document.addEventListener('keydown', onKeyDown, true);
|
|
30
|
+
return () => {
|
|
31
|
+
document.removeEventListener('keydown', onKeyDown, true);
|
|
32
|
+
removeLayer(id);
|
|
33
|
+
};
|
|
34
|
+
}, [active, id]);
|
|
35
|
+
}
|
|
@@ -5,6 +5,7 @@ import type { Insets } from '../types';
|
|
|
5
5
|
import * as React from 'react';
|
|
6
6
|
import {
|
|
7
7
|
Dimensions,
|
|
8
|
+
Platform,
|
|
8
9
|
type LayoutRectangle,
|
|
9
10
|
type ScaledSize,
|
|
10
11
|
type ViewStyle,
|
|
@@ -40,7 +41,8 @@ export function useRelativePosition({
|
|
|
40
41
|
side,
|
|
41
42
|
disablePositioningStyle,
|
|
42
43
|
}: UseRelativePositionArgs) {
|
|
43
|
-
|
|
44
|
+
// On web `screen` is the monitor and `window` the page the content must stay inside; native keeps `screen`.
|
|
45
|
+
const dimensions = Dimensions.get(Platform.OS === 'web' ? 'window' : 'screen');
|
|
44
46
|
|
|
45
47
|
return React.useMemo(() => {
|
|
46
48
|
if (disablePositioningStyle) {
|
|
@@ -230,6 +232,7 @@ function getContentStyle({
|
|
|
230
232
|
dimensions,
|
|
231
233
|
}: GetContentStyleArgs) {
|
|
232
234
|
return Object.assign(
|
|
235
|
+
{},
|
|
233
236
|
POSITION_ABSOLUTE,
|
|
234
237
|
getSidePosition({
|
|
235
238
|
side,
|
|
@@ -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 {
|
|
@@ -99,9 +100,7 @@ const Trigger = React.forwardRef<View, TriggerProps>(function Trigger(
|
|
|
99
100
|
methods: {
|
|
100
101
|
open: () => {
|
|
101
102
|
onOpenChange(true);
|
|
102
|
-
augmentedRef.current
|
|
103
|
-
setTriggerPosition({ width, pageX, pageY, height });
|
|
104
|
-
});
|
|
103
|
+
measureTrigger(augmentedRef.current, setTriggerPosition);
|
|
105
104
|
},
|
|
106
105
|
close: () => {
|
|
107
106
|
setTriggerPosition(null);
|
|
@@ -113,9 +112,7 @@ const Trigger = React.forwardRef<View, TriggerProps>(function Trigger(
|
|
|
113
112
|
const onPress = React.useCallback(
|
|
114
113
|
(ev: GestureResponderEvent) => {
|
|
115
114
|
if (disabled) return;
|
|
116
|
-
augmentedRef.current
|
|
117
|
-
setTriggerPosition({ width, pageX, pageY, height });
|
|
118
|
-
});
|
|
115
|
+
measureTrigger(augmentedRef.current, setTriggerPosition);
|
|
119
116
|
onOpenChange(!open);
|
|
120
117
|
onPressProp?.(ev);
|
|
121
118
|
},
|
package/src/hover-card/types.ts
CHANGED
|
@@ -9,7 +9,9 @@ interface RootProps {
|
|
|
9
9
|
open?: boolean;
|
|
10
10
|
defaultOpen?: boolean;
|
|
11
11
|
onOpenChange?: (open: boolean) => void;
|
|
12
|
+
/** Accepted and currently ignored: no hover delay is applied. */
|
|
12
13
|
openDelay?: number;
|
|
14
|
+
/** Accepted and currently ignored: no hover delay is applied. */
|
|
13
15
|
closeDelay?: number;
|
|
14
16
|
children?: React.ReactNode;
|
|
15
17
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Shared stack of open dismissable layers (Dialog and Popover content) on web.
|
|
2
|
+
// Only the last-pushed layer reacts to Escape or an outside press, so one
|
|
3
|
+
// Escape closes one layer.
|
|
4
|
+
const layers: string[] = [];
|
|
5
|
+
|
|
6
|
+
function pushLayer(id: string): void {
|
|
7
|
+
layers.push(id);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function removeLayer(id: string): void {
|
|
11
|
+
const index = layers.indexOf(id);
|
|
12
|
+
if (index !== -1) layers.splice(index, 1);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function isTopLayer(id: string): boolean {
|
|
16
|
+
return layers[layers.length - 1] === id;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { isTopLayer, pushLayer, removeLayer };
|