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