@stackoverflow/stacks 1.8.0 → 1.9.1

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 (137) hide show
  1. package/README.md +161 -153
  2. package/dist/{controllers/s-banner.d.ts → components/banner/banner.d.ts} +1 -1
  3. package/dist/{controllers/s-expandable-control.d.ts → components/expandable/expandable.d.ts} +1 -1
  4. package/dist/{controllers/s-modal.d.ts → components/modal/modal.d.ts} +1 -1
  5. package/dist/{controllers/s-navigation-tablist.d.ts → components/navigation/navigation.d.ts} +1 -1
  6. package/dist/{controllers/s-popover.d.ts → components/popover/popover.d.ts} +1 -1
  7. package/dist/{controllers/s-tooltip.d.ts → components/popover/tooltip.d.ts} +1 -1
  8. package/dist/components/table/table.d.ts +30 -0
  9. package/dist/{controllers/s-toast.d.ts → components/toast/toast.d.ts} +1 -1
  10. package/dist/{controllers/s-uploader.d.ts → components/uploader/uploader.d.ts} +1 -1
  11. package/dist/controllers.d.ts +9 -0
  12. package/dist/css/stacks.css +2063 -1994
  13. package/dist/css/stacks.min.css +1 -1
  14. package/dist/index.d.ts +1 -1
  15. package/dist/js/stacks.js +1465 -1436
  16. package/dist/js/stacks.min.js +1 -1
  17. package/lib/{css/atomic/borders.less → atomic/border.less} +397 -379
  18. package/lib/{css/atomic/colors.less → atomic/color.less} +210 -210
  19. package/lib/{css/atomic → atomic}/flex.less +426 -426
  20. package/lib/{css/atomic → atomic}/gap.less +44 -44
  21. package/lib/{css/atomic → atomic}/grid.less +139 -139
  22. package/lib/{css/atomic → atomic}/misc.less +343 -343
  23. package/lib/{css/atomic → atomic}/spacing.less +342 -342
  24. package/lib/{css/atomic → atomic}/typography.less +267 -267
  25. package/lib/{css/atomic → atomic}/width-height.less +194 -194
  26. package/lib/{css/base → base}/body.less +44 -44
  27. package/lib/{css/base → base}/configuration-static.less +61 -61
  28. package/lib/{css/base → base}/fieldset.less +5 -5
  29. package/lib/{css/base/icons.less → base/icon.less} +11 -20
  30. package/lib/{css/base/internals.less → base/internal.less} +220 -220
  31. package/lib/{css/base → base}/reset-meyer.less +64 -64
  32. package/lib/{css/base → base}/reset-normalize.less +449 -449
  33. package/lib/{css/base → base}/reset.less +20 -20
  34. package/lib/components/activity-indicator/activity-indicator.a11y.test.ts +21 -0
  35. package/lib/{css/components → components/activity-indicator}/activity-indicator.less +40 -40
  36. package/lib/components/activity-indicator/activity-indicator.visual.test.ts +23 -0
  37. package/lib/{css/components/anchors.less → components/anchor/anchor.less} +61 -61
  38. package/lib/components/avatar/avatar.a11y.test.ts +36 -0
  39. package/lib/{css/components/avatars.less → components/avatar/avatar.less} +108 -108
  40. package/lib/components/avatar/avatar.visual.test.ts +54 -0
  41. package/lib/components/award-bling/award-bling.a11y.test.ts +17 -0
  42. package/lib/{css/components → components/award-bling}/award-bling.less +31 -31
  43. package/lib/components/award-bling/award-bling.visual.test.ts +26 -0
  44. package/lib/{css/components/badges.less → components/badge/badge.less} +251 -251
  45. package/lib/components/banner/banner.a11y.test.ts +37 -0
  46. package/lib/components/banner/banner.less +51 -0
  47. package/lib/{test/s-banner.test.ts → components/banner/banner.test.ts} +73 -73
  48. package/lib/{ts/controllers/s-banner.ts → components/banner/banner.ts} +149 -149
  49. package/lib/components/banner/banner.visual.test.ts +37 -0
  50. package/lib/components/block-link/block-link.a11y.test.ts +68 -0
  51. package/lib/{css/components → components/block-link}/block-link.less +80 -80
  52. package/lib/components/block-link/block-link.visual.test.ts +61 -0
  53. package/lib/components/breadcrumbs/breadcrumbs.a11y.test.ts +37 -0
  54. package/lib/{css/components → components/breadcrumbs}/breadcrumbs.less +41 -41
  55. package/lib/components/breadcrumbs/breadcrumbs.visual.test.ts +37 -0
  56. package/lib/components/button/button.a11y.test.ts +32 -0
  57. package/lib/{css/components/buttons.less → components/button/button.less} +502 -501
  58. package/lib/components/button/button.visual.test.ts +52 -0
  59. package/lib/{css/components/button-groups.less → components/button-group/button-group.less} +83 -83
  60. package/lib/components/card/card.a11y.test.ts +13 -0
  61. package/lib/{css/components/cards.less → components/card/card.less} +29 -29
  62. package/lib/components/card/card.visual.test.ts +54 -0
  63. package/lib/components/check-control/check-control.less +17 -0
  64. package/lib/components/check-group/check-group.less +19 -0
  65. package/lib/{css/components/checkboxes-radios.less → components/checkbox_radio/checkbox_radio.less} +158 -158
  66. package/lib/{css/components/code-blocks.less → components/code-block/code-block.less} +116 -116
  67. package/lib/{css/components → components/description}/description.less +9 -9
  68. package/lib/{css/components/empty-states.less → components/empty-state/empty-state.less} +16 -16
  69. package/lib/{css/components → components/expandable}/expandable.less +118 -115
  70. package/lib/components/expandable/expandable.test.ts +51 -0
  71. package/lib/{ts/controllers/s-expandable-control.ts → components/expandable/expandable.ts} +238 -238
  72. package/lib/components/input-fill/input-fill.less +35 -0
  73. package/lib/components/input-icon/input-icon.less +45 -0
  74. package/lib/components/input-message/input-message.less +48 -0
  75. package/lib/{css/components/inputs.less → components/input_textarea/input_textarea.less} +166 -297
  76. package/lib/{css/components/labels.less → components/label/label.less} +111 -111
  77. package/lib/{css/components → components/link}/link.less +119 -119
  78. package/lib/{css/components/link-previews.less → components/link-preview/link-preview.less} +139 -139
  79. package/lib/{css/components → components/menu}/menu.less +41 -41
  80. package/lib/{css/components/modals.less → components/modal/modal.less} +113 -113
  81. package/lib/{ts/controllers/s-modal.ts → components/modal/modal.ts} +379 -379
  82. package/lib/{css/components → components/navigation}/navigation.less +134 -134
  83. package/lib/{ts/controllers/s-navigation-tablist.ts → components/navigation/navigation.ts} +128 -128
  84. package/lib/{css/components/notices.less → components/notice/notice.less} +203 -292
  85. package/lib/{css/components/page-titles.less → components/page-title/page-title.less} +51 -51
  86. package/lib/{css/components → components/pagination}/pagination.less +52 -52
  87. package/lib/{css/components/popovers.less → components/popover/popover.less} +148 -147
  88. package/lib/{ts/controllers/s-popover.ts → components/popover/popover.ts} +651 -651
  89. package/lib/{test/s-tooltip.test.ts → components/popover/tooltip.test.ts} +62 -62
  90. package/lib/{ts/controllers/s-tooltip.ts → components/popover/tooltip.ts} +343 -343
  91. package/lib/{test/s-tooltip.visual.test.ts → components/popover/tooltip.visual.test.ts} +31 -31
  92. package/lib/{css/components → components/post-summary}/post-summary.less +415 -415
  93. package/lib/{css/components/progress-bars.less → components/progress-bar/progress-bar.less} +291 -291
  94. package/lib/{css/components → components/prose}/prose.less +452 -452
  95. package/lib/{css/components → components/select}/select.less +148 -148
  96. package/lib/{css/components/sidebar-widgets.less → components/sidebar-widget/sidebar-widget.less} +257 -259
  97. package/lib/{css/components → components/spinner}/spinner.less +103 -103
  98. package/lib/{css/components → components/table}/table.less +307 -297
  99. package/lib/components/table/table.test.ts +366 -0
  100. package/lib/{ts/controllers/s-table.ts → components/table/table.ts} +296 -263
  101. package/lib/components/table-container/table-container.less +4 -0
  102. package/lib/{css/components/tags.less → components/tag/tag.less} +213 -213
  103. package/lib/components/toast/toast.less +35 -0
  104. package/lib/{test/s-toast.test.ts → components/toast/toast.test.ts} +63 -63
  105. package/lib/{ts/controllers/s-toast.ts → components/toast/toast.ts} +357 -357
  106. package/lib/components/toast/toast.visual.test.ts +27 -0
  107. package/lib/{css/components/toggle-switches.less → components/toggle-switch/toggle-switch.less} +110 -110
  108. package/lib/{css/components → components/topbar}/topbar.less +436 -435
  109. package/lib/{css/components → components/uploader}/uploader.less +195 -195
  110. package/lib/{ts/controllers/s-uploader.ts → components/uploader/uploader.ts} +205 -205
  111. package/lib/{css/components/user-cards.less → components/user-card/user-card.less} +129 -129
  112. package/lib/controllers.ts +33 -0
  113. package/lib/{css/exports → exports}/constants-colors.less +1112 -1111
  114. package/lib/{css/exports → exports}/constants-helpers.less +108 -108
  115. package/lib/{css/exports → exports}/constants-type.less +153 -153
  116. package/lib/{css/exports → exports}/exports.less +15 -15
  117. package/lib/{css/exports → exports}/mixins.less +299 -299
  118. package/lib/{ts/index.ts → index.ts} +32 -32
  119. package/lib/{css/input-utils.less → input-utils.less} +44 -44
  120. package/lib/{css/stacks-dynamic.less → stacks-dynamic.less} +24 -25
  121. package/lib/stacks-static.less +93 -0
  122. package/lib/{css/stacks.less → stacks.less} +13 -13
  123. package/lib/{ts/stacks.ts → stacks.ts} +113 -113
  124. package/lib/test/open-wc-testing-patch.d.ts +26 -0
  125. package/lib/test/test-utils.ts +466 -0
  126. package/lib/tsconfig.build.json +4 -0
  127. package/lib/tsconfig.json +16 -13
  128. package/package.json +106 -105
  129. package/dist/controllers/index.d.ts +0 -9
  130. package/dist/controllers/s-table.d.ts +0 -8
  131. package/lib/css/stacks-static.less +0 -87
  132. package/lib/test/s-avatar.a11y.test.ts +0 -77
  133. package/lib/test/s-banner.visual.test.ts +0 -61
  134. package/lib/test/s-btn.a11y.test.ts +0 -123
  135. package/lib/test/s-btn.visual.test.ts +0 -16
  136. package/lib/test/s-toast.visual.test.ts +0 -48
  137. package/lib/ts/controllers/index.ts +0 -17
