@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.
- package/README.md +47 -47
- package/dist/css/stacks.css +108 -0
- package/dist/css/stacks.min.css +1 -1
- package/lib/css/atomic/borders.less +378 -378
- package/lib/css/atomic/colors.less +209 -209
- package/lib/css/atomic/flex.less +375 -375
- package/lib/css/atomic/grid.less +174 -174
- package/lib/css/atomic/misc.less +343 -343
- package/lib/css/atomic/spacing.less +332 -314
- package/lib/css/atomic/typography.less +273 -273
- package/lib/css/atomic/width-height.less +194 -194
- package/lib/css/base/body.less +44 -44
- package/lib/css/base/configuration-static.less +61 -61
- package/lib/css/base/icons.less +20 -20
- package/lib/css/base/internals.less +220 -220
- package/lib/css/base/reset-meyer.less +64 -64
- package/lib/css/base/reset-normalize.less +449 -449
- package/lib/css/base/reset.less +20 -20
- package/lib/css/components/activity-indicator.less +45 -45
- package/lib/css/components/avatars.less +189 -189
- package/lib/css/components/badges.less +209 -209
- package/lib/css/components/banners.less +80 -80
- package/lib/css/components/blank-states.less +26 -26
- package/lib/css/components/breadcrumbs.less +44 -44
- package/lib/css/components/button-groups.less +104 -104
- package/lib/css/components/buttons.less +665 -665
- package/lib/css/components/cards.less +44 -44
- package/lib/css/components/code-blocks.less +130 -130
- package/lib/css/components/collapsible.less +104 -104
- package/lib/css/components/inputs.less +728 -728
- package/lib/css/components/link-previews.less +136 -136
- package/lib/css/components/links.less +218 -218
- package/lib/css/components/menu.less +47 -47
- package/lib/css/components/modals.less +133 -133
- package/lib/css/components/navigation.less +146 -146
- package/lib/css/components/notices.less +233 -233
- package/lib/css/components/page-titles.less +60 -60
- package/lib/css/components/pagination.less +55 -55
- package/lib/css/components/popovers.less +197 -197
- package/lib/css/components/post-summary.less +425 -425
- package/lib/css/components/progress-bars.less +330 -330
- package/lib/css/components/prose.less +503 -503
- package/lib/css/components/spinner.less +107 -107
- package/lib/css/components/tables.less +341 -341
- package/lib/css/components/tags.less +236 -236
- package/lib/css/components/toggle-switches.less +144 -144
- package/lib/css/components/topbar.less +427 -427
- package/lib/css/components/uploader.less +210 -210
- package/lib/css/components/user-cards.less +169 -169
- package/lib/css/components/widget-dynamic.less +33 -33
- package/lib/css/components/widget-static.less +273 -273
- package/lib/css/exports/constants-colors.less +1092 -1092
- package/lib/css/exports/constants-helpers.less +108 -108
- package/lib/css/exports/constants-type.less +153 -153
- package/lib/css/exports/exports.less +15 -15
- package/lib/css/exports/mixins.less +237 -237
- package/lib/css/stacks-dynamic.less +35 -35
- package/lib/css/stacks-static.less +86 -86
- package/lib/css/stacks.less +13 -13
- package/lib/ts/controllers/index.ts +7 -7
- package/lib/ts/controllers/s-expandable-control.ts +188 -188
- package/lib/ts/controllers/s-modal.ts +321 -321
- package/lib/ts/controllers/s-navigation-tablist.ts +117 -117
- package/lib/ts/controllers/s-popover.ts +547 -547
- package/lib/ts/controllers/s-table.ts +220 -220
- package/lib/ts/controllers/s-tooltip.ts +246 -246
- package/lib/ts/controllers/s-uploader.ts +172 -172
- package/lib/ts/index.ts +20 -20
- package/lib/ts/stacks.ts +88 -88
- package/lib/tsconfig.json +13 -13
- package/package.json +87 -87
|
@@ -1,86 +1,86 @@
|
|
|
1
|
-
//
|
|
2
|
-
// STACK OVERFLOW
|
|
3
|
-
// STATIC STACK ELEMENTS
|
|
4
|
-
//
|
|
5
|
-
// This CSS comes from Stacks, our CSS & Pattern library for rapidly building
|
|
6
|
-
// Stack Overflow. For documentation of all these classes and how to contribute,
|
|
7
|
-
// visit https://stackoverflow.design/
|
|
8
|
-
//
|
|
9
|
-
// This is where all the magic happens.
|
|
10
|
-
//
|
|
11
|
-
// ============================================================================
|
|
12
|
-
// $ STATIC ELEMENTS
|
|
13
|
-
// The following items are elements which we DO NOT allow communities
|
|
14
|
-
// to modify via variables.
|
|
15
|
-
// ----------------------------------------------------------------------------
|
|
16
|
-
@import "base/reset.less";
|
|
17
|
-
@import "base/icons.less";
|
|
18
|
-
|
|
19
|
-
// -- COMPONENTS
|
|
20
|
-
@import "components/activity-indicator.less";
|
|
21
|
-
@import "components/avatars.less";
|
|
22
|
-
@import "components/badges.less";
|
|
23
|
-
@import "components/blank-states.less";
|
|
24
|
-
@import "components/breadcrumbs.less";
|
|
25
|
-
@import "components/button-groups.less";
|
|
26
|
-
@import "components/cards.less";
|
|
27
|
-
@import "components/code-blocks.less";
|
|
28
|
-
@import "components/collapsible.less";
|
|
29
|
-
@import "components/inputs.less";
|
|
30
|
-
@import "components/menu.less";
|
|
31
|
-
@import "components/modals.less";
|
|
32
|
-
@import "components/navigation.less";
|
|
33
|
-
@import "components/page-titles.less";
|
|
34
|
-
@import "components/popovers.less";
|
|
35
|
-
@import "components/post-summary.less";
|
|
36
|
-
@import "components/progress-bars.less";
|
|
37
|
-
@import "components/prose.less";
|
|
38
|
-
@import "components/spinner.less";
|
|
39
|
-
@import "components/tables.less";
|
|
40
|
-
@import "components/toggle-switches.less";
|
|
41
|
-
@import "components/topbar.less";
|
|
42
|
-
@import "components/uploader.less";
|
|
43
|
-
@import "components/user-cards.less";
|
|
44
|
-
@import "components/widget-static.less";
|
|
45
|
-
|
|
46
|
-
// -- LESS CONSTANTS AND MIXINS
|
|
47
|
-
@import "exports/exports.less";
|
|
48
|
-
|
|
49
|
-
// -- ATOMIC CLASSES
|
|
50
|
-
@import "atomic/borders.less";
|
|
51
|
-
@import "atomic/colors.less";
|
|
52
|
-
@import "atomic/flex.less";
|
|
53
|
-
@import "atomic/grid.less";
|
|
54
|
-
@import "atomic/spacing.less";
|
|
55
|
-
@import "atomic/typography.less";
|
|
56
|
-
@import "atomic/misc.less";
|
|
57
|
-
@import "atomic/width-height.less";
|
|
58
|
-
|
|
59
|
-
/* stylelint-disable */
|
|
60
|
-
#stacks-internals #screen-lg({
|
|
61
|
-
#stacks-internals-collect-large();
|
|
62
|
-
});
|
|
63
|
-
#stacks-internals #screen-md({
|
|
64
|
-
#stacks-internals-collect-medium();
|
|
65
|
-
});
|
|
66
|
-
#stacks-internals #screen-sm({
|
|
67
|
-
#stacks-internals-collect-small();
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
// We need printing styles to be last so they can override all other styles.
|
|
71
|
-
.print\:d-block {
|
|
72
|
-
@media print {
|
|
73
|
-
display: block !important;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
.print\:d-none {
|
|
78
|
-
@media print {
|
|
79
|
-
display: none !important;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
/* stylelint-enable */
|
|
83
|
-
|
|
84
|
-
// -- CONFIG
|
|
85
|
-
@import "base/configuration-static.less";
|
|
86
|
-
@import "base/internals.less";
|
|
1
|
+
//
|
|
2
|
+
// STACK OVERFLOW
|
|
3
|
+
// STATIC STACK ELEMENTS
|
|
4
|
+
//
|
|
5
|
+
// This CSS comes from Stacks, our CSS & Pattern library for rapidly building
|
|
6
|
+
// Stack Overflow. For documentation of all these classes and how to contribute,
|
|
7
|
+
// visit https://stackoverflow.design/
|
|
8
|
+
//
|
|
9
|
+
// This is where all the magic happens.
|
|
10
|
+
//
|
|
11
|
+
// ============================================================================
|
|
12
|
+
// $ STATIC ELEMENTS
|
|
13
|
+
// The following items are elements which we DO NOT allow communities
|
|
14
|
+
// to modify via variables.
|
|
15
|
+
// ----------------------------------------------------------------------------
|
|
16
|
+
@import "base/reset.less";
|
|
17
|
+
@import "base/icons.less";
|
|
18
|
+
|
|
19
|
+
// -- COMPONENTS
|
|
20
|
+
@import "components/activity-indicator.less";
|
|
21
|
+
@import "components/avatars.less";
|
|
22
|
+
@import "components/badges.less";
|
|
23
|
+
@import "components/blank-states.less";
|
|
24
|
+
@import "components/breadcrumbs.less";
|
|
25
|
+
@import "components/button-groups.less";
|
|
26
|
+
@import "components/cards.less";
|
|
27
|
+
@import "components/code-blocks.less";
|
|
28
|
+
@import "components/collapsible.less";
|
|
29
|
+
@import "components/inputs.less";
|
|
30
|
+
@import "components/menu.less";
|
|
31
|
+
@import "components/modals.less";
|
|
32
|
+
@import "components/navigation.less";
|
|
33
|
+
@import "components/page-titles.less";
|
|
34
|
+
@import "components/popovers.less";
|
|
35
|
+
@import "components/post-summary.less";
|
|
36
|
+
@import "components/progress-bars.less";
|
|
37
|
+
@import "components/prose.less";
|
|
38
|
+
@import "components/spinner.less";
|
|
39
|
+
@import "components/tables.less";
|
|
40
|
+
@import "components/toggle-switches.less";
|
|
41
|
+
@import "components/topbar.less";
|
|
42
|
+
@import "components/uploader.less";
|
|
43
|
+
@import "components/user-cards.less";
|
|
44
|
+
@import "components/widget-static.less";
|
|
45
|
+
|
|
46
|
+
// -- LESS CONSTANTS AND MIXINS
|
|
47
|
+
@import "exports/exports.less";
|
|
48
|
+
|
|
49
|
+
// -- ATOMIC CLASSES
|
|
50
|
+
@import "atomic/borders.less";
|
|
51
|
+
@import "atomic/colors.less";
|
|
52
|
+
@import "atomic/flex.less";
|
|
53
|
+
@import "atomic/grid.less";
|
|
54
|
+
@import "atomic/spacing.less";
|
|
55
|
+
@import "atomic/typography.less";
|
|
56
|
+
@import "atomic/misc.less";
|
|
57
|
+
@import "atomic/width-height.less";
|
|
58
|
+
|
|
59
|
+
/* stylelint-disable */
|
|
60
|
+
#stacks-internals #screen-lg({
|
|
61
|
+
#stacks-internals-collect-large();
|
|
62
|
+
});
|
|
63
|
+
#stacks-internals #screen-md({
|
|
64
|
+
#stacks-internals-collect-medium();
|
|
65
|
+
});
|
|
66
|
+
#stacks-internals #screen-sm({
|
|
67
|
+
#stacks-internals-collect-small();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// We need printing styles to be last so they can override all other styles.
|
|
71
|
+
.print\:d-block {
|
|
72
|
+
@media print {
|
|
73
|
+
display: block !important;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
.print\:d-none {
|
|
78
|
+
@media print {
|
|
79
|
+
display: none !important;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/* stylelint-enable */
|
|
83
|
+
|
|
84
|
+
// -- CONFIG
|
|
85
|
+
@import "base/configuration-static.less";
|
|
86
|
+
@import "base/internals.less";
|
package/lib/css/stacks.less
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
//
|
|
2
|
-
// STACK OVERFLOW
|
|
3
|
-
// THE COMPLETE STANDALONE STACKS
|
|
4
|
-
//
|
|
5
|
-
// This CSS comes from Stacks, our CSS & Pattern library for rapidly building
|
|
6
|
-
// Stack Overflow. For documentation of all these classes and how to contribute,
|
|
7
|
-
// visit https://stackoverflow.design/
|
|
8
|
-
//
|
|
9
|
-
// This is where all the magic happens.
|
|
10
|
-
//
|
|
11
|
-
// ============================================================================
|
|
12
|
-
@import "stacks-static.less";
|
|
13
|
-
@import "stacks-dynamic.less";
|
|
1
|
+
//
|
|
2
|
+
// STACK OVERFLOW
|
|
3
|
+
// THE COMPLETE STANDALONE STACKS
|
|
4
|
+
//
|
|
5
|
+
// This CSS comes from Stacks, our CSS & Pattern library for rapidly building
|
|
6
|
+
// Stack Overflow. For documentation of all these classes and how to contribute,
|
|
7
|
+
// visit https://stackoverflow.design/
|
|
8
|
+
//
|
|
9
|
+
// This is where all the magic happens.
|
|
10
|
+
//
|
|
11
|
+
// ============================================================================
|
|
12
|
+
@import "stacks-static.less";
|
|
13
|
+
@import "stacks-dynamic.less";
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
// export all controllers *with helpers* so they can be bulk re-exported by the package entry point
|
|
2
|
-
export { ExpandableController } from './s-expandable-control';
|
|
3
|
-
export { hideModal, ModalController, showModal } from './s-modal';
|
|
4
|
-
export { TabListController } from './s-navigation-tablist';
|
|
5
|
-
export { attachPopover, detachPopover, hidePopover, BasePopoverController, PopoverController, showPopover } from './s-popover';
|
|
6
|
-
export { TableController } from './s-table';
|
|
7
|
-
export { setTooltipHtml, setTooltipText, TooltipController } from './s-tooltip';
|
|
1
|
+
// export all controllers *with helpers* so they can be bulk re-exported by the package entry point
|
|
2
|
+
export { ExpandableController } from './s-expandable-control';
|
|
3
|
+
export { hideModal, ModalController, showModal } from './s-modal';
|
|
4
|
+
export { TabListController } from './s-navigation-tablist';
|
|
5
|
+
export { attachPopover, detachPopover, hidePopover, BasePopoverController, PopoverController, showPopover } from './s-popover';
|
|
6
|
+
export { TableController } from './s-table';
|
|
7
|
+
export { setTooltipHtml, setTooltipText, TooltipController } from './s-tooltip';
|
|
8
8
|
export { UploaderController } from './s-uploader';
|
|
@@ -1,189 +1,189 @@
|
|
|
1
|
-
import * as Stacks from '../stacks';
|
|
2
|
-
|
|
3
|
-
// Radio buttons only trigger a change event when they're *checked*, but not when
|
|
4
|
-
// they're *unchecked*. Therefore, if we have an active `s-collapsible-control` in
|
|
5
|
-
// the document, we listen for change events on *all* radio buttons and find any
|
|
6
|
-
// other radio buttons in the same `name` group, triggering a custom event on all
|
|
7
|
-
// of them so the controller can re-evaluate.
|
|
8
|
-
//
|
|
9
|
-
// We're keeping a count of how many of these controllers are connected to the DOM,
|
|
10
|
-
// so only have this global listener when we actually need it.
|
|
11
|
-
const RADIO_OFF_EVENT = "s-expandable-control:radio-off";
|
|
12
|
-
|
|
13
|
-
function globalChangeListener(e: UIEvent) {
|
|
14
|
-
const target = e.target;
|
|
15
|
-
if (!(target instanceof HTMLInputElement) || target.nodeName !== "INPUT" || target.type !== "radio") {
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
document.querySelectorAll('input[type="radio"][name="' + target.name + '"]')
|
|
19
|
-
.forEach(function (other) {
|
|
20
|
-
if (other === e.target) {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
var customEvent;
|
|
24
|
-
try {
|
|
25
|
-
customEvent = new Event(RADIO_OFF_EVENT);
|
|
26
|
-
} catch (ex) {
|
|
27
|
-
// Internet Explorer
|
|
28
|
-
customEvent = document.createEvent("Event");
|
|
29
|
-
customEvent.initEvent(RADIO_OFF_EVENT, true, true);
|
|
30
|
-
}
|
|
31
|
-
other.dispatchEvent(customEvent);
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
var refCount = 0;
|
|
36
|
-
function globalChangeListenerRequired(required: boolean) {
|
|
37
|
-
if (required) {
|
|
38
|
-
refCount++;
|
|
39
|
-
if (refCount === 1) {
|
|
40
|
-
document.body.addEventListener("change", globalChangeListener as EventListener);
|
|
41
|
-
}
|
|
42
|
-
} else {
|
|
43
|
-
refCount--;
|
|
44
|
-
if (refCount === 0) {
|
|
45
|
-
document.body.removeEventListener("change", globalChangeListener as EventListener);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export class ExpandableController extends Stacks.StacksController {
|
|
51
|
-
private isCollapsed!: () => boolean;
|
|
52
|
-
private events!: string[];
|
|
53
|
-
private isCheckable!: boolean;
|
|
54
|
-
private isRadio!: boolean;
|
|
55
|
-
private lastKeydownClickTimestamp: number = 0;
|
|
56
|
-
|
|
57
|
-
initialize() {
|
|
58
|
-
if (this.element.nodeName === "INPUT" && ["radio", "checkbox"].indexOf((<HTMLInputElement>this.element).type) >= 0) {
|
|
59
|
-
this.isCollapsed = this._isCollapsedForCheckable;
|
|
60
|
-
this.events = ["change", RADIO_OFF_EVENT];
|
|
61
|
-
this.isCheckable = true;
|
|
62
|
-
this.isRadio = (<HTMLInputElement>this.element).type === "radio";
|
|
63
|
-
} else {
|
|
64
|
-
this.isCollapsed = this._isCollapsedForClickable;
|
|
65
|
-
this.events = ["click", "keydown"];
|
|
66
|
-
}
|
|
67
|
-
this.listener = this.listener.bind(this);
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
// for non-checkable elements, the initial source of truth is the collapsed/expanded
|
|
72
|
-
// state of the controlled element (unless the element doesn't exist)
|
|
73
|
-
_isCollapsedForClickable() {
|
|
74
|
-
var cc = this.controlledCollapsibles;
|
|
75
|
-
// the element is considered collapsed if *any* target element is collapsed
|
|
76
|
-
return cc.length > 0 ? !cc.every(element => element.classList.contains("is-expanded")) : this.element.getAttribute("aria-expanded") === "false";
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
// for checkable elements, the initial source of truth is the checked state
|
|
80
|
-
_isCollapsedForCheckable() {
|
|
81
|
-
return !(<HTMLInputElement>this.element).checked;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
get controlledCollapsibles() {
|
|
86
|
-
const attr = this.element.getAttribute("aria-controls");
|
|
87
|
-
if (!attr) {
|
|
88
|
-
throw `[aria-controls="targetId1 ... targetIdN"] attribute required`;
|
|
89
|
-
}
|
|
90
|
-
const result = attr.split(/\s+/g)
|
|
91
|
-
.map(s => document.getElementById(s))
|
|
92
|
-
.filter((e): e is HTMLElement => !!e);
|
|
93
|
-
if (!result.length) {
|
|
94
|
-
throw "couldn't find controls"
|
|
95
|
-
}
|
|
96
|
-
return result;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
_dispatchShowHideEvent(isShow: boolean) {
|
|
100
|
-
this.triggerEvent(isShow ? "show" : "hide");
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
_toggleClass(doAdd: boolean) {
|
|
104
|
-
if (!this.data.has("toggle-class")) {
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
var cl = this.element.classList;
|
|
108
|
-
var toggleClass = this.data.get("toggle-class");
|
|
109
|
-
if (!toggleClass) {
|
|
110
|
-
throw "couldn't find toggle class"
|
|
111
|
-
}
|
|
112
|
-
toggleClass.split(/\s+/).forEach(function (cls) {
|
|
113
|
-
cl.toggle(cls, !!doAdd);
|
|
114
|
-
});
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
listener(e: Event) {
|
|
118
|
-
var newCollapsed;
|
|
119
|
-
if (this.isCheckable) {
|
|
120
|
-
newCollapsed = !(<HTMLInputElement>this.element).checked;
|
|
121
|
-
} else {
|
|
122
|
-
if (e.type == "keydown" && (e instanceof KeyboardEvent && e.keyCode != 13 && e.keyCode != 32)) {
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
if (e.target !== e.currentTarget && ["A", "BUTTON"].indexOf((<HTMLElement>e.target).nodeName) >= 0) {
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
e.preventDefault();
|
|
130
|
-
|
|
131
|
-
// Prevent "click" events from toggling the expandable within 300ms of "keydown".
|
|
132
|
-
// e.preventDefault() should have done the same, but https://bugzilla.mozilla.org/show_bug.cgi?id=1487102
|
|
133
|
-
// doesn't guarantee it.
|
|
134
|
-
if (e.type == "keydown") {
|
|
135
|
-
this.lastKeydownClickTimestamp = Date.now();
|
|
136
|
-
} else if (e.type == "click" && Date.now() - this.lastKeydownClickTimestamp < 300) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
newCollapsed = this.element.getAttribute("aria-expanded") === "true";
|
|
140
|
-
if (e.type === "click") {
|
|
141
|
-
(<HTMLInputElement>this.element).blur();
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
this.element.setAttribute("aria-expanded", newCollapsed ? "false" : "true");
|
|
145
|
-
for (let controlledElement of this.controlledCollapsibles) {
|
|
146
|
-
controlledElement.classList.toggle("is-expanded", !newCollapsed);
|
|
147
|
-
}
|
|
148
|
-
this._dispatchShowHideEvent(!newCollapsed);
|
|
149
|
-
this._toggleClass(!newCollapsed);
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
connect() {
|
|
153
|
-
this.events.forEach(e => {
|
|
154
|
-
this.element.addEventListener(e, this.listener);
|
|
155
|
-
}, this);
|
|
156
|
-
|
|
157
|
-
if (this.isRadio) {
|
|
158
|
-
globalChangeListenerRequired(true);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// synchronize state -- in all cases, this means setting the correct `aria-expanded`
|
|
162
|
-
// attribute; for checkable controls this also means setting the `is-collapsed` class
|
|
163
|
-
this.element.setAttribute("aria-expanded", this.isCollapsed() ? "false" : "true");
|
|
164
|
-
if (this.isCheckable) {
|
|
165
|
-
var cc = this.controlledCollapsibles;
|
|
166
|
-
if (cc.length) {
|
|
167
|
-
var expected = !this.isCollapsed();
|
|
168
|
-
// if any element does not match the expected state, set them all to the expected state
|
|
169
|
-
if (cc.some(element => element.classList.contains("is-expanded") !== expected)) {
|
|
170
|
-
for (let controlledElement of this.controlledCollapsibles) {
|
|
171
|
-
controlledElement.classList.toggle("is-expanded", expected);
|
|
172
|
-
}
|
|
173
|
-
this._dispatchShowHideEvent(expected);
|
|
174
|
-
this._toggleClass(expected);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
disconnect() {
|
|
181
|
-
this.events.forEach(e => {
|
|
182
|
-
this.element.removeEventListener(e, this.listener);
|
|
183
|
-
}, this);
|
|
184
|
-
|
|
185
|
-
if (this.isRadio) {
|
|
186
|
-
globalChangeListenerRequired(false);
|
|
187
|
-
}
|
|
188
|
-
};
|
|
1
|
+
import * as Stacks from '../stacks';
|
|
2
|
+
|
|
3
|
+
// Radio buttons only trigger a change event when they're *checked*, but not when
|
|
4
|
+
// they're *unchecked*. Therefore, if we have an active `s-collapsible-control` in
|
|
5
|
+
// the document, we listen for change events on *all* radio buttons and find any
|
|
6
|
+
// other radio buttons in the same `name` group, triggering a custom event on all
|
|
7
|
+
// of them so the controller can re-evaluate.
|
|
8
|
+
//
|
|
9
|
+
// We're keeping a count of how many of these controllers are connected to the DOM,
|
|
10
|
+
// so only have this global listener when we actually need it.
|
|
11
|
+
const RADIO_OFF_EVENT = "s-expandable-control:radio-off";
|
|
12
|
+
|
|
13
|
+
function globalChangeListener(e: UIEvent) {
|
|
14
|
+
const target = e.target;
|
|
15
|
+
if (!(target instanceof HTMLInputElement) || target.nodeName !== "INPUT" || target.type !== "radio") {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
document.querySelectorAll('input[type="radio"][name="' + target.name + '"]')
|
|
19
|
+
.forEach(function (other) {
|
|
20
|
+
if (other === e.target) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
var customEvent;
|
|
24
|
+
try {
|
|
25
|
+
customEvent = new Event(RADIO_OFF_EVENT);
|
|
26
|
+
} catch (ex) {
|
|
27
|
+
// Internet Explorer
|
|
28
|
+
customEvent = document.createEvent("Event");
|
|
29
|
+
customEvent.initEvent(RADIO_OFF_EVENT, true, true);
|
|
30
|
+
}
|
|
31
|
+
other.dispatchEvent(customEvent);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
var refCount = 0;
|
|
36
|
+
function globalChangeListenerRequired(required: boolean) {
|
|
37
|
+
if (required) {
|
|
38
|
+
refCount++;
|
|
39
|
+
if (refCount === 1) {
|
|
40
|
+
document.body.addEventListener("change", globalChangeListener as EventListener);
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
refCount--;
|
|
44
|
+
if (refCount === 0) {
|
|
45
|
+
document.body.removeEventListener("change", globalChangeListener as EventListener);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class ExpandableController extends Stacks.StacksController {
|
|
51
|
+
private isCollapsed!: () => boolean;
|
|
52
|
+
private events!: string[];
|
|
53
|
+
private isCheckable!: boolean;
|
|
54
|
+
private isRadio!: boolean;
|
|
55
|
+
private lastKeydownClickTimestamp: number = 0;
|
|
56
|
+
|
|
57
|
+
initialize() {
|
|
58
|
+
if (this.element.nodeName === "INPUT" && ["radio", "checkbox"].indexOf((<HTMLInputElement>this.element).type) >= 0) {
|
|
59
|
+
this.isCollapsed = this._isCollapsedForCheckable;
|
|
60
|
+
this.events = ["change", RADIO_OFF_EVENT];
|
|
61
|
+
this.isCheckable = true;
|
|
62
|
+
this.isRadio = (<HTMLInputElement>this.element).type === "radio";
|
|
63
|
+
} else {
|
|
64
|
+
this.isCollapsed = this._isCollapsedForClickable;
|
|
65
|
+
this.events = ["click", "keydown"];
|
|
66
|
+
}
|
|
67
|
+
this.listener = this.listener.bind(this);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
// for non-checkable elements, the initial source of truth is the collapsed/expanded
|
|
72
|
+
// state of the controlled element (unless the element doesn't exist)
|
|
73
|
+
_isCollapsedForClickable() {
|
|
74
|
+
var cc = this.controlledCollapsibles;
|
|
75
|
+
// the element is considered collapsed if *any* target element is collapsed
|
|
76
|
+
return cc.length > 0 ? !cc.every(element => element.classList.contains("is-expanded")) : this.element.getAttribute("aria-expanded") === "false";
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// for checkable elements, the initial source of truth is the checked state
|
|
80
|
+
_isCollapsedForCheckable() {
|
|
81
|
+
return !(<HTMLInputElement>this.element).checked;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
get controlledCollapsibles() {
|
|
86
|
+
const attr = this.element.getAttribute("aria-controls");
|
|
87
|
+
if (!attr) {
|
|
88
|
+
throw `[aria-controls="targetId1 ... targetIdN"] attribute required`;
|
|
89
|
+
}
|
|
90
|
+
const result = attr.split(/\s+/g)
|
|
91
|
+
.map(s => document.getElementById(s))
|
|
92
|
+
.filter((e): e is HTMLElement => !!e);
|
|
93
|
+
if (!result.length) {
|
|
94
|
+
throw "couldn't find controls"
|
|
95
|
+
}
|
|
96
|
+
return result;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
_dispatchShowHideEvent(isShow: boolean) {
|
|
100
|
+
this.triggerEvent(isShow ? "show" : "hide");
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
_toggleClass(doAdd: boolean) {
|
|
104
|
+
if (!this.data.has("toggle-class")) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
var cl = this.element.classList;
|
|
108
|
+
var toggleClass = this.data.get("toggle-class");
|
|
109
|
+
if (!toggleClass) {
|
|
110
|
+
throw "couldn't find toggle class"
|
|
111
|
+
}
|
|
112
|
+
toggleClass.split(/\s+/).forEach(function (cls) {
|
|
113
|
+
cl.toggle(cls, !!doAdd);
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
listener(e: Event) {
|
|
118
|
+
var newCollapsed;
|
|
119
|
+
if (this.isCheckable) {
|
|
120
|
+
newCollapsed = !(<HTMLInputElement>this.element).checked;
|
|
121
|
+
} else {
|
|
122
|
+
if (e.type == "keydown" && (e instanceof KeyboardEvent && e.keyCode != 13 && e.keyCode != 32)) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (e.target !== e.currentTarget && ["A", "BUTTON"].indexOf((<HTMLElement>e.target).nodeName) >= 0) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
e.preventDefault();
|
|
130
|
+
|
|
131
|
+
// Prevent "click" events from toggling the expandable within 300ms of "keydown".
|
|
132
|
+
// e.preventDefault() should have done the same, but https://bugzilla.mozilla.org/show_bug.cgi?id=1487102
|
|
133
|
+
// doesn't guarantee it.
|
|
134
|
+
if (e.type == "keydown") {
|
|
135
|
+
this.lastKeydownClickTimestamp = Date.now();
|
|
136
|
+
} else if (e.type == "click" && Date.now() - this.lastKeydownClickTimestamp < 300) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
newCollapsed = this.element.getAttribute("aria-expanded") === "true";
|
|
140
|
+
if (e.type === "click") {
|
|
141
|
+
(<HTMLInputElement>this.element).blur();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
this.element.setAttribute("aria-expanded", newCollapsed ? "false" : "true");
|
|
145
|
+
for (let controlledElement of this.controlledCollapsibles) {
|
|
146
|
+
controlledElement.classList.toggle("is-expanded", !newCollapsed);
|
|
147
|
+
}
|
|
148
|
+
this._dispatchShowHideEvent(!newCollapsed);
|
|
149
|
+
this._toggleClass(!newCollapsed);
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
connect() {
|
|
153
|
+
this.events.forEach(e => {
|
|
154
|
+
this.element.addEventListener(e, this.listener);
|
|
155
|
+
}, this);
|
|
156
|
+
|
|
157
|
+
if (this.isRadio) {
|
|
158
|
+
globalChangeListenerRequired(true);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// synchronize state -- in all cases, this means setting the correct `aria-expanded`
|
|
162
|
+
// attribute; for checkable controls this also means setting the `is-collapsed` class
|
|
163
|
+
this.element.setAttribute("aria-expanded", this.isCollapsed() ? "false" : "true");
|
|
164
|
+
if (this.isCheckable) {
|
|
165
|
+
var cc = this.controlledCollapsibles;
|
|
166
|
+
if (cc.length) {
|
|
167
|
+
var expected = !this.isCollapsed();
|
|
168
|
+
// if any element does not match the expected state, set them all to the expected state
|
|
169
|
+
if (cc.some(element => element.classList.contains("is-expanded") !== expected)) {
|
|
170
|
+
for (let controlledElement of this.controlledCollapsibles) {
|
|
171
|
+
controlledElement.classList.toggle("is-expanded", expected);
|
|
172
|
+
}
|
|
173
|
+
this._dispatchShowHideEvent(expected);
|
|
174
|
+
this._toggleClass(expected);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
disconnect() {
|
|
181
|
+
this.events.forEach(e => {
|
|
182
|
+
this.element.removeEventListener(e, this.listener);
|
|
183
|
+
}, this);
|
|
184
|
+
|
|
185
|
+
if (this.isRadio) {
|
|
186
|
+
globalChangeListenerRequired(false);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
189
|
}
|