-
Notifications
You must be signed in to change notification settings - Fork 76
/
flow-item.tsx
426 lines (368 loc) · 13 KB
/
flow-item.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import {
Component,
Element,
Event,
EventEmitter,
h,
Host,
Method,
Prop,
State,
VNode,
Watch,
} from "@stencil/core";
import { getElementDir } from "../../utils/dom";
import {
InteractiveComponent,
InteractiveContainer,
updateHostInteraction,
} from "../../utils/interactive";
import {
componentFocusable,
LoadableComponent,
setComponentLoaded,
setUpLoadableComponent,
} from "../../utils/loadable";
import { connectLocalized, disconnectLocalized, LocalizedComponent } from "../../utils/locale";
import {
connectMessages,
disconnectMessages,
setUpMessages,
T9nComponent,
updateMessages,
} from "../../utils/t9n";
import { HeadingLevel } from "../functional/Heading";
import { SLOTS as PANEL_SLOTS } from "../panel/resources";
import { OverlayPositioning } from "../../utils/floating-ui";
import { CollapseDirection } from "../interfaces";
import { Scale } from "../interfaces";
import { FlowItemMessages } from "./assets/flow-item/t9n";
import { CSS, ICONS, SLOTS } from "./resources";
/**
* @slot - A slot for adding custom content.
* @slot action-bar - A slot for adding a `calcite-action-bar` to the component.
* @slot alerts - A slot for adding `calcite-alert`s to the component.
* @slot content-top - A slot for adding content above the unnamed (default) slot and below the action-bar slot (if populated).
* @slot content-bottom - A slot for adding content below the unnamed (default) slot and above the footer slot (if populated)
* @slot header-actions-start - A slot for adding `calcite-action`s or content to the start side of the component's header.
* @slot header-actions-end - A slot for adding `calcite-action`s or content to the end side of the component's header.
* @slot header-content - A slot for adding custom content to the component's header.
* @slot header-menu-actions - A slot for adding an overflow menu with `calcite-action`s inside a `calcite-dropdown`.
* @slot fab - A slot for adding a `calcite-fab` (floating action button) to perform an action.
* @slot footer - A slot for adding custom content to the component's footer. Should not be used with the `"footer-start"` or `"footer-end"` slots.
* @slot footer-actions - [Deprecated] Use the `"footer"` slot instead. A slot for adding `calcite-button`s to the component's footer.
* @slot footer-end - A slot for adding a trailing footer custom content. Should not be used with the `"footer"` slot.
* @slot footer-start - A slot for adding a leading footer custom content. Should not be used with the `"footer"` slot.
*/
@Component({
tag: "calcite-flow-item",
styleUrl: "flow-item.scss",
shadow: true,
assetsDirs: ["assets"],
})
export class FlowItem
implements InteractiveComponent, LoadableComponent, LocalizedComponent, T9nComponent
{
// --------------------------------------------------------------------------
//
// Properties
//
// --------------------------------------------------------------------------
/** When `true`, displays a close button in the trailing side of the component's header. */
@Prop({ reflect: true }) closable = false;
/** When `true`, the component will be hidden. */
@Prop({ reflect: true }) closed = false;
/**
* When `true`, hides the component's content area.
*/
@Prop({ reflect: true }) collapsed = false;
/**
* Specifies the direction of the collapse.
*
* @internal
*/
@Prop() collapseDirection: CollapseDirection = "down";
/**
* When `true`, the component is collapsible.
*/
@Prop({ reflect: true }) collapsible = false;
/**
* When provided, the method will be called before it is removed from its parent `calcite-flow`.
*/
@Prop() beforeBack: () => Promise<void>;
/** Passes a function to run before the component closes. */
@Prop() beforeClose: () => Promise<void>;
/** A description for the component. */
@Prop() description: string;
/**
* When `true`, interaction is prevented and the component is displayed with lower opacity.
*/
@Prop({ reflect: true }) disabled = 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`, the action menu items in the `header-menu-actions` slot are open.
*/
@Prop({ reflect: true }) menuOpen = false;
/**
* 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<FlowItemMessages>;
@Watch("messageOverrides")
onMessagesChange(): void {
/* wired up by t9n util */
}
/**
* 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: FlowItemMessages;
/**
* 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";
/** Specifies the size of the component. */
@Prop({ reflect: true }) scale: Scale = "m";
/**
* When `true`, displays a back button in the component's header.
*
* @internal
*/
@Prop() showBackButton = false;
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback(): void {
connectLocalized(this);
connectMessages(this);
}
async componentWillLoad(): Promise<void> {
await setUpMessages(this);
setUpLoadableComponent(this);
}
componentDidRender(): void {
updateHostInteraction(this);
}
disconnectedCallback(): void {
disconnectLocalized(this);
disconnectMessages(this);
}
componentDidLoad(): void {
setComponentLoaded(this);
}
// --------------------------------------------------------------------------
//
// Events
//
// --------------------------------------------------------------------------
/**
* Fires when the back button is clicked.
*/
@Event({ cancelable: true }) calciteFlowItemBack: EventEmitter<void>;
/**
* Fires when the content is scrolled.
*/
@Event({ cancelable: false }) calciteFlowItemScroll: EventEmitter<void>;
/**
* Fires when the close button is clicked.
*/
@Event({ cancelable: false }) calciteFlowItemClose: EventEmitter<void>;
/**
* Fires when the collapse button is clicked.
*/
@Event({ cancelable: false }) calciteFlowItemToggle: EventEmitter<void>;
// --------------------------------------------------------------------------
//
// Private Properties
//
// --------------------------------------------------------------------------
@Element() el: HTMLCalciteFlowItemElement;
containerEl: HTMLCalcitePanelElement;
backButtonEl: HTMLCalciteActionElement;
@State() defaultMessages: FlowItemMessages;
@State() effectiveLocale = "";
@Watch("effectiveLocale")
effectiveLocaleChange(): void {
updateMessages(this, this.effectiveLocale);
}
// --------------------------------------------------------------------------
//
// Methods
//
// --------------------------------------------------------------------------
/**
* Sets focus on the component.
*
* @returns promise.
*/
@Method()
async setFocus(): Promise<void> {
await componentFocusable(this);
const { backButtonEl, containerEl } = this;
if (backButtonEl) {
return backButtonEl.setFocus();
} else if (containerEl) {
return containerEl.setFocus();
}
}
/**
* Scrolls the component's content to a specified set of coordinates.
*
* @example
* myCalciteFlowItem.scrollContentTo({
* left: 0, // Specifies the number of pixels along the X axis to scroll the window or element.
* top: 0, // Specifies the number of pixels along the Y axis to scroll the window or element
* behavior: "auto" // Specifies whether the scrolling should animate smoothly (smooth), or happen instantly in a single jump (auto, the default value).
* });
* @param options - allows specific coordinates to be defined.
* @returns - promise that resolves once the content is scrolled to.
*/
@Method()
async scrollContentTo(options?: ScrollToOptions): Promise<void> {
await this.containerEl?.scrollContentTo(options);
}
// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------
handleInternalPanelScroll = (event: CustomEvent<void>): void => {
if (event.target !== this.containerEl) {
return;
}
event.stopPropagation();
this.calciteFlowItemScroll.emit();
};
handleInternalPanelClose = (event: CustomEvent<void>): void => {
if (event.target !== this.containerEl) {
return;
}
event.stopPropagation();
this.closed = true;
this.calciteFlowItemClose.emit();
};
handleInternalPanelToggle = (event: CustomEvent<void>): void => {
if (event.target !== this.containerEl) {
return;
}
event.stopPropagation();
this.collapsed = (event.target as HTMLCalcitePanelElement).collapsed;
this.calciteFlowItemToggle.emit();
};
backButtonClick = (): void => {
this.calciteFlowItemBack.emit();
};
setBackRef = (node: HTMLCalciteActionElement): void => {
this.backButtonEl = node;
};
setContainerRef = (node: HTMLCalcitePanelElement): void => {
this.containerEl = node;
};
// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------
renderBackButton(): VNode {
const { el } = this;
const rtl = getElementDir(el) === "rtl";
const { showBackButton, backButtonClick, messages } = this;
const label = messages.back;
const icon = rtl ? ICONS.backRight : ICONS.backLeft;
return showBackButton ? (
<calcite-action
aria-label={label}
class={CSS.backButton}
icon={icon}
key="flow-back-button"
onClick={backButtonClick}
ref={this.setBackRef}
scale="s"
slot="header-actions-start"
text={label}
title={label}
/>
) : null;
}
render(): VNode {
const {
collapsed,
collapseDirection,
collapsible,
closable,
closed,
description,
disabled,
heading,
headingLevel,
loading,
menuOpen,
messages,
overlayPositioning,
beforeClose,
} = this;
return (
<Host>
<InteractiveContainer disabled={disabled}>
<calcite-panel
beforeClose={beforeClose}
closable={closable}
closed={closed}
collapseDirection={collapseDirection}
collapsed={collapsed}
collapsible={collapsible}
description={description}
disabled={disabled}
heading={heading}
headingLevel={headingLevel}
loading={loading}
menuOpen={menuOpen}
messageOverrides={messages}
onCalcitePanelClose={this.handleInternalPanelClose}
onCalcitePanelScroll={this.handleInternalPanelScroll}
onCalcitePanelToggle={this.handleInternalPanelToggle}
overlayPositioning={overlayPositioning}
ref={this.setContainerRef}
scale={this.scale}
>
{this.renderBackButton()}
<slot name={SLOTS.actionBar} slot={PANEL_SLOTS.actionBar} />
<slot name={SLOTS.alerts} slot={PANEL_SLOTS.alerts} />
<slot name={SLOTS.headerActionsStart} slot={PANEL_SLOTS.headerActionsStart} />
<slot name={SLOTS.headerActionsEnd} slot={PANEL_SLOTS.headerActionsEnd} />
<slot name={SLOTS.headerContent} slot={PANEL_SLOTS.headerContent} />
<slot name={SLOTS.headerMenuActions} slot={PANEL_SLOTS.headerMenuActions} />
<slot name={SLOTS.fab} slot={PANEL_SLOTS.fab} />
<slot name={SLOTS.contentTop} slot={PANEL_SLOTS.contentTop} />
<slot name={SLOTS.contentBottom} slot={PANEL_SLOTS.contentBottom} />
<slot name={SLOTS.footerStart} slot={PANEL_SLOTS.footerStart} />
<slot name={SLOTS.footer} slot={PANEL_SLOTS.footer} />
<slot name={SLOTS.footerEnd} slot={PANEL_SLOTS.footerEnd} />
<slot name={SLOTS.footerActions} slot={PANEL_SLOTS.footerActions} />
<slot />
</calcite-panel>
</InteractiveContainer>
</Host>
);
}
}