@onekeyfe/react-native-native-overlay 6.8.0-4 → 6.8.0-6
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/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayHost.kt +37 -5
- package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayPackage.kt +1 -1
- package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayToastCardView.kt +871 -0
- package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayToastSpec.kt +327 -0
- package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayToasterView.kt +574 -0
- package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayToasterViewManager.kt +54 -0
- package/android/src/test/java/com/onekey/nativeoverlay/NativeOverlayAccessibilityIsolationTest.kt +172 -0
- package/docs/SPEC.md +120 -9
- package/ios/NativeOverlayToastCardView.swift +572 -0
- package/ios/NativeOverlayToastSpec.swift +356 -0
- package/ios/NativeOverlayToasterView.swift +519 -0
- package/ios/RCTNativeOverlayToasterComponentView.h +11 -0
- package/ios/RCTNativeOverlayToasterComponentView.mm +85 -0
- package/lib/module/NativeOverlayToasterNativeComponent.ts +24 -0
- package/lib/module/index.js +2 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/toast/OverlayToastHost.js +42 -0
- package/lib/module/toast/OverlayToastHost.js.map +1 -0
- package/lib/module/toast/OverlayToastHost.web.js +32 -0
- package/lib/module/toast/OverlayToastHost.web.js.map +1 -0
- package/lib/module/toast/OverlayToastStore.js +217 -0
- package/lib/module/toast/OverlayToastStore.js.map +1 -0
- package/lib/module/toast/toastBridge.js +65 -0
- package/lib/module/toast/toastBridge.js.map +1 -0
- package/lib/module/toast/toastDomContent.web.js +298 -0
- package/lib/module/toast/toastDomContent.web.js.map +1 -0
- package/lib/module/toast/toastDomRenderer.web.js +629 -0
- package/lib/module/toast/toastDomRenderer.web.js.map +1 -0
- package/lib/module/toast/toastStyles.web.js +97 -0
- package/lib/module/toast/toastStyles.web.js.map +1 -0
- package/lib/module/toast/types.js +2 -0
- package/lib/module/toast/types.js.map +1 -0
- package/lib/module/web/overlayLayers.js +5 -0
- package/lib/module/web/overlayLayers.js.map +1 -1
- package/lib/typescript/src/NativeOverlayToasterNativeComponent.d.ts +19 -0
- package/lib/typescript/src/NativeOverlayToasterNativeComponent.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +4 -0
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/toast/OverlayToastHost.d.ts +6 -0
- package/lib/typescript/src/toast/OverlayToastHost.d.ts.map +1 -0
- package/lib/typescript/src/toast/OverlayToastHost.web.d.ts +6 -0
- package/lib/typescript/src/toast/OverlayToastHost.web.d.ts.map +1 -0
- package/lib/typescript/src/toast/OverlayToastStore.d.ts +47 -0
- package/lib/typescript/src/toast/OverlayToastStore.d.ts.map +1 -0
- package/lib/typescript/src/toast/toastBridge.d.ts +11 -0
- package/lib/typescript/src/toast/toastBridge.d.ts.map +1 -0
- package/lib/typescript/src/toast/toastDomContent.web.d.ts +15 -0
- package/lib/typescript/src/toast/toastDomContent.web.d.ts.map +1 -0
- package/lib/typescript/src/toast/toastDomRenderer.web.d.ts +8 -0
- package/lib/typescript/src/toast/toastDomRenderer.web.d.ts.map +1 -0
- package/lib/typescript/src/toast/toastStyles.web.d.ts +2 -0
- package/lib/typescript/src/toast/toastStyles.web.d.ts.map +1 -0
- package/lib/typescript/src/toast/types.d.ts +240 -0
- package/lib/typescript/src/toast/types.d.ts.map +1 -0
- package/lib/typescript/src/web/overlayLayers.d.ts.map +1 -1
- package/package.json +3 -2
- package/react-native.config.js +1 -0
- package/src/NativeOverlayToasterNativeComponent.ts +24 -0
- package/src/index.ts +23 -0
- package/src/toast/OverlayToastHost.tsx +62 -0
- package/src/toast/OverlayToastHost.web.tsx +29 -0
- package/src/toast/OverlayToastStore.ts +254 -0
- package/src/toast/toastBridge.ts +62 -0
- package/src/toast/toastDomContent.web.ts +351 -0
- package/src/toast/toastDomRenderer.web.ts +748 -0
- package/src/toast/toastStyles.web.ts +97 -0
- package/src/toast/types.ts +251 -0
- package/src/web/overlayLayers.ts +7 -0
|
@@ -0,0 +1,748 @@
|
|
|
1
|
+
/* cspell:ignore dasharray dashoffset */
|
|
2
|
+
import { getOverlayLayerRoot, syncOverlayInert } from '../web/overlayLayers';
|
|
3
|
+
|
|
4
|
+
import { buildToastContent } from './toastDomContent.web';
|
|
5
|
+
import { ensureOverlayToastStyles } from './toastStyles.web';
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
IOverlayToastDismissReason,
|
|
9
|
+
IOverlayToastEvent,
|
|
10
|
+
IOverlayToastItem,
|
|
11
|
+
IOverlayToastPosition,
|
|
12
|
+
IOverlayToastRenderer,
|
|
13
|
+
IOverlayToastSpec,
|
|
14
|
+
IOverlayToasterConfig,
|
|
15
|
+
} from './types';
|
|
16
|
+
|
|
17
|
+
// The chrome constants of the renderer this replaces (sonner 1.4.41).
|
|
18
|
+
const VISIBLE_TOASTS = 3;
|
|
19
|
+
const VIEWPORT_OFFSET = 32;
|
|
20
|
+
const TOAST_WIDTH = 356;
|
|
21
|
+
const GAP = 14;
|
|
22
|
+
const SWIPE_THRESHOLD = 20;
|
|
23
|
+
const SWIPE_VELOCITY = 0.11; // px per ms
|
|
24
|
+
// Equal to the exit transition.
|
|
25
|
+
const TIME_BEFORE_UNMOUNT = 200;
|
|
26
|
+
const DEFAULT_POSITION: IOverlayToastPosition = 'bottom-right';
|
|
27
|
+
const DEFAULT_OBSTRUCTION_GAP = 8;
|
|
28
|
+
const DEFAULT_OBSTRUCTION_ROOM = 88;
|
|
29
|
+
const OBSTRUCTION_SHIFT_MS = 220;
|
|
30
|
+
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
31
|
+
|
|
32
|
+
interface IToastNode {
|
|
33
|
+
id: string;
|
|
34
|
+
spec: IOverlayToastSpec;
|
|
35
|
+
revision: number;
|
|
36
|
+
generation: number;
|
|
37
|
+
position: IOverlayToastPosition;
|
|
38
|
+
li: HTMLLIElement;
|
|
39
|
+
content: HTMLElement;
|
|
40
|
+
mounted: boolean;
|
|
41
|
+
removed: boolean;
|
|
42
|
+
swiping: boolean;
|
|
43
|
+
swipeOut: boolean;
|
|
44
|
+
offsetBeforeRemove: number;
|
|
45
|
+
initialHeight: number;
|
|
46
|
+
closeTimer?: ReturnType<typeof setTimeout>;
|
|
47
|
+
unmountTimer?: ReturnType<typeof setTimeout>;
|
|
48
|
+
pointerStart: { x: number; y: number } | null;
|
|
49
|
+
dragStartedAt: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface IToastGroup {
|
|
53
|
+
position: IOverlayToastPosition;
|
|
54
|
+
ol: HTMLOListElement;
|
|
55
|
+
hovered: boolean;
|
|
56
|
+
/** Newest first, leaving cards included until they unmount. */
|
|
57
|
+
toasts: IToastNode[];
|
|
58
|
+
/** Newest first, without leaving cards. */
|
|
59
|
+
heights: { id: string; height: number }[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function getDocumentDirection(): 'ltr' | 'rtl' {
|
|
63
|
+
const attribute = document.documentElement.getAttribute('dir');
|
|
64
|
+
if (attribute === 'rtl' || attribute === 'ltr') {
|
|
65
|
+
return attribute;
|
|
66
|
+
}
|
|
67
|
+
return globalThis.getComputedStyle(document.documentElement).direction ===
|
|
68
|
+
'rtl'
|
|
69
|
+
? 'rtl'
|
|
70
|
+
: 'ltr';
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function createCloseButton(label: string): HTMLButtonElement {
|
|
74
|
+
const button = document.createElement('button');
|
|
75
|
+
button.type = 'button';
|
|
76
|
+
button.setAttribute('aria-label', label);
|
|
77
|
+
button.setAttribute('data-close-button', '');
|
|
78
|
+
const svg = document.createElementNS(SVG_NS, 'svg');
|
|
79
|
+
const attributes: Record<string, string> = {
|
|
80
|
+
'width': '12',
|
|
81
|
+
'height': '12',
|
|
82
|
+
'viewBox': '0 0 24 24',
|
|
83
|
+
'fill': 'none',
|
|
84
|
+
'stroke': 'currentColor',
|
|
85
|
+
'stroke-width': '1.5',
|
|
86
|
+
'stroke-linecap': 'round',
|
|
87
|
+
'stroke-linejoin': 'round',
|
|
88
|
+
};
|
|
89
|
+
Object.entries(attributes).forEach(([name, value]) =>
|
|
90
|
+
svg.setAttribute(name, value)
|
|
91
|
+
);
|
|
92
|
+
[
|
|
93
|
+
['18', '6', '6', '18'],
|
|
94
|
+
['6', '6', '18', '18'],
|
|
95
|
+
].forEach(([x1, y1, x2, y2]) => {
|
|
96
|
+
const line = document.createElementNS(SVG_NS, 'line');
|
|
97
|
+
line.setAttribute('x1', x1 ?? '');
|
|
98
|
+
line.setAttribute('y1', y1 ?? '');
|
|
99
|
+
line.setAttribute('x2', x2 ?? '');
|
|
100
|
+
line.setAttribute('y2', y2 ?? '');
|
|
101
|
+
svg.append(line);
|
|
102
|
+
});
|
|
103
|
+
button.append(svg);
|
|
104
|
+
return button;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Draws message toasts with plain DOM in the `toast` overlay layer. No React:
|
|
109
|
+
* the cards, their stacking, timers, swipe and transitions live here, and the
|
|
110
|
+
* stylesheet reproduces the previous renderer's chrome rule for rule.
|
|
111
|
+
*/
|
|
112
|
+
export function createToastDomRenderer(
|
|
113
|
+
report: (event: IOverlayToastEvent) => void
|
|
114
|
+
): IOverlayToastRenderer {
|
|
115
|
+
ensureOverlayToastStyles();
|
|
116
|
+
|
|
117
|
+
// Events raised while a sync is applying wait for it to finish: the store
|
|
118
|
+
// answers an event by syncing again.
|
|
119
|
+
let syncing = false;
|
|
120
|
+
const pendingEvents: IOverlayToastEvent[] = [];
|
|
121
|
+
const onEvent = (event: IOverlayToastEvent) => {
|
|
122
|
+
if (syncing) {
|
|
123
|
+
pendingEvents.push(event);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
report(event);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const nodes = new Map<string, IToastNode>();
|
|
130
|
+
const groups = new Map<IOverlayToastPosition, IToastGroup>();
|
|
131
|
+
let config: IOverlayToasterConfig = {};
|
|
132
|
+
let section: HTMLElement | null = null;
|
|
133
|
+
let expanded = false;
|
|
134
|
+
let interacting = false;
|
|
135
|
+
let destroyed = false;
|
|
136
|
+
|
|
137
|
+
const isHovered = (): boolean =>
|
|
138
|
+
[...groups.values()].some((group) => group.hovered);
|
|
139
|
+
|
|
140
|
+
const positionOf = (spec: IOverlayToastSpec): IOverlayToastPosition =>
|
|
141
|
+
spec.position ?? config.position ?? DEFAULT_POSITION;
|
|
142
|
+
|
|
143
|
+
function ensureSection(): HTMLElement {
|
|
144
|
+
const root = getOverlayLayerRoot('toast');
|
|
145
|
+
if (!section) {
|
|
146
|
+
section = document.createElement('section');
|
|
147
|
+
section.setAttribute('aria-label', 'Notifications alt+T');
|
|
148
|
+
section.setAttribute('data-onekey-toast-section', '');
|
|
149
|
+
section.tabIndex = -1;
|
|
150
|
+
}
|
|
151
|
+
if (config.testID) {
|
|
152
|
+
section.setAttribute('data-testid', config.testID);
|
|
153
|
+
}
|
|
154
|
+
if (section.parentElement !== root) {
|
|
155
|
+
root.prepend(section);
|
|
156
|
+
syncOverlayInert();
|
|
157
|
+
}
|
|
158
|
+
return section;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function setExpanded(value: boolean) {
|
|
162
|
+
if (expanded !== value) {
|
|
163
|
+
expanded = value;
|
|
164
|
+
update();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function setInteracting(value: boolean) {
|
|
169
|
+
if (interacting !== value) {
|
|
170
|
+
interacting = value;
|
|
171
|
+
if (!value && !isHovered()) {
|
|
172
|
+
expanded = false;
|
|
173
|
+
}
|
|
174
|
+
update();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function ensureGroup(position: IOverlayToastPosition): IToastGroup {
|
|
179
|
+
let group = groups.get(position);
|
|
180
|
+
if (!group) {
|
|
181
|
+
const ol = document.createElement('ol');
|
|
182
|
+
ol.tabIndex = -1;
|
|
183
|
+
ol.setAttribute('data-onekey-toast-toaster', '');
|
|
184
|
+
const nextGroup: IToastGroup = {
|
|
185
|
+
position,
|
|
186
|
+
ol,
|
|
187
|
+
hovered: false,
|
|
188
|
+
toasts: [],
|
|
189
|
+
heights: [],
|
|
190
|
+
};
|
|
191
|
+
ol.addEventListener('mouseenter', () => {
|
|
192
|
+
nextGroup.hovered = true;
|
|
193
|
+
expanded = nodes.size > 1;
|
|
194
|
+
update();
|
|
195
|
+
});
|
|
196
|
+
ol.addEventListener('mousemove', () => {
|
|
197
|
+
if (!nextGroup.hovered || (nodes.size > 1 && !expanded)) {
|
|
198
|
+
nextGroup.hovered = true;
|
|
199
|
+
expanded = nodes.size > 1;
|
|
200
|
+
update();
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
ol.addEventListener('mouseleave', () => {
|
|
204
|
+
nextGroup.hovered = false;
|
|
205
|
+
// Keep the stack open while a card is being dragged.
|
|
206
|
+
if (!interacting && !isHovered()) {
|
|
207
|
+
expanded = false;
|
|
208
|
+
}
|
|
209
|
+
update();
|
|
210
|
+
});
|
|
211
|
+
ol.addEventListener('pointerdown', () => setInteracting(true));
|
|
212
|
+
ol.addEventListener('pointerup', () => setInteracting(false));
|
|
213
|
+
ol.addEventListener('pointercancel', () => setInteracting(false));
|
|
214
|
+
group = nextGroup;
|
|
215
|
+
groups.set(position, group);
|
|
216
|
+
}
|
|
217
|
+
if (group.ol.parentElement !== ensureSection()) {
|
|
218
|
+
ensureSection().append(group.ol);
|
|
219
|
+
}
|
|
220
|
+
return group;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function offsetOf(group: IToastGroup, node: IToastNode): number {
|
|
224
|
+
// The height index moves before the card list does, so the layout of the
|
|
225
|
+
// cards behind a leaving one starts closing at once.
|
|
226
|
+
const heightIndex = Math.max(
|
|
227
|
+
group.heights.findIndex((height) => height.id === node.id),
|
|
228
|
+
0
|
|
229
|
+
);
|
|
230
|
+
const heightsBefore = group.heights
|
|
231
|
+
.slice(0, heightIndex)
|
|
232
|
+
.reduce((sum, height) => sum + height.height, 0);
|
|
233
|
+
return heightIndex * GAP + heightsBefore;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function measure(node: IToastNode) {
|
|
237
|
+
const group = groups.get(node.position);
|
|
238
|
+
if (!group || node.removed) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
// The card's own height: a card further back in the stack is scaled
|
|
242
|
+
// and pinned to the front card's height, neither of which is its size.
|
|
243
|
+
const { style } = node.li;
|
|
244
|
+
const originalHeight = style.height;
|
|
245
|
+
const originalTransform = style.transform;
|
|
246
|
+
style.height = 'auto';
|
|
247
|
+
style.transform = 'none';
|
|
248
|
+
const { height } = node.li.getBoundingClientRect();
|
|
249
|
+
style.height = originalHeight;
|
|
250
|
+
style.transform = originalTransform;
|
|
251
|
+
node.initialHeight = height;
|
|
252
|
+
const existing = group.heights.find((entry) => entry.id === node.id);
|
|
253
|
+
if (existing) {
|
|
254
|
+
existing.height = height;
|
|
255
|
+
} else {
|
|
256
|
+
group.heights.unshift({ id: node.id, height });
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function clearCloseTimer(node: IToastNode) {
|
|
261
|
+
if (node.closeTimer !== undefined) {
|
|
262
|
+
clearTimeout(node.closeTimer);
|
|
263
|
+
node.closeTimer = undefined;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function syncTimers() {
|
|
268
|
+
const paused = expanded || isHovered() || interacting;
|
|
269
|
+
nodes.forEach((node) => {
|
|
270
|
+
if (node.removed || !Number.isFinite(node.spec.duration)) {
|
|
271
|
+
clearCloseTimer(node);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if (paused) {
|
|
275
|
+
clearCloseTimer(node);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (node.closeTimer === undefined) {
|
|
279
|
+
// A resumed toast starts over with its whole lifetime, as before.
|
|
280
|
+
node.closeTimer = setTimeout(() => {
|
|
281
|
+
node.closeTimer = undefined;
|
|
282
|
+
deleteToast(node, 'timeout', true);
|
|
283
|
+
}, node.spec.duration);
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function unmount(node: IToastNode, reason: IOverlayToastDismissReason) {
|
|
289
|
+
if (node.unmountTimer !== undefined) {
|
|
290
|
+
clearTimeout(node.unmountTimer);
|
|
291
|
+
node.unmountTimer = undefined;
|
|
292
|
+
}
|
|
293
|
+
clearCloseTimer(node);
|
|
294
|
+
node.li.remove();
|
|
295
|
+
const group = groups.get(node.position);
|
|
296
|
+
if (group) {
|
|
297
|
+
group.toasts = group.toasts.filter((entry) => entry !== node);
|
|
298
|
+
group.heights = group.heights.filter((entry) => entry.id !== node.id);
|
|
299
|
+
}
|
|
300
|
+
if (nodes.get(node.id) === node) {
|
|
301
|
+
nodes.delete(node.id);
|
|
302
|
+
}
|
|
303
|
+
onEvent({
|
|
304
|
+
id: node.id,
|
|
305
|
+
generation: node.generation,
|
|
306
|
+
type: 'close',
|
|
307
|
+
reason,
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function deleteToast(
|
|
312
|
+
node: IToastNode,
|
|
313
|
+
reason: IOverlayToastDismissReason,
|
|
314
|
+
notify: boolean
|
|
315
|
+
) {
|
|
316
|
+
if (node.removed) {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const group = groups.get(node.position);
|
|
320
|
+
node.removed = true;
|
|
321
|
+
// Keep the offset the card had, for the exit transition.
|
|
322
|
+
node.offsetBeforeRemove = group ? offsetOf(group, node) : 0;
|
|
323
|
+
if (group) {
|
|
324
|
+
group.heights = group.heights.filter((entry) => entry.id !== node.id);
|
|
325
|
+
}
|
|
326
|
+
clearCloseTimer(node);
|
|
327
|
+
if (notify) {
|
|
328
|
+
onEvent({
|
|
329
|
+
id: node.id,
|
|
330
|
+
generation: node.generation,
|
|
331
|
+
type: 'dismiss',
|
|
332
|
+
reason,
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
node.unmountTimer = setTimeout(() => {
|
|
336
|
+
node.unmountTimer = undefined;
|
|
337
|
+
unmount(node, reason);
|
|
338
|
+
update();
|
|
339
|
+
}, TIME_BEFORE_UNMOUNT);
|
|
340
|
+
update();
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function attachSwipe(node: IToastNode) {
|
|
344
|
+
const { li } = node;
|
|
345
|
+
li.addEventListener('pointerdown', (event) => {
|
|
346
|
+
if (node.removed) {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
node.dragStartedAt = Date.now();
|
|
350
|
+
const group = groups.get(node.position);
|
|
351
|
+
node.offsetBeforeRemove = group ? offsetOf(group, node) : 0;
|
|
352
|
+
const target = event.target as HTMLElement;
|
|
353
|
+
if (target.closest?.('button, [data-okt-link]')) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
// Keep the pointer while it travels outside the card.
|
|
357
|
+
target.setPointerCapture?.(event.pointerId);
|
|
358
|
+
node.swiping = true;
|
|
359
|
+
node.pointerStart = { x: event.clientX, y: event.clientY };
|
|
360
|
+
update();
|
|
361
|
+
});
|
|
362
|
+
li.addEventListener('pointerup', () => {
|
|
363
|
+
if (node.swipeOut || node.removed) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
node.pointerStart = null;
|
|
367
|
+
const swipeAmount = Number(
|
|
368
|
+
li.style.getPropertyValue('--swipe-amount').replace('px', '') || 0
|
|
369
|
+
);
|
|
370
|
+
const timeTaken = Date.now() - node.dragStartedAt;
|
|
371
|
+
const velocity = Math.abs(swipeAmount) / timeTaken;
|
|
372
|
+
if (
|
|
373
|
+
Math.abs(swipeAmount) >= SWIPE_THRESHOLD ||
|
|
374
|
+
velocity > SWIPE_VELOCITY
|
|
375
|
+
) {
|
|
376
|
+
node.swipeOut = true;
|
|
377
|
+
deleteToast(node, 'swipe', true);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
li.style.setProperty('--swipe-amount', '0px');
|
|
381
|
+
node.swiping = false;
|
|
382
|
+
update();
|
|
383
|
+
});
|
|
384
|
+
li.addEventListener('pointercancel', () => {
|
|
385
|
+
if (node.swipeOut || node.removed) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
node.pointerStart = null;
|
|
389
|
+
li.style.setProperty('--swipe-amount', '0px');
|
|
390
|
+
node.swiping = false;
|
|
391
|
+
update();
|
|
392
|
+
});
|
|
393
|
+
li.addEventListener('pointermove', (event) => {
|
|
394
|
+
if (!node.pointerStart) {
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
const yPosition = event.clientY - node.pointerStart.y;
|
|
398
|
+
const xPosition = event.clientX - node.pointerStart.x;
|
|
399
|
+
const isTop = node.position.startsWith('top');
|
|
400
|
+
// Top toasts leave upwards, bottom toasts downwards.
|
|
401
|
+
const clampedY = isTop ? Math.min(0, yPosition) : Math.max(0, yPosition);
|
|
402
|
+
const swipeStartThreshold = event.pointerType === 'touch' ? 10 : 2;
|
|
403
|
+
if (Math.abs(clampedY) > swipeStartThreshold) {
|
|
404
|
+
li.style.setProperty('--swipe-amount', `${yPosition}px`);
|
|
405
|
+
} else if (Math.abs(xPosition) > swipeStartThreshold) {
|
|
406
|
+
// A sideways drag ends the gesture for this press.
|
|
407
|
+
node.pointerStart = null;
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function renderContent(node: IToastNode) {
|
|
413
|
+
const next = buildToastContent(node.spec, (event) => {
|
|
414
|
+
if (!node.removed) {
|
|
415
|
+
onEvent({
|
|
416
|
+
...event,
|
|
417
|
+
id: node.id,
|
|
418
|
+
generation: node.generation,
|
|
419
|
+
} as IOverlayToastEvent);
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
if (node.content.parentElement === node.li) {
|
|
423
|
+
node.content.replaceWith(next);
|
|
424
|
+
} else {
|
|
425
|
+
node.li.append(next);
|
|
426
|
+
}
|
|
427
|
+
node.content = next;
|
|
428
|
+
if (node.spec.testID) {
|
|
429
|
+
node.li.setAttribute('data-testid', node.spec.testID);
|
|
430
|
+
} else {
|
|
431
|
+
node.li.removeAttribute('data-testid');
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function createNode(item: IOverlayToastItem): IToastNode {
|
|
436
|
+
const li = document.createElement('li');
|
|
437
|
+
li.setAttribute('aria-live', 'polite');
|
|
438
|
+
li.setAttribute('aria-atomic', 'true');
|
|
439
|
+
li.setAttribute('role', 'status');
|
|
440
|
+
li.tabIndex = 0;
|
|
441
|
+
li.setAttribute('data-onekey-toast', '');
|
|
442
|
+
li.setAttribute('data-styled', 'true');
|
|
443
|
+
li.setAttribute('data-dismissible', 'true');
|
|
444
|
+
li.setAttribute('data-invert', 'false');
|
|
445
|
+
const closeButton = createCloseButton(
|
|
446
|
+
config.closeButtonLabel ?? 'Close toast'
|
|
447
|
+
);
|
|
448
|
+
li.append(closeButton);
|
|
449
|
+
const node: IToastNode = {
|
|
450
|
+
id: item.spec.id,
|
|
451
|
+
spec: item.spec,
|
|
452
|
+
revision: item.revision,
|
|
453
|
+
generation: item.generation,
|
|
454
|
+
position: positionOf(item.spec),
|
|
455
|
+
li,
|
|
456
|
+
content: document.createElement('div'),
|
|
457
|
+
mounted: false,
|
|
458
|
+
removed: false,
|
|
459
|
+
swiping: false,
|
|
460
|
+
swipeOut: false,
|
|
461
|
+
offsetBeforeRemove: 0,
|
|
462
|
+
initialHeight: 0,
|
|
463
|
+
pointerStart: null,
|
|
464
|
+
dragStartedAt: 0,
|
|
465
|
+
};
|
|
466
|
+
closeButton.addEventListener('click', () =>
|
|
467
|
+
deleteToast(node, 'close', true)
|
|
468
|
+
);
|
|
469
|
+
li.addEventListener('keydown', (event) => {
|
|
470
|
+
if (
|
|
471
|
+
!node.removed &&
|
|
472
|
+
node.spec.layout === 'notification' &&
|
|
473
|
+
node.spec.pressable &&
|
|
474
|
+
event.target === li &&
|
|
475
|
+
(event.key === 'Enter' || event.key === ' ')
|
|
476
|
+
) {
|
|
477
|
+
event.preventDefault();
|
|
478
|
+
onEvent({
|
|
479
|
+
id: node.id,
|
|
480
|
+
generation: node.generation,
|
|
481
|
+
type: 'press',
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
});
|
|
485
|
+
renderContent(node);
|
|
486
|
+
attachSwipe(node);
|
|
487
|
+
return node;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
function mountNode(node: IToastNode) {
|
|
491
|
+
const group = ensureGroup(node.position);
|
|
492
|
+
group.toasts.unshift(node);
|
|
493
|
+
group.ol.prepend(node.li);
|
|
494
|
+
nodes.set(node.id, node);
|
|
495
|
+
applyGroup(group);
|
|
496
|
+
// Measuring lays the card out in its resting-out state, so switching it
|
|
497
|
+
// to mounted right after starts the enter transition from there. No frame
|
|
498
|
+
// callback: those do not run while the page is hidden.
|
|
499
|
+
measure(node);
|
|
500
|
+
node.mounted = true;
|
|
501
|
+
update();
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function obstructionTop(): number {
|
|
505
|
+
const bottom = config.topObstruction ?? 0;
|
|
506
|
+
if (bottom <= 0) {
|
|
507
|
+
return 0;
|
|
508
|
+
}
|
|
509
|
+
const gap = config.topObstructionGap ?? DEFAULT_OBSTRUCTION_GAP;
|
|
510
|
+
const room = config.topObstructionRoom ?? DEFAULT_OBSTRUCTION_ROOM;
|
|
511
|
+
return Math.max(0, Math.min(bottom + gap, globalThis.innerHeight - room));
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function applyGroup(group: IToastGroup) {
|
|
515
|
+
const { ol } = group;
|
|
516
|
+
const [yPosition, xPosition] = group.position.split('-');
|
|
517
|
+
ol.dir = getDocumentDirection();
|
|
518
|
+
ol.setAttribute('data-theme', config.theme ?? 'light');
|
|
519
|
+
ol.setAttribute('data-y-position', yPosition ?? 'bottom');
|
|
520
|
+
ol.setAttribute('data-x-position', xPosition ?? 'right');
|
|
521
|
+
ol.style.setProperty(
|
|
522
|
+
'--front-toast-height',
|
|
523
|
+
`${group.heights[0]?.height ?? 0}px`
|
|
524
|
+
);
|
|
525
|
+
ol.style.setProperty('--offset', `${VIEWPORT_OFFSET}px`);
|
|
526
|
+
ol.style.setProperty('--width', `${TOAST_WIDTH}px`);
|
|
527
|
+
ol.style.setProperty('--gap', `${GAP}px`);
|
|
528
|
+
if (config.webContentMaxHeight !== undefined) {
|
|
529
|
+
ol.style.setProperty(
|
|
530
|
+
'--okt-max-height',
|
|
531
|
+
`${config.webContentMaxHeight}px`
|
|
532
|
+
);
|
|
533
|
+
} else {
|
|
534
|
+
ol.style.removeProperty('--okt-max-height');
|
|
535
|
+
}
|
|
536
|
+
// Top toasts rest under whatever hangs from the top of the window. An
|
|
537
|
+
// inline `top`: the narrow-window rule pins the toaster otherwise.
|
|
538
|
+
const top = yPosition === 'top' ? obstructionTop() : 0;
|
|
539
|
+
const reducedMotion =
|
|
540
|
+
config.respectReducedMotion !== false &&
|
|
541
|
+
globalThis.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
|
|
542
|
+
ol.style.transition = reducedMotion
|
|
543
|
+
? ''
|
|
544
|
+
: `top ${OBSTRUCTION_SHIFT_MS}ms ease`;
|
|
545
|
+
ol.style.top = top > 0 ? `${top}px` : '';
|
|
546
|
+
|
|
547
|
+
group.toasts.forEach((node, index) => {
|
|
548
|
+
const { li } = node;
|
|
549
|
+
li.setAttribute('data-mounted', String(node.mounted));
|
|
550
|
+
li.setAttribute('data-removed', String(node.removed));
|
|
551
|
+
li.setAttribute('data-visible', String(index + 1 <= VISIBLE_TOASTS));
|
|
552
|
+
li.setAttribute('data-y-position', yPosition ?? 'bottom');
|
|
553
|
+
li.setAttribute('data-x-position', xPosition ?? 'right');
|
|
554
|
+
li.setAttribute('data-index', String(index));
|
|
555
|
+
li.setAttribute('data-front', String(index === 0));
|
|
556
|
+
li.setAttribute('data-swiping', String(node.swiping));
|
|
557
|
+
li.setAttribute('data-swipe-out', String(node.swipeOut));
|
|
558
|
+
li.setAttribute('data-expanded', String(expanded));
|
|
559
|
+
li.style.setProperty('--index', String(index));
|
|
560
|
+
li.style.setProperty('--toasts-before', String(index));
|
|
561
|
+
li.style.setProperty('--z-index', String(group.toasts.length - index));
|
|
562
|
+
li.style.setProperty(
|
|
563
|
+
'--offset',
|
|
564
|
+
`${node.removed ? node.offsetBeforeRemove : offsetOf(group, node)}px`
|
|
565
|
+
);
|
|
566
|
+
li.style.setProperty('--initial-height', `${node.initialHeight}px`);
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
function update() {
|
|
571
|
+
if (destroyed) {
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
let releasedHover = false;
|
|
575
|
+
groups.forEach((group, position) => {
|
|
576
|
+
if (group.toasts.length === 0) {
|
|
577
|
+
if (group.hovered) {
|
|
578
|
+
releasedHover = true;
|
|
579
|
+
}
|
|
580
|
+
group.ol.remove();
|
|
581
|
+
groups.delete(position);
|
|
582
|
+
}
|
|
583
|
+
});
|
|
584
|
+
const hovered = isHovered();
|
|
585
|
+
if (nodes.size <= 1 && expanded) {
|
|
586
|
+
// One card has nothing to fan out.
|
|
587
|
+
expanded = false;
|
|
588
|
+
} else if (releasedHover && !hovered && !interacting) {
|
|
589
|
+
expanded = false;
|
|
590
|
+
} else if (nodes.size > 1 && hovered && !expanded) {
|
|
591
|
+
expanded = true;
|
|
592
|
+
}
|
|
593
|
+
groups.forEach((group) => {
|
|
594
|
+
applyGroup(group);
|
|
595
|
+
});
|
|
596
|
+
if (nodes.size === 0) {
|
|
597
|
+
section?.remove();
|
|
598
|
+
interacting = false;
|
|
599
|
+
}
|
|
600
|
+
syncTimers();
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function moveNode(
|
|
604
|
+
node: IToastNode,
|
|
605
|
+
position: IOverlayToastPosition
|
|
606
|
+
): IToastGroup {
|
|
607
|
+
const previous = groups.get(node.position);
|
|
608
|
+
if (previous) {
|
|
609
|
+
previous.toasts = previous.toasts.filter((entry) => entry !== node);
|
|
610
|
+
previous.heights = previous.heights.filter(
|
|
611
|
+
(entry) => entry.id !== node.id
|
|
612
|
+
);
|
|
613
|
+
}
|
|
614
|
+
node.position = position;
|
|
615
|
+
const group = ensureGroup(position);
|
|
616
|
+
group.toasts.push(node);
|
|
617
|
+
group.ol.append(node.li);
|
|
618
|
+
if (!node.removed) {
|
|
619
|
+
group.heights.push({ id: node.id, height: node.initialHeight });
|
|
620
|
+
}
|
|
621
|
+
return group;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
function reconcileGroupOrder(
|
|
625
|
+
group: IToastGroup,
|
|
626
|
+
order: ReadonlyMap<string, number>
|
|
627
|
+
) {
|
|
628
|
+
group.toasts.sort(
|
|
629
|
+
(a, b) => (order.get(a.id) ?? 0) - (order.get(b.id) ?? 0)
|
|
630
|
+
);
|
|
631
|
+
group.toasts.forEach((node) => group.ol.append(node.li));
|
|
632
|
+
group.heights.sort(
|
|
633
|
+
(a, b) => (order.get(a.id) ?? 0) - (order.get(b.id) ?? 0)
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function onKeyDown(event: KeyboardEvent) {
|
|
638
|
+
if (nodes.size === 0 || section?.inert || section?.parentElement?.inert) {
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
const lists = [...groups.values()].map((group) => group.ol);
|
|
642
|
+
if (event.altKey && event.code === 'KeyT') {
|
|
643
|
+
setExpanded(true);
|
|
644
|
+
lists[0]?.focus();
|
|
645
|
+
}
|
|
646
|
+
if (
|
|
647
|
+
event.code === 'Escape' &&
|
|
648
|
+
lists.some(
|
|
649
|
+
(list) =>
|
|
650
|
+
document.activeElement === list ||
|
|
651
|
+
list.contains(document.activeElement)
|
|
652
|
+
)
|
|
653
|
+
) {
|
|
654
|
+
setExpanded(false);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
function onResize() {
|
|
659
|
+
if ((config.topObstruction ?? 0) > 0) {
|
|
660
|
+
update();
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
document.addEventListener('keydown', onKeyDown);
|
|
665
|
+
globalThis.addEventListener('resize', onResize);
|
|
666
|
+
|
|
667
|
+
return {
|
|
668
|
+
sync(items, nextConfig) {
|
|
669
|
+
if (destroyed) {
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
syncing = true;
|
|
673
|
+
config = nextConfig;
|
|
674
|
+
const present = new Set(items.map((item) => item.spec.id));
|
|
675
|
+
// Cards the list no longer knows leave without ceremony.
|
|
676
|
+
[...nodes.values()].forEach((node) => {
|
|
677
|
+
if (!present.has(node.id)) {
|
|
678
|
+
unmount(node, 'programmatic');
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
const movedGroups = new Set<IToastGroup>();
|
|
682
|
+
// Oldest first, so each new card lands in front of the previous one.
|
|
683
|
+
[...items].reverse().forEach((item) => {
|
|
684
|
+
let node = nodes.get(item.spec.id);
|
|
685
|
+
if (node?.removed && item.visible) {
|
|
686
|
+
// Shown again while the previous card was leaving.
|
|
687
|
+
unmount(node, 'programmatic');
|
|
688
|
+
node = undefined;
|
|
689
|
+
}
|
|
690
|
+
if (!node) {
|
|
691
|
+
if (item.visible) {
|
|
692
|
+
mountNode(createNode(item));
|
|
693
|
+
} else {
|
|
694
|
+
onEvent({
|
|
695
|
+
id: item.spec.id,
|
|
696
|
+
generation: item.generation,
|
|
697
|
+
type: 'close',
|
|
698
|
+
reason: 'programmatic',
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
if (node.revision !== item.revision) {
|
|
704
|
+
node.revision = item.revision;
|
|
705
|
+
node.spec = item.spec;
|
|
706
|
+
renderContent(node);
|
|
707
|
+
measure(node);
|
|
708
|
+
}
|
|
709
|
+
if (node.generation !== item.generation) {
|
|
710
|
+
// Shown again: the toast starts its lifetime over.
|
|
711
|
+
node.generation = item.generation;
|
|
712
|
+
clearCloseTimer(node);
|
|
713
|
+
}
|
|
714
|
+
const position = positionOf(node.spec);
|
|
715
|
+
if (position !== node.position) {
|
|
716
|
+
movedGroups.add(moveNode(node, position));
|
|
717
|
+
}
|
|
718
|
+
if (!item.visible) {
|
|
719
|
+
deleteToast(node, 'programmatic', false);
|
|
720
|
+
}
|
|
721
|
+
});
|
|
722
|
+
if (movedGroups.size > 0) {
|
|
723
|
+
const order = new Map(
|
|
724
|
+
items.map((item, index) => [item.spec.id, index])
|
|
725
|
+
);
|
|
726
|
+
movedGroups.forEach((group) => reconcileGroupOrder(group, order));
|
|
727
|
+
}
|
|
728
|
+
update();
|
|
729
|
+
syncing = false;
|
|
730
|
+
pendingEvents.splice(0).forEach(report);
|
|
731
|
+
},
|
|
732
|
+
destroy() {
|
|
733
|
+
destroyed = true;
|
|
734
|
+
document.removeEventListener('keydown', onKeyDown);
|
|
735
|
+
globalThis.removeEventListener('resize', onResize);
|
|
736
|
+
nodes.forEach((node) => {
|
|
737
|
+
clearCloseTimer(node);
|
|
738
|
+
if (node.unmountTimer !== undefined) {
|
|
739
|
+
clearTimeout(node.unmountTimer);
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
nodes.clear();
|
|
743
|
+
groups.clear();
|
|
744
|
+
section?.remove();
|
|
745
|
+
section = null;
|
|
746
|
+
},
|
|
747
|
+
};
|
|
748
|
+
}
|