@stackoverflow/stacks 0.76.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.
Files changed (86) hide show
  1. package/LICENSE.MD +9 -9
  2. package/README.md +47 -47
  3. package/dist/controllers/index.d.ts +7 -7
  4. package/dist/controllers/s-expandable-control.d.ts +17 -17
  5. package/dist/controllers/s-modal.d.ts +97 -97
  6. package/dist/controllers/s-navigation-tablist.d.ts +36 -36
  7. package/dist/controllers/s-popover.d.ts +155 -155
  8. package/dist/controllers/s-table.d.ts +8 -8
  9. package/dist/controllers/s-tooltip.d.ts +82 -82
  10. package/dist/controllers/s-uploader.d.ts +48 -48
  11. package/dist/css/stacks.css +4321 -2635
  12. package/dist/css/stacks.min.css +1 -1
  13. package/dist/index.d.ts +3 -3
  14. package/dist/js/stacks.js +1978 -1978
  15. package/dist/js/stacks.min.js +1 -1
  16. package/dist/stacks.d.ts +21 -21
  17. package/lib/css/atomic/{_stacks-borders.less → borders.less} +378 -378
  18. package/lib/css/atomic/{_stacks-colors.less → colors.less} +209 -209
  19. package/lib/css/atomic/{_stacks-flex.less → flex.less} +375 -374
  20. package/lib/css/atomic/{_stacks-grid.less → grid.less} +174 -174
  21. package/lib/css/atomic/{_stacks-misc.less → misc.less} +343 -343
  22. package/lib/css/atomic/spacing.less +314 -0
  23. package/lib/css/atomic/{_stacks-typography.less → typography.less} +273 -273
  24. package/lib/css/atomic/width-height.less +194 -0
  25. package/lib/css/base/{_stacks-body.less → body.less} +44 -44
  26. package/lib/css/base/{_stacks-configuration-static.less → configuration-static.less} +61 -59
  27. package/lib/css/base/{_stacks-icons.less → icons.less} +20 -20
  28. package/lib/css/base/{_stacks-internals.less → internals.less} +220 -220
  29. package/lib/css/base/{_stacks-reset-meyer.less → reset-meyer.less} +64 -64
  30. package/lib/css/base/{_stacks-reset-normalize.less → reset-normalize.less} +449 -449
  31. package/lib/css/base/{_stacks-reset.less → reset.less} +20 -20
  32. package/lib/css/components/{_stacks-activity-indicator.less → activity-indicator.less} +45 -45
  33. package/lib/css/components/{_stacks-avatars.less → avatars.less} +189 -189
  34. package/lib/css/components/{_stacks-badges.less → badges.less} +209 -209
  35. package/lib/css/components/{_stacks-banners.less → banners.less} +80 -80
  36. package/lib/css/components/{_stacks-blank-states.less → blank-states.less} +26 -26
  37. package/lib/css/components/{_stacks-breadcrumbs.less → breadcrumbs.less} +44 -44
  38. package/lib/css/components/{_stacks-button-groups.less → button-groups.less} +104 -104
  39. package/lib/css/components/{_stacks-buttons.less → buttons.less} +665 -658
  40. package/lib/css/components/{_stacks-cards.less → cards.less} +44 -44
  41. package/lib/css/components/{_stacks-code-blocks.less → code-blocks.less} +130 -130
  42. package/lib/css/components/{_stacks-collapsible.less → collapsible.less} +104 -104
  43. package/lib/css/components/{_stacks-inputs.less → inputs.less} +728 -728
  44. package/lib/css/components/{_stacks-link-previews.less → link-previews.less} +136 -136
  45. package/lib/css/components/{_stacks-links.less → links.less} +218 -218
  46. package/lib/css/components/{_stacks-menu.less → menu.less} +47 -47
  47. package/lib/css/components/{_stacks-modals.less → modals.less} +133 -133
  48. package/lib/css/components/{_stacks-navigation.less → navigation.less} +146 -146
  49. package/lib/css/components/{_stacks-notices.less → notices.less} +233 -233
  50. package/lib/css/components/{_stacks-page-titles.less → page-titles.less} +60 -60
  51. package/lib/css/components/{_stacks-pagination.less → pagination.less} +55 -55
  52. package/lib/css/components/{_stacks-popovers.less → popovers.less} +197 -197
  53. package/lib/css/components/{_stacks-post-summary.less → post-summary.less} +425 -425
  54. package/lib/css/components/{_stacks-progress-bars.less → progress-bars.less} +330 -331
  55. package/lib/css/components/{_stacks-prose.less → prose.less} +503 -503
  56. package/lib/css/components/{_stacks-spinner.less → spinner.less} +107 -107
  57. package/lib/css/components/{_stacks-tables.less → tables.less} +341 -341
  58. package/lib/css/components/{_stacks-tags.less → tags.less} +236 -236
  59. package/lib/css/components/{_stacks-toggle-switches.less → toggle-switches.less} +144 -144
  60. package/lib/css/components/{_stacks-topbar.less → topbar.less} +427 -425
  61. package/lib/css/components/{_stacks-uploader.less → uploader.less} +210 -210
  62. package/lib/css/components/{_stacks-user-cards.less → user-cards.less} +169 -169
  63. package/lib/css/components/{_stacks-widget-dynamic.less → widget-dynamic.less} +33 -33
  64. package/lib/css/components/{_stacks-widget-static.less → widget-static.less} +273 -272
  65. package/lib/css/exports/{_stacks-constants-colors.less → constants-colors.less} +1092 -1112
  66. package/lib/css/exports/constants-helpers.less +108 -0
  67. package/lib/css/exports/{_stacks-constants-type.less → constants-type.less} +153 -152
  68. package/lib/css/exports/{_stacks-exports.less → exports.less} +15 -15
  69. package/lib/css/exports/{_stacks-mixins.less → mixins.less} +237 -220
  70. package/lib/css/stacks-dynamic.less +35 -35
  71. package/lib/css/stacks-static.less +86 -86
  72. package/lib/ts/controllers/index.ts +7 -7
  73. package/lib/ts/controllers/s-expandable-control.ts +188 -188
  74. package/lib/ts/controllers/s-modal.ts +321 -321
  75. package/lib/ts/controllers/s-navigation-tablist.ts +117 -117
  76. package/lib/ts/controllers/s-popover.ts +547 -547
  77. package/lib/ts/controllers/s-table.ts +220 -220
  78. package/lib/ts/controllers/s-tooltip.ts +246 -246
  79. package/lib/ts/controllers/s-uploader.ts +172 -172
  80. package/lib/ts/index.ts +20 -20
  81. package/lib/ts/stacks.ts +88 -88
  82. package/lib/tsconfig.json +13 -13
  83. package/package.json +87 -87
  84. package/lib/css/atomic/_stacks-spacing.less +0 -168
  85. package/lib/css/atomic/_stacks-width-height.less +0 -195
  86. package/lib/css/exports/_stacks-constants-helpers.less +0 -139
