-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathaction.tsx
356 lines (306 loc) · 9.22 KB
/
action.tsx
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import {
Component,
Element,
forceUpdate,
h,
Host,
Method,
Prop,
State,
VNode,
Watch,
} from "@stencil/core";
import { toAriaBoolean } from "../../utils/dom";
import { guid } from "../../utils/guid";
import {
InteractiveComponent,
InteractiveContainer,
updateHostInteraction,
} from "../../utils/interactive";
import {
componentFocusable,
LoadableComponent,
setComponentLoaded,
setUpLoadableComponent,
} from "../../utils/loadable";
import { connectLocalized, disconnectLocalized, LocalizedComponent } from "../../utils/locale";
import { createObserver } from "../../utils/observers";
import { getIconScale } from "../../utils/component";
import {
connectMessages,
disconnectMessages,
setUpMessages,
T9nComponent,
updateMessages,
} from "../../utils/t9n";
import { Alignment, Appearance, Scale } from "../interfaces";
import { IconNameOrString } from "../icon/interfaces";
import { isBrowser } from "../../utils/browser";
import { ActionMessages } from "./assets/action/t9n";
import { CSS, SLOTS } from "./resources";
/**
* @slot - A slot for adding a `calcite-icon`.
* @slot tooltip - [Deprecated] Use the `calcite-tooltip` component instead.
*/
@Component({
tag: "calcite-action",
styleUrl: "action.scss",
shadow: true,
assetsDirs: ["assets"],
})
export class Action
implements InteractiveComponent, LocalizedComponent, T9nComponent, LoadableComponent
{
// --------------------------------------------------------------------------
//
// Properties
//
// --------------------------------------------------------------------------
/**
* When `true`, the component is highlighted.
*/
@Prop({ reflect: true }) active = false;
/**
* Specifies the horizontal alignment of button elements with text content.
*/
@Prop({ reflect: true }) alignment: Alignment;
/** Specifies the appearance of the component. */
@Prop({ reflect: true }) appearance: Extract<"solid" | "transparent", Appearance> = "solid";
/**
* When `true`, the side padding of the component is reduced.
*
* @deprecated No longer necessary.
*/
@Prop({ reflect: true }) compact = false;
/**
* When `true`, interaction is prevented and the component is displayed with lower opacity.
*/
@Prop({ reflect: true }) disabled = false;
/** Specifies an icon to display. */
@Prop() icon: IconNameOrString;
/** When `true`, the icon will be flipped when the element direction is right-to-left (`"rtl"`). */
@Prop({ reflect: true }) iconFlipRtl = false;
/**
* When `true`, displays a visual indicator.
*/
@Prop({ reflect: true }) indicator = false;
/**
* Specifies the label of the component. If no label is provided, the label inherits what's provided for the `text` prop.
*/
@Prop() label: string;
/**
* When `true`, a busy indicator is displayed.
*/
@Prop({ reflect: true }) loading = false;
/**
* Specifies the size of the component.
*/
@Prop({ reflect: true }) scale: Scale = "m";
/**
* Specifies text that accompanies the icon.
*/
@Prop() text!: string;
/**
* Indicates whether the text is displayed.
*/
@Prop({ reflect: true }) textEnabled = false;
/**
* Made into a prop for testing purposes only
*
* @internal
*/
// eslint-disable-next-line @stencil-community/strict-mutable -- updated by t9n module
@Prop({ mutable: true }) messages: ActionMessages;
/**
* Use this property to override individual strings used by the component.
*/
// eslint-disable-next-line @stencil-community/strict-mutable -- updated by t9n module
@Prop({ mutable: true }) messageOverrides: Partial<ActionMessages>;
@Watch("messageOverrides")
onMessagesChange(): void {
/* wired up by t9n util */
}
// --------------------------------------------------------------------------
//
// Private Properties
//
// --------------------------------------------------------------------------
@Element() el: HTMLCalciteActionElement;
buttonEl: HTMLButtonElement;
mutationObserver = createObserver("mutation", () => forceUpdate(this));
@State() effectiveLocale = "";
@Watch("effectiveLocale")
effectiveLocaleChange(): void {
updateMessages(this, this.effectiveLocale);
}
@State() defaultMessages: ActionMessages;
guid = `calcite-action-${guid()}`;
indicatorId = `${this.guid}-indicator`;
buttonId = `${this.guid}-button`;
// --------------------------------------------------------------------------
//
// Lifecycle
//
// --------------------------------------------------------------------------
connectedCallback(): void {
connectLocalized(this);
connectMessages(this);
this.mutationObserver?.observe(this.el, { childList: true, subtree: true });
}
async componentWillLoad(): Promise<void> {
setUpLoadableComponent(this);
if (isBrowser()) {
await setUpMessages(this);
}
}
componentDidLoad(): void {
setComponentLoaded(this);
}
disconnectedCallback(): void {
disconnectLocalized(this);
disconnectMessages(this);
this.mutationObserver?.disconnect();
}
componentDidRender(): void {
updateHostInteraction(this);
}
// --------------------------------------------------------------------------
//
// Methods
//
// --------------------------------------------------------------------------
/** Sets focus on the component. */
@Method()
async setFocus(): Promise<void> {
await componentFocusable(this);
this.buttonEl?.focus();
}
// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------
renderTextContainer(): VNode {
const { text, textEnabled } = this;
const textContainerClasses = {
[CSS.textContainer]: true,
[CSS.textContainerVisible]: textEnabled,
};
return text ? (
<div class={textContainerClasses} key="text-container">
{text}
</div>
) : null;
}
renderIndicatorText(): VNode {
const { indicator, messages, indicatorId, buttonId } = this;
return (
<div
aria-labelledby={buttonId}
aria-live="polite"
class={CSS.indicatorText}
id={indicatorId}
role="region"
>
{indicator ? messages.indicator : null}
</div>
);
}
renderIconContainer(): VNode {
const { loading, icon, scale, el, iconFlipRtl, indicator } = this;
const loaderScale = scale === "l" ? "l" : "m";
const calciteLoaderNode = loading ? (
<calcite-loader inline label={this.messages.loading} scale={loaderScale} />
) : null;
const calciteIconNode = icon ? (
<calcite-icon
class={{ [CSS.indicatorWithIcon]: indicator }}
flipRtl={iconFlipRtl}
icon={icon}
scale={getIconScale(this.scale)}
/>
) : null;
const iconNode = calciteLoaderNode || calciteIconNode;
const hasIconToDisplay = iconNode || el.children?.length;
const slotContainerNode = (
<div
class={{
[CSS.slotContainer]: true,
[CSS.slotContainerHidden]: loading,
}}
>
<slot />
</div>
);
return hasIconToDisplay ? (
<div aria-hidden="true" class={CSS.iconContainer} key="icon-container">
{iconNode}
{slotContainerNode}
</div>
) : null;
}
render(): VNode {
const {
active,
compact,
disabled,
icon,
loading,
textEnabled,
label,
text,
indicator,
indicatorId,
buttonId,
messages,
} = this;
const labelFallback = label || text;
const ariaLabel = labelFallback
? `${labelFallback}${indicator ? ` (${messages.indicator})` : ""}`
: "";
const buttonClasses = {
[CSS.button]: true,
[CSS.buttonTextVisible]: textEnabled,
[CSS.buttonCompact]: compact,
};
return (
<Host>
<InteractiveContainer disabled={disabled}>
<button
aria-busy={toAriaBoolean(loading)}
aria-controls={indicator ? indicatorId : null}
aria-disabled={toAriaBoolean(disabled)}
aria-label={ariaLabel}
aria-pressed={toAriaBoolean(active)}
class={buttonClasses}
disabled={disabled}
id={buttonId}
ref={(buttonEl): HTMLButtonElement => (this.buttonEl = buttonEl)}
>
{this.renderIconContainer()}
{this.renderTextContainer()}
{!icon && indicator && <div class={CSS.indicatorWithoutIcon} key="indicator-no-icon" />}
</button>
<slot name={SLOTS.tooltip} onSlotchange={this.handleTooltipSlotChange} />
{this.renderIndicatorText()}
</InteractiveContainer>
</Host>
);
}
// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------
handleTooltipSlotChange = (event: Event): void => {
const tooltips = (event.target as HTMLSlotElement)
.assignedElements({
flatten: true,
})
.filter((el): el is HTMLCalciteTooltipElement => el?.matches("calcite-tooltip"));
const tooltip = tooltips[0];
if (tooltip) {
tooltip.referenceElement = this.buttonEl;
}
};
}