-
Notifications
You must be signed in to change notification settings - Fork 2
/
step.ts
317 lines (276 loc) · 9.68 KB
/
step.ts
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
import { LitElement, html, nothing } from 'lit';
import { property, query, queryAssignedElements } from 'lit/decorators.js';
import { type Ref, createRef, ref } from 'lit/directives/ref.js';
import { when } from 'lit/directives/when.js';
import { EaseInOut } from '../../animations/easings.js';
import { addAnimationController } from '../../animations/player.js';
import { themes } from '../../theming/theming-decorator.js';
import { watch } from '../common/decorators/watch.js';
import { registerComponent } from '../common/definitions/register.js';
import { partNameMap } from '../common/util.js';
import { type Animation, animations } from './animations.js';
import { styles as shared } from './themes/step/shared/step.common.css.js';
import { styles } from './themes/step/step.base.css.js';
import { all } from './themes/step/themes.js';
/**
* The step component is used within the `igc-stepper` element and it holds the content of each step.
* It also supports custom indicators, title and subtitle.
*
* @element igc-step
*
* @slot - Renders the content of the step.
* @slot indicator - Renders the indicator of the step. By default, it displays the step index + 1.
* @slot title - Renders the title of the step.
* @slot subtitle - Renders the subtitle of the step.
*
* @csspart header-container - Wrapper of the step's `header` and its separators.
* @csspart disabled - Indicates a disabled state. Applies to `header-container`.
* @csspart complete-start - Indicates a complete state of the current step. Applies to `header-container`.
* @csspart complete-end - Indicates a complete state of the previous step. Applies to `header-container`.
* @csspart optional - Indicates an optional state. Applies to `header-container`.
* @csspart invalid - Indicates an invalid state. Applies to `header-container`.
* @csspart top - Indicates that the title should be above the indicator. Applies to `header-container`.
* @csspart bottom - Indicates that the title should be below the indicator. Applies to `header-container`.
* @csspart start - Indicates that the title should be before the indicator. Applies to `header-container`.
* @csspart end - Indicates that the title should be after the indicator. Applies to `header-container`.
* @csspart header - Wrapper of the step's `indicator` and `text`.
* @csspart indicator - The indicator of the step.
* @csspart text - Wrapper of the step's `title` and `subtitle`.
* @csspart empty - Indicates that no title and subtitle has been provided to the step. Applies to `text`.
* @csspart title - The title of the step.
* @csspart subtitle - The subtitle of the step.
* @csspart body - Wrapper of the step's `content`.
* @csspart content - The steps `content`.
*/
@themes(all)
export default class IgcStepComponent extends LitElement {
public static readonly tagName = 'igc-step';
public static override styles = [styles, shared];
/* blazorSuppress */
public static register() {
registerComponent(IgcStepComponent);
}
private bodyRef: Ref<HTMLElement> = createRef();
private animationPlayer = addAnimationController(this, this.bodyRef);
@queryAssignedElements({ slot: 'title' })
private _titleChildren!: Array<HTMLElement>;
@queryAssignedElements({ slot: 'subtitle' })
private _subTitleChildren!: Array<HTMLElement>;
/* blazorSuppress */
@query('[part~="header"]')
public header!: HTMLElement;
/* blazorSuppress */
@query('[part~="body"]')
public contentBody!: HTMLElement;
/** Gets/sets whether the step is invalid. */
@property({ reflect: true, type: Boolean })
public invalid = false;
/** Gets/sets whether the step is activе. */
@property({ reflect: true, type: Boolean })
public active = false;
/**
* Gets/sets whether the step is optional.
*
* @remarks
* Optional steps validity does not affect the default behavior when the stepper is in linear mode i.e.
* if optional step is invalid the user could still move to the next step.
*/
@property({ type: Boolean })
public optional = false;
/** Gets/sets whether the step is interactable. */
@property({ reflect: true, type: Boolean })
public disabled = false;
/** Gets/sets whether the step is completed.
*
* @remarks
* When set to `true` the following separator is styled `solid`.
*/
@property({ reflect: true, type: Boolean })
public complete = false;
/** @hidden @internal @private */
@property({ attribute: false })
public previousComplete = false;
/** @hidden @internal @private */
@property({ attribute: false })
public stepType: 'indicator' | 'title' | 'full' = 'full';
/** @hidden @internal @private */
@property({ attribute: false })
public titlePosition?: 'bottom' | 'top' | 'end' | 'start';
/** @hidden @internal @private */
@property({ attribute: false })
public orientation: 'horizontal' | 'vertical' = 'horizontal';
/** @hidden @internal @private */
@property({ attribute: false })
public index = -1;
/** @hidden @internal @private */
@property({ attribute: false })
public contentTop = false;
/** @hidden @internal @private */
@property({ attribute: false })
public linearDisabled = false;
/** @hidden @internal @private */
@property({ attribute: false })
public visited = false;
/** @hidden @intrnal @private */
@property({ attribute: false })
public animation: Animation = 'fade';
/** @hidden @internal @private */
@property({ attribute: false })
public animationDuration = 350;
public async toggleAnimation(
type: 'in' | 'out',
direction: 'normal' | 'reverse' = 'normal'
) {
const animation = animations.get(this.animation)!.get(type)!;
const options: KeyframeAnimationOptions = {
duration: this.animationDuration,
easing: EaseInOut.Quad,
direction,
};
const [_, event] = await Promise.all([
this.animationPlayer.stopAll(),
this.animationPlayer.play(animation(options)),
]);
return event.type;
}
@watch('active', { waitUntilFirstUpdate: true })
protected activeChange() {
if (this.active) {
this.dispatchEvent(
new CustomEvent('stepActiveChanged', { bubbles: true, detail: false })
);
}
}
@watch('disabled', { waitUntilFirstUpdate: true })
@watch('invalid', { waitUntilFirstUpdate: true })
@watch('optional', { waitUntilFirstUpdate: true })
protected disabledInvalidOptionalChange() {
this.dispatchEvent(
new CustomEvent('stepDisabledInvalidChanged', { bubbles: true })
);
}
@watch('complete', { waitUntilFirstUpdate: true })
protected completeChange() {
this.dispatchEvent(
new CustomEvent('stepCompleteChanged', { bubbles: true })
);
}
private handleClick(event: MouseEvent): void {
event.stopPropagation();
if (this.isAccessible) {
this.dispatchEvent(
new CustomEvent('stepActiveChanged', { bubbles: true, detail: true })
);
}
}
private handleKeydown(event: KeyboardEvent): void {
this.dispatchEvent(
new CustomEvent('stepHeaderKeydown', {
bubbles: true,
detail: { event, focusedStep: this },
})
);
}
/** @hidden @internal */
public get isAccessible(): boolean {
return !this.disabled && !this.linearDisabled;
}
private get headerContainerParts() {
return {
'header-container': true,
disabled: !this.isAccessible,
'complete-start': this.complete,
'complete-end': this.previousComplete,
optional: this.optional,
invalid:
this.invalid && this.visited && !this.active && this.isAccessible,
top: this.titlePosition === 'top',
bottom:
this.titlePosition === 'bottom' ||
(this.orientation === 'horizontal' && !this.titlePosition),
start: this.titlePosition === 'start',
end:
this.titlePosition === 'end' ||
(this.orientation === 'vertical' && !this.titlePosition),
};
}
private get textParts() {
return {
text: true,
empty:
this.stepType === 'indicator'
? true
: this.stepType === 'full' &&
!this._titleChildren.length &&
!this._subTitleChildren.length,
};
}
protected renderIndicator() {
if (this.stepType !== 'title') {
return html`
<div part="indicator">
<slot name="indicator">
<span>${this.index + 1}</span>
</slot>
</div>
`;
}
return nothing;
}
protected renderTitleAndSubtitle() {
return html`
<div part="${partNameMap(this.textParts)}">
<div part="title">
<slot name="title"></slot>
</div>
<div part="subtitle">
<slot name="subtitle"></slot>
</div>
</div>
`;
}
protected renderContent() {
return html`<div
${ref(this.bodyRef)}
id="igc-step-content-${this.index}"
part="body"
role="tabpanel"
aria-labelledby="igc-step-header-${this.index}"
>
<div part="content">
<slot></slot>
</div>
</div>`;
}
protected override render() {
return html`
${when(
this.contentTop && this.orientation === 'horizontal',
() => this.renderContent(),
() => nothing
)}
<div part="${partNameMap(this.headerContainerParts)}">
<div
part="header"
tabindex="${this.active ? '0' : '-1'}"
role="tab"
aria-selected="${this.active}"
@click=${this.handleClick}
@keydown=${this.handleKeydown}
>
${this.renderIndicator()} ${this.renderTitleAndSubtitle()}
</div>
</div>
${when(
this.orientation === 'vertical' || !this.contentTop,
() => this.renderContent(),
() => nothing
)}
`;
}
}
declare global {
interface HTMLElementTagNameMap {
'igc-step': IgcStepComponent;
}
}