-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathtemplate.js
206 lines (198 loc) · 5.15 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { Template as Icon } from "@spectrum-css/icon/stories/template.js";
import { Template as InfieldProgressCircle } from "@spectrum-css/infieldprogresscircle/stories/template.js";
import { Container, 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 { styleMap } from "lit/directives/style-map.js";
import { when } from "lit/directives/when.js";
import { capitalize } from "lodash-es";
import "../index.css";
export const Template = ({
rootClass = "spectrum-Button",
id = getRandomId("button"),
testId,
customClasses = [],
customStyles = {},
size = "m",
label,
hideLabel = false,
iconName,
iconSet = "workflow",
iconAfterLabel = false,
variant,
staticColor,
treatment,
onclick,
isDisabled = false,
isHovered = false,
isFocused = false,
isActive = false,
isPending = false,
ariaExpanded,
ariaControls,
noWrap = false,
} = {}, context = {}) => {
const { updateArgs } = context;
return html`
<button
class=${classMap({
[rootClass]: true,
[`${rootClass}--${treatment}`]: typeof treatment !== "undefined",
[`${rootClass}--${variant}`]: typeof variant !== "undefined",
[`${rootClass}--size${size?.toUpperCase()}`]:
typeof size !== "undefined",
[`${rootClass}--static${capitalize(staticColor)}`]:
typeof staticColor !== "undefined",
[`${rootClass}--iconOnly`]: hideLabel,
["is-pending"]: isPending,
["is-disabled"]: isDisabled,
["is-hover"]: isHovered,
["is-focus-visible"]: isFocused,
["is-active"]: isActive,
[`${rootClass}--noWrap`]: noWrap,
...customClasses.reduce((a, c) => ({ ...a, [c]: true }), {}),
})}
id=${ifDefined(id)}
style=${styleMap(customStyles)}
?disabled=${isDisabled}
@click=${onclick ??
function () {
// Toggle the is-pending state on-click
updateArgs({ isPending: true });
setTimeout(() => {
updateArgs({ isPending: false });
}, 3000);
}}
@focusin=${function() {
updateArgs({ isFocused: true });
}}
@focusout=${function() {
updateArgs({ isFocused: false });
}}
aria-label=${ifDefined(hideLabel ? iconName : undefined)}
aria-expanded=${ifDefined(ariaExpanded?.toString())}
aria-controls=${ifDefined(ariaControls)}
data-testid=${ifDefined(testId)}
>
${when(iconName && !iconAfterLabel, () =>
Icon({ iconName, setName: iconSet, size }, context)
)}
${when(label && !hideLabel, () =>
html`<span class=${`${rootClass}-label`}>${label}</span>`
)}
${when(iconName && iconAfterLabel, () =>
Icon({ iconName, setName: iconSet, size }, context)
)}
${when(isPending, () =>
InfieldProgressCircle({
size: size,
staticColor,
isIndeterminate: true,
testId: "infield-progress-circle"
}, context)
)}
</button>
`;
};
/**
* Displays multiple buttons with text label, icon + text label, and icon only.
* Used in the display of some docs-only stories.
*/
export const ButtonsWithIconOptions = ({
iconName,
...args
}, context = {}) => Container({
withBorder: false,
direction: "row",
wrapperStyles: {
columnGap: "12px",
},
content: html`
${Template({
...args,
iconName: undefined,
}, context)}
${Template({
...args,
iconName: iconName ?? "Edit",
}, context)}
${Template({
...args,
hideLabel: true,
iconName: iconName ?? "Edit",
}, context)}
`,
}, context);
/**
* Display the buttons with icon options for each treatment option.
*/
export const TreatmentTemplate = (args, context = {}) => Container({
withBorder: false,
direction: "column",
wrapperStyles: {
rowGap: "12px",
},
content: html`${["fill", "outline"].map((treatment) => ButtonsWithIconOptions({ ...args, treatment }, context))}`,
}, context);
/**
* Display the text overflow behavior of buttons.
*/
export const TextOverflowTemplate = (args, context = {}) => Container({
withBorder: false,
direction: "column",
wrapperStyles: {
rowGap: "12px",
},
content: html`
${Template({
...args,
customStyles: {
"max-inline-size": "480px",
},
label: "An example of text overflow behavior when there is no icon. When the button text is too long for the horizontal space available, it wraps to form another line.",
}, context)}
${Template({
...args,
customStyles: {
"max-inline-size": "480px",
},
iconName: "Edit",
iconSet: "workflow",
label: "An example of text overflow behavior when the button has an icon. When the button text is too long for the horizontal space available, it wraps to form another line.",
}, context)}
`,
}, context);
export const TextWrapTemplate = (args, context = {}) => Container({
direction: "column",
wrapperStyles: {
rowGap: "12px",
maxInlineSize: "120px",
},
content: html`
${Template({
...args,
customStyles: {},
label: "Be a premium member",
noWrap: true,
}, context)}
${Template({
...args,
customStyles: {
"max-inline-size": "100%",
},
label: "Be a premium member",
noWrap: true,
}, context)}
${Template({
...args,
customStyles: {
"max-inline-size": "100%",
},
iconName: "Star",
iconSet: "workflow",
label: "Be a premium member",
noWrap: true,
}, context)}
`,
}, context);