-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathblock.tsx
412 lines (355 loc) · 11.3 KB
/
block.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import {
Component,
Element,
Event,
EventEmitter,
h,
Host,
Method,
Prop,
State,
VNode,
Watch,
} from "@stencil/core";
import {
ConditionalSlotComponent,
connectConditionalSlotComponent,
disconnectConditionalSlotComponent,
} from "../../utils/conditionalSlot";
import { focusFirstTabbable, getSlotted, toAriaBoolean } from "../../utils/dom";
import {
connectInteractive,
disconnectInteractive,
InteractiveComponent,
InteractiveContainer,
updateHostInteraction,
} from "../../utils/interactive";
import { connectLocalized, disconnectLocalized, LocalizedComponent } from "../../utils/locale";
import {
connectMessages,
disconnectMessages,
setUpMessages,
T9nComponent,
updateMessages,
} from "../../utils/t9n";
import { Heading, HeadingLevel } from "../functional/Heading";
import { Status } from "../interfaces";
import {
componentFocusable,
LoadableComponent,
setComponentLoaded,
setUpLoadableComponent,
} from "../../utils/loadable";
import { onToggleOpenCloseComponent, OpenCloseComponent } from "../../utils/openCloseComponent";
import { OverlayPositioning } from "../../utils/floating-ui";
import { CSS, ICONS, IDS, SLOTS } from "./resources";
import { BlockMessages } from "./assets/block/t9n";
/**
* @slot - A slot for adding custom content.
* @slot icon - A slot for adding a leading header icon with `calcite-icon`.
* @slot control - A slot for adding a single HTML input element in a header.
* @slot header-menu-actions - A slot for adding an overflow menu with `calcite-action`s inside a dropdown menu.
*/
@Component({
tag: "calcite-block",
styleUrl: "block.scss",
shadow: true,
assetsDirs: ["assets"],
})
export class Block
implements
ConditionalSlotComponent,
InteractiveComponent,
LocalizedComponent,
T9nComponent,
LoadableComponent,
OpenCloseComponent
{
// --------------------------------------------------------------------------
//
// Public Properties
//
// --------------------------------------------------------------------------
/**
* When `true`, the component is collapsible.
*/
@Prop({ reflect: true }) collapsible = false;
/**
* When `true`, interaction is prevented and the component is displayed with lower opacity.
*/
@Prop({ reflect: true }) disabled = false;
/**
* When `true`, displays a drag handle in the header.
*/
@Prop({ reflect: true }) dragHandle = false;
/**
* The component header text.
*/
@Prop() heading!: string;
/**
* Specifies the heading level of the component's `heading` for proper document structure, without affecting visual styling.
*/
@Prop({ reflect: true }) headingLevel: HeadingLevel;
/**
* When `true`, a busy indicator is displayed.
*/
@Prop({ reflect: true }) loading = false;
/**
* When `true`, expands the component and its contents.
*/
@Prop({ reflect: true, mutable: true }) open = false;
@Watch("open")
openHandler(): void {
onToggleOpenCloseComponent(this);
}
/**
* Displays a status-related indicator icon.
*/
@Prop({ reflect: true }) status: Status;
/**
* A description for the component, which displays below the heading.
*/
@Prop() description: string;
/**
* 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: BlockMessages;
/**
* 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<BlockMessages>;
@Watch("messageOverrides")
onMessagesChange(): void {
/* wired up by t9n util */
}
/**
* Determines the type of positioning to use for the overlaid content.
*
* Using `"absolute"` will work for most cases. The component will be positioned inside of overflowing parent containers and will affect the container's layout.
*
* `"fixed"` should be used to escape an overflowing parent container, or when the reference element's `position` CSS property is `"fixed"`.
*
*/
@Prop({ reflect: true }) overlayPositioning: OverlayPositioning = "absolute";
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/**
* Sets focus on the component's first tabbable element.
*
*/
@Method()
async setFocus(): Promise<void> {
await componentFocusable(this);
focusFirstTabbable(this.el);
}
onBeforeOpen(): void {
this.calciteBlockBeforeOpen.emit();
}
onOpen(): void {
this.calciteBlockOpen.emit();
}
onBeforeClose(): void {
this.calciteBlockBeforeClose.emit();
}
onClose(): void {
this.calciteBlockClose.emit();
}
// --------------------------------------------------------------------------
//
// Private Properties
//
// --------------------------------------------------------------------------
@Element() el: HTMLCalciteBlockElement;
@State() effectiveLocale: string;
@Watch("effectiveLocale")
effectiveLocaleChange(): void {
updateMessages(this, this.effectiveLocale);
}
@State() defaultMessages: BlockMessages;
openTransitionProp = "opacity";
transitionEl: HTMLElement;
// --------------------------------------------------------------------------
//
// Lifecycle
//
// --------------------------------------------------------------------------
connectedCallback(): void {
connectConditionalSlotComponent(this);
connectInteractive(this);
connectLocalized(this);
connectMessages(this);
}
disconnectedCallback(): void {
disconnectInteractive(this);
disconnectLocalized(this);
disconnectMessages(this);
disconnectConditionalSlotComponent(this);
}
async componentWillLoad(): Promise<void> {
await setUpMessages(this);
setUpLoadableComponent(this);
if (this.open) {
onToggleOpenCloseComponent(this);
}
}
componentDidLoad(): void {
setComponentLoaded(this);
}
componentDidRender(): void {
updateHostInteraction(this);
}
// --------------------------------------------------------------------------
//
// Events
//
// --------------------------------------------------------------------------
/** Fires when the component is requested to be closed and before the closing transition begins. */
@Event({ cancelable: false }) calciteBlockBeforeClose: EventEmitter<void>;
/** Fires when the component is added to the DOM but not rendered, and before the opening transition begins. */
@Event({ cancelable: false }) calciteBlockBeforeOpen: EventEmitter<void>;
/** Fires when the component is closed and animation is complete. */
@Event({ cancelable: false }) calciteBlockClose: EventEmitter<void>;
/** Fires when the component is open and animation is complete. */
@Event({ cancelable: false }) calciteBlockOpen: EventEmitter<void>;
/**
* Fires when the component's header is clicked.
*
* @deprecated Use `openClose` events such as `calciteBlockOpen`, `calciteBlockClose`, `calciteBlockBeforeOpen`, and `calciteBlockBeforeClose` instead.
*/
@Event({ cancelable: false }) calciteBlockToggle: EventEmitter<void>;
// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------
onHeaderClick = (): void => {
this.open = !this.open;
this.calciteBlockToggle.emit();
};
private setTransitionEl = (el: HTMLElement): void => {
this.transitionEl = el;
};
// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------
renderScrim(): VNode[] {
const { loading } = this;
const defaultSlot = <slot />;
return [loading ? <calcite-scrim loading={loading} /> : null, defaultSlot];
}
renderIcon(): VNode[] {
const { loading, messages, status } = this;
const hasSlottedIcon = !!getSlotted(this.el, SLOTS.icon);
return loading ? (
<div class={CSS.icon} key="loader">
<calcite-loader inline label={messages.loading} />
</div>
) : status ? (
<div class={CSS.icon} key="status-icon">
<calcite-icon
class={{
[CSS.statusIcon]: true,
[CSS.valid]: status == "valid",
[CSS.invalid]: status == "invalid",
}}
icon={ICONS[status]}
scale="s"
/>
</div>
) : hasSlottedIcon ? (
<div class={CSS.icon} key="icon-slot">
<slot key="icon-slot" name={SLOTS.icon} />
</div>
) : null;
}
renderTitle(): VNode {
const { heading, headingLevel, description } = this;
return heading || description ? (
<div class={CSS.title}>
<Heading class={CSS.heading} level={headingLevel}>
{heading}
</Heading>
{description ? <div class={CSS.description}>{description}</div> : null}
</div>
) : null;
}
render(): VNode {
const { collapsible, el, loading, open, heading, messages } = this;
const toggleLabel = open ? messages.collapse : messages.expand;
const headerContent = (
<header class={CSS.header} id={IDS.header}>
{this.renderIcon()}
{this.renderTitle()}
</header>
);
const hasControl = !!getSlotted(el, SLOTS.control);
const hasMenuActions = !!getSlotted(el, SLOTS.headerMenuActions);
const collapseIcon = open ? ICONS.opened : ICONS.closed;
const headerNode = (
<div class={CSS.headerContainer}>
{this.dragHandle ? <calcite-handle label={heading} /> : null}
{collapsible ? (
<button
aria-controls={IDS.content}
aria-describedby={IDS.header}
aria-expanded={collapsible ? toAriaBoolean(open) : null}
class={CSS.toggle}
id={IDS.toggle}
onClick={this.onHeaderClick}
title={toggleLabel}
>
{headerContent}
<calcite-icon aria-hidden="true" class={CSS.toggleIcon} icon={collapseIcon} scale="s" />
</button>
) : (
headerContent
)}
{hasControl ? (
<div aria-labelledby={IDS.header} class={CSS.controlContainer}>
<slot name={SLOTS.control} />
</div>
) : null}
{hasMenuActions ? (
<calcite-action-menu
label={messages.options}
overlayPositioning={this.overlayPositioning}
>
<slot name={SLOTS.headerMenuActions} />
</calcite-action-menu>
) : null}
</div>
);
return (
<Host>
<InteractiveContainer disabled={this.disabled}>
<article
aria-busy={toAriaBoolean(loading)}
class={{
[CSS.container]: true,
}}
>
{headerNode}
<section
aria-labelledby={IDS.toggle}
class={CSS.content}
hidden={!open}
id={IDS.content}
ref={this.setTransitionEl}
>
{this.renderScrim()}
</section>
</article>
</InteractiveContainer>
</Host>
);
}
}