@stackoverflow/stacks 3.0.0-beta.29 → 3.0.0-beta.30
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/css/stacks.css +680 -0
- package/dist/css/stacks.min.css +1 -1
- package/lib/exports/color-sets.less +26 -0
- package/package.json +1 -1
- package/dist/components/expandable/expandable.d.ts +0 -17
- package/dist/components/uploader/uploader.d.ts +0 -48
- package/lib/components/breadcrumbs/breadcrumbs.less +0 -41
- package/lib/components/button-group/button-group.less +0 -82
- package/lib/components/card/card.less +0 -37
- package/lib/components/expandable/expandable.less +0 -119
- package/lib/components/expandable/expandable.ts +0 -238
- package/lib/components/link-preview/link-preview.less +0 -140
- package/lib/components/page-title/page-title.less +0 -51
- package/lib/components/progress-bar/progress-bar.less +0 -292
- package/lib/components/topbar/topbar.less +0 -553
- package/lib/components/uploader/uploader.less +0 -205
- package/lib/components/uploader/uploader.ts +0 -207
|
@@ -591,6 +591,27 @@
|
|
|
591
591
|
default: var(--black);
|
|
592
592
|
}
|
|
593
593
|
|
|
594
|
+
// brand colors
|
|
595
|
+
.set-brand() {
|
|
596
|
+
default: #FF5E00;
|
|
597
|
+
black: #201C1D;
|
|
598
|
+
off-white: #F0EFEE;
|
|
599
|
+
blue-light: #C6D1E1;
|
|
600
|
+
blue: #5074EF;
|
|
601
|
+
blue-dark: #00165E;
|
|
602
|
+
brown-light: #998B7A;
|
|
603
|
+
green: #86AF25;
|
|
604
|
+
green-dark: #263603;
|
|
605
|
+
orange-medium: #6E1527;
|
|
606
|
+
orange-dark: #31070F;
|
|
607
|
+
pink: #F39FFF;
|
|
608
|
+
pink-dark: #4D1955;
|
|
609
|
+
purple: #9D9CFF;
|
|
610
|
+
purple-dark: #390A91;
|
|
611
|
+
yellow: #FFCC00;
|
|
612
|
+
yellow-dark: #423101;
|
|
613
|
+
}
|
|
614
|
+
|
|
594
615
|
// Color sets
|
|
595
616
|
.sets-mode() {
|
|
596
617
|
light: .sets-light();
|
|
@@ -612,6 +633,7 @@
|
|
|
612
633
|
gold: .set-gold();
|
|
613
634
|
silver: .set-silver();
|
|
614
635
|
bronze: .set-bronze();
|
|
636
|
+
brand: .set-brand();
|
|
615
637
|
}
|
|
616
638
|
|
|
617
639
|
.sets-dark() {
|
|
@@ -682,6 +704,7 @@
|
|
|
682
704
|
focus-ring: .set-focus-ring();
|
|
683
705
|
highlight: .set-highlight();
|
|
684
706
|
scrollbar: .set-scrollbar();
|
|
707
|
+
brand: .set-brand();
|
|
685
708
|
}
|
|
686
709
|
|
|
687
710
|
.sets-utility-dark() {
|
|
@@ -692,6 +715,7 @@
|
|
|
692
715
|
focus-ring: .set-focus-ring-dark();
|
|
693
716
|
highlight: .set-highlight-dark();
|
|
694
717
|
scrollbar: .set-scrollbar-dark();
|
|
718
|
+
brand: .set-brand();
|
|
695
719
|
}
|
|
696
720
|
|
|
697
721
|
.sets-utility-hc() {
|
|
@@ -702,6 +726,7 @@
|
|
|
702
726
|
focus-ring: .set-focus-ring-hc();
|
|
703
727
|
highlight: .set-highlight-hc();
|
|
704
728
|
scrollbar: .set-scrollbar-hc();
|
|
729
|
+
brand: .set-brand();
|
|
705
730
|
}
|
|
706
731
|
|
|
707
732
|
.sets-utility-dark-hc() {
|
|
@@ -712,6 +737,7 @@
|
|
|
712
737
|
focus-ring: .set-focus-ring-hc();
|
|
713
738
|
highlight: .set-highlight-hc-dark();
|
|
714
739
|
scrollbar: .set-scrollbar-hc-dark();
|
|
740
|
+
brand: .set-brand();
|
|
715
741
|
}
|
|
716
742
|
|
|
717
743
|
// Theme values
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stackoverflow/stacks",
|
|
3
3
|
"description": "Stack Overflow’s CSS and Design Pattern Library. Stacks is an atomic CSS library with classes and components for rapidly building Stack Overflow.",
|
|
4
|
-
"version": "3.0.0-beta.
|
|
4
|
+
"version": "3.0.0-beta.30",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
7
7
|
"lib",
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import * as Stacks from "../../stacks";
|
|
2
|
-
export declare class ExpandableController extends Stacks.StacksController {
|
|
3
|
-
private isCollapsed;
|
|
4
|
-
private events;
|
|
5
|
-
private isCheckable;
|
|
6
|
-
private isRadio;
|
|
7
|
-
private lastKeydownClickTimestamp;
|
|
8
|
-
initialize(): void;
|
|
9
|
-
_isCollapsedForClickable(): boolean;
|
|
10
|
-
_isCollapsedForCheckable(): boolean;
|
|
11
|
-
get controlledExpandables(): HTMLElement[];
|
|
12
|
-
_dispatchShowHideEvent(isShow: boolean): void;
|
|
13
|
-
_toggleClass(doAdd: boolean): void;
|
|
14
|
-
listener(e: Event): void;
|
|
15
|
-
connect(): void;
|
|
16
|
-
disconnect(): void;
|
|
17
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import * as Stacks from "../../stacks";
|
|
2
|
-
export declare class UploaderController extends Stacks.StacksController {
|
|
3
|
-
static targets: string[];
|
|
4
|
-
readonly inputTarget: HTMLInputElement;
|
|
5
|
-
readonly previewsTarget: HTMLElement;
|
|
6
|
-
readonly uploaderTarget: HTMLElement;
|
|
7
|
-
private boundDragEnter;
|
|
8
|
-
private boundDragLeave;
|
|
9
|
-
private static readonly FILE_DISPLAY_LIMIT;
|
|
10
|
-
private static readonly MAX_FILE_SIZE;
|
|
11
|
-
connect(): void;
|
|
12
|
-
disconnect(): void;
|
|
13
|
-
/**
|
|
14
|
-
* Handles rendering the file preview state on input change
|
|
15
|
-
*/
|
|
16
|
-
handleInput(): void;
|
|
17
|
-
/**
|
|
18
|
-
* Resets the Uploader to initial state
|
|
19
|
-
*/
|
|
20
|
-
reset(): void;
|
|
21
|
-
/**
|
|
22
|
-
* Set hide/show and disabled state on elements depending on preview state
|
|
23
|
-
* @param {boolean} shouldPreview - Uploader is entering a preview state
|
|
24
|
-
*/
|
|
25
|
-
private handleVisible;
|
|
26
|
-
/**
|
|
27
|
-
* Adds a DOM element to preview a selected file
|
|
28
|
-
* @param {FilePreview} file
|
|
29
|
-
*/
|
|
30
|
-
private addFilePreview;
|
|
31
|
-
/**
|
|
32
|
-
* Toggles display and disabled state for select elements on valid input
|
|
33
|
-
* @param {boolean} active - Uploader is in active state (typically on 'dragenter')
|
|
34
|
-
*/
|
|
35
|
-
private handleUploaderActive;
|
|
36
|
-
/**
|
|
37
|
-
* Converts the file data into a data URL
|
|
38
|
-
* @param {File} file
|
|
39
|
-
* @returns an object containing a FilePreview object
|
|
40
|
-
*/
|
|
41
|
-
private fileToDataURL;
|
|
42
|
-
/**
|
|
43
|
-
* Gets an array of FilePreviews from a FileList
|
|
44
|
-
* @param {FileList|[]} files
|
|
45
|
-
* @returns an array of FilePreview objects from a FileList
|
|
46
|
-
*/
|
|
47
|
-
private getDataURLs;
|
|
48
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
.s-breadcrumbs {
|
|
2
|
-
--_br-divider-px: var(--su4);
|
|
3
|
-
--_br-link-fc: var(--fc-light);
|
|
4
|
-
|
|
5
|
-
// CONTEXTUAL STYLES
|
|
6
|
-
#stacks-internals #screen-sm({
|
|
7
|
-
--_br-divider-px: var(--su2);
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
// CHILD ELEMENTS
|
|
11
|
-
& &--divider {
|
|
12
|
-
.highcontrast-mode({
|
|
13
|
-
color: var(--fc-light);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
margin-left: var(--_br-divider-px);
|
|
17
|
-
margin-right: var(--_br-divider-px);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
& &--item {
|
|
21
|
-
align-items: center;
|
|
22
|
-
display: flex;
|
|
23
|
-
flex-wrap: nowrap;
|
|
24
|
-
margin-bottom: var(--su2);
|
|
25
|
-
margin-top: var(--su2);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
& &--link {
|
|
29
|
-
color: var(--_br-link-fc);
|
|
30
|
-
|
|
31
|
-
&:hover {
|
|
32
|
-
--_br-link-fc: var(--fc-medium);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
align-items: flex-start;
|
|
37
|
-
color: var(--black-300);
|
|
38
|
-
display: flex;
|
|
39
|
-
flex-wrap: wrap;
|
|
40
|
-
font-size: var(--fs-caption);
|
|
41
|
-
}
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
.s-btn-group {
|
|
2
|
-
// CONTEXTUAL STYLES
|
|
3
|
-
#stacks-internals #screen-sm({
|
|
4
|
-
.s-btn {
|
|
5
|
-
--_bu-px: 0.4em;
|
|
6
|
-
|
|
7
|
-
&.s-btn__dropdown {
|
|
8
|
-
padding-right: 1.2em;
|
|
9
|
-
|
|
10
|
-
&:after {
|
|
11
|
-
right: var(--_bu-px);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}, @force-selector: true);
|
|
16
|
-
|
|
17
|
-
// CHILD ELEMENTS
|
|
18
|
-
form {
|
|
19
|
-
display: flex;
|
|
20
|
-
margin-right: var(--sun1);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// --_bu-py values set below to ensure btn-group height matches same-sized button height
|
|
24
|
-
// See https://github.com/StackEng/StackOverflow/pull/18992#pullrequestreview-1947490680
|
|
25
|
-
.s-btn {
|
|
26
|
-
--_bu-br: var(--br-md);
|
|
27
|
-
--_bu-bc-hover: transparent;
|
|
28
|
-
--_bu-px: calc(var(--su12) - var(--su1)); // 11px
|
|
29
|
-
--_bu-py: calc(var(--su6) + 0.65px); // 6.65px
|
|
30
|
-
|
|
31
|
-
&.s-btn__xs {
|
|
32
|
-
--_bu-px: calc(var(--su8) - var(--su1)); // 7px
|
|
33
|
-
--_bu-py: calc(var(--su2) + 0.9px); // 2.9px
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
&.s-btn__sm {
|
|
37
|
-
--_bu-px: calc(var(--su8) + var(--su1)); // 9px
|
|
38
|
-
--_bu-py: calc(var(--su4) + (var(--su2) - 0.15px)); // 5.85px
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
&.s-btn__md {
|
|
42
|
-
--_bu-px: var(--su12);
|
|
43
|
-
--_bu-py: calc(var(--su8) + 0.15px); // 8.15px
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
&.is-selected,
|
|
47
|
-
&--radio:checked + .s-btn {
|
|
48
|
-
font-weight: bold;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
.s-btn--badge {
|
|
52
|
-
// set negative margins so button height isn't affect by badge
|
|
53
|
-
margin-bottom: -100%;
|
|
54
|
-
margin-top: -100%;
|
|
55
|
-
font-weight: normal;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
& .s-btn--text {
|
|
59
|
-
&:before {
|
|
60
|
-
content: attr(data-text);
|
|
61
|
-
font-weight: bold;
|
|
62
|
-
height: 0;
|
|
63
|
-
pointer-events: none;
|
|
64
|
-
user-select: none;
|
|
65
|
-
visibility: hidden;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
display: inline-flex;
|
|
69
|
-
flex-direction: column;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
font-weight: 400;
|
|
73
|
-
white-space: nowrap; // When the buttons wrap, they get super tall and mess up the whole layout
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// STATIC COMPONENT STYLES
|
|
77
|
-
border: var(--su1) solid var(--black-300);
|
|
78
|
-
border-radius: var(--br-md);
|
|
79
|
-
display: inline-flex;
|
|
80
|
-
flex-wrap: wrap;
|
|
81
|
-
padding: var(--su3);
|
|
82
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
.s-card {
|
|
2
|
-
--_ca-bc: var(--bc-medium);
|
|
3
|
-
|
|
4
|
-
// CONTEXTUAL STYLES
|
|
5
|
-
a& {
|
|
6
|
-
text-decoration: none !important;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
// VARIANTS
|
|
10
|
-
&&__muted {
|
|
11
|
-
--_ca-bc: var(--bc-light);
|
|
12
|
-
|
|
13
|
-
// Dim only the first child card content
|
|
14
|
-
> * {
|
|
15
|
-
opacity: 0.65;
|
|
16
|
-
|
|
17
|
-
.dark-mode({
|
|
18
|
-
opacity: 0.7;
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
.highcontrast-mode({
|
|
22
|
-
opacity: 0.75;
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// CHILD ELEMENTS
|
|
28
|
-
> :last-child {
|
|
29
|
-
margin-bottom: 0;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
border: var(--su1) solid var(--_ca-bc);
|
|
33
|
-
|
|
34
|
-
background-color: var(--white);
|
|
35
|
-
border-radius: var(--br-md);
|
|
36
|
-
padding: var(--su12);
|
|
37
|
-
}
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
.s-expandable { // [1]
|
|
2
|
-
// COMPONENT-SPECIFIC CONSTANTS
|
|
3
|
-
@ex-clip-path: polygon(-1000000px -1000000px, 1000000px -1000000px, 1000000px 1000000px, -1000000px 1000000px);
|
|
4
|
-
@ex-min-expected-height: 10px;
|
|
5
|
-
@ex-transition-duration: 100ms;
|
|
6
|
-
// COMPONENT-SPECIFIC CUSTOM PROPERTIES
|
|
7
|
-
--_ex-after-h: 10px;
|
|
8
|
-
--_ex-after-hmx: 0;
|
|
9
|
-
--_ex-after-transition:
|
|
10
|
-
height @ex-transition-duration linear,
|
|
11
|
-
max-height 0s @ex-transition-duration linear;
|
|
12
|
-
--_ex-content-hmx: 1000000px;
|
|
13
|
-
--_ex-content-mb: 0;
|
|
14
|
-
--_ex-content-o: unset;
|
|
15
|
-
--_ex-content-transform: unset;
|
|
16
|
-
--_ex-content-transition:
|
|
17
|
-
margin-bottom @ex-transition-duration cubic-bezier(0, 0, 0, 1),
|
|
18
|
-
transform @ex-transition-duration cubic-bezier(1, 0, 1, 1),
|
|
19
|
-
opacity @ex-transition-duration cubic-bezier(1, 0, 1, 1);
|
|
20
|
-
--_ex-content-v: unset;
|
|
21
|
-
|
|
22
|
-
&:not(.is-expanded) {
|
|
23
|
-
--_ex-after-h: 0;
|
|
24
|
-
--_ex-after-hmx: @ex-min-expected-height;
|
|
25
|
-
--_ex-after-transition: height @ex-min-expected-height linear;
|
|
26
|
-
--_ex-content-hmx: 0;
|
|
27
|
-
--_ex-content-mb: -1500px;
|
|
28
|
-
--_ex-content-o: 0;
|
|
29
|
-
--_ex-content-transform: scaleY(0);
|
|
30
|
-
--_ex-content-transition:
|
|
31
|
-
margin-bottom @ex-transition-duration cubic-bezier(1, 0, 1, 1),
|
|
32
|
-
visibility 0s @ex-transition-duration,
|
|
33
|
-
max-height 0s @ex-transition-duration,
|
|
34
|
-
transform @ex-transition-duration cubic-bezier(0, 1, 1, 1),
|
|
35
|
-
opacity @ex-transition-duration cubic-bezier(0, 1, 1, 1);
|
|
36
|
-
--_ex-content-v: hidden;
|
|
37
|
-
|
|
38
|
-
& .s-expandable--content {
|
|
39
|
-
@supports ((-webkit-clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 100%)) or (clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 100%))) {
|
|
40
|
-
--_ex-content-o: 0;
|
|
41
|
-
--_ex-content-transform: none;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
-webkit-clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 100%);
|
|
46
|
-
clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 100%);
|
|
47
|
-
overflow: hidden;
|
|
48
|
-
transition: none;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
&:after {
|
|
52
|
-
height: var(--_ex-after-h);
|
|
53
|
-
max-height: var(--_ex-after-hmx);
|
|
54
|
-
transition: var(--_ex-after-transition);
|
|
55
|
-
|
|
56
|
-
-ms-flex-preferred-size: 0;
|
|
57
|
-
content: '';
|
|
58
|
-
flex-basis: 0;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
& &--content {
|
|
62
|
-
max-height: var(--_ex-content-hmx);
|
|
63
|
-
margin-bottom: var(--_ex-content-mb);
|
|
64
|
-
opacity: var(--_ex-content-o);
|
|
65
|
-
-webkit-transform: var(--_ex-content-transform);
|
|
66
|
-
transform: var(--_ex-content-transform);
|
|
67
|
-
visibility: var(--_ex-content-v);
|
|
68
|
-
|
|
69
|
-
-ms-flex-preferred-size: 100%;
|
|
70
|
-
flex-basis: 100%;
|
|
71
|
-
-webkit-transform-origin: 0 0;
|
|
72
|
-
transform-origin: 0 0;
|
|
73
|
-
transition: var(--_ex-content-transition);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
align-items: flex-start; // see comment above
|
|
77
|
-
display: flex;
|
|
78
|
-
-webkit-clip-path: @ex-clip-path;
|
|
79
|
-
clip-path: @ex-clip-path;
|
|
80
|
-
transition: clip-path 0s var(--_ex-transition-duration), -webkit-clip-path 0s var(--_ex-transition-duration);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// [1] see http://stackoverflow.com/a/43965099 for how this works:
|
|
84
|
-
|
|
85
|
-
// Notes on the clip-path stuff: What we would really like is overflow: hidden during the transition,
|
|
86
|
-
// but not when the element is expanded. Unfortunately, although the CSS spec provides for it, there's
|
|
87
|
-
// no browser support yet for transitioning non-interpolatable properties, and therefore we cannot say
|
|
88
|
-
// "change overflow to visible *after* the transition".
|
|
89
|
-
//
|
|
90
|
-
// So we use clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 100%), which is essentially the same
|
|
91
|
-
// as overflow: hidden, but unlike overflow, clip-path is interpolatable -- but only if the opposite state
|
|
92
|
-
// is also a polygon, and has the same vertice count. So our version of overflow: visible is a rectangle
|
|
93
|
-
// of 2x2 million pixels, which should be enough for everybody (TM).
|
|
94
|
-
//
|
|
95
|
-
// At the time of writing, clip-path works in Chrome, Firefox Beta, and Safari (with vendor prefix).
|
|
96
|
-
//
|
|
97
|
-
// In browsers that do not support this yet, we have to have another way to prevent the expandable content
|
|
98
|
-
// from being visible below the bottom edge during the transition, and the best I could come up with is
|
|
99
|
-
// transitioning to scaleY(0) with a timing function that's faster than the height-reducing transitions.
|
|
100
|
-
// This does kinda look like a deliberate effect, even more so if we also transition to transparency
|
|
101
|
-
// (which we therefore do), so I feel it's an okay fallback.
|
|
102
|
-
//
|
|
103
|
-
// We still set overflow to hidden in the collapsed state (which also applies to the collapsing transition),
|
|
104
|
-
// we just can't do that for the expanding transition.
|
|
105
|
-
//
|
|
106
|
-
// A major drawback of clip-path is that even though the content is clipped (not visible), it will still
|
|
107
|
-
// contribute to the document height. This caused a jumping around of the scrollbar (and possibly even
|
|
108
|
-
// of the viewport) when the expandable was at the bottom of the page, because once the negative bottom
|
|
109
|
-
// margin of the -expandable-group exceeds the actual height, any additional pixels were added on to the
|
|
110
|
-
// bottom. The fix is to set the -expandable-group's flex alignment to flex-start, which forces the
|
|
111
|
-
// (no longer visible) element itself to remain at the top, thereby forcing the excess pixels to be added
|
|
112
|
-
// above the top, not below the bottom. And because extending content above the document top will not do
|
|
113
|
-
// anything to the document height, there is no jumping during the transition.
|
|
114
|
-
// see "@ex-transition-duration"
|
|
115
|
-
|
|
116
|
-
// Per the answer referenced above, the component can only guarantee smooth transitions if above a minimum
|
|
117
|
-
// height and can only guarantee the element will be hidden is below a maximimum height.
|
|
118
|
-
// The minimum height has been set at 10px because that's below the height of a single line of text in an s-description.
|
|
119
|
-
// see "@ex-min-expected-height" and "--_ex-content-mb"
|
|
@@ -1,238 +0,0 @@
|
|
|
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-expandable-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 (
|
|
16
|
-
!(target instanceof HTMLInputElement) ||
|
|
17
|
-
target.nodeName !== "INPUT" ||
|
|
18
|
-
target.type !== "radio"
|
|
19
|
-
) {
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
document
|
|
23
|
-
.querySelectorAll('input[type="radio"][name="' + target.name + '"]')
|
|
24
|
-
.forEach(function (other) {
|
|
25
|
-
if (other === e.target) {
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
let customEvent;
|
|
29
|
-
try {
|
|
30
|
-
customEvent = new Event(RADIO_OFF_EVENT);
|
|
31
|
-
} catch {
|
|
32
|
-
// Internet Explorer
|
|
33
|
-
customEvent = document.createEvent("Event");
|
|
34
|
-
customEvent.initEvent(RADIO_OFF_EVENT, true, true);
|
|
35
|
-
}
|
|
36
|
-
other.dispatchEvent(customEvent);
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
let refCount = 0;
|
|
41
|
-
function globalChangeListenerRequired(required: boolean) {
|
|
42
|
-
if (required) {
|
|
43
|
-
refCount++;
|
|
44
|
-
if (refCount === 1) {
|
|
45
|
-
document.body.addEventListener(
|
|
46
|
-
"change",
|
|
47
|
-
globalChangeListener as EventListener
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
} else {
|
|
51
|
-
refCount--;
|
|
52
|
-
if (refCount === 0) {
|
|
53
|
-
document.body.removeEventListener(
|
|
54
|
-
"change",
|
|
55
|
-
globalChangeListener as EventListener
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export class ExpandableController extends Stacks.StacksController {
|
|
62
|
-
private isCollapsed!: () => boolean;
|
|
63
|
-
private events!: string[];
|
|
64
|
-
private isCheckable!: boolean;
|
|
65
|
-
private isRadio!: boolean;
|
|
66
|
-
private lastKeydownClickTimestamp = 0;
|
|
67
|
-
|
|
68
|
-
initialize() {
|
|
69
|
-
if (
|
|
70
|
-
this.element.nodeName === "INPUT" &&
|
|
71
|
-
["radio", "checkbox"].indexOf(
|
|
72
|
-
(<HTMLInputElement>this.element).type
|
|
73
|
-
) >= 0
|
|
74
|
-
) {
|
|
75
|
-
this.isCollapsed = this._isCollapsedForCheckable.bind(this);
|
|
76
|
-
this.events = ["change", RADIO_OFF_EVENT];
|
|
77
|
-
this.isCheckable = true;
|
|
78
|
-
this.isRadio = (<HTMLInputElement>this.element).type === "radio";
|
|
79
|
-
} else {
|
|
80
|
-
this.isCollapsed = this._isCollapsedForClickable.bind(this);
|
|
81
|
-
this.events = ["click", "keydown"];
|
|
82
|
-
}
|
|
83
|
-
this.listener = this.listener.bind(this);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// for non-checkable elements, the initial source of truth is the collapsed/expanded
|
|
87
|
-
// state of the controlled element (unless the element doesn't exist)
|
|
88
|
-
_isCollapsedForClickable() {
|
|
89
|
-
const cc = this.controlledExpandables;
|
|
90
|
-
// the element is considered collapsed if *any* target element is collapsed
|
|
91
|
-
return cc.length > 0
|
|
92
|
-
? !cc.every((element) => element.classList.contains("is-expanded"))
|
|
93
|
-
: this.element.getAttribute("aria-expanded") === "false";
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// for checkable elements, the initial source of truth is the checked state
|
|
97
|
-
_isCollapsedForCheckable() {
|
|
98
|
-
return !(<HTMLInputElement>this.element).checked;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
get controlledExpandables() {
|
|
102
|
-
const attr = this.element.getAttribute("aria-controls");
|
|
103
|
-
if (!attr) {
|
|
104
|
-
throw `[aria-controls="targetId1 ... targetIdN"] attribute required`;
|
|
105
|
-
}
|
|
106
|
-
const result = attr
|
|
107
|
-
.split(/\s+/g)
|
|
108
|
-
.map((s) => document.getElementById(s))
|
|
109
|
-
.filter((e): e is HTMLElement => !!e);
|
|
110
|
-
if (!result.length) {
|
|
111
|
-
throw "couldn't find controls";
|
|
112
|
-
}
|
|
113
|
-
return result;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
_dispatchShowHideEvent(isShow: boolean) {
|
|
117
|
-
this.triggerEvent(isShow ? "show" : "hide");
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
_toggleClass(doAdd: boolean) {
|
|
121
|
-
if (!this.data.has("toggle-class")) {
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
const cl = this.element.classList;
|
|
125
|
-
const toggleClass = this.data.get("toggle-class");
|
|
126
|
-
if (!toggleClass) {
|
|
127
|
-
throw "couldn't find toggle class";
|
|
128
|
-
}
|
|
129
|
-
toggleClass.split(/\s+/).forEach(function (cls) {
|
|
130
|
-
cl.toggle(cls, !!doAdd);
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
listener(e: Event) {
|
|
135
|
-
let newCollapsed;
|
|
136
|
-
if (this.isCheckable) {
|
|
137
|
-
newCollapsed = !(<HTMLInputElement>this.element).checked;
|
|
138
|
-
} else {
|
|
139
|
-
if (
|
|
140
|
-
e.type == "keydown" &&
|
|
141
|
-
e instanceof KeyboardEvent &&
|
|
142
|
-
e.keyCode != 13 &&
|
|
143
|
-
e.keyCode != 32
|
|
144
|
-
) {
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
if (
|
|
148
|
-
e.target !== e.currentTarget &&
|
|
149
|
-
["A", "BUTTON"].indexOf((<HTMLElement>e.target).nodeName) >= 0
|
|
150
|
-
) {
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
e.preventDefault();
|
|
155
|
-
|
|
156
|
-
// Prevent "click" events from toggling the expandable within 300ms of "keydown".
|
|
157
|
-
// e.preventDefault() should have done the same, but https://bugzilla.mozilla.org/show_bug.cgi?id=1487102
|
|
158
|
-
// doesn't guarantee it.
|
|
159
|
-
if (e.type == "keydown") {
|
|
160
|
-
this.lastKeydownClickTimestamp = Date.now();
|
|
161
|
-
} else if (
|
|
162
|
-
e.type == "click" &&
|
|
163
|
-
Date.now() - this.lastKeydownClickTimestamp < 300
|
|
164
|
-
) {
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
newCollapsed =
|
|
168
|
-
this.element.getAttribute("aria-expanded") === "true";
|
|
169
|
-
if (e.type === "click") {
|
|
170
|
-
(<HTMLInputElement>this.element).blur();
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
this.element.setAttribute(
|
|
174
|
-
"aria-expanded",
|
|
175
|
-
newCollapsed ? "false" : "true"
|
|
176
|
-
);
|
|
177
|
-
for (const controlledElement of this.controlledExpandables) {
|
|
178
|
-
controlledElement.classList.toggle("is-expanded", !newCollapsed);
|
|
179
|
-
}
|
|
180
|
-
this._dispatchShowHideEvent(!newCollapsed);
|
|
181
|
-
this._toggleClass(!newCollapsed);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
connect() {
|
|
185
|
-
this.events.forEach((e) => {
|
|
186
|
-
this.element.addEventListener(e, this.listener.bind(this));
|
|
187
|
-
}, this);
|
|
188
|
-
|
|
189
|
-
if (this.isRadio) {
|
|
190
|
-
globalChangeListenerRequired(true);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// synchronize state -- in all cases, this means setting the correct `aria-expanded`
|
|
194
|
-
// attribute; for checkable controls this also means setting the `is-collapsed` class.
|
|
195
|
-
// Note: aria-expanded is currently an invalid attribute on radio elements
|
|
196
|
-
// Support for aria-expanded is being debated by the W3C https://github.com/w3c/aria/issues/1404 as recently as June 2022
|
|
197
|
-
if (!this.isRadio) {
|
|
198
|
-
this.element.setAttribute(
|
|
199
|
-
"aria-expanded",
|
|
200
|
-
this.isCollapsed() ? "false" : "true"
|
|
201
|
-
);
|
|
202
|
-
}
|
|
203
|
-
if (this.isCheckable) {
|
|
204
|
-
const cc = this.controlledExpandables;
|
|
205
|
-
if (cc.length) {
|
|
206
|
-
const expected = !this.isCollapsed();
|
|
207
|
-
// if any element does not match the expected state, set them all to the expected state
|
|
208
|
-
if (
|
|
209
|
-
cc.some(
|
|
210
|
-
(element) =>
|
|
211
|
-
element.classList.contains("is-expanded") !==
|
|
212
|
-
expected
|
|
213
|
-
)
|
|
214
|
-
) {
|
|
215
|
-
for (const controlledElement of this
|
|
216
|
-
.controlledExpandables) {
|
|
217
|
-
controlledElement.classList.toggle(
|
|
218
|
-
"is-expanded",
|
|
219
|
-
expected
|
|
220
|
-
);
|
|
221
|
-
}
|
|
222
|
-
this._dispatchShowHideEvent(expected);
|
|
223
|
-
this._toggleClass(expected);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
disconnect() {
|
|
230
|
-
this.events.forEach((e) => {
|
|
231
|
-
this.element.removeEventListener(e, this.listener.bind(this));
|
|
232
|
-
}, this);
|
|
233
|
-
|
|
234
|
-
if (this.isRadio) {
|
|
235
|
-
globalChangeListenerRequired(false);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|