@stackoverflow/stacks 1.9.0 → 1.9.2

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