@@ -1,357 +1,357 @@
1
- import * as Stacks from "../stacks";
2
-
3
- export class ToastController extends Stacks.StacksController {
4
- static targets = ["toast", "initialFocus"];
5
-
6
- declare readonly toastTarget: HTMLElement;
7
- declare readonly initialFocusTargets: HTMLElement[];
8
-
9
- private _boundClickFn!: (event: MouseEvent) => void;
10
- private _boundKeypressFn!: (event: KeyboardEvent) => void;
11
-
12
- private activeTimeout!: number;
13
- private returnElement!: HTMLElement;
14
-
15
- connect() {
16
- this.validate();
17
- }
18
-
19
- /**
20
- * Disconnects all added event listeners on controller disconnect
21
- */
22
- disconnect() {
23
- this.unbindDocumentEvents();
24
- }
25
-
26
- /**
27
- * Toggles the visibility of the toast
28
- */
29
- toggle(dispatcher: Event | Element | null = null) {
30
- this._toggle(undefined, dispatcher);
31
- }
32
-
33
- /**
34
- * Shows the toast
35
- */
36
- show(dispatcher: Event | Element | null = null) {
37
- this._toggle(true, dispatcher);
38
- }
39
-
40
- /**
41
- * Hides the toast
42
- */
43
- hide(dispatcher: Event | Element | null = null) {
44
- this._toggle(false, dispatcher);
45
- }
46
-
47
- /**
48
- * Validates the toast settings and attempts to set necessary internal variables
49
- */
50
- private validate() {
51
- // check for returnElement support
52
- const returnElementSelector = this.data.get("return-element");
53
- if (returnElementSelector) {
54
- this.returnElement = <HTMLElement>(
55
- document.querySelector(returnElementSelector)
56
- );
57
-
58
- if (!this.returnElement) {
59
- throw (
60
- "Unable to find element by return-element selector: " +
61
- returnElementSelector
62
- );
63
- }
64
- }
65
- }
66
-
67
- /**
68
- * Toggles the visibility of the toast element
69
- * @param show Optional parameter that force shows/hides the element or toggles it if left undefined
70
- */
71
- private _toggle(
72
- show?: boolean | undefined,
73
- dispatcher: Event | Element | null = null
74
- ) {
75
- let toShow = show;
76
- const isVisible =
77
- this.toastTarget.getAttribute("aria-hidden") === "false";
78
-
79
- // if we're letting the class toggle, we need to figure out if the toast is visible manually
80
- if (typeof toShow === "undefined") {
81
- toShow = !isVisible;
82
- }
83
-
84
- // if the state matches the disired state, return without changing anything
85
- if ((toShow && isVisible) || (!toShow && !isVisible)) {
86
- return;
87
- }
88
-
89
- const dispatchingElement = this.getDispatcher(dispatcher);
90
-
91
- // show/hide events trigger before toggling the class
92
- const triggeredEvent = this.triggerEvent(
93
- toShow ? "show" : "hide",
94
- {
95
- returnElement: this.returnElement,
96
- dispatcher: this.getDispatcher(dispatchingElement),
97
- },
98
- this.toastTarget
99
- );
100
-
101
- // if this pre-show/hide event was prevented, don't attempt to continue changing the toast state
102
- if (triggeredEvent.defaultPrevented) {
103
- return;
104
- }
105
-
106
- this.returnElement = triggeredEvent.detail.returnElement;
107
- this.toastTarget.setAttribute("aria-hidden", toShow ? "false" : "true");
108
-
109
- if (toShow) {
110
- this.bindDocumentEvents();
111
- this.hideAfterTimeout();
112
-
113
- if (this.data.get("prevent-focus-capture") !== "true") {
114
- this.focusInsideToast();
115
- }
116
- } else {
117
- this.unbindDocumentEvents();
118
- this.focusReturnElement();
119
- this.removeToastOnHide();
120
- this.clearActiveTimeout();
121
- }
122
-
123
- // check for transitionend support
124
- const supportsTransitionEnd =
125
- this.toastTarget.ontransitionend !== undefined;
126
-
127
- // shown/hidden events trigger after toggling the class
128
- if (supportsTransitionEnd) {
129
- // wait until after the toast finishes transitioning to fire the event
130
- this.toastTarget.addEventListener(
131
- "transitionend",
132
- () => {
133
- //TODO this is firing waaay to soon?
134
- this.triggerEvent(
135
- toShow ? "shown" : "hidden",
136
- {
137
- dispatcher: dispatchingElement,
138
- },
139
- this.toastTarget
140
- );
141
- },
142
- { once: true }
143
- );
144
- } else {
145
- this.triggerEvent(
146
- toShow ? "shown" : "hidden",
147
- {
148
- dispatcher: dispatchingElement,
149
- },
150
- this.toastTarget
151
- );
152
- }
153
- }
154
-
155
- /**
156
- * Listens for the s-toast:hidden event and focuses the returnElement when it is fired
157
- */
158
- private focusReturnElement() {
159
- if (!this.returnElement) {
160
- return;
161
- }
162
-
163
- this.toastTarget.addEventListener(
164
- "s-toast:hidden",
165
- () => {
166
- // double check the element still exists when the event is called
167
- if (
168
- this.returnElement &&
169
- document.body.contains(this.returnElement)
170
- ) {
171
- this.returnElement.focus();
172
- }
173
- },
174
- { once: true }
175
- );
176
- }
177
-
178
- /**
179
- * Remove the element on hide if the `remove-when-hidden` flag is set
180
- */
181
- private removeToastOnHide() {
182
- if (this.data.get("remove-when-hidden") !== "true") {
183
- return;
184
- }
185
-
186
- this.toastTarget.addEventListener(
187
- "s-toast:hidden",
188
- () => {
189
- this.element.remove();
190
- },
191
- { once: true }
192
- );
193
- }
194
-
195
- /**
196
- * Hide the element after a delay
197
- */
198
- private hideAfterTimeout() {
199
- if (
200
- this.data.get("prevent-auto-hide") === "true" ||
201
- this.data.get("hide-after-timeout") === "0"
202
- ) {
203
- return;
204
- }
205
-
206
- const timeout =
207
- parseInt(this.data.get("hide-after-timeout") as string, 10) || 3000;
208
-
209
- this.activeTimeout = window.setTimeout(() => this.hide(), timeout);
210
- }
211
-
212
- /**
213
- * Cancels the activeTimeout
214
- */
215
- clearActiveTimeout() {
216
- clearTimeout(this.activeTimeout);
217
- }
218
-
219
- /**
220
- * Gets all elements within the toast that could receive keyboard focus.
221
- */
222
- private getAllTabbables() {
223
- return Array.from(
224
- this.toastTarget.querySelectorAll<HTMLElement>(
225
- "[href], input, select, textarea, button, [tabindex]"
226
- )
227
- ).filter((el: Element) =>
228
- el.matches(":not([disabled]):not([tabindex='-1'])")
229
- );
230
- }
231
-
232
- /**
233
- * Returns the first visible element in an array or `undefined` if no elements are visible.
234
- */
235
- private firstVisible(elements?: HTMLElement[]) {
236
- // https://stackoverflow.com/a/21696585
237
- return elements?.find((el) => el.offsetParent !== null);
238
- }
239
-
240
- /**
241
- * Attempts to shift keyboard focus into the toast.
242
- * If elements with `data-s-toast-target="initialFocus"` are present and visible, one of those will be selected.
243
- * Otherwise, the first visible focusable element will receive focus.
244
- */
245
- private focusInsideToast() {
246
- this.toastTarget.addEventListener(
247
- "s-toast:shown",
248
- () => {
249
- const initialFocus =
250
- this.firstVisible(this.initialFocusTargets) ??
251
- this.firstVisible(this.getAllTabbables());
252
- initialFocus?.focus();
253
- },
254
- { once: true }
255
- );
256
- }
257
- /**
258
- * Binds global events to the document for hiding toasts on user interaction
259
- */
260
- private bindDocumentEvents() {
261
- // in order for removeEventListener to remove the right event, this bound function needs a constant reference
262
- this._boundClickFn =
263
- this._boundClickFn || this.hideOnOutsideClick.bind(this);
264
- this._boundKeypressFn =
265
- this._boundKeypressFn || this.hideOnEscapePress.bind(this);
266
-
267
- document.addEventListener("mousedown", this._boundClickFn);
268
- document.addEventListener("keyup", this._boundKeypressFn);
269
- }
270
-
271
- /**
272
- * Unbinds global events to the document for hiding toasts on user interaction
273
- */
274
- private unbindDocumentEvents() {
275
- document.removeEventListener("mousedown", this._boundClickFn);
276
- document.removeEventListener("keyup", this._boundKeypressFn);
277
- }
278
-
279
- /**
280
- * Forces the toast to hide if a user clicks outside of it or its reference element
281
- */
282
- private hideOnOutsideClick(e: Event) {
283
- const target = <Node>e.target;
284
- // check if the document was clicked inside either the toggle element or the toast itself
285
- // note: .contains also returns true if the node itself matches the target element
286
- if (
287
- !this.toastTarget?.contains(target) &&
288
- document.body.contains(target) &&
289
- this.data.get("hide-on-outside-click") !== "false"
290
- ) {
291
- this._toggle(false, e);
292
- }
293
- }
294
-
295
- /**
296
- * Forces the toast to hide if the user presses escape while it, one of its childen, or the reference element are focused
297
- */
298
- private hideOnEscapePress(e: KeyboardEvent) {
299
- // if the ESC key (27) wasn't pressed or if no toasts are showing, return
300
- if (
301
- e.which !== 27 ||
302
- this.toastTarget.getAttribute("aria-hidden") === "true"
303
- ) {
304
- return;
305
- }
306
-
307
- this._toggle(false, e);
308
- }
309
-
310
- /**
311
- * Determines the correct dispatching element from a potential input
312
- * @param dispatcher The event or element to get the dispatcher from
313
- */
314
- private getDispatcher(dispatcher: Event | Element | null = null): Element {
315
- if (dispatcher instanceof Event) {
316
- return <Element>dispatcher.target;
317
- } else if (dispatcher instanceof Element) {
318
- return dispatcher;
319
- } else {
320
- return this.element;
321
- }
322
- }
323
- }
324
-
325
- /**
326
- * Helper to manually show an s-toast element via external JS
327
- * @param element the element the `data-controller="s-toast"` attribute is on
328
- */
329
- export function showToast(element: HTMLElement) {
330
- toggleToast(element, true);
331
- }
332
-
333
- /**
334
- * Helper to manually hide an s-toast element via external JS
335
- * @param element the element the `data-controller="s-toast"` attribute is on
336
- */
337
- export function hideToast(element: HTMLElement) {
338
- toggleToast(element, false);
339
- }
340
-
341
- /**
342
- * Helper to manually show an s-toast element via external JS
343
- * @param element the element the `data-controller="s-toast"` attribute is on
344
- * @param show whether to force show/hide the toast; toggles the toast if left undefined
345
- */
346
- function toggleToast(element: HTMLElement, show?: boolean | undefined) {
347
- const controller = Stacks.application.getControllerForElementAndIdentifier(
348
- element,
349
- "s-toast"
350
- ) as ToastController;
351
-
352
- if (!controller) {
353
- throw "Unable to get s-toast controller from element";
354
- }
355
-
356
- show ? controller.show() : controller.hide();
357
- }
1
+ import * as Stacks from "../../stacks";
2
+
3
+ export class ToastController extends Stacks.StacksController {
4
+ static targets = ["toast", "initialFocus"];
5
+
6
+ declare readonly toastTarget: HTMLElement;
7
+ declare readonly initialFocusTargets: HTMLElement[];
8
+
9
+ private _boundClickFn!: (event: MouseEvent) => void;
10
+ private _boundKeypressFn!: (event: KeyboardEvent) => void;
11
+
12
+ private activeTimeout!: number;
13
+ private returnElement!: HTMLElement;
14
+
15
+ connect() {
16
+ this.validate();
17
+ }
18
+
19
+ /**
20
+ * Disconnects all added event listeners on controller disconnect
21
+ */
22
+ disconnect() {
23
+ this.unbindDocumentEvents();
24
+ }
25
+
26
+ /**
27
+ * Toggles the visibility of the toast
28
+ */
29
+ toggle(dispatcher: Event | Element | null = null) {
30
+ this._toggle(undefined, dispatcher);
31
+ }
32
+
33
+ /**
34
+ * Shows the toast
35
+ */
36
+ show(dispatcher: Event | Element | null = null) {
37
+ this._toggle(true, dispatcher);
38
+ }
39
+
40
+ /**
41
+ * Hides the toast
42
+ */
43
+ hide(dispatcher: Event | Element | null = null) {
44
+ this._toggle(false, dispatcher);
45
+ }
46
+
47
+ /**
48
+ * Validates the toast settings and attempts to set necessary internal variables
49
+ */
50
+ private validate() {
51
+ // check for returnElement support
52
+ const returnElementSelector = this.data.get("return-element");
53
+ if (returnElementSelector) {
54
+ this.returnElement = <HTMLElement>(
55
+ document.querySelector(returnElementSelector)
56
+ );
57
+
58
+ if (!this.returnElement) {
59
+ throw (
60
+ "Unable to find element by return-element selector: " +
61
+ returnElementSelector
62
+ );
63
+ }
64
+ }
65
+ }
66
+
67
+ /**
68
+ * Toggles the visibility of the toast element
69
+ * @param show Optional parameter that force shows/hides the element or toggles it if left undefined
70
+ */
71
+ private _toggle(
72
+ show?: boolean | undefined,
73
+ dispatcher: Event | Element | null = null
74
+ ) {
75
+ let toShow = show;
76
+ const isVisible =
77
+ this.toastTarget.getAttribute("aria-hidden") === "false";
78
+
79
+ // if we're letting the class toggle, we need to figure out if the toast is visible manually
80
+ if (typeof toShow === "undefined") {
81
+ toShow = !isVisible;
82
+ }
83
+
84
+ // if the state matches the disired state, return without changing anything
85
+ if ((toShow && isVisible) || (!toShow && !isVisible)) {
86
+ return;
87
+ }
88
+
89
+ const dispatchingElement = this.getDispatcher(dispatcher);
90
+
91
+ // show/hide events trigger before toggling the class
92
+ const triggeredEvent = this.triggerEvent(
93
+ toShow ? "show" : "hide",
94
+ {
95
+ returnElement: this.returnElement,
96
+ dispatcher: this.getDispatcher(dispatchingElement),
97
+ },
98
+ this.toastTarget
99
+ );
100
+
101
+ // if this pre-show/hide event was prevented, don't attempt to continue changing the toast state
102
+ if (triggeredEvent.defaultPrevented) {
103
+ return;
104
+ }
105
+
106
+ this.returnElement = triggeredEvent.detail.returnElement;
107
+ this.toastTarget.setAttribute("aria-hidden", toShow ? "false" : "true");
108
+
109
+ if (toShow) {
110
+ this.bindDocumentEvents();
111
+ this.hideAfterTimeout();
112
+
113
+ if (this.data.get("prevent-focus-capture") !== "true") {
114
+ this.focusInsideToast();
115
+ }
116
+ } else {
117
+ this.unbindDocumentEvents();
118
+ this.focusReturnElement();
119
+ this.removeToastOnHide();
120
+ this.clearActiveTimeout();
121
+ }
122
+
123
+ // check for transitionend support
124
+ const supportsTransitionEnd =
125
+ this.toastTarget.ontransitionend !== undefined;
126
+
127
+ // shown/hidden events trigger after toggling the class
128
+ if (supportsTransitionEnd) {
129
+ // wait until after the toast finishes transitioning to fire the event
130
+ this.toastTarget.addEventListener(
131
+ "transitionend",
132
+ () => {
133
+ //TODO this is firing waaay to soon?
134
+ this.triggerEvent(
135
+ toShow ? "shown" : "hidden",
136
+ {
137
+ dispatcher: dispatchingElement,
138
+ },
139
+ this.toastTarget
140
+ );
141
+ },
142
+ { once: true }
143
+ );
144
+ } else {
145
+ this.triggerEvent(
146
+ toShow ? "shown" : "hidden",
147
+ {
148
+ dispatcher: dispatchingElement,
149
+ },
150
+ this.toastTarget
151
+ );
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Listens for the s-toast:hidden event and focuses the returnElement when it is fired
157
+ */
158
+ private focusReturnElement() {
159
+ if (!this.returnElement) {
160
+ return;
161
+ }
162
+
163
+ this.toastTarget.addEventListener(
164
+ "s-toast:hidden",
165
+ () => {
166
+ // double check the element still exists when the event is called
167
+ if (
168
+ this.returnElement &&
169
+ document.body.contains(this.returnElement)
170
+ ) {
171
+ this.returnElement.focus();
172
+ }
173
+ },
174
+ { once: true }
175
+ );
176
+ }
177
+
178
+ /**
179
+ * Remove the element on hide if the `remove-when-hidden` flag is set
180
+ */
181
+ private removeToastOnHide() {
182
+ if (this.data.get("remove-when-hidden") !== "true") {
183
+ return;
184
+ }
185
+
186
+ this.toastTarget.addEventListener(
187
+ "s-toast:hidden",
188
+ () => {
189
+ this.element.remove();
190
+ },
191
+ { once: true }
192
+ );
193
+ }
194
+
195
+ /**
196
+ * Hide the element after a delay
197
+ */
198
+ private hideAfterTimeout() {
199
+ if (
200
+ this.data.get("prevent-auto-hide") === "true" ||
201
+ this.data.get("hide-after-timeout") === "0"
202
+ ) {
203
+ return;
204
+ }
205
+
206
+ const timeout =
207
+ parseInt(this.data.get("hide-after-timeout") as string, 10) || 3000;
208
+
209
+ this.activeTimeout = window.setTimeout(() => this.hide(), timeout);
210
+ }
211
+
212
+ /**
213
+ * Cancels the activeTimeout
214
+ */
215
+ clearActiveTimeout() {
216
+ clearTimeout(this.activeTimeout);
217
+ }
218
+
219
+ /**
220
+ * Gets all elements within the toast that could receive keyboard focus.
221
+ */
222
+ private getAllTabbables() {
223
+ return Array.from(
224
+ this.toastTarget.querySelectorAll<HTMLElement>(
225
+ "[href], input, select, textarea, button, [tabindex]"
226
+ )
227
+ ).filter((el: Element) =>
228
+ el.matches(":not([disabled]):not([tabindex='-1'])")
229
+ );
230
+ }
231
+
232
+ /**
233
+ * Returns the first visible element in an array or `undefined` if no elements are visible.
234
+ */
235
+ private firstVisible(elements?: HTMLElement[]) {
236
+ // https://stackoverflow.com/a/21696585
237
+ return elements?.find((el) => el.offsetParent !== null);
238
+ }
239
+
240
+ /**
241
+ * Attempts to shift keyboard focus into the toast.
242
+ * If elements with `data-s-toast-target="initialFocus"` are present and visible, one of those will be selected.
243
+ * Otherwise, the first visible focusable element will receive focus.
244
+ */
245
+ private focusInsideToast() {
246
+ this.toastTarget.addEventListener(
247
+ "s-toast:shown",
248
+ () => {
249
+ const initialFocus =
250
+ this.firstVisible(this.initialFocusTargets) ??
251
+ this.firstVisible(this.getAllTabbables());
252
+ initialFocus?.focus();
253
+ },
254
+ { once: true }
255
+ );
256
+ }
257
+ /**
258
+ * Binds global events to the document for hiding toasts on user interaction
259
+ */
260
+ private bindDocumentEvents() {
261
+ // in order for removeEventListener to remove the right event, this bound function needs a constant reference
262
+ this._boundClickFn =
263
+ this._boundClickFn || this.hideOnOutsideClick.bind(this);
264
+ this._boundKeypressFn =
265
+ this._boundKeypressFn || this.hideOnEscapePress.bind(this);
266
+
267
+ document.addEventListener("mousedown", this._boundClickFn);
268
+ document.addEventListener("keyup", this._boundKeypressFn);
269
+ }
270
+
271
+ /**
272
+ * Unbinds global events to the document for hiding toasts on user interaction
273
+ */
274
+ private unbindDocumentEvents() {
275
+ document.removeEventListener("mousedown", this._boundClickFn);
276
+ document.removeEventListener("keyup", this._boundKeypressFn);
277
+ }
278
+
279
+ /**
280
+ * Forces the toast to hide if a user clicks outside of it or its reference element
281
+ */
282
+ private hideOnOutsideClick(e: Event) {
283
+ const target = <Node>e.target;
284
+ // check if the document was clicked inside either the toggle element or the toast itself
285
+ // note: .contains also returns true if the node itself matches the target element
286
+ if (
287
+ !this.toastTarget?.contains(target) &&
288
+ document.body.contains(target) &&
289
+ this.data.get("hide-on-outside-click") !== "false"
290
+ ) {
291
+ this._toggle(false, e);
292
+ }
293
+ }
294
+
295
+ /**
296
+ * Forces the toast to hide if the user presses escape while it, one of its childen, or the reference element are focused
297
+ */
298
+ private hideOnEscapePress(e: KeyboardEvent) {
299
+ // if the ESC key (27) wasn't pressed or if no toasts are showing, return
300
+ if (
301
+ e.which !== 27 ||
302
+ this.toastTarget.getAttribute("aria-hidden") === "true"
303
+ ) {
304
+ return;
305
+ }
306
+
307
+ this._toggle(false, e);
308
+ }
309
+
310
+ /**
311
+ * Determines the correct dispatching element from a potential input
312
+ * @param dispatcher The event or element to get the dispatcher from
313
+ */
314
+ private getDispatcher(dispatcher: Event | Element | null = null): Element {
315
+ if (dispatcher instanceof Event) {
316
+ return <Element>dispatcher.target;
317
+ } else if (dispatcher instanceof Element) {
318
+ return dispatcher;
319
+ } else {
320
+ return this.element;
321
+ }
322
+ }
323
+ }
324
+
325
+ /**
326
+ * Helper to manually show an s-toast element via external JS
327
+ * @param element the element the `data-controller="s-toast"` attribute is on
328
+ */
329
+ export function showToast(element: HTMLElement) {
330
+ toggleToast(element, true);
331
+ }
332
+
333
+ /**
334
+ * Helper to manually hide an s-toast element via external JS
335
+ * @param element the element the `data-controller="s-toast"` attribute is on
336
+ */
337
+ export function hideToast(element: HTMLElement) {
338
+ toggleToast(element, false);
339
+ }
340
+
341
+ /**
342
+ * Helper to manually show an s-toast element via external JS
343
+ * @param element the element the `data-controller="s-toast"` attribute is on
344
+ * @param show whether to force show/hide the toast; toggles the toast if left undefined
345
+ */
346
+ function toggleToast(element: HTMLElement, show?: boolean | undefined) {
347
+ const controller = Stacks.application.getControllerForElementAndIdentifier(
348
+ element,
349
+ "s-toast"
350
+ ) as ToastController;
351
+
352
+ if (!controller) {
353
+ throw "Unable to get s-toast controller from element";
354
+ }
355
+
356
+ show ? controller.show() : controller.hide();
357
+ }