-
Notifications
You must be signed in to change notification settings - Fork 2
/
expansion-panel.ts
273 lines (234 loc) · 7.59 KB
/
expansion-panel.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
import { LitElement, html } from 'lit';
import { property, queryAssignedElements } from 'lit/decorators.js';
import { Ref, createRef, ref } from 'lit/directives/ref.js';
import { styles } from './themes/expansion-panel.base.css.js';
import { styles as shared } from './themes/shared/expansion-panel.common.css.js';
import { all } from './themes/themes.js';
import { addAnimationController } from '../../animations/player.js';
import { growVerIn, growVerOut } from '../../animations/presets/grow/index.js';
import { themes } from '../../theming/theming-decorator.js';
import {
addKeybindings,
altKey,
arrowDown,
arrowUp,
} from '../common/controllers/key-bindings.js';
import { registerComponent } from '../common/definitions/register.js';
import type { Constructor } from '../common/mixins/constructor.js';
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
import { createCounter } from '../common/util.js';
import IgcIconComponent from '../icon/icon.js';
export interface IgcExpansionPanelComponentEventMap {
igcOpening: CustomEvent<IgcExpansionPanelComponent>;
igcOpened: CustomEvent<IgcExpansionPanelComponent>;
igcClosing: CustomEvent<IgcExpansionPanelComponent>;
igcClosed: CustomEvent<IgcExpansionPanelComponent>;
}
/**
* The Expansion Panel Component provides a way to display information in a toggleable way -
* compact summary view containing title and description and expanded detail view containing
* additional content to the summary header.
*
* @element igc-expansion-panel
*
* @slot - renders the default content of the panel
* @slot title - renders the title of the panel's header
* @slot subtitle - renders the subtitle of the panel's header
* @slot indicator - renders the expand/collapsed indicator
* @slot indicator-expanded - renders the expanded state of the indicator
*
* @fires igcOpening - Emitted before opening the expansion panel.
* @fires igcOpened - Emitted after the expansion panel is opened.
* @fires igcClosing - Emitted before closing the expansion panel.
* @fires igcClosed - Emitted after the expansion panel is closed.
*
* @csspart header - The container of the expansion indicator, title and subtitle.
* @csspart title - The title container.
* @csspart subtitle - The subtitle container.
* @csspart indicator - The indicator container.
* @csspart content - The expansion panel's content wrapper.
*/
@themes(all)
export default class IgcExpansionPanelComponent extends EventEmitterMixin<
IgcExpansionPanelComponentEventMap,
Constructor<LitElement>
>(LitElement) {
public static readonly tagName = 'igc-expansion-panel';
public static styles = [styles, shared];
/* blazorSuppress */
public static register() {
registerComponent(this, IgcIconComponent);
}
private static readonly increment = createCounter();
private headerRef: Ref<HTMLDivElement> = createRef();
private contentRef: Ref<HTMLDivElement> = createRef();
private animationPlayer = addAnimationController(this, this.contentRef);
@queryAssignedElements({ slot: 'indicator-expanded' })
private _indicatorExpandedElements!: HTMLElement[];
/**
* Indicates whether the contents of the control should be visible.
* @attr
*/
@property({ reflect: true, type: Boolean })
public open = false;
/**
* Get/Set whether the expansion panel is disabled. Disabled panels are ignored for user interactions.
* @attr
*/
@property({ reflect: true, type: Boolean })
public disabled = false;
/**
* The indicator position of the expansion panel.
* @attr indicator-position
*/
@property({ reflect: true, attribute: 'indicator-position' })
public indicatorPosition: 'start' | 'end' | 'none' = 'start';
private panelId!: string;
constructor() {
super();
addKeybindings(this, {
ref: this.headerRef,
bindingDefaults: { preventDefault: true },
})
.setActivateHandler(this.toggleWithEvent)
.set([altKey, arrowDown], this.openWithEvent)
.set([altKey, arrowUp], this.closeWithEvent);
}
public override connectedCallback() {
super.connectedCallback();
this.panelId =
this.getAttribute('id') ||
`igc-expansion-panel-${IgcExpansionPanelComponent.increment()}`;
}
private handleClicked() {
this.headerRef.value!.focus();
this.toggleWithEvent();
}
private toggleWithEvent() {
if (this.disabled) return;
this.open ? this.closeWithEvent() : this.openWithEvent();
}
private async toggleAnimation(dir: 'open' | 'close') {
const animation = dir === 'open' ? growVerIn : growVerOut;
const [_, event] = await Promise.all([
this.animationPlayer.stopAll(),
this.animationPlayer.play(animation()),
]);
return event.type === 'finish';
}
private async openWithEvent() {
if (
this.open ||
!this.emitEvent('igcOpening', { cancelable: true, detail: this })
) {
return;
}
this.open = true;
if (await this.toggleAnimation('open')) {
this.emitEvent('igcOpened', { detail: this });
}
}
private async closeWithEvent() {
if (
!this.open ||
!this.emitEvent('igcClosing', { cancelable: true, detail: this })
) {
return;
}
this.open = false;
if (await this.toggleAnimation('close')) {
this.emitEvent('igcClosed', { detail: this });
}
}
/** Toggles panel open state. */
public toggle(): void {
this.open ? this.hide() : this.show();
}
/** Hides the panel content. */
public hide(): void {
if (this.open) {
this.toggleAnimation('close');
}
this.open = false;
}
/** Shows the panel content. */
public show(): void {
if (!this.open) {
this.toggleAnimation('open');
}
this.open = true;
}
private handleSlotChange() {
this.requestUpdate();
}
private indicatorTemplate() {
const indicatorHidden =
this.open && this._indicatorExpandedElements.length > 0;
const indicatorExpandedHidden =
this._indicatorExpandedElements.length < 1 || !this.open;
return html`
<div part="indicator" aria-hidden="true">
<slot
name="indicator"
?hidden=${indicatorHidden}
@slotchange=${this.handleSlotChange}
>
<igc-icon
name=${this.open ? 'keyboard_arrow_up' : 'keyboard_arrow_down'}
collection="internal"
>
</igc-icon>
</slot>
<slot
name="indicator-expanded"
?hidden=${indicatorExpandedHidden}
@slotchange=${this.handleSlotChange}
></slot>
</div>
`;
}
private headerTemplate() {
return html`
<div
${ref(this.headerRef)}
part="header"
id="${this.panelId!}-header"
role="button"
aria-expanded="${this.open}"
aria-disabled="${this.disabled}"
aria-controls="${this.panelId!}-content"
tabindex=${this.disabled ? '-1' : '0'}
@click=${this.handleClicked}
>
${this.indicatorTemplate()}
<div>
<slot name="title" part="title"></slot>
<slot name="subtitle" part="subtitle"></slot>
</div>
</div>
`;
}
private contentTemplate() {
return html`
<div
${ref(this.contentRef)}
part="content"
role="region"
id="${this.panelId!}-content"
aria-labelledby="${this.panelId!}-header"
.inert=${!this.open}
aria-hidden=${!this.open}
>
<slot></slot>
</div>
`;
}
protected override render() {
return html`${this.headerTemplate()}${this.contentTemplate()}`;
}
}
declare global {
interface HTMLElementTagNameMap {
'igc-expansion-panel': IgcExpansionPanelComponent;
}
}