@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,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
+ }