@tamagui/dismissable 1.0.1-beta.99 → 1.0.1-rc.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/dist/cjs/Dismissable.js +73 -85
- package/dist/cjs/Dismissable.js.map +3 -3
- package/dist/cjs/Dismissable.native.js +1 -0
- package/dist/cjs/Dismissable.native.js.map +1 -1
- package/dist/cjs/DismissableProps.js +1 -0
- package/dist/cjs/DismissableProps.js.map +2 -2
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/Dismissable.js +67 -83
- package/dist/esm/Dismissable.js.map +3 -3
- package/dist/esm/Dismissable.native.js +0 -0
- package/dist/esm/Dismissable.native.js.map +1 -1
- package/dist/esm/DismissableProps.js +0 -0
- package/dist/esm/DismissableProps.js.map +0 -0
- package/dist/esm/index.js +0 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/jsx/Dismissable.js +56 -38
- package/dist/jsx/Dismissable.js.map +3 -3
- package/dist/jsx/Dismissable.native.js +0 -0
- package/dist/jsx/Dismissable.native.js.map +1 -1
- package/dist/jsx/DismissableProps.js +0 -0
- package/dist/jsx/DismissableProps.js.map +0 -0
- package/dist/jsx/index.js +0 -0
- package/dist/jsx/index.js.map +1 -1
- package/package.json +11 -9
- package/src/Dismissable.tsx +11 -16
- package/src/DismissableProps.tsx +5 -0
- package/types/Dismissable.d.ts +1 -3
- package/types/Dismissable.native.d.ts +0 -0
- package/types/DismissableProps.d.ts +29 -0
- package/types/index.d.ts +0 -0
- package/types/Dismissable.d.ts.map +0 -1
- package/types/Dismissable.native.d.ts.map +0 -1
- package/types/DismissableProps.d.ts.map +0 -1
- package/types/index.d.ts.map +0 -1
package/dist/cjs/Dismissable.js
CHANGED
|
@@ -1,38 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
|
-
var __defProps = Object.defineProperties;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
9
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
-
var __spreadValues = (a, b) => {
|
|
13
|
-
for (var prop in b || (b = {}))
|
|
14
|
-
if (__hasOwnProp.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
if (__getOwnPropSymbols)
|
|
17
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
-
if (__propIsEnum.call(b, prop))
|
|
19
|
-
__defNormalProp(a, prop, b[prop]);
|
|
20
|
-
}
|
|
21
|
-
return a;
|
|
22
|
-
};
|
|
23
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
-
var __objRest = (source, exclude) => {
|
|
25
|
-
var target = {};
|
|
26
|
-
for (var prop in source)
|
|
27
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
28
|
-
target[prop] = source[prop];
|
|
29
|
-
if (source != null && __getOwnPropSymbols)
|
|
30
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
31
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
32
|
-
target[prop] = source[prop];
|
|
33
|
-
}
|
|
34
|
-
return target;
|
|
35
|
-
};
|
|
36
8
|
var __export = (target, all) => {
|
|
37
9
|
for (var name in all)
|
|
38
10
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -45,20 +17,22 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
45
17
|
}
|
|
46
18
|
return to;
|
|
47
19
|
};
|
|
48
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
49
24
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
50
25
|
var Dismissable_exports = {};
|
|
51
26
|
__export(Dismissable_exports, {
|
|
52
|
-
Branch: () => Branch,
|
|
53
27
|
Dismissable: () => Dismissable,
|
|
54
|
-
DismissableBranch: () => DismissableBranch
|
|
55
|
-
Root: () => Root
|
|
28
|
+
DismissableBranch: () => DismissableBranch
|
|
56
29
|
});
|
|
57
30
|
module.exports = __toCommonJS(Dismissable_exports);
|
|
58
|
-
var
|
|
31
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
59
32
|
var import_react_use_escape_keydown = require("@radix-ui/react-use-escape-keydown");
|
|
60
33
|
var import_compose_refs = require("@tamagui/compose-refs");
|
|
61
34
|
var import_core = require("@tamagui/core");
|
|
35
|
+
var import_use_event = require("@tamagui/use-event");
|
|
62
36
|
var React = __toESM(require("react"));
|
|
63
37
|
var ReactDOM = __toESM(require("react-dom"));
|
|
64
38
|
function dispatchDiscreteCustomEvent(target, event) {
|
|
@@ -76,21 +50,16 @@ const DismissableContext = React.createContext({
|
|
|
76
50
|
branches: /* @__PURE__ */ new Set()
|
|
77
51
|
});
|
|
78
52
|
const Dismissable = React.forwardRef((props, forwardedRef) => {
|
|
79
|
-
const
|
|
53
|
+
const {
|
|
80
54
|
disableOutsidePointerEvents = false,
|
|
55
|
+
forceUnmount,
|
|
81
56
|
onEscapeKeyDown,
|
|
82
57
|
onPointerDownOutside,
|
|
83
58
|
onFocusOutside,
|
|
84
59
|
onInteractOutside,
|
|
85
|
-
onDismiss
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
"onEscapeKeyDown",
|
|
89
|
-
"onPointerDownOutside",
|
|
90
|
-
"onFocusOutside",
|
|
91
|
-
"onInteractOutside",
|
|
92
|
-
"onDismiss"
|
|
93
|
-
]);
|
|
60
|
+
onDismiss,
|
|
61
|
+
...layerProps
|
|
62
|
+
} = props;
|
|
94
63
|
const context = React.useContext(DismissableContext);
|
|
95
64
|
const [node, setNode] = React.useState(null);
|
|
96
65
|
const [, force] = React.useState({});
|
|
@@ -150,6 +119,8 @@ const Dismissable = React.forwardRef((props, forwardedRef) => {
|
|
|
150
119
|
};
|
|
151
120
|
}, [node, disableOutsidePointerEvents, context]);
|
|
152
121
|
React.useEffect(() => {
|
|
122
|
+
if (forceUnmount)
|
|
123
|
+
return;
|
|
153
124
|
return () => {
|
|
154
125
|
if (!node)
|
|
155
126
|
return;
|
|
@@ -157,68 +128,89 @@ const Dismissable = React.forwardRef((props, forwardedRef) => {
|
|
|
157
128
|
context.layersWithOutsidePointerEventsDisabled.delete(node);
|
|
158
129
|
dispatchUpdate();
|
|
159
130
|
};
|
|
160
|
-
}, [node, context]);
|
|
131
|
+
}, [node, context, forceUnmount]);
|
|
161
132
|
React.useEffect(() => {
|
|
162
|
-
const handleUpdate = () =>
|
|
133
|
+
const handleUpdate = () => {
|
|
134
|
+
force({});
|
|
135
|
+
};
|
|
163
136
|
document.addEventListener(CONTEXT_UPDATE, handleUpdate);
|
|
164
137
|
return () => document.removeEventListener(CONTEXT_UPDATE, handleUpdate);
|
|
165
138
|
}, []);
|
|
166
|
-
return /* @__PURE__ */
|
|
139
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
|
|
140
|
+
...layerProps,
|
|
167
141
|
ref: composedRefs,
|
|
168
|
-
style:
|
|
142
|
+
style: {
|
|
169
143
|
display: "contents",
|
|
170
|
-
pointerEvents: isBodyPointerEventsDisabled ? isPointerEventsEnabled ? "auto" : "none" : void 0
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
144
|
+
pointerEvents: isBodyPointerEventsDisabled ? isPointerEventsEnabled ? "auto" : "none" : void 0,
|
|
145
|
+
...props.style
|
|
146
|
+
},
|
|
147
|
+
onFocusCapture: (0, import_core.composeEventHandlers)(
|
|
148
|
+
props.onFocusCapture,
|
|
149
|
+
focusOutside.onFocusCapture
|
|
150
|
+
),
|
|
151
|
+
onBlurCapture: (0, import_core.composeEventHandlers)(
|
|
152
|
+
props.onBlurCapture,
|
|
153
|
+
focusOutside.onBlurCapture
|
|
154
|
+
),
|
|
155
|
+
onPointerDownCapture: (0, import_core.composeEventHandlers)(
|
|
156
|
+
props.onPointerDownCapture,
|
|
157
|
+
pointerDownOutside.onPointerDownCapture
|
|
158
|
+
)
|
|
159
|
+
});
|
|
176
160
|
});
|
|
177
161
|
Dismissable.displayName = DISMISSABLE_LAYER_NAME;
|
|
178
162
|
const BRANCH_NAME = "DismissableBranch";
|
|
179
|
-
const DismissableBranch = React.forwardRef(
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
});
|
|
163
|
+
const DismissableBranch = React.forwardRef(
|
|
164
|
+
(props, forwardedRef) => {
|
|
165
|
+
const context = React.useContext(DismissableContext);
|
|
166
|
+
const ref = React.useRef(null);
|
|
167
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, ref);
|
|
168
|
+
React.useEffect(() => {
|
|
169
|
+
const node = ref.current;
|
|
170
|
+
if (node) {
|
|
171
|
+
context.branches.add(node);
|
|
172
|
+
return () => {
|
|
173
|
+
context.branches.delete(node);
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
}, [context.branches]);
|
|
177
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
|
|
178
|
+
style: { display: "contents" },
|
|
179
|
+
...props,
|
|
180
|
+
ref: composedRefs
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
);
|
|
198
184
|
DismissableBranch.displayName = BRANCH_NAME;
|
|
199
185
|
function usePointerDownOutside(onPointerDownOutside) {
|
|
200
|
-
const handlePointerDownOutside = (0,
|
|
186
|
+
const handlePointerDownOutside = (0, import_use_event.useEvent)(onPointerDownOutside);
|
|
201
187
|
const isPointerInsideReactTreeRef = React.useRef(false);
|
|
202
188
|
const handleClickRef = React.useRef(() => {
|
|
203
189
|
});
|
|
204
190
|
React.useEffect(() => {
|
|
205
191
|
const handlePointerDown = (event) => {
|
|
206
192
|
if (event.target && !isPointerInsideReactTreeRef.current) {
|
|
207
|
-
let
|
|
208
|
-
handleAndDispatchCustomEvent(
|
|
193
|
+
let handleAndDispatchPointerDownOutsideEvent2 = function() {
|
|
194
|
+
handleAndDispatchCustomEvent(
|
|
195
|
+
POINTER_DOWN_OUTSIDE,
|
|
196
|
+
handlePointerDownOutside,
|
|
197
|
+
eventDetail,
|
|
198
|
+
{ discrete: true }
|
|
199
|
+
);
|
|
209
200
|
};
|
|
201
|
+
var handleAndDispatchPointerDownOutsideEvent = handleAndDispatchPointerDownOutsideEvent2;
|
|
210
202
|
const eventDetail = { originalEvent: event };
|
|
211
203
|
if (event.pointerType === "touch") {
|
|
212
204
|
document.removeEventListener("click", handleClickRef.current);
|
|
213
|
-
handleClickRef.current =
|
|
205
|
+
handleClickRef.current = handleAndDispatchPointerDownOutsideEvent2;
|
|
214
206
|
document.addEventListener("click", handleClickRef.current, { once: true });
|
|
215
207
|
} else {
|
|
216
|
-
|
|
208
|
+
handleAndDispatchPointerDownOutsideEvent2();
|
|
217
209
|
}
|
|
218
210
|
}
|
|
219
211
|
isPointerInsideReactTreeRef.current = false;
|
|
220
212
|
};
|
|
221
|
-
const timerId =
|
|
213
|
+
const timerId = setTimeout(() => {
|
|
222
214
|
document.addEventListener("pointerdown", handlePointerDown);
|
|
223
215
|
}, 0);
|
|
224
216
|
return () => {
|
|
@@ -232,7 +224,7 @@ function usePointerDownOutside(onPointerDownOutside) {
|
|
|
232
224
|
};
|
|
233
225
|
}
|
|
234
226
|
function useFocusOutside(onFocusOutside) {
|
|
235
|
-
const handleFocusOutside = (0,
|
|
227
|
+
const handleFocusOutside = (0, import_use_event.useEvent)(onFocusOutside);
|
|
236
228
|
const isFocusInsideReactTreeRef = React.useRef(false);
|
|
237
229
|
React.useEffect(() => {
|
|
238
230
|
const handleFocus = (event) => {
|
|
@@ -266,13 +258,9 @@ function handleAndDispatchCustomEvent(name, handler, detail, { discrete }) {
|
|
|
266
258
|
target.dispatchEvent(event);
|
|
267
259
|
}
|
|
268
260
|
}
|
|
269
|
-
const Root = Dismissable;
|
|
270
|
-
const Branch = DismissableBranch;
|
|
271
261
|
// Annotate the CommonJS export names for ESM import in node:
|
|
272
262
|
0 && (module.exports = {
|
|
273
|
-
Branch,
|
|
274
263
|
Dismissable,
|
|
275
|
-
DismissableBranch
|
|
276
|
-
Root
|
|
264
|
+
DismissableBranch
|
|
277
265
|
});
|
|
278
266
|
//# sourceMappingURL=Dismissable.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/Dismissable.tsx"],
|
|
4
|
-
"sourcesContent": ["// forked from radix-ui\n// https://github.com/radix-ui/primitives/blob/cfd8dcba5fa6a0e751486af418d05a7b88a7f541/packages/react/dismissable-layer/src/DismissableLayer.tsx#L324\n\nimport { useCallbackRef } from '@radix-ui/react-use-callback-ref'\nimport { useEscapeKeydown } from '@radix-ui/react-use-escape-keydown'\nimport { useComposedRefs } from '@tamagui/compose-refs'\nimport { composeEventHandlers } from '@tamagui/core'\nimport * as React from 'react'\nimport * as ReactDOM from 'react-dom'\n\nimport { DismissableBranchProps, DismissableProps } from './DismissableProps'\n\nfunction dispatchDiscreteCustomEvent<E extends CustomEvent>(target: E['target'], event: E) {\n if (target) ReactDOM.flushSync(() => target.dispatchEvent(event))\n}\n\n/* -------------------------------------------------------------------------------------------------\n * Dismissable\n * -----------------------------------------------------------------------------------------------*/\n\nconst DISMISSABLE_LAYER_NAME = 'Dismissable'\nconst CONTEXT_UPDATE = 'dismissable.update'\nconst POINTER_DOWN_OUTSIDE = 'dismissable.pointerDownOutside'\nconst FOCUS_OUTSIDE = 'dismissable.focusOutside'\n\nlet originalBodyPointerEvents: string\n\nconst DismissableContext = React.createContext({\n layers: new Set<HTMLDivElement>(),\n layersWithOutsidePointerEventsDisabled: new Set<HTMLDivElement>(),\n branches: new Set<HTMLDivElement>(),\n})\n\nconst Dismissable = React.forwardRef<HTMLDivElement, DismissableProps>((props, forwardedRef) => {\n const {\n disableOutsidePointerEvents = false,\n onEscapeKeyDown,\n onPointerDownOutside,\n onFocusOutside,\n onInteractOutside,\n onDismiss,\n ...layerProps\n } = props\n const context = React.useContext(DismissableContext)\n const [node, setNode] = React.useState<HTMLDivElement | null>(null)\n const [, force] = React.useState({})\n const composedRefs = useComposedRefs(forwardedRef, (node) => setNode(node))\n const layers = Array.from(context.layers)\n const [highestLayerWithOutsidePointerEventsDisabled] = [...context.layersWithOutsidePointerEventsDisabled].slice(-1); // prettier-ignore\n const highestLayerWithOutsidePointerEventsDisabledIndex = layers.indexOf(highestLayerWithOutsidePointerEventsDisabled); // prettier-ignore\n const index = node ? layers.indexOf(node) : -1\n const isBodyPointerEventsDisabled = context.layersWithOutsidePointerEventsDisabled.size > 0\n const isPointerEventsEnabled = index >= highestLayerWithOutsidePointerEventsDisabledIndex\n\n const pointerDownOutside = usePointerDownOutside((event) => {\n const target = event.target as HTMLDivElement\n const isPointerDownOnBranch = [...context.branches].some((branch) => branch.contains(target))\n if (!isPointerEventsEnabled || isPointerDownOnBranch) return\n onPointerDownOutside?.(event)\n onInteractOutside?.(event)\n if (!event.defaultPrevented) onDismiss?.()\n })\n\n const focusOutside = useFocusOutside((event) => {\n const target = event.target as HTMLDivElement\n const isFocusInBranch = [...context.branches].some((branch) => branch.contains(target))\n if (isFocusInBranch) return\n onFocusOutside?.(event)\n onInteractOutside?.(event)\n if (!event.defaultPrevented) onDismiss?.()\n })\n\n useEscapeKeydown((event) => {\n const isHighestLayer = index === context.layers.size - 1\n if (!isHighestLayer) return\n onEscapeKeyDown?.(event)\n if (!event.defaultPrevented && onDismiss) {\n event.preventDefault()\n onDismiss()\n }\n })\n\n React.useEffect(() => {\n if (!node) return\n if (disableOutsidePointerEvents) {\n if (context.layersWithOutsidePointerEventsDisabled.size === 0) {\n originalBodyPointerEvents = document.body.style.pointerEvents\n document.body.style.pointerEvents = 'none'\n }\n context.layersWithOutsidePointerEventsDisabled.add(node)\n }\n context.layers.add(node)\n dispatchUpdate()\n return () => {\n if (\n disableOutsidePointerEvents &&\n context.layersWithOutsidePointerEventsDisabled.size === 1\n ) {\n document.body.style.pointerEvents = originalBodyPointerEvents\n }\n }\n }, [node, disableOutsidePointerEvents, context])\n\n /**\n * We purposefully prevent combining this effect with the `disableOutsidePointerEvents` effect\n * because a change to `disableOutsidePointerEvents` would remove this layer from the stack\n * and add it to the end again so the layering order wouldn't be _creation order_.\n * We only want them to be removed from context stacks when unmounted.\n */\n React.useEffect(() => {\n return () => {\n if (!node) return\n context.layers.delete(node)\n context.layersWithOutsidePointerEventsDisabled.delete(node)\n dispatchUpdate()\n }\n }, [node, context])\n\n React.useEffect(() => {\n const handleUpdate = () => force({})\n document.addEventListener(CONTEXT_UPDATE, handleUpdate)\n return () => document.removeEventListener(CONTEXT_UPDATE, handleUpdate)\n }, [])\n\n return (\n <div\n {...layerProps}\n // @ts-ignore\n ref={composedRefs}\n style={{\n display: 'contents',\n pointerEvents: isBodyPointerEventsDisabled\n ? isPointerEventsEnabled\n ? 'auto'\n : 'none'\n : undefined,\n // @ts-ignore\n ...props.style,\n }}\n onFocusCapture={composeEventHandlers(\n // @ts-ignore\n props.onFocusCapture,\n focusOutside.onFocusCapture\n )}\n onBlurCapture={composeEventHandlers(\n // @ts-ignore\n props.onBlurCapture,\n focusOutside.onBlurCapture\n )}\n // @ts-ignore\n onPointerDownCapture={composeEventHandlers(\n // @ts-ignore\n props.onPointerDownCapture,\n pointerDownOutside.onPointerDownCapture\n )}\n />\n )\n})\n\nDismissable.displayName = DISMISSABLE_LAYER_NAME\n\n/* -------------------------------------------------------------------------------------------------\n * DismissableBranch\n * -----------------------------------------------------------------------------------------------*/\n\nconst BRANCH_NAME = 'DismissableBranch'\n\nconst DismissableBranch = React.forwardRef<HTMLDivElement, DismissableBranchProps>(\n (props, forwardedRef) => {\n const context = React.useContext(DismissableContext)\n const ref = React.useRef<HTMLDivElement>(null)\n const composedRefs = useComposedRefs(forwardedRef, ref)\n\n React.useEffect(() => {\n const node = ref.current\n if (node) {\n context.branches.add(node)\n return () => {\n context.branches.delete(node)\n }\n }\n }, [context.branches])\n\n return <div style={{ display: 'contents' }} {...props} ref={composedRefs} />\n }\n)\n\nDismissableBranch.displayName = BRANCH_NAME\n\n/* -----------------------------------------------------------------------------------------------*/\n\nexport type PointerDownOutsideEvent = CustomEvent<{ originalEvent: PointerEvent }>\nexport type FocusOutsideEvent = CustomEvent<{ originalEvent: FocusEvent }>\n\n/**\n * Listens for `pointerdown` outside a react subtree. We use `pointerdown` rather than `pointerup`\n * to mimic layer dismissing behaviour present in OS.\n * Returns props to pass to the node we want to check for outside events.\n */\nfunction usePointerDownOutside(onPointerDownOutside?: (event: PointerDownOutsideEvent) => void) {\n const handlePointerDownOutside = useCallbackRef(onPointerDownOutside) as EventListener\n const isPointerInsideReactTreeRef = React.useRef(false)\n const handleClickRef = React.useRef(() => {})\n\n React.useEffect(() => {\n const handlePointerDown = (event: PointerEvent) => {\n if (event.target && !isPointerInsideReactTreeRef.current) {\n const eventDetail = { originalEvent: event }\n\n function handleAndDispatchPointerDownOutsideEvent() {\n handleAndDispatchCustomEvent(\n POINTER_DOWN_OUTSIDE,\n handlePointerDownOutside,\n eventDetail,\n { discrete: true }\n )\n }\n\n /**\n * On touch devices, we need to wait for a click event because browsers implement\n * a ~350ms delay between the time the user stops touching the display and when the\n * browser executres events. We need to ensure we don't reactivate pointer-events within\n * this timeframe otherwise the browser may execute events that should have been prevented.\n *\n * Additionally, this also lets us deal automatically with cancellations when a click event\n * isn't raised because the page was considered scrolled/drag-scrolled, long-pressed, etc.\n *\n * This is why we also continuously remove the previous listener, because we cannot be\n * certain that it was raised, and therefore cleaned-up.\n */\n if (event.pointerType === 'touch') {\n document.removeEventListener('click', handleClickRef.current)\n handleClickRef.current = handleAndDispatchPointerDownOutsideEvent\n document.addEventListener('click', handleClickRef.current, { once: true })\n } else {\n handleAndDispatchPointerDownOutsideEvent()\n }\n }\n isPointerInsideReactTreeRef.current = false\n }\n /**\n * if this hook executes in a component that mounts via a `pointerdown` event, the event\n * would bubble up to the document and trigger a `pointerDownOutside` event. We avoid\n * this by delaying the event listener registration on the document.\n * This is not React specific, but rather how the DOM works, ie:\n * ```\n * button.addEventListener('pointerdown', () => {\n * console.log('I will log');\n * document.addEventListener('pointerdown', () => {\n * console.log('I will also log');\n * })\n * });\n */\n const timerId = window.setTimeout(() => {\n document.addEventListener('pointerdown', handlePointerDown)\n }, 0)\n return () => {\n window.clearTimeout(timerId)\n document.removeEventListener('pointerdown', handlePointerDown)\n document.removeEventListener('click', handleClickRef.current)\n }\n }, [handlePointerDownOutside])\n\n return {\n // ensures we check React component tree (not just DOM tree)\n onPointerDownCapture: () => (isPointerInsideReactTreeRef.current = true),\n }\n}\n\n/**\n * Listens for when focus happens outside a react subtree.\n * Returns props to pass to the root (node) of the subtree we want to check.\n */\nfunction useFocusOutside(onFocusOutside?: (event: FocusOutsideEvent) => void) {\n const handleFocusOutside = useCallbackRef(onFocusOutside) as EventListener\n const isFocusInsideReactTreeRef = React.useRef(false)\n\n React.useEffect(() => {\n const handleFocus = (event: FocusEvent) => {\n if (event.target && !isFocusInsideReactTreeRef.current) {\n const eventDetail = { originalEvent: event }\n handleAndDispatchCustomEvent(FOCUS_OUTSIDE, handleFocusOutside, eventDetail, {\n discrete: false,\n })\n }\n }\n document.addEventListener('focusin', handleFocus)\n return () => document.removeEventListener('focusin', handleFocus)\n }, [handleFocusOutside])\n\n return {\n onFocusCapture: () => (isFocusInsideReactTreeRef.current = true),\n onBlurCapture: () => (isFocusInsideReactTreeRef.current = false),\n }\n}\n\nfunction dispatchUpdate() {\n const event = new CustomEvent(CONTEXT_UPDATE)\n document.dispatchEvent(event)\n}\n\nfunction handleAndDispatchCustomEvent<E extends CustomEvent, OriginalEvent extends Event>(\n name: string,\n handler: ((event: E) => void) | undefined,\n detail: { originalEvent: OriginalEvent } & (E extends CustomEvent<infer D> ? D : never),\n { discrete }: { discrete: boolean }\n) {\n const target = detail.originalEvent.target\n const event = new CustomEvent(name, { bubbles: false, cancelable: true, detail })\n if (handler) target.addEventListener(name, handler as EventListener, { once: true })\n\n if (discrete) {\n dispatchDiscreteCustomEvent(target, event)\n } else {\n target.dispatchEvent(event)\n }\n}\n\nconst Root = Dismissable\nconst Branch = DismissableBranch\n\nexport {\n Dismissable,\n DismissableBranch,\n //\n Root,\n Branch,\n}\n\nexport type { DismissableProps }\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": []
|
|
4
|
+
"sourcesContent": ["// forked from radix-ui\n// https://github.com/radix-ui/primitives/blob/cfd8dcba5fa6a0e751486af418d05a7b88a7f541/packages/react/dismissable-layer/src/DismissableLayer.tsx#L324\n\nimport { useEscapeKeydown } from '@radix-ui/react-use-escape-keydown'\nimport { useComposedRefs } from '@tamagui/compose-refs'\nimport { composeEventHandlers } from '@tamagui/core'\nimport { useEvent } from '@tamagui/use-event'\nimport * as React from 'react'\nimport * as ReactDOM from 'react-dom'\n\nimport { DismissableBranchProps, DismissableProps } from './DismissableProps'\n\nfunction dispatchDiscreteCustomEvent<E extends CustomEvent>(target: E['target'], event: E) {\n if (target) ReactDOM.flushSync(() => target.dispatchEvent(event))\n}\n\n/* -------------------------------------------------------------------------------------------------\n * Dismissable\n * -----------------------------------------------------------------------------------------------*/\n\nconst DISMISSABLE_LAYER_NAME = 'Dismissable'\nconst CONTEXT_UPDATE = 'dismissable.update'\nconst POINTER_DOWN_OUTSIDE = 'dismissable.pointerDownOutside'\nconst FOCUS_OUTSIDE = 'dismissable.focusOutside'\n\nlet originalBodyPointerEvents: string\n\nconst DismissableContext = React.createContext({\n layers: new Set<HTMLDivElement>(),\n layersWithOutsidePointerEventsDisabled: new Set<HTMLDivElement>(),\n branches: new Set<HTMLDivElement>(),\n})\n\nconst Dismissable = React.forwardRef<HTMLDivElement, DismissableProps>((props, forwardedRef) => {\n const {\n disableOutsidePointerEvents = false,\n forceUnmount,\n onEscapeKeyDown,\n onPointerDownOutside,\n onFocusOutside,\n onInteractOutside,\n onDismiss,\n ...layerProps\n } = props\n const context = React.useContext(DismissableContext)\n const [node, setNode] = React.useState<HTMLDivElement | null>(null)\n const [, force] = React.useState({})\n const composedRefs = useComposedRefs(forwardedRef, (node) => setNode(node))\n const layers = Array.from(context.layers)\n const [highestLayerWithOutsidePointerEventsDisabled] = [...context.layersWithOutsidePointerEventsDisabled].slice(-1); // prettier-ignore\n const highestLayerWithOutsidePointerEventsDisabledIndex = layers.indexOf(highestLayerWithOutsidePointerEventsDisabled); // prettier-ignore\n const index = node ? layers.indexOf(node) : -1\n const isBodyPointerEventsDisabled = context.layersWithOutsidePointerEventsDisabled.size > 0\n const isPointerEventsEnabled = index >= highestLayerWithOutsidePointerEventsDisabledIndex\n\n const pointerDownOutside = usePointerDownOutside((event) => {\n const target = event.target as HTMLDivElement\n const isPointerDownOnBranch = [...context.branches].some((branch) => branch.contains(target))\n if (!isPointerEventsEnabled || isPointerDownOnBranch) return\n onPointerDownOutside?.(event)\n onInteractOutside?.(event)\n if (!event.defaultPrevented) onDismiss?.()\n })\n\n const focusOutside = useFocusOutside((event) => {\n const target = event.target as HTMLDivElement\n const isFocusInBranch = [...context.branches].some((branch) => branch.contains(target))\n if (isFocusInBranch) return\n onFocusOutside?.(event)\n onInteractOutside?.(event)\n if (!event.defaultPrevented) onDismiss?.()\n })\n\n useEscapeKeydown((event) => {\n const isHighestLayer = index === context.layers.size - 1\n if (!isHighestLayer) return\n onEscapeKeyDown?.(event)\n if (!event.defaultPrevented && onDismiss) {\n event.preventDefault()\n onDismiss()\n }\n })\n\n React.useEffect(() => {\n if (!node) return\n if (disableOutsidePointerEvents) {\n if (context.layersWithOutsidePointerEventsDisabled.size === 0) {\n originalBodyPointerEvents = document.body.style.pointerEvents\n document.body.style.pointerEvents = 'none'\n }\n context.layersWithOutsidePointerEventsDisabled.add(node)\n }\n context.layers.add(node)\n dispatchUpdate()\n return () => {\n if (\n disableOutsidePointerEvents &&\n context.layersWithOutsidePointerEventsDisabled.size === 1\n ) {\n document.body.style.pointerEvents = originalBodyPointerEvents\n }\n }\n }, [node, disableOutsidePointerEvents, context])\n\n /**\n * We purposefully prevent combining this effect with the `disableOutsidePointerEvents` effect\n * because a change to `disableOutsidePointerEvents` would remove this layer from the stack\n * and add it to the end again so the layering order wouldn't be _creation order_.\n * We only want them to be removed from context stacks when unmounted.\n */\n React.useEffect(() => {\n if (forceUnmount) return\n return () => {\n if (!node) return\n context.layers.delete(node)\n context.layersWithOutsidePointerEventsDisabled.delete(node)\n dispatchUpdate()\n }\n }, [node, context, forceUnmount])\n\n React.useEffect(() => {\n const handleUpdate = () => {\n force({})\n }\n document.addEventListener(CONTEXT_UPDATE, handleUpdate)\n return () => document.removeEventListener(CONTEXT_UPDATE, handleUpdate)\n }, [])\n\n return (\n <div\n {...layerProps}\n // @ts-ignore\n ref={composedRefs}\n style={{\n display: 'contents',\n pointerEvents: isBodyPointerEventsDisabled\n ? isPointerEventsEnabled\n ? 'auto'\n : 'none'\n : undefined,\n // @ts-ignore\n ...props.style,\n }}\n onFocusCapture={composeEventHandlers(\n // @ts-ignore\n props.onFocusCapture,\n focusOutside.onFocusCapture\n )}\n onBlurCapture={composeEventHandlers(\n // @ts-ignore\n props.onBlurCapture,\n focusOutside.onBlurCapture\n )}\n // @ts-ignore\n onPointerDownCapture={composeEventHandlers(\n // @ts-ignore\n props.onPointerDownCapture,\n pointerDownOutside.onPointerDownCapture\n )}\n />\n )\n})\n\nDismissable.displayName = DISMISSABLE_LAYER_NAME\n\n/* -------------------------------------------------------------------------------------------------\n * DismissableBranch\n * -----------------------------------------------------------------------------------------------*/\n\nconst BRANCH_NAME = 'DismissableBranch'\n\nconst DismissableBranch = React.forwardRef<HTMLDivElement, DismissableBranchProps>(\n (props, forwardedRef) => {\n const context = React.useContext(DismissableContext)\n const ref = React.useRef<HTMLDivElement>(null)\n const composedRefs = useComposedRefs(forwardedRef, ref)\n\n React.useEffect(() => {\n const node = ref.current\n if (node) {\n context.branches.add(node)\n return () => {\n context.branches.delete(node)\n }\n }\n }, [context.branches])\n\n return <div style={{ display: 'contents' }} {...props} ref={composedRefs} />\n }\n)\n\nDismissableBranch.displayName = BRANCH_NAME\n\n/* -----------------------------------------------------------------------------------------------*/\n\nexport type PointerDownOutsideEvent = CustomEvent<{ originalEvent: PointerEvent }>\nexport type FocusOutsideEvent = CustomEvent<{ originalEvent: FocusEvent }>\n\n/**\n * Listens for `pointerdown` outside a react subtree. We use `pointerdown` rather than `pointerup`\n * to mimic layer dismissing behaviour present in OS.\n * Returns props to pass to the node we want to check for outside events.\n */\nfunction usePointerDownOutside(onPointerDownOutside?: (event: PointerDownOutsideEvent) => void) {\n const handlePointerDownOutside = useEvent(onPointerDownOutside) as EventListener\n const isPointerInsideReactTreeRef = React.useRef(false)\n const handleClickRef = React.useRef(() => {})\n\n React.useEffect(() => {\n const handlePointerDown = (event: PointerEvent) => {\n if (event.target && !isPointerInsideReactTreeRef.current) {\n const eventDetail = { originalEvent: event }\n\n function handleAndDispatchPointerDownOutsideEvent() {\n handleAndDispatchCustomEvent(\n POINTER_DOWN_OUTSIDE,\n handlePointerDownOutside,\n eventDetail,\n { discrete: true }\n )\n }\n\n /**\n * On touch devices, we need to wait for a click event because browsers implement\n * a ~350ms delay between the time the user stops touching the display and when the\n * browser executres events. We need to ensure we don't reactivate pointer-events within\n * this timeframe otherwise the browser may execute events that should have been prevented.\n *\n * Additionally, this also lets us deal automatically with cancellations when a click event\n * isn't raised because the page was considered scrolled/drag-scrolled, long-pressed, etc.\n *\n * This is why we also continuously remove the previous listener, because we cannot be\n * certain that it was raised, and therefore cleaned-up.\n */\n if (event.pointerType === 'touch') {\n document.removeEventListener('click', handleClickRef.current)\n handleClickRef.current = handleAndDispatchPointerDownOutsideEvent\n document.addEventListener('click', handleClickRef.current, { once: true })\n } else {\n handleAndDispatchPointerDownOutsideEvent()\n }\n }\n isPointerInsideReactTreeRef.current = false\n }\n /**\n * if this hook executes in a component that mounts via a `pointerdown` event, the event\n * would bubble up to the document and trigger a `pointerDownOutside` event. We avoid\n * this by delaying the event listener registration on the document.\n * This is not React specific, but rather how the DOM works, ie:\n * ```\n * button.addEventListener('pointerdown', () => {\n * console.log('I will log');\n * document.addEventListener('pointerdown', () => {\n * console.log('I will also log');\n * })\n * });\n */\n const timerId = setTimeout(() => {\n document.addEventListener('pointerdown', handlePointerDown)\n }, 0)\n return () => {\n window.clearTimeout(timerId)\n document.removeEventListener('pointerdown', handlePointerDown)\n document.removeEventListener('click', handleClickRef.current)\n }\n }, [handlePointerDownOutside])\n\n return {\n // ensures we check React component tree (not just DOM tree)\n onPointerDownCapture: () => (isPointerInsideReactTreeRef.current = true),\n }\n}\n\n/**\n * Listens for when focus happens outside a react subtree.\n * Returns props to pass to the root (node) of the subtree we want to check.\n */\nfunction useFocusOutside(onFocusOutside?: (event: FocusOutsideEvent) => void) {\n const handleFocusOutside = useEvent(onFocusOutside) as EventListener\n const isFocusInsideReactTreeRef = React.useRef(false)\n\n React.useEffect(() => {\n const handleFocus = (event: FocusEvent) => {\n if (event.target && !isFocusInsideReactTreeRef.current) {\n const eventDetail = { originalEvent: event }\n handleAndDispatchCustomEvent(FOCUS_OUTSIDE, handleFocusOutside, eventDetail, {\n discrete: false,\n })\n }\n }\n document.addEventListener('focusin', handleFocus)\n return () => document.removeEventListener('focusin', handleFocus)\n }, [handleFocusOutside])\n\n return {\n onFocusCapture: () => (isFocusInsideReactTreeRef.current = true),\n onBlurCapture: () => (isFocusInsideReactTreeRef.current = false),\n }\n}\n\nfunction dispatchUpdate() {\n const event = new CustomEvent(CONTEXT_UPDATE)\n document.dispatchEvent(event)\n}\n\nfunction handleAndDispatchCustomEvent<E extends CustomEvent, OriginalEvent extends Event>(\n name: string,\n handler: ((event: E) => void) | undefined,\n detail: { originalEvent: OriginalEvent } & (E extends CustomEvent<infer D> ? D : never),\n { discrete }: { discrete: boolean }\n) {\n const target = detail.originalEvent.target\n const event = new CustomEvent(name, { bubbles: false, cancelable: true, detail })\n if (handler) target.addEventListener(name, handler as EventListener, { once: true })\n\n if (discrete) {\n dispatchDiscreteCustomEvent(target, event)\n } else {\n target.dispatchEvent(event)\n }\n}\n\nexport { Dismissable, DismissableBranch }\n\nexport type { DismissableProps }\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAiII;AA9HJ,sCAAiC;AACjC,0BAAgC;AAChC,kBAAqC;AACrC,uBAAyB;AACzB,YAAuB;AACvB,eAA0B;AAI1B,SAAS,4BAAmD,QAAqB,OAAU;AACzF,MAAI;AAAQ,aAAS,UAAU,MAAM,OAAO,cAAc,KAAK,CAAC;AAClE;AAMA,MAAM,yBAAyB;AAC/B,MAAM,iBAAiB;AACvB,MAAM,uBAAuB;AAC7B,MAAM,gBAAgB;AAEtB,IAAI;AAEJ,MAAM,qBAAqB,MAAM,cAAc;AAAA,EAC7C,QAAQ,oBAAI,IAAoB;AAAA,EAChC,wCAAwC,oBAAI,IAAoB;AAA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WAAS,cAAc,KAAK;AAC9B;AAEA,SAAS,6BACP,MACA,SACA,QACA,EAAE,SAAS,GACX;AACA,QAAM,SAAS,OAAO,cAAc;AACpC,QAAM,QAAQ,IAAI,YAAY,MAAM,EAAE,SAAS,OAAO,YAAY,MAAM,OAAO,CAAC;AAChF,MAAI;AAAS,WAAO,iBAAiB,MAAM,SAA0B,EAAE,MAAM,KAAK,CAAC;AAEnF,MAAI,UAAU;AACZ,gCAA4B,QAAQ,KAAK;AAAA,EAC3C,OAAO;AACL,WAAO,cAAc,KAAK;AAAA,EAC5B;AACF;",
|
|
6
|
+
"names": ["node", "handleAndDispatchPointerDownOutsideEvent"]
|
|
7
7
|
}
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/Dismissable.native.tsx"],
|
|
4
4
|
"sourcesContent": ["import { forwardRef } from 'react'\n\nimport { DismissableBranchProps, DismissableProps } from './DismissableProps'\n\nexport const Dismissable = forwardRef((props: DismissableProps, _ref) => {\n return props.children as any\n})\n\nexport const DismissableBranch = forwardRef((props: DismissableBranchProps, _ref) => {\n return props.children as any\n})\n"],
|
|
5
|
-
"mappings": "
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA2B;AAIpB,MAAM,kBAAc,yBAAW,CAAC,OAAyB,SAAS;AACvE,SAAO,MAAM;AACf,CAAC;AAEM,MAAM,wBAAoB,yBAAW,CAAC,OAA+B,SAAS;AACnF,SAAO,MAAM;AACf,CAAC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/DismissableProps.tsx"],
|
|
4
|
-
"sourcesContent": ["import React from 'react'\n\nimport { FocusOutsideEvent, PointerDownOutsideEvent } from './Dismissable'\n\nexport interface DismissableProps {\n /**\n * When `true`, hover/focus/click interactions will be disabled on elements outside\n * the `Dismissable`. Users will need to click twice on outside elements to\n * interact with them: once to close the `Dismissable`, and again to trigger the element.\n */\n disableOutsidePointerEvents?: boolean\n /**\n * Event handler called when the escape key is down.\n * Can be prevented.\n */\n onEscapeKeyDown?: (event: KeyboardEvent) => void\n /**\n * Event handler called when the a `pointerdown` event happens outside of the `Dismissable`.\n * Can be prevented.\n */\n onPointerDownOutside?: (event: PointerDownOutsideEvent) => void\n /**\n * Event handler called when the focus moves outside of the `Dismissable`.\n * Can be prevented.\n */\n onFocusOutside?: (event: FocusOutsideEvent) => void\n /**\n * Event handler called when an interaction happens outside the `Dismissable`.\n * Specifically, when a `pointerdown` event happens outside or focus moves outside of it.\n * Can be prevented.\n */\n onInteractOutside?: (event: PointerDownOutsideEvent | FocusOutsideEvent) => void\n /**\n * Handler called when the `Dismissable` should be dismissed\n */\n onDismiss?: () => void\n\n children?: React.ReactNode\n}\n\nexport interface DismissableBranchProps {\n children?: React.ReactNode\n}\n"],
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import React from 'react'\n\nimport { FocusOutsideEvent, PointerDownOutsideEvent } from './Dismissable'\n\nexport interface DismissableProps {\n /**\n * When `true`, hover/focus/click interactions will be disabled on elements outside\n * the `Dismissable`. Users will need to click twice on outside elements to\n * interact with them: once to close the `Dismissable`, and again to trigger the element.\n */\n disableOutsidePointerEvents?: boolean\n /**\n * Event handler called when the escape key is down.\n * Can be prevented.\n */\n onEscapeKeyDown?: (event: KeyboardEvent) => void\n /**\n * Event handler called when the a `pointerdown` event happens outside of the `Dismissable`.\n * Can be prevented.\n */\n onPointerDownOutside?: (event: PointerDownOutsideEvent) => void\n /**\n * Event handler called when the focus moves outside of the `Dismissable`.\n * Can be prevented.\n */\n onFocusOutside?: (event: FocusOutsideEvent) => void\n /**\n * Event handler called when an interaction happens outside the `Dismissable`.\n * Specifically, when a `pointerdown` event happens outside or focus moves outside of it.\n * Can be prevented.\n */\n onInteractOutside?: (event: PointerDownOutsideEvent | FocusOutsideEvent) => void\n /**\n * Handler called when the `Dismissable` should be dismissed\n */\n onDismiss?: () => void\n\n /**\n * When using animations on exit, may want to simualte force unmount early\n */\n forceUnmount?: boolean\n\n children?: React.ReactNode\n}\n\nexport interface DismissableBranchProps {\n children?: React.ReactNode\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;AAAA;AAAA;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/cjs/index.js
CHANGED
package/dist/cjs/index.js.map
CHANGED
package/dist/esm/Dismissable.js
CHANGED
|
@@ -1,38 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __spreadValues = (a, b) => {
|
|
9
|
-
for (var prop in b || (b = {}))
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
-
var __objRest = (source, exclude) => {
|
|
21
|
-
var target = {};
|
|
22
|
-
for (var prop in source)
|
|
23
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
-
target[prop] = source[prop];
|
|
25
|
-
if (source != null && __getOwnPropSymbols)
|
|
26
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
-
target[prop] = source[prop];
|
|
29
|
-
}
|
|
30
|
-
return target;
|
|
31
|
-
};
|
|
32
|
-
import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
33
2
|
import { useEscapeKeydown } from "@radix-ui/react-use-escape-keydown";
|
|
34
3
|
import { useComposedRefs } from "@tamagui/compose-refs";
|
|
35
4
|
import { composeEventHandlers } from "@tamagui/core";
|
|
5
|
+
import { useEvent } from "@tamagui/use-event";
|
|
36
6
|
import * as React from "react";
|
|
37
7
|
import * as ReactDOM from "react-dom";
|
|
38
8
|
function dispatchDiscreteCustomEvent(target, event) {
|
|
@@ -50,21 +20,16 @@ const DismissableContext = React.createContext({
|
|
|
50
20
|
branches: /* @__PURE__ */ new Set()
|
|
51
21
|
});
|
|
52
22
|
const Dismissable = React.forwardRef((props, forwardedRef) => {
|
|
53
|
-
const
|
|
23
|
+
const {
|
|
54
24
|
disableOutsidePointerEvents = false,
|
|
25
|
+
forceUnmount,
|
|
55
26
|
onEscapeKeyDown,
|
|
56
27
|
onPointerDownOutside,
|
|
57
28
|
onFocusOutside,
|
|
58
29
|
onInteractOutside,
|
|
59
|
-
onDismiss
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
"onEscapeKeyDown",
|
|
63
|
-
"onPointerDownOutside",
|
|
64
|
-
"onFocusOutside",
|
|
65
|
-
"onInteractOutside",
|
|
66
|
-
"onDismiss"
|
|
67
|
-
]);
|
|
30
|
+
onDismiss,
|
|
31
|
+
...layerProps
|
|
32
|
+
} = props;
|
|
68
33
|
const context = React.useContext(DismissableContext);
|
|
69
34
|
const [node, setNode] = React.useState(null);
|
|
70
35
|
const [, force] = React.useState({});
|
|
@@ -124,6 +89,8 @@ const Dismissable = React.forwardRef((props, forwardedRef) => {
|
|
|
124
89
|
};
|
|
125
90
|
}, [node, disableOutsidePointerEvents, context]);
|
|
126
91
|
React.useEffect(() => {
|
|
92
|
+
if (forceUnmount)
|
|
93
|
+
return;
|
|
127
94
|
return () => {
|
|
128
95
|
if (!node)
|
|
129
96
|
return;
|
|
@@ -131,68 +98,89 @@ const Dismissable = React.forwardRef((props, forwardedRef) => {
|
|
|
131
98
|
context.layersWithOutsidePointerEventsDisabled.delete(node);
|
|
132
99
|
dispatchUpdate();
|
|
133
100
|
};
|
|
134
|
-
}, [node, context]);
|
|
101
|
+
}, [node, context, forceUnmount]);
|
|
135
102
|
React.useEffect(() => {
|
|
136
|
-
const handleUpdate = () =>
|
|
103
|
+
const handleUpdate = () => {
|
|
104
|
+
force({});
|
|
105
|
+
};
|
|
137
106
|
document.addEventListener(CONTEXT_UPDATE, handleUpdate);
|
|
138
107
|
return () => document.removeEventListener(CONTEXT_UPDATE, handleUpdate);
|
|
139
108
|
}, []);
|
|
140
|
-
return /* @__PURE__ */
|
|
109
|
+
return /* @__PURE__ */ jsx("div", {
|
|
110
|
+
...layerProps,
|
|
141
111
|
ref: composedRefs,
|
|
142
|
-
style:
|
|
112
|
+
style: {
|
|
143
113
|
display: "contents",
|
|
144
|
-
pointerEvents: isBodyPointerEventsDisabled ? isPointerEventsEnabled ? "auto" : "none" : void 0
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
114
|
+
pointerEvents: isBodyPointerEventsDisabled ? isPointerEventsEnabled ? "auto" : "none" : void 0,
|
|
115
|
+
...props.style
|
|
116
|
+
},
|
|
117
|
+
onFocusCapture: composeEventHandlers(
|
|
118
|
+
props.onFocusCapture,
|
|
119
|
+
focusOutside.onFocusCapture
|
|
120
|
+
),
|
|
121
|
+
onBlurCapture: composeEventHandlers(
|
|
122
|
+
props.onBlurCapture,
|
|
123
|
+
focusOutside.onBlurCapture
|
|
124
|
+
),
|
|
125
|
+
onPointerDownCapture: composeEventHandlers(
|
|
126
|
+
props.onPointerDownCapture,
|
|
127
|
+
pointerDownOutside.onPointerDownCapture
|
|
128
|
+
)
|
|
129
|
+
});
|
|
150
130
|
});
|
|
151
131
|
Dismissable.displayName = DISMISSABLE_LAYER_NAME;
|
|
152
132
|
const BRANCH_NAME = "DismissableBranch";
|
|
153
|
-
const DismissableBranch = React.forwardRef(
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
});
|
|
133
|
+
const DismissableBranch = React.forwardRef(
|
|
134
|
+
(props, forwardedRef) => {
|
|
135
|
+
const context = React.useContext(DismissableContext);
|
|
136
|
+
const ref = React.useRef(null);
|
|
137
|
+
const composedRefs = useComposedRefs(forwardedRef, ref);
|
|
138
|
+
React.useEffect(() => {
|
|
139
|
+
const node = ref.current;
|
|
140
|
+
if (node) {
|
|
141
|
+
context.branches.add(node);
|
|
142
|
+
return () => {
|
|
143
|
+
context.branches.delete(node);
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
}, [context.branches]);
|
|
147
|
+
return /* @__PURE__ */ jsx("div", {
|
|
148
|
+
style: { display: "contents" },
|
|
149
|
+
...props,
|
|
150
|
+
ref: composedRefs
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
);
|
|
172
154
|
DismissableBranch.displayName = BRANCH_NAME;
|
|
173
155
|
function usePointerDownOutside(onPointerDownOutside) {
|
|
174
|
-
const handlePointerDownOutside =
|
|
156
|
+
const handlePointerDownOutside = useEvent(onPointerDownOutside);
|
|
175
157
|
const isPointerInsideReactTreeRef = React.useRef(false);
|
|
176
158
|
const handleClickRef = React.useRef(() => {
|
|
177
159
|
});
|
|
178
160
|
React.useEffect(() => {
|
|
179
161
|
const handlePointerDown = (event) => {
|
|
180
162
|
if (event.target && !isPointerInsideReactTreeRef.current) {
|
|
181
|
-
let
|
|
182
|
-
handleAndDispatchCustomEvent(
|
|
163
|
+
let handleAndDispatchPointerDownOutsideEvent2 = function() {
|
|
164
|
+
handleAndDispatchCustomEvent(
|
|
165
|
+
POINTER_DOWN_OUTSIDE,
|
|
166
|
+
handlePointerDownOutside,
|
|
167
|
+
eventDetail,
|
|
168
|
+
{ discrete: true }
|
|
169
|
+
);
|
|
183
170
|
};
|
|
171
|
+
var handleAndDispatchPointerDownOutsideEvent = handleAndDispatchPointerDownOutsideEvent2;
|
|
184
172
|
const eventDetail = { originalEvent: event };
|
|
185
173
|
if (event.pointerType === "touch") {
|
|
186
174
|
document.removeEventListener("click", handleClickRef.current);
|
|
187
|
-
handleClickRef.current =
|
|
175
|
+
handleClickRef.current = handleAndDispatchPointerDownOutsideEvent2;
|
|
188
176
|
document.addEventListener("click", handleClickRef.current, { once: true });
|
|
189
177
|
} else {
|
|
190
|
-
|
|
178
|
+
handleAndDispatchPointerDownOutsideEvent2();
|
|
191
179
|
}
|
|
192
180
|
}
|
|
193
181
|
isPointerInsideReactTreeRef.current = false;
|
|
194
182
|
};
|
|
195
|
-
const timerId =
|
|
183
|
+
const timerId = setTimeout(() => {
|
|
196
184
|
document.addEventListener("pointerdown", handlePointerDown);
|
|
197
185
|
}, 0);
|
|
198
186
|
return () => {
|
|
@@ -206,7 +194,7 @@ function usePointerDownOutside(onPointerDownOutside) {
|
|
|
206
194
|
};
|
|
207
195
|
}
|
|
208
196
|
function useFocusOutside(onFocusOutside) {
|
|
209
|
-
const handleFocusOutside =
|
|
197
|
+
const handleFocusOutside = useEvent(onFocusOutside);
|
|
210
198
|
const isFocusInsideReactTreeRef = React.useRef(false);
|
|
211
199
|
React.useEffect(() => {
|
|
212
200
|
const handleFocus = (event) => {
|
|
@@ -240,12 +228,8 @@ function handleAndDispatchCustomEvent(name, handler, detail, { discrete }) {
|
|
|
240
228
|
target.dispatchEvent(event);
|
|
241
229
|
}
|
|
242
230
|
}
|
|
243
|
-
const Root = Dismissable;
|
|
244
|
-
const Branch = DismissableBranch;
|
|
245
231
|
export {
|
|
246
|
-
Branch,
|
|
247
232
|
Dismissable,
|
|
248
|
-
DismissableBranch
|
|
249
|
-
Root
|
|
233
|
+
DismissableBranch
|
|
250
234
|
};
|
|
251
235
|
//# sourceMappingURL=Dismissable.js.map
|