basecoat-css 1.0.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/README.md +1 -1
  2. package/dist/base/base.css +11 -1
  3. package/dist/base/scroll-fade.css +41 -0
  4. package/dist/base/transition-details.css +12 -0
  5. package/dist/basecoat-base.bundle.css +2532 -0
  6. package/dist/basecoat-base.cdn.css +1948 -677
  7. package/dist/basecoat-base.cdn.min.css +1 -2
  8. package/dist/basecoat-compat.cdn.css +251 -76
  9. package/dist/basecoat-compat.cdn.min.css +1 -2
  10. package/dist/basecoat-components.css +10 -0
  11. package/dist/basecoat-luma.bundle.css +4309 -0
  12. package/dist/basecoat-luma.cdn.css +5309 -3216
  13. package/dist/basecoat-luma.cdn.min.css +1 -2
  14. package/dist/basecoat-lyra.bundle.css +4287 -0
  15. package/dist/basecoat-lyra.cdn.css +5130 -3099
  16. package/dist/basecoat-lyra.cdn.min.css +1 -2
  17. package/dist/basecoat-maia.bundle.css +4275 -0
  18. package/dist/basecoat-maia.cdn.css +5220 -3158
  19. package/dist/basecoat-maia.cdn.min.css +1 -2
  20. package/dist/basecoat-mira.bundle.css +4284 -0
  21. package/dist/basecoat-mira.cdn.css +3530 -1494
  22. package/dist/basecoat-mira.cdn.min.css +1 -2
  23. package/dist/basecoat-nova.bundle.css +4300 -0
  24. package/dist/basecoat-nova.cdn.css +3685 -1623
  25. package/dist/basecoat-nova.cdn.min.css +1 -2
  26. package/dist/basecoat-rhea.bundle.css +4314 -0
  27. package/dist/basecoat-rhea.cdn.css +5315 -3219
  28. package/dist/basecoat-rhea.cdn.min.css +1 -2
  29. package/dist/basecoat-sera.bundle.css +4295 -0
  30. package/dist/basecoat-sera.cdn.css +3219 -1191
  31. package/dist/basecoat-sera.cdn.min.css +1 -2
  32. package/dist/basecoat-vega.bundle.css +4289 -0
  33. package/dist/basecoat-vega.cdn.css +3681 -1616
  34. package/dist/basecoat-vega.cdn.min.css +1 -2
  35. package/dist/basecoat.bundle.css +4289 -0
  36. package/dist/basecoat.cdn.css +3681 -1616
  37. package/dist/basecoat.cdn.min.css +1 -2
  38. package/dist/compat/legacy.css +3 -18
  39. package/dist/components/accordion.css +15 -1
  40. package/dist/components/alert.css +4 -0
  41. package/dist/components/attachment.css +66 -0
  42. package/dist/components/bubble.css +28 -0
  43. package/dist/components/button-group.css +2 -1
  44. package/dist/components/button.css +23 -0
  45. package/dist/components/carousel.css +30 -0
  46. package/dist/components/checkbox.css +5 -1
  47. package/dist/components/code-block.css +80 -0
  48. package/dist/components/collapsible.css +0 -9
  49. package/dist/components/command.css +13 -0
  50. package/dist/components/dialog.css +13 -1
  51. package/dist/components/drawer.css +48 -14
  52. package/dist/components/input-otp.css +49 -0
  53. package/dist/components/item.css +6 -0
  54. package/dist/components/marker.css +20 -0
  55. package/dist/components/message.css +24 -0
  56. package/dist/components/range.css +7 -0
  57. package/dist/components/scrollbar.css +20 -42
  58. package/dist/components/sheet.css +123 -0
  59. package/dist/components/shimmer.css +49 -0
  60. package/dist/components/sidebar.css +19 -8
  61. package/dist/components/toast.css +59 -44
  62. package/dist/components/tooltip.css +1 -1
  63. package/dist/components/trace.css +34 -0
  64. package/dist/js/all.js +513 -122
  65. package/dist/js/all.min.js +1 -1
  66. package/dist/js/carousel.js +105 -0
  67. package/dist/js/carousel.min.js +1 -0
  68. package/dist/js/code-block.js +47 -0
  69. package/dist/js/code-block.min.js +1 -0
  70. package/dist/js/combobox.js +6 -2
  71. package/dist/js/combobox.min.js +1 -1
  72. package/dist/js/drawer.js +6 -2
  73. package/dist/js/drawer.min.js +1 -1
  74. package/dist/js/dropdown-menu.js +1 -1
  75. package/dist/js/dropdown-menu.min.js +1 -1
  76. package/dist/js/input-otp.js +76 -0
  77. package/dist/js/input-otp.min.js +1 -0
  78. package/dist/js/popover.js +1 -1
  79. package/dist/js/popover.min.js +1 -1
  80. package/dist/js/select.js +1 -1
  81. package/dist/js/select.min.js +1 -1
  82. package/dist/js/sidebar.js +1 -1
  83. package/dist/js/sidebar.min.js +1 -1
  84. package/dist/js/tabs.js +31 -58
  85. package/dist/js/tabs.min.js +1 -1
  86. package/dist/js/toast.js +236 -57
  87. package/dist/js/toast.min.js +1 -1
  88. package/dist/styles/luma.css +451 -50
  89. package/dist/styles/lyra.css +418 -42
  90. package/dist/styles/maia.css +427 -48
  91. package/dist/styles/mira.css +422 -43
  92. package/dist/styles/nova.css +432 -51
  93. package/dist/styles/rhea.css +453 -52
  94. package/dist/styles/sera.css +416 -50
  95. package/dist/styles/vega.css +418 -47
  96. package/dist/typeset.cdn.css +381 -0
  97. package/dist/typeset.cdn.min.css +1 -0
  98. package/dist/typeset.css +435 -0
  99. package/package.json +18 -6
  100. package/templates/jinja/command.html.jinja +1 -1
  101. package/templates/jinja/dialog.html.jinja +1 -1
  102. package/templates/jinja/tabs.html.jinja +0 -1
  103. package/templates/jinja/toast.html.jinja +29 -26
  104. package/templates/nunjucks/command.njk +1 -1
  105. package/templates/nunjucks/dialog.njk +1 -1
  106. package/templates/nunjucks/tabs.njk +1 -1
  107. package/templates/nunjucks/toast.njk +30 -27
