@stackoverflow/stacks 0.74.0 → 1.0.0-beta.0
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.
- package/dist/controllers/index.d.ts +7 -0
- package/dist/controllers/s-expandable-control.d.ts +17 -0
- package/dist/controllers/s-modal.d.ts +97 -0
- package/dist/controllers/s-navigation-tablist.d.ts +36 -0
- package/dist/controllers/s-popover.d.ts +155 -0
- package/dist/controllers/s-table.d.ts +8 -0
- package/dist/controllers/s-tooltip.d.ts +82 -0
- package/dist/controllers/s-uploader.d.ts +48 -0
- package/dist/css/stacks.css +4633 -2703
- package/dist/css/stacks.min.css +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/js/stacks.js +6026 -5403
- package/dist/js/stacks.min.js +1 -1
- package/dist/stacks.d.ts +21 -0
- package/lib/css/atomic/{_stacks-borders.less → borders.less} +30 -30
- package/lib/css/atomic/{_stacks-colors.less → colors.less} +0 -0
- package/lib/css/atomic/{_stacks-flex.less → flex.less} +14 -13
- package/lib/css/atomic/{_stacks-grid.less → grid.less} +12 -11
- package/lib/css/atomic/{_stacks-misc.less → misc.less} +23 -22
- package/lib/css/atomic/spacing.less +314 -0
- package/lib/css/atomic/{_stacks-typography.less → typography.less} +29 -29
- package/lib/css/atomic/width-height.less +194 -0
- package/lib/css/base/{_stacks-body.less → body.less} +3 -5
- package/lib/css/base/{_stacks-configuration-static.less → configuration-static.less} +3 -1
- package/lib/css/base/{_stacks-icons.less → icons.less} +0 -0
- package/lib/css/base/{_stacks-internals.less → internals.less} +0 -10
- package/lib/css/base/{_stacks-reset-meyer.less → reset-meyer.less} +0 -0
- package/lib/css/base/{_stacks-reset-normalize.less → reset-normalize.less} +0 -0
- package/lib/css/base/{_stacks-reset.less → reset.less} +2 -2
- package/lib/css/components/{_stacks-activity-indicator.less → activity-indicator.less} +9 -9
- package/lib/css/components/{_stacks-avatars.less → avatars.less} +40 -40
- package/lib/css/components/{_stacks-badges.less → badges.less} +11 -11
- package/lib/css/components/{_stacks-banners.less → banners.less} +5 -6
- package/lib/css/components/{_stacks-blank-states.less → blank-states.less} +2 -2
- package/lib/css/components/{_stacks-breadcrumbs.less → breadcrumbs.less} +7 -7
- package/lib/css/components/{_stacks-button-groups.less → button-groups.less} +2 -2
- package/lib/css/components/{_stacks-buttons.less → buttons.less} +77 -72
- package/lib/css/components/{_stacks-cards.less → cards.less} +2 -2
- package/lib/css/components/{_stacks-code-blocks.less → code-blocks.less} +8 -8
- package/lib/css/components/{_stacks-collapsible.less → collapsible.less} +0 -0
- package/lib/css/components/{_stacks-inputs.less → inputs.less} +41 -41
- package/lib/css/components/{_stacks-link-previews.less → link-previews.less} +17 -17
- package/lib/css/components/{_stacks-links.less → links.less} +4 -4
- package/lib/css/components/{_stacks-menu.less → menu.less} +5 -5
- package/lib/css/components/{_stacks-modals.less → modals.less} +20 -20
- package/lib/css/components/{_stacks-navigation.less → navigation.less} +12 -12
- package/lib/css/components/{_stacks-notices.less → notices.less} +12 -12
- package/lib/css/components/{_stacks-page-titles.less → page-titles.less} +9 -9
- package/lib/css/components/{_stacks-pagination.less → pagination.less} +8 -8
- package/lib/css/components/{_stacks-popovers.less → popovers.less} +17 -17
- package/lib/css/components/{_stacks-post-summary.less → post-summary.less} +155 -60
- package/lib/css/components/{_stacks-progress-bars.less → progress-bars.less} +29 -30
- package/lib/css/components/{_stacks-prose.less → prose.less} +31 -31
- package/lib/css/components/{_stacks-spinner.less → spinner.less} +14 -14
- package/lib/css/components/{_stacks-tables.less → tables.less} +10 -10
- package/lib/css/components/{_stacks-tags.less → tags.less} +33 -41
- package/lib/css/components/{_stacks-toggle-switches.less → toggle-switches.less} +5 -5
- package/lib/css/components/{_stacks-topbar.less → topbar.less} +27 -40
- package/lib/css/components/{_stacks-uploader.less → uploader.less} +18 -18
- package/lib/css/components/{_stacks-user-cards.less → user-cards.less} +14 -14
- package/lib/css/components/{_stacks-widget-dynamic.less → widget-dynamic.less} +1 -1
- package/lib/css/components/{_stacks-widget-static.less → widget-static.less} +39 -38
- package/lib/css/exports/{_stacks-constants-colors.less → constants-colors.less} +21 -29
- package/lib/css/exports/constants-helpers.less +108 -0
- package/lib/css/exports/constants-type.less +153 -0
- package/lib/css/exports/{_stacks-exports.less → exports.less} +4 -4
- package/lib/css/exports/{_stacks-mixins.less → mixins.less} +18 -1
- package/lib/css/stacks-dynamic.less +12 -13
- package/lib/css/stacks-static.less +38 -38
- package/lib/ts/controllers/index.ts +8 -0
- package/lib/ts/controllers/s-expandable-control.ts +163 -164
- package/lib/ts/controllers/s-modal.ts +259 -261
- package/lib/ts/controllers/s-navigation-tablist.ts +96 -97
- package/lib/ts/controllers/s-popover.ts +438 -440
- package/lib/ts/controllers/s-table.ts +203 -203
- package/lib/ts/controllers/s-tooltip.ts +195 -196
- package/lib/ts/controllers/s-uploader.ts +162 -164
- package/lib/ts/index.ts +20 -0
- package/lib/ts/stacks.ts +73 -68
- package/lib/tsconfig.json +8 -6
- package/package.json +43 -27
- package/dist/css/stacks-flexgrid-shim.min.css +0 -1
- package/lib/css/atomic/_stacks-spacing.less +0 -162
- package/lib/css/atomic/_stacks-width-height.less +0 -189
- package/lib/css/base/_stacks-configuration-dynamic.less +0 -106
- package/lib/css/exports/_stacks-constants-helpers.less +0 -139
- package/lib/css/exports/_stacks-constants-type.less +0 -91
- package/lib/ts/finalize.ts +0 -1
- package/lib/ts/stimulus.d.ts +0 -4
|
@@ -1,323 +1,321 @@
|
|
|
1
|
-
|
|
1
|
+
import * as Stacks from "../stacks";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
export class ModalController extends Stacks.StacksController {
|
|
4
|
+
static targets = ["modal", "initialFocus"];
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
private modalTarget!: HTMLElement;
|
|
7
|
+
private initialFocusTargets!: HTMLElement[];
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
private _boundClickFn!: any;
|
|
10
|
+
private _boundKeypressFn!: any;
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
private returnElement!: HTMLElement;
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
private _boundTabTrap!: any;
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
connect () {
|
|
17
|
+
this.validate();
|
|
18
|
+
}
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Disconnects all added event listeners on controller disconnect
|
|
22
|
+
*/
|
|
23
|
+
disconnect() {
|
|
24
|
+
this.unbindDocumentEvents();
|
|
25
|
+
};
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Toggles the visibility of the modal
|
|
29
|
+
*/
|
|
30
|
+
toggle (dispatcher: Event|Element|null = null) {
|
|
31
|
+
this._toggle(undefined, dispatcher);
|
|
32
|
+
}
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Shows the modal
|
|
36
|
+
*/
|
|
37
|
+
show (dispatcher: Event|Element|null = null) {
|
|
38
|
+
this._toggle(true, dispatcher);
|
|
39
|
+
}
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Hides the modal
|
|
43
|
+
*/
|
|
44
|
+
hide (dispatcher: Event|Element|null = null) {
|
|
45
|
+
this._toggle(false, dispatcher);
|
|
46
|
+
}
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
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;
|
|
60
59
|
}
|
|
61
60
|
}
|
|
61
|
+
}
|
|
62
62
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
}
|
|
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";
|
|
75
70
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
+
}
|
|
80
75
|
|
|
81
|
-
|
|
76
|
+
// if the state matches the disired state, return without changing anything
|
|
77
|
+
if ((toShow && isVisible) || (!toShow && !isVisible)) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
82
80
|
|
|
83
|
-
|
|
84
|
-
var triggeredEvent = this.triggerEvent(toShow ? "show" : "hide", {
|
|
85
|
-
returnElement: this.returnElement,
|
|
86
|
-
dispatcher: this.getDispatcher(dispatchingElement)
|
|
87
|
-
}, this.modalTarget);
|
|
81
|
+
let dispatchingElement = this.getDispatcher(dispatcher);
|
|
88
82
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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);
|
|
93
88
|
|
|
94
|
-
|
|
95
|
-
|
|
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
|
+
}
|
|
96
93
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
this.focusInsideModal();
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
this.unbindDocumentEvents();
|
|
103
|
-
this.focusReturnElement();
|
|
104
|
-
this.removeModalOnHide();
|
|
105
|
-
}
|
|
94
|
+
this.returnElement = triggeredEvent.detail.returnElement;
|
|
95
|
+
this.modalTarget.setAttribute("aria-hidden", toShow ? "false" : "true");
|
|
106
96
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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?
|
|
120
115
|
this.triggerEvent(toShow ? "shown" : "hidden", {
|
|
121
116
|
dispatcher: dispatchingElement
|
|
122
117
|
}, this.modalTarget);
|
|
123
|
-
}
|
|
118
|
+
}, { once: true });
|
|
119
|
+
} else {
|
|
120
|
+
this.triggerEvent(toShow ? "shown" : "hidden", {
|
|
121
|
+
dispatcher: dispatchingElement
|
|
122
|
+
}, this.modalTarget);
|
|
124
123
|
}
|
|
124
|
+
}
|
|
125
125
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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 });
|
|
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;
|
|
140
132
|
}
|
|
141
133
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
if (this.data.get("remove-when-hidden") !== "true") {
|
|
147
|
-
return;
|
|
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();
|
|
148
138
|
}
|
|
139
|
+
}, {once: true });
|
|
140
|
+
}
|
|
149
141
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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);
|
|
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;
|
|
169
148
|
}
|
|
170
149
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
return this.firstVisible([...elements].reverse());
|
|
176
|
-
}
|
|
150
|
+
this.modalTarget.addEventListener("s-modal:hidden", () => {
|
|
151
|
+
this.element.remove();
|
|
152
|
+
}, {once: true });
|
|
153
|
+
}
|
|
177
154
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
var initialFocus = this.firstVisible(this.initialFocusTargets) ?? this.firstVisible(this.getAllTabbables());
|
|
186
|
-
initialFocus?.focus();
|
|
187
|
-
}, {once: true });
|
|
188
|
-
}
|
|
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
|
+
}
|
|
189
162
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
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
|
+
}
|
|
194
170
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
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
|
+
}
|
|
202
177
|
|
|
203
|
-
|
|
204
|
-
|
|
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
|
+
}
|
|
205
189
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
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
|
-
}
|
|
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();
|
|
225
201
|
}
|
|
226
|
-
}
|
|
227
202
|
|
|
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);
|
|
203
|
+
return;
|
|
240
204
|
}
|
|
241
205
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
private unbindDocumentEvents () {
|
|
246
|
-
document.removeEventListener("mousedown", this._boundClickFn);
|
|
247
|
-
document.removeEventListener("keyup", this._boundKeypressFn);
|
|
248
|
-
document.removeEventListener("keydown", this._boundTabTrap);
|
|
249
|
-
}
|
|
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();
|
|
250
209
|
|
|
251
|
-
|
|
252
|
-
|
|
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
|
-
}
|
|
210
|
+
var firstTabbable = this.firstVisible(tabbables);
|
|
211
|
+
var lastTabbable = this.lastVisible(tabbables);
|
|
262
212
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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
|
+
}
|
|
270
224
|
}
|
|
271
|
-
|
|
272
|
-
this._toggle(false, e);
|
|
273
225
|
}
|
|
226
|
+
}
|
|
274
227
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
return this.element;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
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);
|
|
290
240
|
}
|
|
291
241
|
|
|
292
242
|
/**
|
|
293
|
-
*
|
|
294
|
-
* @param element the element the `data-controller="s-modal"` attribute is on
|
|
243
|
+
* Unbinds global events to the document for hiding popovers on user interaction
|
|
295
244
|
*/
|
|
296
|
-
|
|
297
|
-
|
|
245
|
+
private unbindDocumentEvents () {
|
|
246
|
+
document.removeEventListener("mousedown", this._boundClickFn);
|
|
247
|
+
document.removeEventListener("keyup", this._boundKeypressFn);
|
|
248
|
+
document.removeEventListener("keydown", this._boundTabTrap);
|
|
298
249
|
}
|
|
299
|
-
|
|
250
|
+
|
|
300
251
|
/**
|
|
301
|
-
*
|
|
302
|
-
* @param element the element the `data-controller="s-modal"` attribute is on
|
|
252
|
+
* Forces the popover to hide if a user clicks outside of it or its reference element
|
|
303
253
|
*/
|
|
304
|
-
|
|
305
|
-
|
|
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
|
+
}
|
|
306
261
|
}
|
|
307
262
|
|
|
308
263
|
/**
|
|
309
|
-
*
|
|
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
|
|
264
|
+
* Forces the popover to hide if the user presses escape while it, one of its childen, or the reference element are focused
|
|
312
265
|
*/
|
|
313
|
-
|
|
314
|
-
|
|
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
|
+
}
|
|
315
274
|
|
|
316
|
-
|
|
317
|
-
|
|
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;
|
|
318
285
|
}
|
|
286
|
+
else {
|
|
287
|
+
return this.element;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
319
291
|
|
|
320
|
-
|
|
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";
|
|
321
318
|
}
|
|
319
|
+
|
|
320
|
+
show ? controller.show() : controller.hide();
|
|
322
321
|
}
|
|
323
|
-
Stacks.application.register("s-modal", Stacks.ModalController);
|