@stackoverflow/stacks 2.7.3 → 2.7.4

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 (83) hide show
  1. package/LICENSE.MD +9 -9
  2. package/README.md +158 -180
  3. package/dist/css/stacks.css +6 -0
  4. package/dist/js/stacks.min.js +1 -1
  5. package/lib/atomic/border.less +139 -139
  6. package/lib/atomic/color.less +36 -36
  7. package/lib/atomic/flex.less +426 -426
  8. package/lib/atomic/gap.less +44 -44
  9. package/lib/atomic/grid.less +139 -139
  10. package/lib/atomic/misc.less +374 -374
  11. package/lib/atomic/spacing.less +98 -98
  12. package/lib/atomic/typography.less +266 -264
  13. package/lib/atomic/width-height.less +194 -194
  14. package/lib/base/body.less +44 -44
  15. package/lib/base/configuration-static.less +61 -61
  16. package/lib/base/fieldset.less +5 -5
  17. package/lib/base/icon.less +11 -11
  18. package/lib/base/internal.less +220 -220
  19. package/lib/base/reset-meyer.less +64 -64
  20. package/lib/base/reset-normalize.less +449 -449
  21. package/lib/base/reset.less +20 -20
  22. package/lib/components/activity-indicator/activity-indicator.less +53 -53
  23. package/lib/components/avatar/avatar.less +108 -108
  24. package/lib/components/award-bling/award-bling.less +31 -31
  25. package/lib/components/banner/banner.less +44 -44
  26. package/lib/components/banner/banner.ts +149 -149
  27. package/lib/components/block-link/block-link.less +82 -82
  28. package/lib/components/breadcrumbs/breadcrumbs.less +41 -41
  29. package/lib/components/button-group/button-group.less +82 -82
  30. package/lib/components/card/card.less +37 -37
  31. package/lib/components/check-control/check-control.less +17 -17
  32. package/lib/components/check-group/check-group.less +19 -19
  33. package/lib/components/checkbox_radio/checkbox_radio.less +159 -159
  34. package/lib/components/code-block/code-block.fixtures.ts +88 -88
  35. package/lib/components/code-block/code-block.less +116 -116
  36. package/lib/components/description/description.less +9 -9
  37. package/lib/components/empty-state/empty-state.less +16 -16
  38. package/lib/components/expandable/expandable.less +118 -118
  39. package/lib/components/input-fill/input-fill.less +35 -35
  40. package/lib/components/input-icon/input-icon.less +45 -45
  41. package/lib/components/input-message/input-message.less +49 -49
  42. package/lib/components/label/label.less +110 -110
  43. package/lib/components/link-preview/link-preview.less +148 -148
  44. package/lib/components/menu/menu.less +41 -41
  45. package/lib/components/modal/modal.less +118 -118
  46. package/lib/components/modal/modal.ts +383 -383
  47. package/lib/components/navigation/navigation.less +136 -136
  48. package/lib/components/navigation/navigation.ts +128 -128
  49. package/lib/components/page-title/page-title.less +51 -51
  50. package/lib/components/popover/popover.less +159 -159
  51. package/lib/components/popover/popover.ts +651 -651
  52. package/lib/components/post-summary/post-summary.less +457 -457
  53. package/lib/components/progress-bar/progress-bar.less +291 -291
  54. package/lib/components/prose/prose.less +452 -452
  55. package/lib/components/select/select.less +138 -138
  56. package/lib/components/spinner/spinner.less +103 -103
  57. package/lib/components/table/table.ts +296 -296
  58. package/lib/components/table-container/table-container.less +4 -4
  59. package/lib/components/tag/tag.less +186 -186
  60. package/lib/components/toast/toast.less +35 -35
  61. package/lib/components/toast/toast.ts +357 -357
  62. package/lib/components/toggle-switch/toggle-switch.less +104 -104
  63. package/lib/components/topbar/topbar.less +553 -553
  64. package/lib/components/uploader/uploader.less +205 -205
  65. package/lib/components/user-card/user-card.less +129 -129
  66. package/lib/controllers.ts +33 -33
  67. package/lib/exports/color-mixins.less +283 -283
  68. package/lib/exports/constants-helpers.less +108 -108
  69. package/lib/exports/constants-type.less +155 -155
  70. package/lib/exports/exports.less +15 -15
  71. package/lib/exports/mixins.less +334 -333
  72. package/lib/exports/spacing-mixins.less +67 -67
  73. package/lib/index.ts +32 -32
  74. package/lib/input-utils.less +41 -41
  75. package/lib/stacks-dynamic.less +24 -24
  76. package/lib/stacks-static.less +93 -93
  77. package/lib/stacks.less +13 -13
  78. package/lib/test/assertions.ts +36 -36
  79. package/lib/test/less-test-utils.ts +28 -28
  80. package/lib/test/open-wc-testing-patch.d.ts +26 -26
  81. package/lib/tsconfig.build.json +4 -4
  82. package/lib/tsconfig.json +17 -17
  83. package/package.json +26 -22
