@onekeyfe/react-native-native-overlay 6.7.0-alpha.475 → 6.8.0-11

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