@tallyui/primitives 3.0.0 → 3.0.2
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 +5 -1
- package/dist/index.js +36 -7
- package/package.json +1 -1
- package/src/dialog/dialog.tsx +39 -7
- package/src/dialog/types.ts +8 -2
package/dist/index.d.ts
CHANGED
|
@@ -303,9 +303,13 @@ interface PortalProps$7 extends ForceMountable {
|
|
|
303
303
|
hostName?: string;
|
|
304
304
|
children?: React.ReactNode;
|
|
305
305
|
}
|
|
306
|
-
interface OverlayProps$6 extends
|
|
306
|
+
interface OverlayProps$6 extends SlottablePressableProps, ForceMountable {
|
|
307
|
+
/** Close the dialog when the overlay is pressed. Default true. */
|
|
308
|
+
closeOnPress?: boolean;
|
|
307
309
|
}
|
|
308
310
|
interface ContentProps$a extends SlottableViewProps, ForceMountable {
|
|
311
|
+
/** Platform: WEB ONLY. Called on Escape before the dialog closes; call event.preventDefault() to keep it open. */
|
|
312
|
+
onEscapeKeyDown?: (event: KeyboardEvent) => void;
|
|
309
313
|
}
|
|
310
314
|
interface TitleProps$1 extends SlottableTextProps {
|
|
311
315
|
}
|
package/dist/index.js
CHANGED
|
@@ -684,8 +684,9 @@ __export(dialog_exports, {
|
|
|
684
684
|
|
|
685
685
|
// src/dialog/dialog.tsx
|
|
686
686
|
import * as React7 from "react";
|
|
687
|
-
import { Pressable as Pressable2, Text as Text2, View as View2 } from "react-native";
|
|
687
|
+
import { Platform, Pressable as Pressable2, Text as Text2, View as View2 } from "react-native";
|
|
688
688
|
import { jsx as jsx5 } from "react/jsx-runtime";
|
|
689
|
+
var openContentStack = [];
|
|
689
690
|
var RootContext2 = React7.createContext(null);
|
|
690
691
|
function useRootContext2() {
|
|
691
692
|
const context = React7.useContext(RootContext2);
|
|
@@ -748,18 +749,46 @@ function Portal3({ forceMount, hostName, children }) {
|
|
|
748
749
|
return /* @__PURE__ */ jsx5(Portal, { hostName, name: `${internalValue.nativeID}_portal`, children: /* @__PURE__ */ jsx5(InternalContext2.Provider, { value: internalValue, children: /* @__PURE__ */ jsx5(RootContext2.Provider, { value: rootValue, children }) }) });
|
|
749
750
|
}
|
|
750
751
|
Portal3.displayName = "DialogPortal";
|
|
751
|
-
var Overlay3 = React7.forwardRef(function Overlay4({ asChild, forceMount, ...props }, ref) {
|
|
752
|
-
const { open } = useRootContext2();
|
|
752
|
+
var Overlay3 = React7.forwardRef(function Overlay4({ asChild, forceMount, onPress: onPressProp, closeOnPress = true, ...props }, ref) {
|
|
753
|
+
const { open, onOpenChange } = useRootContext2();
|
|
754
|
+
const onPress = React7.useCallback(
|
|
755
|
+
(ev) => {
|
|
756
|
+
if (closeOnPress) {
|
|
757
|
+
onOpenChange(false);
|
|
758
|
+
}
|
|
759
|
+
onPressProp?.(ev);
|
|
760
|
+
},
|
|
761
|
+
[closeOnPress, onOpenChange, onPressProp]
|
|
762
|
+
);
|
|
753
763
|
if (!forceMount && !open) {
|
|
754
764
|
return null;
|
|
755
765
|
}
|
|
756
|
-
const Component = asChild ? Slot :
|
|
757
|
-
return /* @__PURE__ */ jsx5(Component, { ref, "aria-modal": true, ...props });
|
|
766
|
+
const Component = asChild ? Slot : Pressable2;
|
|
767
|
+
return /* @__PURE__ */ jsx5(Component, { ref, "aria-modal": true, onPress, ...props });
|
|
758
768
|
});
|
|
759
769
|
Overlay3.displayName = "DialogOverlay";
|
|
760
|
-
var Content3 = React7.forwardRef(function Content4({ asChild, forceMount, ...props }, ref) {
|
|
761
|
-
const { open } = useRootContext2();
|
|
770
|
+
var Content3 = React7.forwardRef(function Content4({ asChild, forceMount, onEscapeKeyDown, ...props }, ref) {
|
|
771
|
+
const { open, onOpenChange } = useRootContext2();
|
|
762
772
|
const { nativeID } = useInternalContext2();
|
|
773
|
+
const onEscapeKeyDownRef = React7.useRef(onEscapeKeyDown);
|
|
774
|
+
const onOpenChangeRef = React7.useRef(onOpenChange);
|
|
775
|
+
onEscapeKeyDownRef.current = onEscapeKeyDown;
|
|
776
|
+
onOpenChangeRef.current = onOpenChange;
|
|
777
|
+
React7.useEffect(() => {
|
|
778
|
+
if (!open && !forceMount || Platform.OS !== "web" || typeof document === "undefined") return;
|
|
779
|
+
openContentStack.push(nativeID);
|
|
780
|
+
const onKeyDown = (event) => {
|
|
781
|
+
if (event.key !== "Escape" || openContentStack[openContentStack.length - 1] !== nativeID) return;
|
|
782
|
+
onEscapeKeyDownRef.current?.(event);
|
|
783
|
+
if (!event.defaultPrevented) onOpenChangeRef.current(false);
|
|
784
|
+
};
|
|
785
|
+
document.addEventListener("keydown", onKeyDown);
|
|
786
|
+
return () => {
|
|
787
|
+
document.removeEventListener("keydown", onKeyDown);
|
|
788
|
+
const index = openContentStack.indexOf(nativeID);
|
|
789
|
+
if (index !== -1) openContentStack.splice(index, 1);
|
|
790
|
+
};
|
|
791
|
+
}, [open, forceMount, nativeID]);
|
|
763
792
|
if (!forceMount && !open) {
|
|
764
793
|
return null;
|
|
765
794
|
}
|
package/package.json
CHANGED
package/src/dialog/dialog.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import { Pressable, Text, View, type GestureResponderEvent } from 'react-native';
|
|
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
5
|
import { Slot } from '../slot';
|
|
@@ -15,6 +15,8 @@ import type {
|
|
|
15
15
|
TriggerProps,
|
|
16
16
|
} from './types';
|
|
17
17
|
|
|
18
|
+
const openContentStack: string[] = [];
|
|
19
|
+
|
|
18
20
|
// ---------------------------------------------------------------------------
|
|
19
21
|
// Contexts
|
|
20
22
|
// ---------------------------------------------------------------------------
|
|
@@ -128,18 +130,28 @@ Portal.displayName = 'DialogPortal';
|
|
|
128
130
|
// ---------------------------------------------------------------------------
|
|
129
131
|
|
|
130
132
|
const Overlay = React.forwardRef<View, OverlayProps>(function Overlay(
|
|
131
|
-
{ asChild, forceMount, ...props },
|
|
133
|
+
{ asChild, forceMount, onPress: onPressProp, closeOnPress = true, ...props },
|
|
132
134
|
ref
|
|
133
135
|
) {
|
|
134
|
-
const { open } = useRootContext();
|
|
136
|
+
const { open, onOpenChange } = useRootContext();
|
|
137
|
+
|
|
138
|
+
const onPress = React.useCallback(
|
|
139
|
+
(ev: GestureResponderEvent) => {
|
|
140
|
+
if (closeOnPress) {
|
|
141
|
+
onOpenChange(false);
|
|
142
|
+
}
|
|
143
|
+
onPressProp?.(ev);
|
|
144
|
+
},
|
|
145
|
+
[closeOnPress, onOpenChange, onPressProp]
|
|
146
|
+
);
|
|
135
147
|
|
|
136
148
|
if (!forceMount && !open) {
|
|
137
149
|
return null;
|
|
138
150
|
}
|
|
139
151
|
|
|
140
|
-
const Component = asChild ? Slot :
|
|
152
|
+
const Component = asChild ? Slot : Pressable;
|
|
141
153
|
|
|
142
|
-
return <Component ref={ref} aria-modal={true} {...props} />;
|
|
154
|
+
return <Component ref={ref} aria-modal={true} onPress={onPress} {...props} />;
|
|
143
155
|
});
|
|
144
156
|
|
|
145
157
|
Overlay.displayName = 'DialogOverlay';
|
|
@@ -149,12 +161,32 @@ Overlay.displayName = 'DialogOverlay';
|
|
|
149
161
|
// ---------------------------------------------------------------------------
|
|
150
162
|
|
|
151
163
|
const Content = React.forwardRef<View, ContentProps>(function Content(
|
|
152
|
-
{ asChild, forceMount, ...props },
|
|
164
|
+
{ asChild, forceMount, onEscapeKeyDown, ...props },
|
|
153
165
|
ref
|
|
154
166
|
) {
|
|
155
|
-
const { open } = useRootContext();
|
|
167
|
+
const { open, onOpenChange } = useRootContext();
|
|
156
168
|
const { nativeID } = useInternalContext();
|
|
157
169
|
|
|
170
|
+
const onEscapeKeyDownRef = React.useRef(onEscapeKeyDown);
|
|
171
|
+
const onOpenChangeRef = React.useRef(onOpenChange);
|
|
172
|
+
onEscapeKeyDownRef.current = onEscapeKeyDown;
|
|
173
|
+
onOpenChangeRef.current = onOpenChange;
|
|
174
|
+
React.useEffect(() => {
|
|
175
|
+
if ((!open && !forceMount) || Platform.OS !== 'web' || typeof document === 'undefined') return;
|
|
176
|
+
openContentStack.push(nativeID);
|
|
177
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
178
|
+
if (event.key !== 'Escape' || openContentStack[openContentStack.length - 1] !== nativeID) return;
|
|
179
|
+
onEscapeKeyDownRef.current?.(event);
|
|
180
|
+
if (!event.defaultPrevented) onOpenChangeRef.current(false);
|
|
181
|
+
};
|
|
182
|
+
document.addEventListener('keydown', onKeyDown);
|
|
183
|
+
return () => {
|
|
184
|
+
document.removeEventListener('keydown', onKeyDown);
|
|
185
|
+
const index = openContentStack.indexOf(nativeID);
|
|
186
|
+
if (index !== -1) openContentStack.splice(index, 1);
|
|
187
|
+
};
|
|
188
|
+
}, [open, forceMount, nativeID]);
|
|
189
|
+
|
|
158
190
|
if (!forceMount && !open) {
|
|
159
191
|
return null;
|
|
160
192
|
}
|
package/src/dialog/types.ts
CHANGED
|
@@ -19,9 +19,15 @@ interface PortalProps extends ForceMountable {
|
|
|
19
19
|
children?: React.ReactNode;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
interface OverlayProps extends
|
|
22
|
+
interface OverlayProps extends SlottablePressableProps, ForceMountable {
|
|
23
|
+
/** Close the dialog when the overlay is pressed. Default true. */
|
|
24
|
+
closeOnPress?: boolean;
|
|
25
|
+
}
|
|
23
26
|
|
|
24
|
-
interface ContentProps extends SlottableViewProps, ForceMountable {
|
|
27
|
+
interface ContentProps extends SlottableViewProps, ForceMountable {
|
|
28
|
+
/** Platform: WEB ONLY. Called on Escape before the dialog closes; call event.preventDefault() to keep it open. */
|
|
29
|
+
onEscapeKeyDown?: (event: KeyboardEvent) => void;
|
|
30
|
+
}
|
|
25
31
|
|
|
26
32
|
interface TitleProps extends SlottableTextProps {}
|
|
27
33
|
|