@@ -1,383 +1,383 @@
1
- import * as Stacks from "../../stacks";
2
-
3
- export class ModalController extends Stacks.StacksController {
4
- static targets = ["modal", "initialFocus"];
5
-
6
- declare readonly modalTarget: HTMLElement;
7
- declare readonly 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
-
232
- // Only set focus if focus is not already set on an element within the modal
233
- if (!this.modalTarget.contains(document.activeElement)) {
234
- initialFocus?.focus();
235
- }
236
- },
237
- { once: true }
238
- );
239
- }
240
-
241
- /**
242
- * Returns keyboard focus to the modal if it has left or is about to leave.
243
- */
244
- private keepFocusWithinModal(e: KeyboardEvent) {
245
- // If somehow the user has tabbed out of the modal or if focus started outside the modal, push them to the first item.
246
- if (!this.modalTarget.contains(<Element>e.target)) {
247
- const focusTarget = this.firstVisible(this.getAllTabbables());
248
- if (focusTarget) {
249
- e.preventDefault();
250
- focusTarget.focus();
251
- }
252
-
253
- return;
254
- }
255
-
256
- // If we observe a tab keydown and we're on an edge, cycle the focus to the other side.
257
- if (e.key === "Tab") {
258
- const tabbables = this.getAllTabbables();
259
-
260
- const firstTabbable = this.firstVisible(tabbables);
261
- const lastTabbable = this.lastVisible(tabbables);
262
-
263
- if (firstTabbable && lastTabbable) {
264
- if (firstTabbable === lastTabbable) {
265
- e.preventDefault();
266
- firstTabbable.focus();
267
- } else if (e.shiftKey && e.target === firstTabbable) {
268
- e.preventDefault();
269
- lastTabbable.focus();
270
- } else if (!e.shiftKey && e.target === lastTabbable) {
271
- e.preventDefault();
272
- firstTabbable.focus();
273
- }
274
- }
275
- }
276
- }
277
-
278
- /**
279
- * Binds global events to the document for hiding popovers on user interaction
280
- */
281
- private bindDocumentEvents() {
282
- // in order for removeEventListener to remove the right event, this bound function needs a constant reference
283
- this._boundClickFn =
284
- this._boundClickFn || this.hideOnOutsideClick.bind(this);
285
- this._boundKeypressFn =
286
- this._boundKeypressFn || this.hideOnEscapePress.bind(this);
287
- this._boundTabTrap =
288
- this._boundTabTrap || this.keepFocusWithinModal.bind(this);
289
-
290
- document.addEventListener("mousedown", this._boundClickFn);
291
- document.addEventListener("keyup", this._boundKeypressFn);
292
- document.addEventListener("keydown", this._boundTabTrap);
293
- }
294
-
295
- /**
296
- * Unbinds global events to the document for hiding popovers on user interaction
297
- */
298
- private unbindDocumentEvents() {
299
- document.removeEventListener("mousedown", this._boundClickFn);
300
- document.removeEventListener("keyup", this._boundKeypressFn);
301
- document.removeEventListener("keydown", this._boundTabTrap);
302
- }
303
-
304
- /**
305
- * Forces the popover to hide if a user clicks outside of it or its reference element
306
- */
307
- private hideOnOutsideClick(e: Event) {
308
- const target = <Node>e.target;
309
- // check if the document was clicked inside either the toggle element or the modal itself
310
- // note: .contains also returns true if the node itself matches the target element
311
- if (
312
- !this.modalTarget
313
- .querySelector(".s-modal--dialog")
314
- ?.contains(target) &&
315
- document.body.contains(target)
316
- ) {
317
- this._toggle(false, e);
318
- }
319
- }
320
-
321
- /**
322
- * Forces the popover to hide if the user presses escape while it, one of its childen, or the reference element are focused
323
- */
324
- private hideOnEscapePress(e: KeyboardEvent) {
325
- // if the ESC key (27) wasn't pressed or if no popovers are showing, return
326
- if (
327
- e.which !== 27 ||
328
- this.modalTarget.getAttribute("aria-hidden") === "true"
329
- ) {
330
- return;
331
- }
332
-
333
- this._toggle(false, e);
334
- }
335
-
336
- /**
337
- * Determines the correct dispatching element from a potential input
338
- * @param dispatcher The event or element to get the dispatcher from
339
- */
340
- private getDispatcher(dispatcher: Event | Element | null = null): Element {
341
- if (dispatcher instanceof Event) {
342
- return <Element>dispatcher.target;
343
- } else if (dispatcher instanceof Element) {
344
- return dispatcher;
345
- } else {
346
- return this.element;
347
- }
348
- }
349
- }
350
-
351
- /**
352
- * Helper to manually show an s-modal element via external JS
353
- * @param element the element the `data-controller="s-modal"` attribute is on
354
- */
355
- export function showModal(element: HTMLElement) {
356
- toggleModal(element, true);
357
- }
358
-
359
- /**
360
- * Helper to manually hide an s-modal element via external JS
361
- * @param element the element the `data-controller="s-modal"` attribute is on
362
- */
363
- export function hideModal(element: HTMLElement) {
364
- toggleModal(element, false);
365
- }
366
-
367
- /**
368
- * Helper to manually show an s-modal element via external JS
369
- * @param element the element the `data-controller="s-modal"` attribute is on
370
- * @param show whether to force show/hide the modal; toggles the modal if left undefined
371
- */
372
- function toggleModal(element: HTMLElement, show?: boolean | undefined) {
373
- const controller = Stacks.application.getControllerForElementAndIdentifier(
374
- element,
375
- "s-modal"
376
- ) as ModalController;
377
-
378
- if (!controller) {
379
- throw "Unable to get s-modal controller from element";
380
- }
381
-
382
- show ? controller.show() : controller.hide();
383
- }
1
+ import * as Stacks from "../../stacks";
2
+
3
+ export class ModalController extends Stacks.StacksController {
4
+ static targets = ["modal", "initialFocus"];
5
+
6
+ declare readonly modalTarget: HTMLElement;
7
+ declare readonly 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
+
232
+ // Only set focus if focus is not already set on an element within the modal
233
+ if (!this.modalTarget.contains(document.activeElement)) {
234
+ initialFocus?.focus();
235
+ }
236
+ },
237
+ { once: true }
238
+ );
239
+ }
240
+
241
+ /**
242
+ * Returns keyboard focus to the modal if it has left or is about to leave.
243
+ */
244
+ private keepFocusWithinModal(e: KeyboardEvent) {
245
+ // If somehow the user has tabbed out of the modal or if focus started outside the modal, push them to the first item.
246
+ if (!this.modalTarget.contains(<Element>e.target)) {
247
+ const focusTarget = this.firstVisible(this.getAllTabbables());
248
+ if (focusTarget) {
249
+ e.preventDefault();
250
+ focusTarget.focus();
251
+ }
252
+
253
+ return;
254
+ }
255
+
256
+ // If we observe a tab keydown and we're on an edge, cycle the focus to the other side.
257
+ if (e.key === "Tab") {
258
+ const tabbables = this.getAllTabbables();
259
+
260
+ const firstTabbable = this.firstVisible(tabbables);
261
+ const lastTabbable = this.lastVisible(tabbables);
262
+
263
+ if (firstTabbable && lastTabbable) {
264
+ if (firstTabbable === lastTabbable) {
265
+ e.preventDefault();
266
+ firstTabbable.focus();
267
+ } else if (e.shiftKey && e.target === firstTabbable) {
268
+ e.preventDefault();
269
+ lastTabbable.focus();
270
+ } else if (!e.shiftKey && e.target === lastTabbable) {
271
+ e.preventDefault();
272
+ firstTabbable.focus();
273
+ }
274
+ }
275
+ }
276
+ }
277
+
278
+ /**
279
+ * Binds global events to the document for hiding popovers on user interaction
280
+ */
281
+ private bindDocumentEvents() {
282
+ // in order for removeEventListener to remove the right event, this bound function needs a constant reference
283
+ this._boundClickFn =
284
+ this._boundClickFn || this.hideOnOutsideClick.bind(this);
285
+ this._boundKeypressFn =
286
+ this._boundKeypressFn || this.hideOnEscapePress.bind(this);
287
+ this._boundTabTrap =
288
+ this._boundTabTrap || this.keepFocusWithinModal.bind(this);
289
+
290
+ document.addEventListener("mousedown", this._boundClickFn);
291
+ document.addEventListener("keyup", this._boundKeypressFn);
292
+ document.addEventListener("keydown", this._boundTabTrap);
293
+ }
294
+
295
+ /**
296
+ * Unbinds global events to the document for hiding popovers on user interaction
297
+ */
298
+ private unbindDocumentEvents() {
299
+ document.removeEventListener("mousedown", this._boundClickFn);
300
+ document.removeEventListener("keyup", this._boundKeypressFn);
301
+ document.removeEventListener("keydown", this._boundTabTrap);
302
+ }
303
+
304
+ /**
305
+ * Forces the popover to hide if a user clicks outside of it or its reference element
306
+ */
307
+ private hideOnOutsideClick(e: Event) {
308
+ const target = <Node>e.target;
309
+ // check if the document was clicked inside either the toggle element or the modal itself
310
+ // note: .contains also returns true if the node itself matches the target element
311
+ if (
312
+ !this.modalTarget
313
+ .querySelector(".s-modal--dialog")
314
+ ?.contains(target) &&
315
+ document.body.contains(target)
316
+ ) {
317
+ this._toggle(false, e);
318
+ }
319
+ }
320
+
321
+ /**
322
+ * Forces the popover to hide if the user presses escape while it, one of its childen, or the reference element are focused
323
+ */
324
+ private hideOnEscapePress(e: KeyboardEvent) {
325
+ // if the ESC key (27) wasn't pressed or if no popovers are showing, return
326
+ if (
327
+ e.which !== 27 ||
328
+ this.modalTarget.getAttribute("aria-hidden") === "true"
329
+ ) {
330
+ return;
331
+ }
332
+
333
+ this._toggle(false, e);
334
+ }
335
+
336
+ /**
337
+ * Determines the correct dispatching element from a potential input
338
+ * @param dispatcher The event or element to get the dispatcher from
339
+ */
340
+ private getDispatcher(dispatcher: Event | Element | null = null): Element {
341
+ if (dispatcher instanceof Event) {
342
+ return <Element>dispatcher.target;
343
+ } else if (dispatcher instanceof Element) {
344
+ return dispatcher;
345
+ } else {
346
+ return this.element;
347
+ }
348
+ }
349
+ }
350
+
351
+ /**
352
+ * Helper to manually show an s-modal element via external JS
353
+ * @param element the element the `data-controller="s-modal"` attribute is on
354
+ */
355
+ export function showModal(element: HTMLElement) {
356
+ toggleModal(element, true);
357
+ }
358
+
359
+ /**
360
+ * Helper to manually hide an s-modal element via external JS
361
+ * @param element the element the `data-controller="s-modal"` attribute is on
362
+ */
363
+ export function hideModal(element: HTMLElement) {
364
+ toggleModal(element, false);
365
+ }
366
+
367
+ /**
368
+ * Helper to manually show an s-modal element via external JS
369
+ * @param element the element the `data-controller="s-modal"` attribute is on
370
+ * @param show whether to force show/hide the modal; toggles the modal if left undefined
371
+ */
372
+ function toggleModal(element: HTMLElement, show?: boolean | undefined) {
373
+ const controller = Stacks.application.getControllerForElementAndIdentifier(
374
+ element,
375
+ "s-modal"
376
+ ) as ModalController;
377
+
378
+ if (!controller) {
379
+ throw "Unable to get s-modal controller from element";
380
+ }
381
+
382
+ show ? controller.show() : controller.hide();
383
+ }