package/dist/js/toast.js CHANGED
@@ -1,100 +1,238 @@
1
1
  (() => {
2
2
  let toaster;
3
+ let resizeObserver;
3
4
  const toasts = new WeakMap();
4
5
  let isPaused = false;
6
+ let focusBeforeToasts;
7
+ const STACK_GAP = 12;
8
+ const STACK_VISIBLE = 3;
5
9
  const ICONS = {
6
10
  success: '<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="m9 12 2 2 4-4"/></svg>',
7
- error: '<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="m15 9-6 6"/><path d="m9 9 6 6"/></svg>',
11
+ error: '<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15.312 2a2 2 0 0 1 1.414.586l4.688 4.688A2 2 0 0 1 22 8.688v6.624a2 2 0 0 1-.586 1.414l-4.688 4.688a2 2 0 0 1-1.414.586H8.688a2 2 0 0 1-1.414-.586l-4.688-4.688A2 2 0 0 1 2 15.312V8.688a2 2 0 0 1 .586-1.414l4.688-4.688A2 2 0 0 1 8.688 2z"/><path d="m15 9-6 6"/><path d="m9 9 6 6"/></svg>',
8
12
  info: '<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 16v-4"/><path d="M12 8h.01"/></svg>',
13
+ loading: '<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12a9 9 0 1 1-6.219-8.56"/></svg>',
9
14
  warning: '<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/></svg>'
10
15
  };
11
16
 
12
17
  function initToaster(toasterElement) {
13
18
  if (toasterElement.dataset.toasterInitialized) return;
14
19
  toaster = toasterElement;
20
+ isPaused = toaster.matches(':hover, :focus-within');
21
+ let swipe;
15
22
 
16
23
  const handleClick = (event) => {
17
- const actionLink = event.target.closest('.toast footer a');
18
- const actionButton = event.target.closest('.toast footer button');
19
- if (actionLink || actionButton) {
20
- closeToast(event.target.closest('.toast'));
24
+ const control = event.target.closest('.toast footer a, .toast footer button');
25
+ if (control) closeToast(control.closest('.toast'));
26
+ };
27
+ const handleFocusIn = (event) => {
28
+ if (event.relatedTarget instanceof HTMLElement && event.relatedTarget !== document.body && !toaster.contains(event.relatedTarget)) {
29
+ focusBeforeToasts = event.relatedTarget;
30
+ }
31
+ syncPause();
32
+ };
33
+ const syncPause = () => swipe || toaster.matches(':hover, :focus-within')
34
+ ? pauseAllTimeouts()
35
+ : resumeAllTimeouts();
36
+
37
+ const handlePointerDown = (event) => {
38
+ if (!event.isPrimary || event.button !== 0 || event.target.closest('a, button, input, textarea, select, [contenteditable]')) return;
39
+ const element = event.target.closest('.toast:not([aria-hidden="true"])');
40
+ if (!element) return;
41
+ const rtl = getComputedStyle(toaster).direction === 'rtl';
42
+ const sign = (rtl ? -1 : 1) * (toaster.dataset.align === 'start' ? -1 : 1);
43
+ swipe = { element, id: event.pointerId, x: event.clientX, y: event.clientY, time: event.timeStamp, distance: 0, sign };
44
+ element.setPointerCapture(event.pointerId);
45
+ syncPause();
46
+ };
47
+ const handlePointerMove = (event) => {
48
+ if (!swipe || event.pointerId !== swipe.id) return;
49
+ const x = (event.clientX - swipe.x) * swipe.sign;
50
+ const y = event.clientY - swipe.y;
51
+ if (!swipe.axis) {
52
+ if (Math.max(x, y) < 8) return;
53
+ swipe.axis = x > y ? 'x' : 'y';
54
+ swipe.element.dataset.toastSwiping = '';
55
+ updateStack();
21
56
  }
57
+ swipe.distance = Math.max(0, swipe.axis === 'x' ? x : y);
58
+ swipe.element.style.setProperty(`--toast-swipe-${swipe.axis}`, `${swipe.distance * (swipe.axis === 'x' ? swipe.sign : 1)}px`);
59
+ };
60
+ const handlePointerEnd = (event) => {
61
+ if (!swipe || event.pointerId !== swipe.id) return;
62
+ const { element, axis, distance, sign, time } = swipe;
63
+ const dismiss = event.type !== 'pointercancel' && (distance > 40 || (distance > 10 && distance / Math.max(1, event.timeStamp - time) > 0.5));
64
+ delete element.dataset.toastSwiping;
65
+ if (dismiss) {
66
+ element.dataset.toastSwipe = axis;
67
+ element.style.setProperty('--toast-swipe-end', `${sign * 150}%`);
68
+ closeToast(element);
69
+ } else {
70
+ element.style.removeProperty('--toast-swipe-x');
71
+ element.style.removeProperty('--toast-swipe-y');
72
+ }
73
+ if (element.hasPointerCapture(event.pointerId)) element.releasePointerCapture(event.pointerId);
74
+ swipe = null;
75
+ syncPause();
76
+ updateStack();
22
77
  };
23
78
 
24
- toaster.addEventListener('mouseenter', pauseAllTimeouts);
25
- toaster.addEventListener('mouseleave', resumeAllTimeouts);
26
- toaster.addEventListener('click', handleClick);
79
+ const handleFocusOut = (event) => {
80
+ if (!toasterElement.contains(event.relatedTarget)) syncPause();
81
+ };
82
+ const listeners = new AbortController();
83
+ const { signal } = listeners;
84
+ toaster.addEventListener('mouseenter', syncPause, { signal });
85
+ toaster.addEventListener('mouseleave', syncPause, { signal });
86
+ toaster.addEventListener('focusin', handleFocusIn, { signal });
87
+ toaster.addEventListener('focusout', handleFocusOut, { signal });
88
+ toaster.addEventListener('click', handleClick, { signal });
89
+ toaster.addEventListener('pointerdown', handlePointerDown, { signal });
90
+ toaster.addEventListener('pointermove', handlePointerMove, { signal });
91
+ toaster.addEventListener('pointerup', handlePointerEnd, { signal });
92
+ toaster.addEventListener('pointercancel', handlePointerEnd, { signal });
93
+ window.addEventListener('pointerup', handlePointerEnd, { signal });
94
+ window.addEventListener('pointercancel', handlePointerEnd, { signal });
27
95
 
28
96
  toasterElement.toast = (config = {}) => {
29
97
  const toastElement = createToast(config);
30
98
  toasterElement.appendChild(toastElement);
31
- initToast(toastElement);
99
+ initToast(toastElement, config);
32
100
  return toastElement;
33
101
  };
34
102
  toasterElement.closeAll = () => {
35
103
  toasterElement.querySelectorAll('.toast:not([aria-hidden="true"])').forEach(closeToast);
36
104
  };
37
105
 
38
- toaster.querySelectorAll('.toast:not([data-toast-initialized])').forEach(initToast);
39
- toaster._destroy = () => {
40
- toaster.removeEventListener('mouseenter', pauseAllTimeouts);
41
- toaster.removeEventListener('mouseleave', resumeAllTimeouts);
42
- toaster.removeEventListener('click', handleClick);
43
- toaster.querySelectorAll('.toast[data-toast-initialized]').forEach(toast => toast._destroy?.());
44
- delete toaster.toast;
45
- delete toaster.closeAll;
46
- if (toaster === toasterElement) toaster = null;
106
+ const observer = new ResizeObserver(updateStack);
107
+ resizeObserver = observer;
108
+ toaster.querySelectorAll('.toast:not([data-toast-initialized])').forEach(element => initToast(element));
109
+ toasterElement._destroy = () => {
110
+ listeners.abort();
111
+ swipe = null;
112
+ toasterElement.querySelectorAll('.toast[data-toast-initialized]').forEach(toast => toast._destroy?.());
113
+ observer.disconnect();
114
+ delete toasterElement.toast;
115
+ delete toasterElement.closeAll;
116
+ if (toaster === toasterElement) {
117
+ resizeObserver = null;
118
+ isPaused = false;
119
+ focusBeforeToasts = null;
120
+ toaster = null;
121
+ }
47
122
  };
48
123
  toaster.dataset.toasterInitialized = 'true';
124
+ updateStack();
49
125
  toaster.dispatchEvent(new CustomEvent('basecoat:initialized'));
50
126
  }
51
127
 
52
- function initToast(element) {
53
- if (element.dataset.toastInitialized) return;
128
+ function updateStack() {
129
+ if (!toaster) return;
54
130
 
55
- const duration = parseInt(element.dataset.duration);
56
- const timeoutDuration = duration !== -1
57
- ? duration || (element.dataset.category === 'error' ? 5000 : 3000)
58
- : -1;
59
-
60
- const state = {
61
- remainingTime: timeoutDuration,
62
- timeoutId: null,
63
- startTime: null,
64
- };
131
+ const elements = Array.from(toaster.querySelectorAll('.toast:not([aria-hidden="true"])')).reverse();
132
+ let offset = 0;
65
133
 
66
- if (timeoutDuration !== -1) {
67
- if (isPaused) {
68
- state.timeoutId = null;
69
- } else {
70
- state.startTime = Date.now();
71
- state.timeoutId = setTimeout(() => closeToast(element), timeoutDuration);
134
+ const expanded = toaster.matches(':hover, :focus-within, :has([data-toast-swiping])');
135
+ const entries = elements.map(element => {
136
+ const content = element.querySelector('.toast-content') || element;
137
+ const styles = getComputedStyle(element);
138
+ return { element, content, height: content.offsetHeight + parseFloat(styles.borderTopWidth) + parseFloat(styles.borderBottomWidth) };
139
+ });
140
+
141
+ entries.forEach(({ element, content, height }, index) => {
142
+ element.style.setProperty('--toast-offset', `${offset}px`);
143
+ element.style.setProperty('--toast-index', index);
144
+ element.style.setProperty('--toast-height', `${height}px`);
145
+ element.style.setProperty('--toast-front-height', `${entries[0].height}px`);
146
+ element.toggleAttribute('data-toast-stack-overflow', index >= STACK_VISIBLE);
147
+ // Keep hidden content out of the tab order; hovering/focusing expands the stack.
148
+ const hidden = index >= STACK_VISIBLE || (!expanded && index > 0);
149
+ if (hidden && content.contains(document.activeElement)) {
150
+ const target = entries[0].content.querySelector('button:not(:disabled), a[href]') || (focusBeforeToasts?.isConnected ? focusBeforeToasts : toaster);
151
+ if (target === toaster && !toaster.hasAttribute('tabindex')) toaster.tabIndex = -1;
152
+ target.focus({ preventScroll: true });
72
153
  }
154
+ content.inert = hidden;
155
+ offset += height + STACK_GAP;
156
+ });
157
+ }
158
+
159
+ function resetTimeout(element, state) {
160
+ clearTimeout(state.timeoutId);
161
+ const duration = parseInt(element.dataset.duration);
162
+ state.remainingTime = Number.isNaN(duration) || duration === 0
163
+ ? element.dataset.category === 'loading' ? -1 : element.dataset.category === 'error' ? 5000 : 3000
164
+ : duration;
165
+ state.timeoutId = null;
166
+ if (state.remainingTime !== -1 && (!isPaused || !toaster?.contains(element))) {
167
+ state.startTime = Date.now();
168
+ state.timeoutId = setTimeout(() => closeToast(element), state.remainingTime);
73
169
  }
170
+ }
171
+
172
+ function initToast(element, config) {
173
+ if (element.dataset.toastInitialized) return;
174
+ const state = { remainingTime: -1, timeoutId: null, startTime: null };
74
175
  toasts.set(element, state);
176
+ resetTimeout(element, state);
75
177
 
178
+ const content = element.querySelector('.toast-content') || element;
179
+ resizeObserver?.observe(content);
76
180
  element.close = () => closeToast(element);
181
+ if (config !== undefined) {
182
+ let currentConfig = { ...config };
183
+ element.update = (changes = {}) => {
184
+ // An async result must not resurrect a dismissed toast.
185
+ if (!toasts.has(element) || !element.isConnected) return;
186
+ currentConfig = { ...currentConfig, ...changes };
187
+ const replacement = createToast(currentConfig);
188
+ const controls = [...element.querySelectorAll('footer a, footer button')];
189
+ const focusedIndex = controls.indexOf(document.activeElement);
190
+ content.replaceChildren(...replacement.firstElementChild.childNodes);
191
+ content.setAttribute('role', replacement.firstElementChild.getAttribute('role'));
192
+ for (const attribute of ['data-category', 'data-duration']) {
193
+ if (replacement.hasAttribute(attribute)) element.setAttribute(attribute, replacement.getAttribute(attribute));
194
+ else element.removeAttribute(attribute);
195
+ }
196
+ if (focusedIndex >= 0) {
197
+ const nextControls = element.querySelectorAll('footer a, footer button');
198
+ const target = nextControls[focusedIndex] || nextControls[0] || toaster;
199
+ if (target && target === toaster && !target.hasAttribute('tabindex')) target.tabIndex = -1;
200
+ target?.focus({ preventScroll: true });
201
+ }
202
+ resetTimeout(element, toasts.get(element));
203
+ updateStack();
204
+ return element;
205
+ };
206
+ }
77
207
  element._destroy = () => {
78
208
  clearTimeout(state.timeoutId);
209
+ resizeObserver?.unobserve(content);
79
210
  toasts.delete(element);
80
211
  delete element.close;
212
+ delete element.dataset.toastInitialized;
213
+ // Keep update callable for pending promises and forced reinitialization.
214
+ if (!element.isConnected) updateStack();
81
215
  };
216
+ updateStack();
217
+ // Flush the initial offscreen position before transitioning to the stack.
218
+ element.getBoundingClientRect();
82
219
  element.dataset.toastInitialized = 'true';
220
+ element.dataset.basecoatComponent = 'toast';
83
221
  }
84
222
 
85
223
  function pauseAllTimeouts() {
86
224
  if (isPaused || !toaster) return;
87
225
 
88
226
  isPaused = true;
227
+ updateStack();
89
228
 
90
229
  toaster.querySelectorAll('.toast:not([aria-hidden="true"])').forEach(element => {
91
- if (!toasts.has(element)) return;
92
-
93
230
  const state = toasts.get(element);
231
+ if (!state) return;
94
232
  if (state.timeoutId) {
95
233
  clearTimeout(state.timeoutId);
96
234
  state.timeoutId = null;
97
- state.remainingTime -= Date.now() - state.startTime;
235
+ state.remainingTime = Math.max(0, state.remainingTime - (Date.now() - state.startTime));
98
236
  }
99
237
  });
100
238
  }
@@ -103,11 +241,11 @@
103
241
  if (!isPaused || !toaster) return;
104
242
 
105
243
  isPaused = false;
244
+ updateStack();
106
245
 
107
246
  toaster.querySelectorAll('.toast:not([aria-hidden="true"])').forEach(element => {
108
- if (!toasts.has(element)) return;
109
-
110
247
  const state = toasts.get(element);
248
+ if (!state) return;
111
249
  if (state.remainingTime !== -1 && !state.timeoutId) {
112
250
  if (state.remainingTime > 0) {
113
251
  state.startTime = Date.now();
@@ -122,15 +260,50 @@
122
260
  function closeToast(element) {
123
261
  if (!element || !toasts.has(element)) return;
124
262
 
263
+ const host = toaster?.contains(element) ? toaster : null;
125
264
  const state = toasts.get(element);
126
265
  clearTimeout(state.timeoutId);
266
+ resizeObserver?.unobserve(element.querySelector('.toast-content') || element);
127
267
  toasts.delete(element);
128
268
 
129
- if (element.contains(document.activeElement)) document.activeElement.blur();
269
+ if (element.contains(document.activeElement)) {
270
+ const remaining = [...(host?.querySelectorAll('.toast:not([aria-hidden="true"])') || [])].filter(toast => toast !== element);
271
+ const nextControl = remaining.at(-1)?.querySelector('button:not(:disabled), a[href]');
272
+ const keyboard = document.activeElement.matches(':focus-visible');
273
+ const focusTarget = (keyboard && nextControl) || (host && focusBeforeToasts?.isConnected ? focusBeforeToasts : null);
274
+ if (!focusTarget) document.activeElement.blur();
275
+ else {
276
+ focusTarget.focus({ preventScroll: true });
277
+ if (element.contains(document.activeElement)) {
278
+ host.tabIndex = -1;
279
+ host.focus({ preventScroll: true });
280
+ }
281
+ }
282
+ }
283
+ if (host?.matches(':hover, :focus-within, :has([data-toast-swiping])')) element.dataset.toastExpanded = '';
130
284
  element.setAttribute('aria-hidden', 'true');
131
- element.addEventListener('transitionend', () => element.remove(), { once: true });
285
+ element.inert = true;
286
+ updateStack();
287
+
288
+ const remove = () => {
289
+ if (!element.isConnected) return;
290
+ element.remove();
291
+ updateStack();
292
+ };
293
+ requestAnimationFrame(() => {
294
+ const animations = element.getAnimations();
295
+ if (!animations.length) return remove();
296
+ Promise.allSettled(animations.map(animation => animation.finished)).then(remove);
297
+ });
132
298
  }
133
299
 
300
+ const escapeHtml = (value) => String(value)
301
+ .replace(/&/g, '&amp;')
302
+ .replace(/</g, '&lt;')
303
+ .replace(/>/g, '&gt;')
304
+ .replace(/"/g, '&quot;')
305
+ .replace(/'/g, '&#39;');
306
+
134
307
  function createToast(config) {
135
308
  const {
136
309
  category = 'info',
@@ -140,31 +313,37 @@
140
313
  cancel,
141
314
  duration,
142
315
  icon,
316
+ closeButton = true,
317
+ html: allowHtml = false,
143
318
  } = config;
144
319
 
320
+ // Text is escaped unless `html: true`. `icon` is always raw markup (SVG).
321
+ const text = (value) => (allowHtml ? String(value) : escapeHtml(value));
322
+
145
323
  const iconHtml = icon || (category && ICONS[category]) || '';
146
- const titleHtml = title ? `<h2>${title}</h2>` : '';
147
- const descriptionHtml = description ? `<p>${description}</p>` : '';
324
+ const titleHtml = title ? `<h2>${text(title)}</h2>` : '';
325
+ const descriptionHtml = description ? `<p>${text(description)}</p>` : '';
148
326
  const actionHtml = action?.href
149
- ? `<a href="${action.href}" class="btn" data-toast-action>${action.label}</a>`
150
- : action?.onclick
151
- ? `<button type="button" class="btn" data-toast-action onclick="${action.onclick}">${action.label}</button>`
327
+ ? `<a href="${escapeHtml(action.href)}" class="btn" data-variant="outline" data-size="sm" data-toast-action>${text(action.label)}</a>`
328
+ : action
329
+ ? `<button type="button" class="btn" data-variant="outline" data-size="sm" data-toast-action ${action.onclick ? `onclick="${escapeHtml(action.onclick)}"` : ''}>${text(action.label)}</button>`
152
330
  : '';
153
331
  const cancelHtml = cancel
154
- ? `<button type="button" class="btn h-6 text-xs px-2.5 rounded-sm" data-variant="outline" data-toast-cancel onclick="${cancel?.onclick || ''}">${cancel.label}</button>`
332
+ ? `<button type="button" class="btn" data-variant="outline" data-size="sm" data-toast-cancel ${cancel.onclick ? `onclick="${escapeHtml(cancel.onclick)}"` : ''}>${text(cancel.label)}</button>`
155
333
  : '';
156
334
 
157
- const footerHtml = actionHtml || cancelHtml ? `<footer>${actionHtml}${cancelHtml}</footer>` : '';
335
+ const closeHtml = closeButton
336
+ ? '<button type="button" class="btn" data-variant="ghost" data-size="icon-sm" aria-label="Close toast"><svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg></button>'
337
+ : '';
338
+ const footerHtml = actionHtml || cancelHtml || closeHtml ? `<footer>${actionHtml}${cancelHtml}${closeHtml}</footer>` : '';
158
339
 
159
340
  const html = `
160
- <div
341
+ <li
161
342
  class="toast"
162
- role="${category === 'error' ? 'alert' : 'status'}"
163
- aria-atomic="true"
164
- ${category ? `data-category="${category}"` : ''}
165
- ${duration !== undefined ? `data-duration="${duration}"` : ''}
343
+ ${category ? `data-category="${escapeHtml(category)}"` : ''}
344
+ ${duration !== undefined ? `data-duration="${escapeHtml(duration)}"` : ''}
166
345
  >
167
- <div class="toast-content">
346
+ <div class="toast-content" role="${category === 'error' ? 'alert' : 'status'}" aria-atomic="true">
168
347
  ${iconHtml}
169
348
  <section>
170
349
  ${titleHtml}
@@ -172,7 +351,7 @@
172
351
  </section>
173
352
  ${footerHtml}
174
353
  </div>
175
- </div>
354
+ </li>
176
355
  `;
177
356
  const template = document.createElement('template');
178
357
  template.innerHTML = html.trim();
@@ -1 +1 @@
1
- (()=>{let t;const e=new WeakMap;let o=!1;const n={success:'<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="m9 12 2 2 4-4"/></svg>',error:'<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="m15 9-6 6"/><path d="m9 9 6 6"/></svg>',info:'<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 16v-4"/><path d="M12 8h.01"/></svg>',warning:'<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/></svg>'};function i(t){if(t.dataset.toastInitialized)return;const n=parseInt(t.dataset.duration),i=-1!==n?n||("error"===t.dataset.category?5e3:3e3):-1,a={remainingTime:i,timeoutId:null,startTime:null};-1!==i&&(o?a.timeoutId=null:(a.startTime=Date.now(),a.timeoutId=setTimeout(()=>s(t),i))),e.set(t,a),t.close=()=>s(t),t._destroy=()=>{clearTimeout(a.timeoutId),e.delete(t),delete t.close},t.dataset.toastInitialized="true"}function a(){!o&&t&&(o=!0,t.querySelectorAll('.toast:not([aria-hidden="true"])').forEach(t=>{if(!e.has(t))return;const o=e.get(t);o.timeoutId&&(clearTimeout(o.timeoutId),o.timeoutId=null,o.remainingTime-=Date.now()-o.startTime)}))}function r(){o&&t&&(o=!1,t.querySelectorAll('.toast:not([aria-hidden="true"])').forEach(t=>{if(!e.has(t))return;const o=e.get(t);-1===o.remainingTime||o.timeoutId||(o.remainingTime>0?(o.startTime=Date.now(),o.timeoutId=setTimeout(()=>s(t),o.remainingTime)):s(t))}))}function s(t){if(!t||!e.has(t))return;const o=e.get(t);clearTimeout(o.timeoutId),e.delete(t),t.contains(document.activeElement)&&document.activeElement.blur(),t.setAttribute("aria-hidden","true"),t.addEventListener("transitionend",()=>t.remove(),{once:!0})}window.basecoat&&(window.basecoat.register("toaster","#toaster:not([data-toaster-initialized])",function(e){if(e.dataset.toasterInitialized)return;t=e;const o=t=>{const e=t.target.closest(".toast footer a"),o=t.target.closest(".toast footer button");(e||o)&&s(t.target.closest(".toast"))};t.addEventListener("mouseenter",a),t.addEventListener("mouseleave",r),t.addEventListener("click",o),e.toast=(t={})=>{const o=function(t){const{category:e="info",title:o,description:i,action:a,cancel:r,duration:s,icon:l}=t,d=l||e&&n[e]||"",c=o?`<h2>${o}</h2>`:"",u=i?`<p>${i}</p>`:"",h=a?.href?`<a href="${a.href}" class="btn" data-toast-action>${a.label}</a>`:a?.onclick?`<button type="button" class="btn" data-toast-action onclick="${a.onclick}">${a.label}</button>`:"",m=r?`<button type="button" class="btn h-6 text-xs px-2.5 rounded-sm" data-variant="outline" data-toast-cancel onclick="${r?.onclick||""}">${r.label}</button>`:"",v=`\n <div\n class="toast"\n role="${"error"===e?"alert":"status"}"\n aria-atomic="true"\n ${e?`data-category="${e}"`:""}\n ${void 0!==s?`data-duration="${s}"`:""}\n >\n <div class="toast-content">\n ${d}\n <section>\n ${c}\n ${u}\n </section>\n ${h||m?`<footer>${h}${m}</footer>`:""}\n </div>\n </div>\n `,w=document.createElement("template");return w.innerHTML=v.trim(),w.content.firstChild}(t);return e.appendChild(o),i(o),o},e.closeAll=()=>{e.querySelectorAll('.toast:not([aria-hidden="true"])').forEach(s)},t.querySelectorAll(".toast:not([data-toast-initialized])").forEach(i),t._destroy=()=>{t.removeEventListener("mouseenter",a),t.removeEventListener("mouseleave",r),t.removeEventListener("click",o),t.querySelectorAll(".toast[data-toast-initialized]").forEach(t=>t._destroy?.()),delete t.toast,delete t.closeAll,t===e&&(t=null)},t.dataset.toasterInitialized="true",t.dispatchEvent(new CustomEvent("basecoat:initialized"))}),window.basecoat.register("toast",".toast:not([data-toast-initialized])",i))})();
1
+ (()=>{let t,e;const n=new WeakMap;let o,a=!1;const i={success:'<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="m9 12 2 2 4-4"/></svg>',error:'<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15.312 2a2 2 0 0 1 1.414.586l4.688 4.688A2 2 0 0 1 22 8.688v6.624a2 2 0 0 1-.586 1.414l-4.688 4.688a2 2 0 0 1-1.414.586H8.688a2 2 0 0 1-1.414-.586l-4.688-4.688A2 2 0 0 1 2 15.312V8.688a2 2 0 0 1 .586-1.414l4.688-4.688A2 2 0 0 1 8.688 2z"/><path d="m15 9-6 6"/><path d="m9 9 6 6"/></svg>',info:'<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 16v-4"/><path d="M12 8h.01"/></svg>',loading:'<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12a9 9 0 1 1-6.219-8.56"/></svg>',warning:'<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/></svg>'};function r(){if(!t)return;const e=Array.from(t.querySelectorAll('.toast:not([aria-hidden="true"])')).reverse();let n=0;const a=t.matches(":hover, :focus-within, :has([data-toast-swiping])"),i=e.map(t=>{const e=t.querySelector(".toast-content")||t,n=getComputedStyle(t);return{element:t,content:e,height:e.offsetHeight+parseFloat(n.borderTopWidth)+parseFloat(n.borderBottomWidth)}});i.forEach(({element:e,content:r,height:s},l)=>{e.style.setProperty("--toast-offset",`${n}px`),e.style.setProperty("--toast-index",l),e.style.setProperty("--toast-height",`${s}px`),e.style.setProperty("--toast-front-height",`${i[0].height}px`),e.toggleAttribute("data-toast-stack-overflow",l>=3);const d=l>=3||!a&&l>0;if(d&&r.contains(document.activeElement)){const e=i[0].content.querySelector("button:not(:disabled), a[href]")||(o?.isConnected?o:t);e!==t||t.hasAttribute("tabindex")||(t.tabIndex=-1),e.focus({preventScroll:!0})}r.inert=d,n+=s+12})}function s(e,n){clearTimeout(n.timeoutId);const o=parseInt(e.dataset.duration);n.remainingTime=Number.isNaN(o)||0===o?"loading"===e.dataset.category?-1:"error"===e.dataset.category?5e3:3e3:o,n.timeoutId=null,-1===n.remainingTime||a&&t?.contains(e)||(n.startTime=Date.now(),n.timeoutId=setTimeout(()=>d(e),n.remainingTime))}function l(o,a){if(o.dataset.toastInitialized)return;const i={remainingTime:-1,timeoutId:null,startTime:null};n.set(o,i),s(o,i);const l=o.querySelector(".toast-content")||o;if(e?.observe(l),o.close=()=>d(o),void 0!==a){let e={...a};o.update=(a={})=>{if(!n.has(o)||!o.isConnected)return;e={...e,...a};const i=u(e),d=[...o.querySelectorAll("footer a, footer button")].indexOf(document.activeElement);l.replaceChildren(...i.firstElementChild.childNodes),l.setAttribute("role",i.firstElementChild.getAttribute("role"));for(const t of["data-category","data-duration"])i.hasAttribute(t)?o.setAttribute(t,i.getAttribute(t)):o.removeAttribute(t);if(d>=0){const e=o.querySelectorAll("footer a, footer button"),n=e[d]||e[0]||t;n&&n===t&&!n.hasAttribute("tabindex")&&(n.tabIndex=-1),n?.focus({preventScroll:!0})}return s(o,n.get(o)),r(),o}}o._destroy=()=>{clearTimeout(i.timeoutId),e?.unobserve(l),n.delete(o),delete o.close,delete o.dataset.toastInitialized,o.isConnected||r()},r(),o.getBoundingClientRect(),o.dataset.toastInitialized="true",o.dataset.basecoatComponent="toast"}function d(a){if(!a||!n.has(a))return;const i=t?.contains(a)?t:null,s=n.get(a);if(clearTimeout(s.timeoutId),e?.unobserve(a.querySelector(".toast-content")||a),n.delete(a),a.contains(document.activeElement)){const t=[...i?.querySelectorAll('.toast:not([aria-hidden="true"])')||[]].filter(t=>t!==a),e=t.at(-1)?.querySelector("button:not(:disabled), a[href]"),n=document.activeElement.matches(":focus-visible")&&e||(i&&o?.isConnected?o:null);n?(n.focus({preventScroll:!0}),a.contains(document.activeElement)&&(i.tabIndex=-1,i.focus({preventScroll:!0}))):document.activeElement.blur()}i?.matches(":hover, :focus-within, :has([data-toast-swiping])")&&(a.dataset.toastExpanded=""),a.setAttribute("aria-hidden","true"),a.inert=!0,r();const l=()=>{a.isConnected&&(a.remove(),r())};requestAnimationFrame(()=>{const t=a.getAnimations();if(!t.length)return l();Promise.allSettled(t.map(t=>t.finished)).then(l)})}const c=t=>String(t).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#39;");function u(t){const{category:e="info",title:n,description:o,action:a,cancel:r,duration:s,icon:l,closeButton:d=!0,html:u=!1}=t,h=t=>u?String(t):c(t),m=l||e&&i[e]||"",g=n?`<h2>${h(n)}</h2>`:"",p=o?`<p>${h(o)}</p>`:"",v=a?.href?`<a href="${c(a.href)}" class="btn" data-variant="outline" data-size="sm" data-toast-action>${h(a.label)}</a>`:a?`<button type="button" class="btn" data-variant="outline" data-size="sm" data-toast-action ${a.onclick?`onclick="${c(a.onclick)}"`:""}>${h(a.label)}</button>`:"",f=r?`<button type="button" class="btn" data-variant="outline" data-size="sm" data-toast-cancel ${r.onclick?`onclick="${c(r.onclick)}"`:""}>${h(r.label)}</button>`:"",w=d?'<button type="button" class="btn" data-variant="ghost" data-size="icon-sm" aria-label="Close toast"><svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg></button>':"",b=v||f||w?`<footer>${v}${f}${w}</footer>`:"",y=`\n <li\n class="toast"\n ${e?`data-category="${c(e)}"`:""}\n ${void 0!==s?`data-duration="${c(s)}"`:""}\n >\n <div class="toast-content" role="${"error"===e?"alert":"status"}" aria-atomic="true">\n ${m}\n <section>\n ${g}\n ${p}\n </section>\n ${b}\n </div>\n </li>\n `,x=document.createElement("template");return x.innerHTML=y.trim(),x.content.firstChild}window.basecoat&&(window.basecoat.register("toaster","#toaster:not([data-toaster-initialized])",function(i){if(i.dataset.toasterInitialized)return;let s;t=i,a=t.matches(":hover, :focus-within");const c=()=>s||t.matches(":hover, :focus-within")?void(!a&&t&&(a=!0,r(),t.querySelectorAll('.toast:not([aria-hidden="true"])').forEach(t=>{const e=n.get(t);e&&e.timeoutId&&(clearTimeout(e.timeoutId),e.timeoutId=null,e.remainingTime=Math.max(0,e.remainingTime-(Date.now()-e.startTime)))}))):void(a&&t&&(a=!1,r(),t.querySelectorAll('.toast:not([aria-hidden="true"])').forEach(t=>{const e=n.get(t);e&&(-1===e.remainingTime||e.timeoutId||(e.remainingTime>0?(e.startTime=Date.now(),e.timeoutId=setTimeout(()=>d(t),e.remainingTime)):d(t)))}))),h=t=>{if(!s||t.pointerId!==s.id)return;const{element:e,axis:n,distance:o,sign:a,time:i}=s,l="pointercancel"!==t.type&&(o>40||o>10&&o/Math.max(1,t.timeStamp-i)>.5);delete e.dataset.toastSwiping,l?(e.dataset.toastSwipe=n,e.style.setProperty("--toast-swipe-end",150*a+"%"),d(e)):(e.style.removeProperty("--toast-swipe-x"),e.style.removeProperty("--toast-swipe-y")),e.hasPointerCapture(t.pointerId)&&e.releasePointerCapture(t.pointerId),s=null,c(),r()},m=new AbortController,{signal:g}=m;t.addEventListener("mouseenter",c,{signal:g}),t.addEventListener("mouseleave",c,{signal:g}),t.addEventListener("focusin",e=>{e.relatedTarget instanceof HTMLElement&&e.relatedTarget!==document.body&&!t.contains(e.relatedTarget)&&(o=e.relatedTarget),c()},{signal:g}),t.addEventListener("focusout",t=>{i.contains(t.relatedTarget)||c()},{signal:g}),t.addEventListener("click",t=>{const e=t.target.closest(".toast footer a, .toast footer button");e&&d(e.closest(".toast"))},{signal:g}),t.addEventListener("pointerdown",e=>{if(!e.isPrimary||0!==e.button||e.target.closest("a, button, input, textarea, select, [contenteditable]"))return;const n=e.target.closest('.toast:not([aria-hidden="true"])');if(!n)return;const o=("rtl"===getComputedStyle(t).direction?-1:1)*("start"===t.dataset.align?-1:1);s={element:n,id:e.pointerId,x:e.clientX,y:e.clientY,time:e.timeStamp,distance:0,sign:o},n.setPointerCapture(e.pointerId),c()},{signal:g}),t.addEventListener("pointermove",t=>{if(!s||t.pointerId!==s.id)return;const e=(t.clientX-s.x)*s.sign,n=t.clientY-s.y;if(!s.axis){if(Math.max(e,n)<8)return;s.axis=e>n?"x":"y",s.element.dataset.toastSwiping="",r()}s.distance=Math.max(0,"x"===s.axis?e:n),s.element.style.setProperty(`--toast-swipe-${s.axis}`,s.distance*("x"===s.axis?s.sign:1)+"px")},{signal:g}),t.addEventListener("pointerup",h,{signal:g}),t.addEventListener("pointercancel",h,{signal:g}),window.addEventListener("pointerup",h,{signal:g}),window.addEventListener("pointercancel",h,{signal:g}),i.toast=(t={})=>{const e=u(t);return i.appendChild(e),l(e,t),e},i.closeAll=()=>{i.querySelectorAll('.toast:not([aria-hidden="true"])').forEach(d)};const p=new ResizeObserver(r);e=p,t.querySelectorAll(".toast:not([data-toast-initialized])").forEach(t=>l(t)),i._destroy=()=>{m.abort(),s=null,i.querySelectorAll(".toast[data-toast-initialized]").forEach(t=>t._destroy?.()),p.disconnect(),delete i.toast,delete i.closeAll,t===i&&(e=null,a=!1,o=null,t=null)},t.dataset.toasterInitialized="true",r(),t.dispatchEvent(new CustomEvent("basecoat:initialized"))}),window.basecoat.register("toast",".toast:not([data-toast-initialized])",l))})();