-
Notifications
You must be signed in to change notification settings - Fork 6
/
vwc-side-drawer-base.ts
203 lines (174 loc) · 4.51 KB
/
vwc-side-drawer-base.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
import 'blocking-elements';
import 'wicg-inert';
import {
html, LitElement, TemplateResult, property, query
} from 'lit-element';
import { ifDefined } from 'lit-html/directives/if-defined.js';
import { classMap } from 'lit-html/directives/class-map.js';
import type { DocumentWithBlockingElements } from 'blocking-elements';
const blockingElements =
(document as DocumentWithBlockingElements).$blockingElements;
/**
* @cssprop [side-drawer-background-color=Current theme's canvas (background) color] - Controls the background of the side drawer
* @cssprop [side-drawer-color=Current theme's on-canvas (text) color] - Controls the color of the side drawer
* @cssprop [side-drawer-inline-size=280px] - Controls the inline size of the side drawer
* @cssprop [side-drawer-padding-top-bar=16px] - Controls the padding of the side drawer's top bar
* @cssprop [side-drawer-padding-body=16px] - Controls the padding of the side drawer's body
* @cssprop [side-drawer-z-index=6] - Controls the z-index of the side drawer
* */
export class VWCSideDrawerBase extends LitElement {
@query('.side-drawer') protected rootEl!: HTMLElement;
/**
* @prop alternate - [Applies scheme alternate region](../../common/scheme/readme.md)
* accepts boolean value
* @public
* */
@property({
type: Boolean,
reflect: true
})
alternate = false;
/**
* @prop hasTopBar - adds top bar to the side drawer
* accepts boolean value
* @public
* */
@property({
type: Boolean,
reflect: true
})
hasTopBar?: boolean;
/**
* @prop modal - sets the side drawer's type to modal
* accepts boolean value
* @public
* */
@property({
type: Boolean,
reflect: true
})
modal = false;
/**
* @prop open - indicates whether the side drawer is open
* accepts boolean value
* */
@property({
type: Boolean,
reflect: true
})
open = false;
/**
* @prop side - sets the side of the side drawer
* accepts "start" | "end"
* @public
* */
@property({
type: String,
reflect: true
})
position?: 'start' | 'end';
/**
* Opens the side drawer from the closed state.
* @public
*/
show(): void {
this.open = true;
}
/**
* Closes the side drawer from the open state.
* @public
*/
hide(): void {
this.open = false;
}
override disconnectedCallback(): void {
super.disconnectedCallback();
this.#releaseFocusTrap();
}
protected override render(): TemplateResult {
const topBar = this.hasTopBar ? this.renderTopBar() : '';
const scrim = (this.modal && this.open) ? this.renderScrim() : '';
const alternate = this.alternate ? 'vvd-scheme-alternate' : undefined;
const end = this.position === 'end';
const classes = {
'side-drawer-alternate': this.alternate,
'side-drawer-modal': this.modal,
'side-drawer-open': this.open,
'side-drawer-end': end,
};
return html`
<aside part="${ifDefined(alternate)}" class="side-drawer ${classMap(classes)}"
@transitionend=${this.#handleTransitionEnd} @keydown=${this.#handleKeydown}>
${topBar}
<div class="side-drawer-content">
<slot></slot>
</div>
</aside>
<div class="side-drawer-app-content">
<slot name="app-content"></slot>
</div>
${scrim}
`;
}
private renderTopBar(): TemplateResult {
return html`
<header class="side-drawer-top-bar">
<slot name="top-bar"></slot>
</header>`;
}
private renderScrim(): TemplateResult {
return html`
<div class="side-drawer-scrim" @click="${this.#handleScrimClick}" @keydown="${this.#handleScrimClick}"></div>`;
}
#handleScrimClick(): void {
if (this.modal && this.open) {
this.hide();
}
}
#handleKeydown = ({ key }: KeyboardEvent): void => {
if (this.open && key === 'Escape') {
this.hide();
}
};
#handleTransitionEnd = (): void => {
// when side drawer finishes open animation
if (this.open) {
this.#opened();
} else {
// when side drawer finishes hide animation
this.#closed();
}
};
#opened(): void {
if (this.modal) {
this.#trapFocus();
}
this.#notifyOpen();
}
#closed(): void {
if (this.modal) {
this.#releaseFocusTrap();
}
this.#notifyClose();
}
#createAndDispatchEvent(eventName: string): void {
const init: CustomEventInit = {
bubbles: true,
composed: true
};
const ev = new CustomEvent(eventName, init);
this.dispatchEvent(ev);
}
#notifyClose(): void {
this.#createAndDispatchEvent('closed');
}
#notifyOpen(): void {
this.#createAndDispatchEvent('opened');
}
#trapFocus(): void {
blockingElements.push(this.rootEl);
}
#releaseFocusTrap(): void {
blockingElements.remove(this.rootEl);
}
}