-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathtemplate.js
127 lines (122 loc) · 3.42 KB
/
template.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { Template as Icon } from "@spectrum-css/icon/stories/template.js";
import { getRandomId } from "@spectrum-css/preview/decorators";
import { html } from "lit";
import { classMap } from "lit/directives/class-map.js";
import { ifDefined } from "lit/directives/if-defined.js";
import { repeat } from "lit/directives/repeat.js";
import { styleMap } from "lit/directives/style-map.js";
import "../index.css";
import "../themes/spectrum.css";
/* Must be imported last */
import "../themes/express.css";
export const AccordionItem = ({
heading,
content,
rootClass = "spectrum-Accordion-item",
id = getRandomId("accordion-item"),
idx = 0,
isDisabled = false,
isOpen = false,
iconSize = "m",
customStyles = {},
customClasses = [],
onclick,
} = {}, context = {}) => {
return html`
<div
class=${classMap({
[rootClass]: true,
"is-open": isOpen && !isDisabled,
"is-disabled": isDisabled,
...customClasses.reduce((a, c) => ({ ...a, [c]: true }), {}),
})}
id=${ifDefined(id)}
style=${styleMap(customStyles)}
role="presentation"
@click=${onclick}
>
<!-- WAI-ARIA 1.1: Item header is a <button> wrapped within a <h3> element, rather than a <div> element with role="tab" -->
<h3 class="${rootClass}Heading">
<!-- WAI-ARIA 1.1: Item header <button> uses aria-expanded attribute to indicate expanded state. -->
<button
class="${rootClass}Header"
type="button"
?disabled=${isDisabled}
id="spectrum-accordion-item-${idx}-header"
aria-controls="spectrum-accordion-item-${idx}-content"
aria-expanded="${open ? "true" : "false"}"
>
${heading}
</button>
<span class="${rootClass}IconContainer">
${Icon({
iconName: !isOpen ? "ChevronRight" : "ChevronDown",
setName: "ui",
size: iconSize,
customClasses: [`${rootClass}Indicator`],
}, context)}
</span>
</h3>
<!-- WAI-ARIA 1.1: Item content role changed from "tabpanel" to "region" -->
<div
class="${rootClass}Content"
role="region"
id="spectrum-accordion-item-${idx}-content"
aria-labelledby="spectrum-accordion-item-${idx}-header"
>
${content}
</div>
</div>
`;
};
export const Template = ({
rootClass = "spectrum-Accordion",
size = "m",
density = "regular",
items = [],
id = getRandomId("accordion"),
disableAll = false,
collapseAll = false,
customClasses = [],
customStyles = {},
} = {}, context = {}) => {
const { updateArgs } = context;
return html`
<div
class="${classMap({
[rootClass]: true,
[`${rootClass}--size${size?.toUpperCase()}`]:
typeof size !== "undefined",
[`${rootClass}--${density}`]:
typeof density !== "undefined" && density !== "regular",
...customClasses.reduce((a, c) => ({ ...a, [c]: true }), {}),
})}"
id=${ifDefined(id)}
role="region"
style=${styleMap(customStyles)}
>
${repeat(Array.from(items.keys()), (heading, idx) => {
const item = items.get(heading);
return AccordionItem({
...item,
rootClass: `${rootClass}-item`,
heading,
idx,
iconSize: `${size}`,
isDisabled: item.isDisabled || disableAll,
isOpen: collapseAll === true ? false : item.isOpen,
onclick: function() {
if (item.isDisabled) return;
// Update the args
const newItems = new Map(items);
newItems.set(heading, {
...item,
isOpen: !item.isOpen,
});
updateArgs({ items: newItems });
},
}, context);
})}
</div>
`;
};