@@ -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/_stacks-reset.less";
17
- @import "base/_stacks-icons.less";
18
-
19
- // -- COMPONENTS
20
- @import "components/_stacks-activity-indicator.less";
21
- @import "components/_stacks-avatars.less";
22
- @import "components/_stacks-badges.less";
23
- @import "components/_stacks-blank-states.less";
24
- @import "components/_stacks-breadcrumbs.less";
25
- @import "components/_stacks-button-groups.less";
26
- @import "components/_stacks-cards.less";
27
- @import "components/_stacks-code-blocks.less";
28
- @import "components/_stacks-collapsible.less";
29
- @import "components/_stacks-inputs.less";
30
- @import "components/_stacks-menu.less";
31
- @import "components/_stacks-modals.less";
32
- @import "components/_stacks-navigation.less";
33
- @import "components/_stacks-page-titles.less";
34
- @import "components/_stacks-popovers.less";
35
- @import "components/_stacks-post-summary.less";
36
- @import "components/_stacks-progress-bars.less";
37
- @import "components/_stacks-prose.less";
38
- @import "components/_stacks-spinner.less";
39
- @import "components/_stacks-tables.less";
40
- @import "components/_stacks-toggle-switches.less";
41
- @import "components/_stacks-topbar.less";
42
- @import "components/_stacks-uploader.less";
43
- @import "components/_stacks-user-cards.less";
44
- @import "components/_stacks-widget-static.less";
45
-
46
- // -- LESS CONSTANTS AND MIXINS
47
- @import "exports/_stacks-exports.less";
48
-
49
- // -- ATOMIC CLASSES
50
- @import "atomic/_stacks-borders.less";
51
- @import "atomic/_stacks-colors.less";
52
- @import "atomic/_stacks-flex.less";
53
- @import "atomic/_stacks-grid.less";
54
- @import "atomic/_stacks-spacing.less";
55
- @import "atomic/_stacks-typography.less";
56
- @import "atomic/_stacks-misc.less";
57
- @import "atomic/_stacks-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/_stacks-configuration-static.less";
86
- @import "base/_stacks-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";
@@ -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
  }