@tamagui/dismissable 1.0.1-beta.43
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 +285 -0
- package/dist/cjs/Dismissable.js.map +7 -0
- package/dist/cjs/Dismissable.native.js +36 -0
- package/dist/cjs/Dismissable.native.js.map +7 -0
- package/dist/cjs/DismissableProps.js +16 -0
- package/dist/cjs/DismissableProps.js.map +7 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/esm/Dismissable.js +224 -0
- package/dist/esm/Dismissable.js.map +7 -0
- package/dist/esm/Dismissable.native.js +13 -0
- package/dist/esm/Dismissable.native.js.map +7 -0
- package/dist/esm/DismissableProps.js +1 -0
- package/dist/esm/DismissableProps.js.map +7 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/jsx/Dismissable.js +212 -0
- package/dist/jsx/Dismissable.native.js +12 -0
- package/dist/jsx/DismissableProps.js +0 -0
- package/dist/jsx/index.js +1 -0
- package/package.json +38 -0
- package/types/Dismissable.d.ts +15 -0
- package/types/Dismissable.d.ts.map +1 -0
- package/types/Dismissable.native.d.ts +5 -0
- package/types/Dismissable.native.d.ts.map +1 -0
- package/types/DismissableProps.d.ts +15 -0
- package/types/DismissableProps.d.ts.map +1 -0
- package/types/index.d.ts +2 -0
- package/types/index.d.ts.map +1 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
25
|
+
var __objRest = (source, exclude) => {
|
|
26
|
+
var target = {};
|
|
27
|
+
for (var prop in source)
|
|
28
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
29
|
+
target[prop] = source[prop];
|
|
30
|
+
if (source != null && __getOwnPropSymbols)
|
|
31
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
32
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
33
|
+
target[prop] = source[prop];
|
|
34
|
+
}
|
|
35
|
+
return target;
|
|
36
|
+
};
|
|
37
|
+
var __export = (target, all) => {
|
|
38
|
+
for (var name in all)
|
|
39
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
40
|
+
};
|
|
41
|
+
var __copyProps = (to, from, except, desc) => {
|
|
42
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
43
|
+
for (let key of __getOwnPropNames(from))
|
|
44
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
45
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
46
|
+
}
|
|
47
|
+
return to;
|
|
48
|
+
};
|
|
49
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
|
|
50
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
51
|
+
var Dismissable_exports = {};
|
|
52
|
+
__export(Dismissable_exports, {
|
|
53
|
+
Branch: () => Branch,
|
|
54
|
+
Dismissable: () => Dismissable,
|
|
55
|
+
DismissableBranch: () => DismissableBranch,
|
|
56
|
+
Root: () => Root
|
|
57
|
+
});
|
|
58
|
+
module.exports = __toCommonJS(Dismissable_exports);
|
|
59
|
+
var import_react_use_callback_ref = require("@radix-ui/react-use-callback-ref");
|
|
60
|
+
var import_react_use_escape_keydown = require("@radix-ui/react-use-escape-keydown");
|
|
61
|
+
var import_compose_refs = require("@tamagui/compose-refs");
|
|
62
|
+
var import_core = require("@tamagui/core");
|
|
63
|
+
var React = __toESM(require("react"));
|
|
64
|
+
var ReactDOM = __toESM(require("react-dom"));
|
|
65
|
+
function dispatchDiscreteCustomEvent(target, event) {
|
|
66
|
+
if (target)
|
|
67
|
+
ReactDOM.flushSync(() => target.dispatchEvent(event));
|
|
68
|
+
}
|
|
69
|
+
__name(dispatchDiscreteCustomEvent, "dispatchDiscreteCustomEvent");
|
|
70
|
+
const DISMISSABLE_LAYER_NAME = "Dismissable";
|
|
71
|
+
const CONTEXT_UPDATE = "dismissable.update";
|
|
72
|
+
const POINTER_DOWN_OUTSIDE = "dismissable.pointerDownOutside";
|
|
73
|
+
const FOCUS_OUTSIDE = "dismissable.focusOutside";
|
|
74
|
+
let originalBodyPointerEvents;
|
|
75
|
+
const DismissableContext = React.createContext({
|
|
76
|
+
layers: /* @__PURE__ */ new Set(),
|
|
77
|
+
layersWithOutsidePointerEventsDisabled: /* @__PURE__ */ new Set(),
|
|
78
|
+
branches: /* @__PURE__ */ new Set()
|
|
79
|
+
});
|
|
80
|
+
const Dismissable = React.forwardRef((props, forwardedRef) => {
|
|
81
|
+
const _a = props, {
|
|
82
|
+
disableOutsidePointerEvents = false,
|
|
83
|
+
onEscapeKeyDown,
|
|
84
|
+
onPointerDownOutside,
|
|
85
|
+
onFocusOutside,
|
|
86
|
+
onInteractOutside,
|
|
87
|
+
onDismiss
|
|
88
|
+
} = _a, layerProps = __objRest(_a, [
|
|
89
|
+
"disableOutsidePointerEvents",
|
|
90
|
+
"onEscapeKeyDown",
|
|
91
|
+
"onPointerDownOutside",
|
|
92
|
+
"onFocusOutside",
|
|
93
|
+
"onInteractOutside",
|
|
94
|
+
"onDismiss"
|
|
95
|
+
]);
|
|
96
|
+
const context = React.useContext(DismissableContext);
|
|
97
|
+
const [node, setNode] = React.useState(null);
|
|
98
|
+
const [, force] = React.useState({});
|
|
99
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, (node2) => setNode(node2));
|
|
100
|
+
const layers = Array.from(context.layers);
|
|
101
|
+
const [highestLayerWithOutsidePointerEventsDisabled] = [...context.layersWithOutsidePointerEventsDisabled].slice(-1);
|
|
102
|
+
const highestLayerWithOutsidePointerEventsDisabledIndex = layers.indexOf(highestLayerWithOutsidePointerEventsDisabled);
|
|
103
|
+
const index = node ? layers.indexOf(node) : -1;
|
|
104
|
+
const isBodyPointerEventsDisabled = context.layersWithOutsidePointerEventsDisabled.size > 0;
|
|
105
|
+
const isPointerEventsEnabled = index >= highestLayerWithOutsidePointerEventsDisabledIndex;
|
|
106
|
+
const pointerDownOutside = usePointerDownOutside((event) => {
|
|
107
|
+
const target = event.target;
|
|
108
|
+
const isPointerDownOnBranch = [...context.branches].some((branch) => branch.contains(target));
|
|
109
|
+
if (!isPointerEventsEnabled || isPointerDownOnBranch)
|
|
110
|
+
return;
|
|
111
|
+
onPointerDownOutside == null ? void 0 : onPointerDownOutside(event);
|
|
112
|
+
onInteractOutside == null ? void 0 : onInteractOutside(event);
|
|
113
|
+
if (!event.defaultPrevented)
|
|
114
|
+
onDismiss == null ? void 0 : onDismiss();
|
|
115
|
+
});
|
|
116
|
+
const focusOutside = useFocusOutside((event) => {
|
|
117
|
+
const target = event.target;
|
|
118
|
+
const isFocusInBranch = [...context.branches].some((branch) => branch.contains(target));
|
|
119
|
+
if (isFocusInBranch)
|
|
120
|
+
return;
|
|
121
|
+
onFocusOutside == null ? void 0 : onFocusOutside(event);
|
|
122
|
+
onInteractOutside == null ? void 0 : onInteractOutside(event);
|
|
123
|
+
if (!event.defaultPrevented)
|
|
124
|
+
onDismiss == null ? void 0 : onDismiss();
|
|
125
|
+
});
|
|
126
|
+
(0, import_react_use_escape_keydown.useEscapeKeydown)((event) => {
|
|
127
|
+
const isHighestLayer = index === context.layers.size - 1;
|
|
128
|
+
if (!isHighestLayer)
|
|
129
|
+
return;
|
|
130
|
+
onEscapeKeyDown == null ? void 0 : onEscapeKeyDown(event);
|
|
131
|
+
if (!event.defaultPrevented && onDismiss) {
|
|
132
|
+
event.preventDefault();
|
|
133
|
+
onDismiss();
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
React.useEffect(() => {
|
|
137
|
+
if (!node)
|
|
138
|
+
return;
|
|
139
|
+
if (disableOutsidePointerEvents) {
|
|
140
|
+
if (context.layersWithOutsidePointerEventsDisabled.size === 0) {
|
|
141
|
+
originalBodyPointerEvents = document.body.style.pointerEvents;
|
|
142
|
+
document.body.style.pointerEvents = "none";
|
|
143
|
+
}
|
|
144
|
+
context.layersWithOutsidePointerEventsDisabled.add(node);
|
|
145
|
+
}
|
|
146
|
+
context.layers.add(node);
|
|
147
|
+
dispatchUpdate();
|
|
148
|
+
return () => {
|
|
149
|
+
if (disableOutsidePointerEvents && context.layersWithOutsidePointerEventsDisabled.size === 1) {
|
|
150
|
+
document.body.style.pointerEvents = originalBodyPointerEvents;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
}, [node, disableOutsidePointerEvents, context]);
|
|
154
|
+
React.useEffect(() => {
|
|
155
|
+
return () => {
|
|
156
|
+
if (!node)
|
|
157
|
+
return;
|
|
158
|
+
context.layers.delete(node);
|
|
159
|
+
context.layersWithOutsidePointerEventsDisabled.delete(node);
|
|
160
|
+
dispatchUpdate();
|
|
161
|
+
};
|
|
162
|
+
}, [node, context]);
|
|
163
|
+
React.useEffect(() => {
|
|
164
|
+
const handleUpdate = /* @__PURE__ */ __name(() => force({}), "handleUpdate");
|
|
165
|
+
document.addEventListener(CONTEXT_UPDATE, handleUpdate);
|
|
166
|
+
return () => document.removeEventListener(CONTEXT_UPDATE, handleUpdate);
|
|
167
|
+
}, []);
|
|
168
|
+
return /* @__PURE__ */ React.createElement("div", __spreadProps(__spreadValues({}, layerProps), {
|
|
169
|
+
ref: composedRefs,
|
|
170
|
+
style: __spreadValues({
|
|
171
|
+
display: "contents",
|
|
172
|
+
pointerEvents: isBodyPointerEventsDisabled ? isPointerEventsEnabled ? "auto" : "none" : void 0
|
|
173
|
+
}, props.style),
|
|
174
|
+
onFocusCapture: (0, import_core.composeEventHandlers)(props.onFocusCapture, focusOutside.onFocusCapture),
|
|
175
|
+
onBlurCapture: (0, import_core.composeEventHandlers)(props.onBlurCapture, focusOutside.onBlurCapture),
|
|
176
|
+
onPointerDownCapture: (0, import_core.composeEventHandlers)(props.onPointerDownCapture, pointerDownOutside.onPointerDownCapture)
|
|
177
|
+
}));
|
|
178
|
+
});
|
|
179
|
+
Dismissable.displayName = DISMISSABLE_LAYER_NAME;
|
|
180
|
+
const BRANCH_NAME = "DismissableBranch";
|
|
181
|
+
const DismissableBranch = React.forwardRef((props, forwardedRef) => {
|
|
182
|
+
const context = React.useContext(DismissableContext);
|
|
183
|
+
const ref = React.useRef(null);
|
|
184
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, ref);
|
|
185
|
+
React.useEffect(() => {
|
|
186
|
+
const node = ref.current;
|
|
187
|
+
if (node) {
|
|
188
|
+
context.branches.add(node);
|
|
189
|
+
return () => {
|
|
190
|
+
context.branches.delete(node);
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
}, [context.branches]);
|
|
194
|
+
return /* @__PURE__ */ React.createElement("div", __spreadProps(__spreadValues({
|
|
195
|
+
style: { display: "contents" }
|
|
196
|
+
}, props), {
|
|
197
|
+
ref: composedRefs
|
|
198
|
+
}));
|
|
199
|
+
});
|
|
200
|
+
DismissableBranch.displayName = BRANCH_NAME;
|
|
201
|
+
function usePointerDownOutside(onPointerDownOutside) {
|
|
202
|
+
const handlePointerDownOutside = (0, import_react_use_callback_ref.useCallbackRef)(onPointerDownOutside);
|
|
203
|
+
const isPointerInsideReactTreeRef = React.useRef(false);
|
|
204
|
+
const handleClickRef = React.useRef(() => {
|
|
205
|
+
});
|
|
206
|
+
React.useEffect(() => {
|
|
207
|
+
const handlePointerDown = /* @__PURE__ */ __name((event) => {
|
|
208
|
+
if (event.target && !isPointerInsideReactTreeRef.current) {
|
|
209
|
+
let handleAndDispatchPointerDownOutsideEvent = function() {
|
|
210
|
+
handleAndDispatchCustomEvent(POINTER_DOWN_OUTSIDE, handlePointerDownOutside, eventDetail, { discrete: true });
|
|
211
|
+
};
|
|
212
|
+
__name(handleAndDispatchPointerDownOutsideEvent, "handleAndDispatchPointerDownOutsideEvent");
|
|
213
|
+
const eventDetail = { originalEvent: event };
|
|
214
|
+
if (event.pointerType === "touch") {
|
|
215
|
+
document.removeEventListener("click", handleClickRef.current);
|
|
216
|
+
handleClickRef.current = handleAndDispatchPointerDownOutsideEvent;
|
|
217
|
+
document.addEventListener("click", handleClickRef.current, { once: true });
|
|
218
|
+
} else {
|
|
219
|
+
handleAndDispatchPointerDownOutsideEvent();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
isPointerInsideReactTreeRef.current = false;
|
|
223
|
+
}, "handlePointerDown");
|
|
224
|
+
const timerId = window.setTimeout(() => {
|
|
225
|
+
document.addEventListener("pointerdown", handlePointerDown);
|
|
226
|
+
}, 0);
|
|
227
|
+
return () => {
|
|
228
|
+
window.clearTimeout(timerId);
|
|
229
|
+
document.removeEventListener("pointerdown", handlePointerDown);
|
|
230
|
+
document.removeEventListener("click", handleClickRef.current);
|
|
231
|
+
};
|
|
232
|
+
}, [handlePointerDownOutside]);
|
|
233
|
+
return {
|
|
234
|
+
onPointerDownCapture: () => isPointerInsideReactTreeRef.current = true
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
__name(usePointerDownOutside, "usePointerDownOutside");
|
|
238
|
+
function useFocusOutside(onFocusOutside) {
|
|
239
|
+
const handleFocusOutside = (0, import_react_use_callback_ref.useCallbackRef)(onFocusOutside);
|
|
240
|
+
const isFocusInsideReactTreeRef = React.useRef(false);
|
|
241
|
+
React.useEffect(() => {
|
|
242
|
+
const handleFocus = /* @__PURE__ */ __name((event) => {
|
|
243
|
+
if (event.target && !isFocusInsideReactTreeRef.current) {
|
|
244
|
+
const eventDetail = { originalEvent: event };
|
|
245
|
+
handleAndDispatchCustomEvent(FOCUS_OUTSIDE, handleFocusOutside, eventDetail, {
|
|
246
|
+
discrete: false
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}, "handleFocus");
|
|
250
|
+
document.addEventListener("focusin", handleFocus);
|
|
251
|
+
return () => document.removeEventListener("focusin", handleFocus);
|
|
252
|
+
}, [handleFocusOutside]);
|
|
253
|
+
return {
|
|
254
|
+
onFocusCapture: () => isFocusInsideReactTreeRef.current = true,
|
|
255
|
+
onBlurCapture: () => isFocusInsideReactTreeRef.current = false
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
__name(useFocusOutside, "useFocusOutside");
|
|
259
|
+
function dispatchUpdate() {
|
|
260
|
+
const event = new CustomEvent(CONTEXT_UPDATE);
|
|
261
|
+
document.dispatchEvent(event);
|
|
262
|
+
}
|
|
263
|
+
__name(dispatchUpdate, "dispatchUpdate");
|
|
264
|
+
function handleAndDispatchCustomEvent(name, handler, detail, { discrete }) {
|
|
265
|
+
const target = detail.originalEvent.target;
|
|
266
|
+
const event = new CustomEvent(name, { bubbles: false, cancelable: true, detail });
|
|
267
|
+
if (handler)
|
|
268
|
+
target.addEventListener(name, handler, { once: true });
|
|
269
|
+
if (discrete) {
|
|
270
|
+
dispatchDiscreteCustomEvent(target, event);
|
|
271
|
+
} else {
|
|
272
|
+
target.dispatchEvent(event);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
__name(handleAndDispatchCustomEvent, "handleAndDispatchCustomEvent");
|
|
276
|
+
const Root = Dismissable;
|
|
277
|
+
const Branch = DismissableBranch;
|
|
278
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
279
|
+
0 && (module.exports = {
|
|
280
|
+
Branch,
|
|
281
|
+
Dismissable,
|
|
282
|
+
DismissableBranch,
|
|
283
|
+
Root
|
|
284
|
+
});
|
|
285
|
+
//# sourceMappingURL=Dismissable.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 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": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,oCAA+B;AAC/B,sCAAiC;AACjC,0BAAgC;AAChC,kBAAqC;AACrC,YAAuB;AACvB,eAA0B;AAI1B,qCAA4D,QAAqB,OAAU;AACzF,MAAI;AAAQ,aAAS,UAAU,MAAM,OAAO,cAAc,KAAK,CAAC;AAClE;AAFS;AAQT,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;AAAA,EAChE,UAAU,oBAAI,IAAoB;AACpC,CAAC;AAED,MAAM,cAAc,MAAM,WAA6C,CAAC,OAAO,iBAAiB;AAC9F,QAQI,YAPF;A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AM,SAA0B,EAAE,MAAM,KAAK,CAAC;AAEnF,MAAI,UAAU;AACZ,gCAA4B,QAAQ,KAAK;AAAA,EAC3C,OAAO;AACL,WAAO,cAAc,KAAK;AAAA,EAC5B;AACF;AAfS;AAiBT,MAAM,OAAO;AACb,MAAM,SAAS;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var Dismissable_native_exports = {};
|
|
20
|
+
__export(Dismissable_native_exports, {
|
|
21
|
+
Dismissable: () => Dismissable,
|
|
22
|
+
DismissableBranch: () => DismissableBranch
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(Dismissable_native_exports);
|
|
25
|
+
const Dismissable = /* @__PURE__ */ __name((props) => {
|
|
26
|
+
return props.children;
|
|
27
|
+
}, "Dismissable");
|
|
28
|
+
const DismissableBranch = /* @__PURE__ */ __name((props) => {
|
|
29
|
+
return props.children;
|
|
30
|
+
}, "DismissableBranch");
|
|
31
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
32
|
+
0 && (module.exports = {
|
|
33
|
+
Dismissable,
|
|
34
|
+
DismissableBranch
|
|
35
|
+
});
|
|
36
|
+
//# sourceMappingURL=Dismissable.native.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/Dismissable.native.tsx"],
|
|
4
|
+
"sourcesContent": ["import { DismissableBranchProps, DismissableProps } from './DismissableProps'\n\nexport const Dismissable = (props: DismissableProps) => {\n return props.children\n}\n\nexport const DismissableBranch = (props: DismissableBranchProps) => {\n return props.children\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEO,MAAM,cAAc,wBAAC,UAA4B;AACtD,SAAO,MAAM;AACf,GAF2B;AAIpB,MAAM,oBAAoB,wBAAC,UAAkC;AAClE,SAAO,MAAM;AACf,GAFiC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
var DismissableProps_exports = {};
|
|
15
|
+
module.exports = __toCommonJS(DismissableProps_exports);
|
|
16
|
+
//# sourceMappingURL=DismissableProps.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 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": ";;;;;;;;;;;;;AAAA;AAAA;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var src_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(src_exports);
|
|
17
|
+
__reExport(src_exports, require("./Dismissable"), module.exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
|
|
4
|
+
import { useEscapeKeydown } from "@radix-ui/react-use-escape-keydown";
|
|
5
|
+
import { useComposedRefs } from "@tamagui/compose-refs";
|
|
6
|
+
import { composeEventHandlers } from "@tamagui/core";
|
|
7
|
+
import * as React from "react";
|
|
8
|
+
import * as ReactDOM from "react-dom";
|
|
9
|
+
function dispatchDiscreteCustomEvent(target, event) {
|
|
10
|
+
if (target)
|
|
11
|
+
ReactDOM.flushSync(() => target.dispatchEvent(event));
|
|
12
|
+
}
|
|
13
|
+
__name(dispatchDiscreteCustomEvent, "dispatchDiscreteCustomEvent");
|
|
14
|
+
const DISMISSABLE_LAYER_NAME = "Dismissable";
|
|
15
|
+
const CONTEXT_UPDATE = "dismissable.update";
|
|
16
|
+
const POINTER_DOWN_OUTSIDE = "dismissable.pointerDownOutside";
|
|
17
|
+
const FOCUS_OUTSIDE = "dismissable.focusOutside";
|
|
18
|
+
let originalBodyPointerEvents;
|
|
19
|
+
const DismissableContext = React.createContext({
|
|
20
|
+
layers: /* @__PURE__ */ new Set(),
|
|
21
|
+
layersWithOutsidePointerEventsDisabled: /* @__PURE__ */ new Set(),
|
|
22
|
+
branches: /* @__PURE__ */ new Set()
|
|
23
|
+
});
|
|
24
|
+
const Dismissable = React.forwardRef((props, forwardedRef) => {
|
|
25
|
+
const {
|
|
26
|
+
disableOutsidePointerEvents = false,
|
|
27
|
+
onEscapeKeyDown,
|
|
28
|
+
onPointerDownOutside,
|
|
29
|
+
onFocusOutside,
|
|
30
|
+
onInteractOutside,
|
|
31
|
+
onDismiss,
|
|
32
|
+
...layerProps
|
|
33
|
+
} = props;
|
|
34
|
+
const context = React.useContext(DismissableContext);
|
|
35
|
+
const [node, setNode] = React.useState(null);
|
|
36
|
+
const [, force] = React.useState({});
|
|
37
|
+
const composedRefs = useComposedRefs(forwardedRef, (node2) => setNode(node2));
|
|
38
|
+
const layers = Array.from(context.layers);
|
|
39
|
+
const [highestLayerWithOutsidePointerEventsDisabled] = [...context.layersWithOutsidePointerEventsDisabled].slice(-1);
|
|
40
|
+
const highestLayerWithOutsidePointerEventsDisabledIndex = layers.indexOf(highestLayerWithOutsidePointerEventsDisabled);
|
|
41
|
+
const index = node ? layers.indexOf(node) : -1;
|
|
42
|
+
const isBodyPointerEventsDisabled = context.layersWithOutsidePointerEventsDisabled.size > 0;
|
|
43
|
+
const isPointerEventsEnabled = index >= highestLayerWithOutsidePointerEventsDisabledIndex;
|
|
44
|
+
const pointerDownOutside = usePointerDownOutside((event) => {
|
|
45
|
+
const target = event.target;
|
|
46
|
+
const isPointerDownOnBranch = [...context.branches].some((branch) => branch.contains(target));
|
|
47
|
+
if (!isPointerEventsEnabled || isPointerDownOnBranch)
|
|
48
|
+
return;
|
|
49
|
+
onPointerDownOutside == null ? void 0 : onPointerDownOutside(event);
|
|
50
|
+
onInteractOutside == null ? void 0 : onInteractOutside(event);
|
|
51
|
+
if (!event.defaultPrevented)
|
|
52
|
+
onDismiss == null ? void 0 : onDismiss();
|
|
53
|
+
});
|
|
54
|
+
const focusOutside = useFocusOutside((event) => {
|
|
55
|
+
const target = event.target;
|
|
56
|
+
const isFocusInBranch = [...context.branches].some((branch) => branch.contains(target));
|
|
57
|
+
if (isFocusInBranch)
|
|
58
|
+
return;
|
|
59
|
+
onFocusOutside == null ? void 0 : onFocusOutside(event);
|
|
60
|
+
onInteractOutside == null ? void 0 : onInteractOutside(event);
|
|
61
|
+
if (!event.defaultPrevented)
|
|
62
|
+
onDismiss == null ? void 0 : onDismiss();
|
|
63
|
+
});
|
|
64
|
+
useEscapeKeydown((event) => {
|
|
65
|
+
const isHighestLayer = index === context.layers.size - 1;
|
|
66
|
+
if (!isHighestLayer)
|
|
67
|
+
return;
|
|
68
|
+
onEscapeKeyDown == null ? void 0 : onEscapeKeyDown(event);
|
|
69
|
+
if (!event.defaultPrevented && onDismiss) {
|
|
70
|
+
event.preventDefault();
|
|
71
|
+
onDismiss();
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
React.useEffect(() => {
|
|
75
|
+
if (!node)
|
|
76
|
+
return;
|
|
77
|
+
if (disableOutsidePointerEvents) {
|
|
78
|
+
if (context.layersWithOutsidePointerEventsDisabled.size === 0) {
|
|
79
|
+
originalBodyPointerEvents = document.body.style.pointerEvents;
|
|
80
|
+
document.body.style.pointerEvents = "none";
|
|
81
|
+
}
|
|
82
|
+
context.layersWithOutsidePointerEventsDisabled.add(node);
|
|
83
|
+
}
|
|
84
|
+
context.layers.add(node);
|
|
85
|
+
dispatchUpdate();
|
|
86
|
+
return () => {
|
|
87
|
+
if (disableOutsidePointerEvents && context.layersWithOutsidePointerEventsDisabled.size === 1) {
|
|
88
|
+
document.body.style.pointerEvents = originalBodyPointerEvents;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}, [node, disableOutsidePointerEvents, context]);
|
|
92
|
+
React.useEffect(() => {
|
|
93
|
+
return () => {
|
|
94
|
+
if (!node)
|
|
95
|
+
return;
|
|
96
|
+
context.layers.delete(node);
|
|
97
|
+
context.layersWithOutsidePointerEventsDisabled.delete(node);
|
|
98
|
+
dispatchUpdate();
|
|
99
|
+
};
|
|
100
|
+
}, [node, context]);
|
|
101
|
+
React.useEffect(() => {
|
|
102
|
+
const handleUpdate = /* @__PURE__ */ __name(() => force({}), "handleUpdate");
|
|
103
|
+
document.addEventListener(CONTEXT_UPDATE, handleUpdate);
|
|
104
|
+
return () => document.removeEventListener(CONTEXT_UPDATE, handleUpdate);
|
|
105
|
+
}, []);
|
|
106
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
107
|
+
...layerProps,
|
|
108
|
+
ref: composedRefs,
|
|
109
|
+
style: {
|
|
110
|
+
display: "contents",
|
|
111
|
+
pointerEvents: isBodyPointerEventsDisabled ? isPointerEventsEnabled ? "auto" : "none" : void 0,
|
|
112
|
+
...props.style
|
|
113
|
+
},
|
|
114
|
+
onFocusCapture: composeEventHandlers(props.onFocusCapture, focusOutside.onFocusCapture),
|
|
115
|
+
onBlurCapture: composeEventHandlers(props.onBlurCapture, focusOutside.onBlurCapture),
|
|
116
|
+
onPointerDownCapture: composeEventHandlers(props.onPointerDownCapture, pointerDownOutside.onPointerDownCapture)
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
Dismissable.displayName = DISMISSABLE_LAYER_NAME;
|
|
120
|
+
const BRANCH_NAME = "DismissableBranch";
|
|
121
|
+
const DismissableBranch = React.forwardRef((props, forwardedRef) => {
|
|
122
|
+
const context = React.useContext(DismissableContext);
|
|
123
|
+
const ref = React.useRef(null);
|
|
124
|
+
const composedRefs = useComposedRefs(forwardedRef, ref);
|
|
125
|
+
React.useEffect(() => {
|
|
126
|
+
const node = ref.current;
|
|
127
|
+
if (node) {
|
|
128
|
+
context.branches.add(node);
|
|
129
|
+
return () => {
|
|
130
|
+
context.branches.delete(node);
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
}, [context.branches]);
|
|
134
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
135
|
+
style: { display: "contents" },
|
|
136
|
+
...props,
|
|
137
|
+
ref: composedRefs
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
DismissableBranch.displayName = BRANCH_NAME;
|
|
141
|
+
function usePointerDownOutside(onPointerDownOutside) {
|
|
142
|
+
const handlePointerDownOutside = useCallbackRef(onPointerDownOutside);
|
|
143
|
+
const isPointerInsideReactTreeRef = React.useRef(false);
|
|
144
|
+
const handleClickRef = React.useRef(() => {
|
|
145
|
+
});
|
|
146
|
+
React.useEffect(() => {
|
|
147
|
+
const handlePointerDown = /* @__PURE__ */ __name((event) => {
|
|
148
|
+
if (event.target && !isPointerInsideReactTreeRef.current) {
|
|
149
|
+
let handleAndDispatchPointerDownOutsideEvent = function() {
|
|
150
|
+
handleAndDispatchCustomEvent(POINTER_DOWN_OUTSIDE, handlePointerDownOutside, eventDetail, { discrete: true });
|
|
151
|
+
};
|
|
152
|
+
__name(handleAndDispatchPointerDownOutsideEvent, "handleAndDispatchPointerDownOutsideEvent");
|
|
153
|
+
const eventDetail = { originalEvent: event };
|
|
154
|
+
if (event.pointerType === "touch") {
|
|
155
|
+
document.removeEventListener("click", handleClickRef.current);
|
|
156
|
+
handleClickRef.current = handleAndDispatchPointerDownOutsideEvent;
|
|
157
|
+
document.addEventListener("click", handleClickRef.current, { once: true });
|
|
158
|
+
} else {
|
|
159
|
+
handleAndDispatchPointerDownOutsideEvent();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
isPointerInsideReactTreeRef.current = false;
|
|
163
|
+
}, "handlePointerDown");
|
|
164
|
+
const timerId = window.setTimeout(() => {
|
|
165
|
+
document.addEventListener("pointerdown", handlePointerDown);
|
|
166
|
+
}, 0);
|
|
167
|
+
return () => {
|
|
168
|
+
window.clearTimeout(timerId);
|
|
169
|
+
document.removeEventListener("pointerdown", handlePointerDown);
|
|
170
|
+
document.removeEventListener("click", handleClickRef.current);
|
|
171
|
+
};
|
|
172
|
+
}, [handlePointerDownOutside]);
|
|
173
|
+
return {
|
|
174
|
+
onPointerDownCapture: () => isPointerInsideReactTreeRef.current = true
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
__name(usePointerDownOutside, "usePointerDownOutside");
|
|
178
|
+
function useFocusOutside(onFocusOutside) {
|
|
179
|
+
const handleFocusOutside = useCallbackRef(onFocusOutside);
|
|
180
|
+
const isFocusInsideReactTreeRef = React.useRef(false);
|
|
181
|
+
React.useEffect(() => {
|
|
182
|
+
const handleFocus = /* @__PURE__ */ __name((event) => {
|
|
183
|
+
if (event.target && !isFocusInsideReactTreeRef.current) {
|
|
184
|
+
const eventDetail = { originalEvent: event };
|
|
185
|
+
handleAndDispatchCustomEvent(FOCUS_OUTSIDE, handleFocusOutside, eventDetail, {
|
|
186
|
+
discrete: false
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}, "handleFocus");
|
|
190
|
+
document.addEventListener("focusin", handleFocus);
|
|
191
|
+
return () => document.removeEventListener("focusin", handleFocus);
|
|
192
|
+
}, [handleFocusOutside]);
|
|
193
|
+
return {
|
|
194
|
+
onFocusCapture: () => isFocusInsideReactTreeRef.current = true,
|
|
195
|
+
onBlurCapture: () => isFocusInsideReactTreeRef.current = false
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
__name(useFocusOutside, "useFocusOutside");
|
|
199
|
+
function dispatchUpdate() {
|
|
200
|
+
const event = new CustomEvent(CONTEXT_UPDATE);
|
|
201
|
+
document.dispatchEvent(event);
|
|
202
|
+
}
|
|
203
|
+
__name(dispatchUpdate, "dispatchUpdate");
|
|
204
|
+
function handleAndDispatchCustomEvent(name, handler, detail, { discrete }) {
|
|
205
|
+
const target = detail.originalEvent.target;
|
|
206
|
+
const event = new CustomEvent(name, { bubbles: false, cancelable: true, detail });
|
|
207
|
+
if (handler)
|
|
208
|
+
target.addEventListener(name, handler, { once: true });
|
|
209
|
+
if (discrete) {
|
|
210
|
+
dispatchDiscreteCustomEvent(target, event);
|
|
211
|
+
} else {
|
|
212
|
+
target.dispatchEvent(event);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
__name(handleAndDispatchCustomEvent, "handleAndDispatchCustomEvent");
|
|
216
|
+
const Root = Dismissable;
|
|
217
|
+
const Branch = DismissableBranch;
|
|
218
|
+
export {
|
|
219
|
+
Branch,
|
|
220
|
+
Dismissable,
|
|
221
|
+
DismissableBranch,
|
|
222
|
+
Root
|
|
223
|
+
};
|
|
224
|
+
//# sourceMappingURL=Dismissable.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 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": ";;AAGA;AACA;AACA;AACA;AACA;AACA;AAIA,qCAA4D,QAAqB,OAAU;AACzF,MAAI;AAAQ,aAAS,UAAU,MAAM,OAAO,cAAc,KAAK,CAAC;AAClE;AAFS;AAQT,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;AAAA,EAChE,UAAU,oBAAI,IAAoB;AACpC,CAAC;AAED,MAAM,cAAc,MAAM,WAA6C,CAAC,OAAO,iBAAiB;AAC9F,QAAM;AAAA,IACJ,8BAA8B;AAAA,IAC9B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,OACG;AAAA,MACD;AACJ,QAAM,UAAU,MAAM,WAAW,kBAAkB;AACnD,QAAM,CAAC,MAAM,WAAW,MAAM,SAAgC,IAAI;AAClE,QAAM,CAAC,EAAE,SAAS,MAAM,SAAS,CAAC,CAAC;AACnC,QAAM,eAAe,gBAAgB,cAAc,CAAC,UAAS,QAAQ,KAAI,CAAC;AAC1E,QAAM,SAAS,MAAM,KAAK,QAAQ,MAAM;AACxC,QAAM,CAAC,gDAAgD,CAAC,GAAG,QAAQ,sCAAsC,EAAE,MAAM,EAAE;A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,QAAM,UAAU,MAAM;AACpB,UAAM,cAAc,wBAAC,UAAsB;AACzC,UAAI,MAAM,UAAU,CAAC,0BAA0B,SAAS;AACtD,cAAM,cAAc,EAAE,eAAe,MAAM;AAC3C,qCAA6B,eAAe,oBAAoB,aAAa;AAAA,UAC3E,UAAU;AAAA,QACZ,CAAC;AAAA,MACH;AAAA,IACF,GAPoB;AAQpB,aAAS,iBAAiB,WAAW,WAAW;AAChD,WAAO,MAAM,SAAS,oBAAoB,WAAW,WAAW;AAAA,EAClE,GAAG,CAAC,kBAAkB,CAAC;AAEvB,SAAO;AAAA,IACL,gBAAgB,MAAO,0BAA0B,UAAU;AAAA,IAC3D,eAAe,MAAO,0BAA0B,UAAU;AAAA,EAC5D;AACF;AArBS;AAuBT,0BAA0B;AACxB,QAAM,QAAQ,IAAI,YAAY,cAAc;AAC5C,WAAS,cAAc,KAAK;AAC9B;AAHS;AAKT,sCACE,MACA,SACA,QACA,EAAE,YACF;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;AAfS;AAiBT,MAAM,OAAO;AACb,MAAM,SAAS;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
const Dismissable = /* @__PURE__ */ __name((props) => {
|
|
4
|
+
return props.children;
|
|
5
|
+
}, "Dismissable");
|
|
6
|
+
const DismissableBranch = /* @__PURE__ */ __name((props) => {
|
|
7
|
+
return props.children;
|
|
8
|
+
}, "DismissableBranch");
|
|
9
|
+
export {
|
|
10
|
+
Dismissable,
|
|
11
|
+
DismissableBranch
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=Dismissable.native.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/Dismissable.native.tsx"],
|
|
4
|
+
"sourcesContent": ["import { DismissableBranchProps, DismissableProps } from './DismissableProps'\n\nexport const Dismissable = (props: DismissableProps) => {\n return props.children\n}\n\nexport const DismissableBranch = (props: DismissableBranchProps) => {\n return props.children\n}\n"],
|
|
5
|
+
"mappings": ";;AAEO,MAAM,cAAc,wBAAC,UAA4B;AACtD,SAAO,MAAM;AACf,GAF2B;AAIpB,MAAM,oBAAoB,wBAAC,UAAkC;AAClE,SAAO,MAAM;AACf,GAFiC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=DismissableProps.js.map
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
|
|
4
|
+
import { useEscapeKeydown } from "@radix-ui/react-use-escape-keydown";
|
|
5
|
+
import { useComposedRefs } from "@tamagui/compose-refs";
|
|
6
|
+
import { composeEventHandlers } from "@tamagui/core";
|
|
7
|
+
import * as React from "react";
|
|
8
|
+
import * as ReactDOM from "react-dom";
|
|
9
|
+
function dispatchDiscreteCustomEvent(target, event) {
|
|
10
|
+
if (target)
|
|
11
|
+
ReactDOM.flushSync(() => target.dispatchEvent(event));
|
|
12
|
+
}
|
|
13
|
+
__name(dispatchDiscreteCustomEvent, "dispatchDiscreteCustomEvent");
|
|
14
|
+
const DISMISSABLE_LAYER_NAME = "Dismissable";
|
|
15
|
+
const CONTEXT_UPDATE = "dismissable.update";
|
|
16
|
+
const POINTER_DOWN_OUTSIDE = "dismissable.pointerDownOutside";
|
|
17
|
+
const FOCUS_OUTSIDE = "dismissable.focusOutside";
|
|
18
|
+
let originalBodyPointerEvents;
|
|
19
|
+
const DismissableContext = React.createContext({
|
|
20
|
+
layers: /* @__PURE__ */ new Set(),
|
|
21
|
+
layersWithOutsidePointerEventsDisabled: /* @__PURE__ */ new Set(),
|
|
22
|
+
branches: /* @__PURE__ */ new Set()
|
|
23
|
+
});
|
|
24
|
+
const Dismissable = React.forwardRef((props, forwardedRef) => {
|
|
25
|
+
const {
|
|
26
|
+
disableOutsidePointerEvents = false,
|
|
27
|
+
onEscapeKeyDown,
|
|
28
|
+
onPointerDownOutside,
|
|
29
|
+
onFocusOutside,
|
|
30
|
+
onInteractOutside,
|
|
31
|
+
onDismiss,
|
|
32
|
+
...layerProps
|
|
33
|
+
} = props;
|
|
34
|
+
const context = React.useContext(DismissableContext);
|
|
35
|
+
const [node, setNode] = React.useState(null);
|
|
36
|
+
const [, force] = React.useState({});
|
|
37
|
+
const composedRefs = useComposedRefs(forwardedRef, (node2) => setNode(node2));
|
|
38
|
+
const layers = Array.from(context.layers);
|
|
39
|
+
const [highestLayerWithOutsidePointerEventsDisabled] = [...context.layersWithOutsidePointerEventsDisabled].slice(-1);
|
|
40
|
+
const highestLayerWithOutsidePointerEventsDisabledIndex = layers.indexOf(highestLayerWithOutsidePointerEventsDisabled);
|
|
41
|
+
const index = node ? layers.indexOf(node) : -1;
|
|
42
|
+
const isBodyPointerEventsDisabled = context.layersWithOutsidePointerEventsDisabled.size > 0;
|
|
43
|
+
const isPointerEventsEnabled = index >= highestLayerWithOutsidePointerEventsDisabledIndex;
|
|
44
|
+
const pointerDownOutside = usePointerDownOutside((event) => {
|
|
45
|
+
const target = event.target;
|
|
46
|
+
const isPointerDownOnBranch = [...context.branches].some((branch) => branch.contains(target));
|
|
47
|
+
if (!isPointerEventsEnabled || isPointerDownOnBranch)
|
|
48
|
+
return;
|
|
49
|
+
onPointerDownOutside == null ? void 0 : onPointerDownOutside(event);
|
|
50
|
+
onInteractOutside == null ? void 0 : onInteractOutside(event);
|
|
51
|
+
if (!event.defaultPrevented)
|
|
52
|
+
onDismiss == null ? void 0 : onDismiss();
|
|
53
|
+
});
|
|
54
|
+
const focusOutside = useFocusOutside((event) => {
|
|
55
|
+
const target = event.target;
|
|
56
|
+
const isFocusInBranch = [...context.branches].some((branch) => branch.contains(target));
|
|
57
|
+
if (isFocusInBranch)
|
|
58
|
+
return;
|
|
59
|
+
onFocusOutside == null ? void 0 : onFocusOutside(event);
|
|
60
|
+
onInteractOutside == null ? void 0 : onInteractOutside(event);
|
|
61
|
+
if (!event.defaultPrevented)
|
|
62
|
+
onDismiss == null ? void 0 : onDismiss();
|
|
63
|
+
});
|
|
64
|
+
useEscapeKeydown((event) => {
|
|
65
|
+
const isHighestLayer = index === context.layers.size - 1;
|
|
66
|
+
if (!isHighestLayer)
|
|
67
|
+
return;
|
|
68
|
+
onEscapeKeyDown == null ? void 0 : onEscapeKeyDown(event);
|
|
69
|
+
if (!event.defaultPrevented && onDismiss) {
|
|
70
|
+
event.preventDefault();
|
|
71
|
+
onDismiss();
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
React.useEffect(() => {
|
|
75
|
+
if (!node)
|
|
76
|
+
return;
|
|
77
|
+
if (disableOutsidePointerEvents) {
|
|
78
|
+
if (context.layersWithOutsidePointerEventsDisabled.size === 0) {
|
|
79
|
+
originalBodyPointerEvents = document.body.style.pointerEvents;
|
|
80
|
+
document.body.style.pointerEvents = "none";
|
|
81
|
+
}
|
|
82
|
+
context.layersWithOutsidePointerEventsDisabled.add(node);
|
|
83
|
+
}
|
|
84
|
+
context.layers.add(node);
|
|
85
|
+
dispatchUpdate();
|
|
86
|
+
return () => {
|
|
87
|
+
if (disableOutsidePointerEvents && context.layersWithOutsidePointerEventsDisabled.size === 1) {
|
|
88
|
+
document.body.style.pointerEvents = originalBodyPointerEvents;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}, [node, disableOutsidePointerEvents, context]);
|
|
92
|
+
React.useEffect(() => {
|
|
93
|
+
return () => {
|
|
94
|
+
if (!node)
|
|
95
|
+
return;
|
|
96
|
+
context.layers.delete(node);
|
|
97
|
+
context.layersWithOutsidePointerEventsDisabled.delete(node);
|
|
98
|
+
dispatchUpdate();
|
|
99
|
+
};
|
|
100
|
+
}, [node, context]);
|
|
101
|
+
React.useEffect(() => {
|
|
102
|
+
const handleUpdate = /* @__PURE__ */ __name(() => force({}), "handleUpdate");
|
|
103
|
+
document.addEventListener(CONTEXT_UPDATE, handleUpdate);
|
|
104
|
+
return () => document.removeEventListener(CONTEXT_UPDATE, handleUpdate);
|
|
105
|
+
}, []);
|
|
106
|
+
return <div {...layerProps} ref={composedRefs} style={{
|
|
107
|
+
display: "contents",
|
|
108
|
+
pointerEvents: isBodyPointerEventsDisabled ? isPointerEventsEnabled ? "auto" : "none" : void 0,
|
|
109
|
+
...props.style
|
|
110
|
+
}} onFocusCapture={composeEventHandlers(props.onFocusCapture, focusOutside.onFocusCapture)} onBlurCapture={composeEventHandlers(props.onBlurCapture, focusOutside.onBlurCapture)} onPointerDownCapture={composeEventHandlers(props.onPointerDownCapture, pointerDownOutside.onPointerDownCapture)} />;
|
|
111
|
+
});
|
|
112
|
+
Dismissable.displayName = DISMISSABLE_LAYER_NAME;
|
|
113
|
+
const BRANCH_NAME = "DismissableBranch";
|
|
114
|
+
const DismissableBranch = React.forwardRef((props, forwardedRef) => {
|
|
115
|
+
const context = React.useContext(DismissableContext);
|
|
116
|
+
const ref = React.useRef(null);
|
|
117
|
+
const composedRefs = useComposedRefs(forwardedRef, ref);
|
|
118
|
+
React.useEffect(() => {
|
|
119
|
+
const node = ref.current;
|
|
120
|
+
if (node) {
|
|
121
|
+
context.branches.add(node);
|
|
122
|
+
return () => {
|
|
123
|
+
context.branches.delete(node);
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}, [context.branches]);
|
|
127
|
+
return <div style={{ display: "contents" }} {...props} ref={composedRefs} />;
|
|
128
|
+
});
|
|
129
|
+
DismissableBranch.displayName = BRANCH_NAME;
|
|
130
|
+
function usePointerDownOutside(onPointerDownOutside) {
|
|
131
|
+
const handlePointerDownOutside = useCallbackRef(onPointerDownOutside);
|
|
132
|
+
const isPointerInsideReactTreeRef = React.useRef(false);
|
|
133
|
+
const handleClickRef = React.useRef(() => {
|
|
134
|
+
});
|
|
135
|
+
React.useEffect(() => {
|
|
136
|
+
const handlePointerDown = /* @__PURE__ */ __name((event) => {
|
|
137
|
+
if (event.target && !isPointerInsideReactTreeRef.current) {
|
|
138
|
+
let handleAndDispatchPointerDownOutsideEvent = function() {
|
|
139
|
+
handleAndDispatchCustomEvent(POINTER_DOWN_OUTSIDE, handlePointerDownOutside, eventDetail, { discrete: true });
|
|
140
|
+
};
|
|
141
|
+
__name(handleAndDispatchPointerDownOutsideEvent, "handleAndDispatchPointerDownOutsideEvent");
|
|
142
|
+
const eventDetail = { originalEvent: event };
|
|
143
|
+
if (event.pointerType === "touch") {
|
|
144
|
+
document.removeEventListener("click", handleClickRef.current);
|
|
145
|
+
handleClickRef.current = handleAndDispatchPointerDownOutsideEvent;
|
|
146
|
+
document.addEventListener("click", handleClickRef.current, { once: true });
|
|
147
|
+
} else {
|
|
148
|
+
handleAndDispatchPointerDownOutsideEvent();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
isPointerInsideReactTreeRef.current = false;
|
|
152
|
+
}, "handlePointerDown");
|
|
153
|
+
const timerId = window.setTimeout(() => {
|
|
154
|
+
document.addEventListener("pointerdown", handlePointerDown);
|
|
155
|
+
}, 0);
|
|
156
|
+
return () => {
|
|
157
|
+
window.clearTimeout(timerId);
|
|
158
|
+
document.removeEventListener("pointerdown", handlePointerDown);
|
|
159
|
+
document.removeEventListener("click", handleClickRef.current);
|
|
160
|
+
};
|
|
161
|
+
}, [handlePointerDownOutside]);
|
|
162
|
+
return {
|
|
163
|
+
onPointerDownCapture: () => isPointerInsideReactTreeRef.current = true
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
__name(usePointerDownOutside, "usePointerDownOutside");
|
|
167
|
+
function useFocusOutside(onFocusOutside) {
|
|
168
|
+
const handleFocusOutside = useCallbackRef(onFocusOutside);
|
|
169
|
+
const isFocusInsideReactTreeRef = React.useRef(false);
|
|
170
|
+
React.useEffect(() => {
|
|
171
|
+
const handleFocus = /* @__PURE__ */ __name((event) => {
|
|
172
|
+
if (event.target && !isFocusInsideReactTreeRef.current) {
|
|
173
|
+
const eventDetail = { originalEvent: event };
|
|
174
|
+
handleAndDispatchCustomEvent(FOCUS_OUTSIDE, handleFocusOutside, eventDetail, {
|
|
175
|
+
discrete: false
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}, "handleFocus");
|
|
179
|
+
document.addEventListener("focusin", handleFocus);
|
|
180
|
+
return () => document.removeEventListener("focusin", handleFocus);
|
|
181
|
+
}, [handleFocusOutside]);
|
|
182
|
+
return {
|
|
183
|
+
onFocusCapture: () => isFocusInsideReactTreeRef.current = true,
|
|
184
|
+
onBlurCapture: () => isFocusInsideReactTreeRef.current = false
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
__name(useFocusOutside, "useFocusOutside");
|
|
188
|
+
function dispatchUpdate() {
|
|
189
|
+
const event = new CustomEvent(CONTEXT_UPDATE);
|
|
190
|
+
document.dispatchEvent(event);
|
|
191
|
+
}
|
|
192
|
+
__name(dispatchUpdate, "dispatchUpdate");
|
|
193
|
+
function handleAndDispatchCustomEvent(name, handler, detail, { discrete }) {
|
|
194
|
+
const target = detail.originalEvent.target;
|
|
195
|
+
const event = new CustomEvent(name, { bubbles: false, cancelable: true, detail });
|
|
196
|
+
if (handler)
|
|
197
|
+
target.addEventListener(name, handler, { once: true });
|
|
198
|
+
if (discrete) {
|
|
199
|
+
dispatchDiscreteCustomEvent(target, event);
|
|
200
|
+
} else {
|
|
201
|
+
target.dispatchEvent(event);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
__name(handleAndDispatchCustomEvent, "handleAndDispatchCustomEvent");
|
|
205
|
+
const Root = Dismissable;
|
|
206
|
+
const Branch = DismissableBranch;
|
|
207
|
+
export {
|
|
208
|
+
Branch,
|
|
209
|
+
Dismissable,
|
|
210
|
+
DismissableBranch,
|
|
211
|
+
Root
|
|
212
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
const Dismissable = /* @__PURE__ */ __name((props) => {
|
|
4
|
+
return props.children;
|
|
5
|
+
}, "Dismissable");
|
|
6
|
+
const DismissableBranch = /* @__PURE__ */ __name((props) => {
|
|
7
|
+
return props.children;
|
|
8
|
+
}, "DismissableBranch");
|
|
9
|
+
export {
|
|
10
|
+
Dismissable,
|
|
11
|
+
DismissableBranch
|
|
12
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./Dismissable";
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tamagui/dismissable",
|
|
3
|
+
"version": "1.0.1-beta.43",
|
|
4
|
+
"sideEffects": true,
|
|
5
|
+
"source": "src/index.ts",
|
|
6
|
+
"types": "./types/index.d.ts",
|
|
7
|
+
"main": "dist/cjs",
|
|
8
|
+
"module": "dist/esm",
|
|
9
|
+
"module:jsx": "dist/jsx",
|
|
10
|
+
"files": [
|
|
11
|
+
"types",
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tamagui-build",
|
|
16
|
+
"watch": "tamagui-build --watch",
|
|
17
|
+
"clean": "tamagui-build clean",
|
|
18
|
+
"clean:build": "tamagui-build clean:build"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@radix-ui/react-use-callback-ref": "^0.1.0",
|
|
22
|
+
"@radix-ui/react-use-escape-keydown": "^0.1.0",
|
|
23
|
+
"@tamagui/compose-refs": "^1.0.1-beta.43",
|
|
24
|
+
"@tamagui/core": "^1.0.1-beta.43"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"react": "*",
|
|
28
|
+
"react-dom": "*"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@tamagui/build": "^1.0.1-beta.43",
|
|
32
|
+
"react": "*",
|
|
33
|
+
"react-dom": "*"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { DismissableBranchProps, DismissableProps } from './DismissableProps';
|
|
3
|
+
declare const Dismissable: React.ForwardRefExoticComponent<DismissableProps & React.RefAttributes<HTMLDivElement>>;
|
|
4
|
+
declare const DismissableBranch: React.ForwardRefExoticComponent<DismissableBranchProps & React.RefAttributes<HTMLDivElement>>;
|
|
5
|
+
export declare type PointerDownOutsideEvent = CustomEvent<{
|
|
6
|
+
originalEvent: PointerEvent;
|
|
7
|
+
}>;
|
|
8
|
+
export declare type FocusOutsideEvent = CustomEvent<{
|
|
9
|
+
originalEvent: FocusEvent;
|
|
10
|
+
}>;
|
|
11
|
+
declare const Root: React.ForwardRefExoticComponent<DismissableProps & React.RefAttributes<HTMLDivElement>>;
|
|
12
|
+
declare const Branch: React.ForwardRefExoticComponent<DismissableBranchProps & React.RefAttributes<HTMLDivElement>>;
|
|
13
|
+
export { Dismissable, DismissableBranch, Root, Branch, };
|
|
14
|
+
export type { DismissableProps };
|
|
15
|
+
//# sourceMappingURL=Dismissable.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Dismissable.d.ts","sourceRoot":"","sources":["../src/Dismissable.tsx"],"names":[],"mappings":"AAOA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAG9B,OAAO,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAuB7E,QAAA,MAAM,WAAW,yFA4Hf,CAAA;AAUF,QAAA,MAAM,iBAAiB,+FAkBtB,CAAA;AAMD,oBAAY,uBAAuB,GAAG,WAAW,CAAC;IAAE,aAAa,EAAE,YAAY,CAAA;CAAE,CAAC,CAAA;AAClF,oBAAY,iBAAiB,GAAG,WAAW,CAAC;IAAE,aAAa,EAAE,UAAU,CAAA;CAAE,CAAC,CAAA;AA8H1E,QAAA,MAAM,IAAI,yFAAc,CAAA;AACxB,QAAA,MAAM,MAAM,+FAAoB,CAAA;AAEhC,OAAO,EACL,WAAW,EACX,iBAAiB,EAEjB,IAAI,EACJ,MAAM,GACP,CAAA;AAED,YAAY,EAAE,gBAAgB,EAAE,CAAA"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { DismissableBranchProps, DismissableProps } from './DismissableProps';
|
|
3
|
+
export declare const Dismissable: (props: DismissableProps) => import("react").ReactNode;
|
|
4
|
+
export declare const DismissableBranch: (props: DismissableBranchProps) => import("react").ReactNode;
|
|
5
|
+
//# sourceMappingURL=Dismissable.native.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Dismissable.native.d.ts","sourceRoot":"","sources":["../src/Dismissable.native.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAE7E,eAAO,MAAM,WAAW,UAAW,gBAAgB,8BAElD,CAAA;AAED,eAAO,MAAM,iBAAiB,UAAW,sBAAsB,8BAE9D,CAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { FocusOutsideEvent, PointerDownOutsideEvent } from './Dismissable';
|
|
3
|
+
export interface DismissableProps {
|
|
4
|
+
disableOutsidePointerEvents?: boolean;
|
|
5
|
+
onEscapeKeyDown?: (event: KeyboardEvent) => void;
|
|
6
|
+
onPointerDownOutside?: (event: PointerDownOutsideEvent) => void;
|
|
7
|
+
onFocusOutside?: (event: FocusOutsideEvent) => void;
|
|
8
|
+
onInteractOutside?: (event: PointerDownOutsideEvent | FocusOutsideEvent) => void;
|
|
9
|
+
onDismiss?: () => void;
|
|
10
|
+
children?: React.ReactNode;
|
|
11
|
+
}
|
|
12
|
+
export interface DismissableBranchProps {
|
|
13
|
+
children?: React.ReactNode;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=DismissableProps.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DismissableProps.d.ts","sourceRoot":"","sources":["../src/DismissableProps.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAA;AAE1E,MAAM,WAAW,gBAAgB;IAM/B,2BAA2B,CAAC,EAAE,OAAO,CAAA;IAKrC,eAAe,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAA;IAKhD,oBAAoB,CAAC,EAAE,CAAC,KAAK,EAAE,uBAAuB,KAAK,IAAI,CAAA;IAK/D,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAA;IAMnD,iBAAiB,CAAC,EAAE,CAAC,KAAK,EAAE,uBAAuB,GAAG,iBAAiB,KAAK,IAAI,CAAA;IAIhF,SAAS,CAAC,EAAE,MAAM,IAAI,CAAA;IAEtB,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAA;CAC3B;AAED,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAA;CAC3B"}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA"}
|