@destyler/dismissable 0.0.7-beta.9
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/LICENSE +21 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.js +213 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-PRESENT Elone Hoo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { FocusOutsideEvent, InteractOutsideEvent, InteractOutsideHandlers, InteractOutsideHandlers as InteractOutsideHandlers$1, PointerDownOutsideEvent } from "@destyler/interact-outside";
|
|
2
|
+
import { MaybeFunction } from "@destyler/utils";
|
|
3
|
+
|
|
4
|
+
//#region src/dismissable-layer.d.ts
|
|
5
|
+
type MaybeElement = HTMLElement | null;
|
|
6
|
+
type Container = MaybeElement | Array<MaybeElement>;
|
|
7
|
+
type NodeOrFn = MaybeFunction<MaybeElement>;
|
|
8
|
+
interface DismissableElementHandlers extends InteractOutsideHandlers$1 {
|
|
9
|
+
/**
|
|
10
|
+
* Function called when the escape key is pressed
|
|
11
|
+
*/
|
|
12
|
+
onEscapeKeyDown?: ((event: KeyboardEvent) => void) | undefined;
|
|
13
|
+
}
|
|
14
|
+
interface PersistentElementOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Returns the persistent elements that:
|
|
17
|
+
* - should not have pointer-events disabled
|
|
18
|
+
* - should not trigger the dismiss event
|
|
19
|
+
*/
|
|
20
|
+
persistentElements?: Array<() => Element | null> | undefined;
|
|
21
|
+
}
|
|
22
|
+
interface DismissableElementOptions extends DismissableElementHandlers, PersistentElementOptions {
|
|
23
|
+
/**
|
|
24
|
+
* Whether to log debug information
|
|
25
|
+
*/
|
|
26
|
+
debug?: boolean | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Whether to block pointer events outside the dismissable element
|
|
29
|
+
*/
|
|
30
|
+
pointerBlocking?: boolean | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Function called when the dismissable element is dismissed
|
|
33
|
+
*/
|
|
34
|
+
onDismiss: VoidFunction;
|
|
35
|
+
/**
|
|
36
|
+
* Exclude containers from the interact outside event
|
|
37
|
+
*/
|
|
38
|
+
exclude?: MaybeFunction<Container> | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Defer the interact outside event to the next frame
|
|
41
|
+
*/
|
|
42
|
+
defer?: boolean | undefined;
|
|
43
|
+
}
|
|
44
|
+
declare function trackDismissableElement(nodeOrFn: NodeOrFn, options: DismissableElementOptions): () => void;
|
|
45
|
+
declare function trackDismissableBranch(nodeOrFn: NodeOrFn, options?: {
|
|
46
|
+
defer?: boolean;
|
|
47
|
+
}): () => void;
|
|
48
|
+
//#endregion
|
|
49
|
+
export { DismissableElementHandlers, DismissableElementOptions, type FocusOutsideEvent, type InteractOutsideEvent, type InteractOutsideHandlers, PersistentElementOptions, type PointerDownOutsideEvent, trackDismissableBranch, trackDismissableElement };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { addDomEvent, contains, getDocument, getEventTarget, isHTMLElement, raf, setStyle, waitForElements } from "@destyler/dom";
|
|
2
|
+
import { trackInteractOutside } from "@destyler/interact-outside";
|
|
3
|
+
import { isFunction } from "@destyler/utils";
|
|
4
|
+
|
|
5
|
+
//#region src/escape-keydown.ts
|
|
6
|
+
function trackEscapeKeydown(node, fn) {
|
|
7
|
+
const handleKeyDown = (event) => {
|
|
8
|
+
if (event.key !== "Escape") return;
|
|
9
|
+
if (event.isComposing) return;
|
|
10
|
+
fn?.(event);
|
|
11
|
+
};
|
|
12
|
+
return addDomEvent(getDocument(node), "keydown", handleKeyDown, { capture: true });
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/layer-stack.ts
|
|
17
|
+
const layerStack = {
|
|
18
|
+
layers: [],
|
|
19
|
+
branches: [],
|
|
20
|
+
count() {
|
|
21
|
+
return this.layers.length;
|
|
22
|
+
},
|
|
23
|
+
pointerBlockingLayers() {
|
|
24
|
+
return this.layers.filter((layer) => layer.pointerBlocking);
|
|
25
|
+
},
|
|
26
|
+
topMostPointerBlockingLayer() {
|
|
27
|
+
return [...this.pointerBlockingLayers()].slice(-1)[0];
|
|
28
|
+
},
|
|
29
|
+
hasPointerBlockingLayer() {
|
|
30
|
+
return this.pointerBlockingLayers().length > 0;
|
|
31
|
+
},
|
|
32
|
+
isBelowPointerBlockingLayer(node) {
|
|
33
|
+
const index = this.indexOf(node);
|
|
34
|
+
const highestBlockingIndex = this.topMostPointerBlockingLayer() ? this.indexOf(this.topMostPointerBlockingLayer()?.node) : -1;
|
|
35
|
+
return index < highestBlockingIndex;
|
|
36
|
+
},
|
|
37
|
+
isTopMost(node) {
|
|
38
|
+
const layer = this.layers[this.count() - 1];
|
|
39
|
+
return layer?.node === node;
|
|
40
|
+
},
|
|
41
|
+
getNestedLayers(node) {
|
|
42
|
+
return Array.from(this.layers).slice(this.indexOf(node) + 1);
|
|
43
|
+
},
|
|
44
|
+
isInNestedLayer(node, target) {
|
|
45
|
+
return this.getNestedLayers(node).some((layer) => contains(layer.node, target));
|
|
46
|
+
},
|
|
47
|
+
isInBranch(target) {
|
|
48
|
+
return Array.from(this.branches).some((branch) => contains(branch, target));
|
|
49
|
+
},
|
|
50
|
+
add(layer) {
|
|
51
|
+
const num = this.layers.push(layer);
|
|
52
|
+
layer.node.style.setProperty("--layer-index", `${num}`);
|
|
53
|
+
},
|
|
54
|
+
addBranch(node) {
|
|
55
|
+
this.branches.push(node);
|
|
56
|
+
},
|
|
57
|
+
remove(node) {
|
|
58
|
+
const index = this.indexOf(node);
|
|
59
|
+
if (index < 0) return;
|
|
60
|
+
if (index < this.count() - 1) {
|
|
61
|
+
const _layers = this.getNestedLayers(node);
|
|
62
|
+
_layers.forEach((layer) => layer.dismiss());
|
|
63
|
+
}
|
|
64
|
+
this.layers.splice(index, 1);
|
|
65
|
+
node.style.removeProperty("--layer-index");
|
|
66
|
+
},
|
|
67
|
+
removeBranch(node) {
|
|
68
|
+
const index = this.branches.indexOf(node);
|
|
69
|
+
if (index >= 0) this.branches.splice(index, 1);
|
|
70
|
+
},
|
|
71
|
+
indexOf(node) {
|
|
72
|
+
return this.layers.findIndex((layer) => layer.node === node);
|
|
73
|
+
},
|
|
74
|
+
dismiss(node) {
|
|
75
|
+
this.layers[this.indexOf(node)]?.dismiss();
|
|
76
|
+
},
|
|
77
|
+
clear() {
|
|
78
|
+
this.remove(this.layers[0].node);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/pointer-event-outside.ts
|
|
84
|
+
let originalBodyPointerEvents;
|
|
85
|
+
function assignPointerEventToLayers() {
|
|
86
|
+
layerStack.layers.forEach(({ node }) => {
|
|
87
|
+
node.style.pointerEvents = layerStack.isBelowPointerBlockingLayer(node) ? "none" : "auto";
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
function clearPointerEvent(node) {
|
|
91
|
+
node.style.pointerEvents = "";
|
|
92
|
+
}
|
|
93
|
+
function disablePointerEventsOutside(node, persistentElements) {
|
|
94
|
+
const doc = getDocument(node);
|
|
95
|
+
const cleanups = [];
|
|
96
|
+
if (layerStack.hasPointerBlockingLayer() && !doc.body.hasAttribute("data-inert")) {
|
|
97
|
+
originalBodyPointerEvents = document.body.style.pointerEvents;
|
|
98
|
+
queueMicrotask(() => {
|
|
99
|
+
doc.body.style.pointerEvents = "none";
|
|
100
|
+
doc.body.setAttribute("data-inert", "");
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
if (persistentElements) {
|
|
104
|
+
const persistedCleanup = waitForElements(persistentElements, (el) => {
|
|
105
|
+
cleanups.push(setStyle(el, { pointerEvents: "auto" }));
|
|
106
|
+
});
|
|
107
|
+
cleanups.push(persistedCleanup);
|
|
108
|
+
}
|
|
109
|
+
return () => {
|
|
110
|
+
if (layerStack.hasPointerBlockingLayer()) return;
|
|
111
|
+
queueMicrotask(() => {
|
|
112
|
+
doc.body.style.pointerEvents = originalBodyPointerEvents;
|
|
113
|
+
doc.body.removeAttribute("data-inert");
|
|
114
|
+
if (doc.body.style.length === 0) doc.body.removeAttribute("style");
|
|
115
|
+
});
|
|
116
|
+
cleanups.forEach((fn) => fn());
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
//#endregion
|
|
121
|
+
//#region src/dismissable-layer.ts
|
|
122
|
+
function trackDismissableElementImpl(node, options) {
|
|
123
|
+
if (!node) throw new Error("[@destyler/dismissable] node is `null` or `undefined`");
|
|
124
|
+
const { onDismiss, pointerBlocking, exclude: excludeContainers, debug } = options;
|
|
125
|
+
const layer = {
|
|
126
|
+
dismiss: onDismiss,
|
|
127
|
+
node,
|
|
128
|
+
pointerBlocking
|
|
129
|
+
};
|
|
130
|
+
layerStack.add(layer);
|
|
131
|
+
assignPointerEventToLayers();
|
|
132
|
+
function onPointerDownOutside(event) {
|
|
133
|
+
const target = getEventTarget(event.detail.originalEvent);
|
|
134
|
+
if (layerStack.isBelowPointerBlockingLayer(node) || layerStack.isInBranch(target)) return;
|
|
135
|
+
options.onPointerDownOutside?.(event);
|
|
136
|
+
options.onInteractOutside?.(event);
|
|
137
|
+
if (event.defaultPrevented) return;
|
|
138
|
+
if (debug) console.log("onPointerDownOutside:", event.detail.originalEvent);
|
|
139
|
+
onDismiss?.();
|
|
140
|
+
}
|
|
141
|
+
function onFocusOutside(event) {
|
|
142
|
+
const target = getEventTarget(event.detail.originalEvent);
|
|
143
|
+
if (layerStack.isInBranch(target)) return;
|
|
144
|
+
options.onFocusOutside?.(event);
|
|
145
|
+
options.onInteractOutside?.(event);
|
|
146
|
+
if (event.defaultPrevented) return;
|
|
147
|
+
if (debug) console.log("onFocusOutside:", event.detail.originalEvent);
|
|
148
|
+
onDismiss?.();
|
|
149
|
+
}
|
|
150
|
+
function onEscapeKeyDown(event) {
|
|
151
|
+
if (!layerStack.isTopMost(node)) return;
|
|
152
|
+
options.onEscapeKeyDown?.(event);
|
|
153
|
+
if (!event.defaultPrevented && onDismiss) {
|
|
154
|
+
event.preventDefault();
|
|
155
|
+
onDismiss();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function exclude(target) {
|
|
159
|
+
if (!node) return false;
|
|
160
|
+
const containers = typeof excludeContainers === "function" ? excludeContainers() : excludeContainers;
|
|
161
|
+
const _containers = Array.isArray(containers) ? containers : [containers];
|
|
162
|
+
const persistentElements = options.persistentElements?.map((fn) => fn()).filter(isHTMLElement);
|
|
163
|
+
if (persistentElements) _containers.push(...persistentElements);
|
|
164
|
+
return _containers.some((node$1) => contains(node$1, target)) || layerStack.isInNestedLayer(node, target);
|
|
165
|
+
}
|
|
166
|
+
const cleanups = [
|
|
167
|
+
pointerBlocking ? disablePointerEventsOutside(node, options.persistentElements) : void 0,
|
|
168
|
+
trackEscapeKeydown(node, onEscapeKeyDown),
|
|
169
|
+
trackInteractOutside(node, {
|
|
170
|
+
exclude,
|
|
171
|
+
onFocusOutside,
|
|
172
|
+
onPointerDownOutside,
|
|
173
|
+
defer: options.defer
|
|
174
|
+
})
|
|
175
|
+
];
|
|
176
|
+
return () => {
|
|
177
|
+
layerStack.remove(node);
|
|
178
|
+
assignPointerEventToLayers();
|
|
179
|
+
clearPointerEvent(node);
|
|
180
|
+
cleanups.forEach((fn) => fn?.());
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
function trackDismissableElement(nodeOrFn, options) {
|
|
184
|
+
const { defer } = options;
|
|
185
|
+
const func = defer ? raf : (v) => v();
|
|
186
|
+
const cleanups = [];
|
|
187
|
+
cleanups.push(func(() => {
|
|
188
|
+
const node = isFunction(nodeOrFn) ? nodeOrFn() : nodeOrFn;
|
|
189
|
+
cleanups.push(trackDismissableElementImpl(node, options));
|
|
190
|
+
}));
|
|
191
|
+
return () => {
|
|
192
|
+
cleanups.forEach((fn) => fn?.());
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function trackDismissableBranch(nodeOrFn, options = {}) {
|
|
196
|
+
const { defer } = options;
|
|
197
|
+
const func = defer ? raf : (v) => v();
|
|
198
|
+
const cleanups = [];
|
|
199
|
+
cleanups.push(func(() => {
|
|
200
|
+
const node = isFunction(nodeOrFn) ? nodeOrFn() : nodeOrFn;
|
|
201
|
+
if (!node) throw new Error("[@destyler/dismissable] branch node is `null` or `undefined`");
|
|
202
|
+
layerStack.addBranch(node);
|
|
203
|
+
cleanups.push(() => {
|
|
204
|
+
layerStack.removeBranch(node);
|
|
205
|
+
});
|
|
206
|
+
}));
|
|
207
|
+
return () => {
|
|
208
|
+
cleanups.forEach((fn) => fn?.());
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
//#endregion
|
|
213
|
+
export { trackDismissableBranch, trackDismissableElement };
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@destyler/dismissable",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.7-beta.9",
|
|
5
|
+
"description": "Dismissable layer utilities for the dom",
|
|
6
|
+
"author": "Elone Hoo <elonehoo@gmail.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"main": "dist/index.js",
|
|
12
|
+
"module": "dist/index.js",
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
|
+
"files": [
|
|
15
|
+
"*.d.ts",
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@destyler/utils": "0.0.7-beta.9",
|
|
20
|
+
"@destyler/dom": "0.0.7-beta.9",
|
|
21
|
+
"@destyler/interact-outside": "0.0.7-beta.9"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"clear": "rimraf ./dist",
|
|
25
|
+
"clear:node_modules": "rimraf ./node_modules",
|
|
26
|
+
"build": "pnpm run clear && tsdown",
|
|
27
|
+
"watch": "tsdown --watch"
|
|
28
|
+
}
|
|
29
|
+
}
|