This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
calcite-panel.tsx
268 lines (223 loc) · 6.98 KB
/
calcite-panel.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
import {
Component,
Element,
Event,
EventEmitter,
Host,
Method,
Prop,
Watch,
h
} from "@stencil/core";
import { CSS, ICONS, SLOTS, TEXT } from "./resources";
import { getElementDir, getSlotted } from "../utils/dom";
import classnames from "classnames";
import { CSS_UTILITY } from "../utils/resources";
import { VNode } from "@stencil/core/internal";
import { CalciteScale, CalciteTheme } from "../interfaces";
import CalciteScrim from "../utils/CalciteScrim";
type FocusId = "dismiss-button";
/**
* @slot header-content - A slot for adding content in the center of the header.
* @slot header-leading-content - A slot for adding a `calcite-action` on the leading side of the header.
* @slot header-trailing-content - A slot for adding a `calcite-action` on the trailing side of the header.
* @slot fab - A slot for adding a `calcite-fab` (floating action button) to perform an action.
* @slot footer - A slot for adding `calcite-button`s to the footer.
* @slot - A slot for adding content to the panel.
*/
@Component({
tag: "calcite-panel",
styleUrl: "calcite-panel.scss",
shadow: true
})
export class CalcitePanel {
// --------------------------------------------------------------------------
//
// Properties
//
// --------------------------------------------------------------------------
/**
* Hides the panel.
*/
@Prop({ mutable: true, reflect: true }) dismissed = false;
@Watch("dismissed")
dismissedHandler() {
this.calcitePanelDismissedChange.emit();
}
/**
* When true, disabled prevents interaction. This state shows items with lower opacity/grayed.
*/
@Prop({ reflect: true }) disabled = false;
/**
* Displays a close button in the trailing side of the header.
*/
@Prop({ reflect: true }) dismissible = false;
/**
* Specifies the maxiumum height of the panel.
*/
@Prop({ reflect: true }) heightScale: CalciteScale;
/**
* When true, content is waiting to be loaded. This state shows a busy indicator.
*/
@Prop({ reflect: true }) loading = false;
/**
* 'Close' text string for the close button. The close button will only be shown when 'dismissible' is true.
*/
@Prop() textClose = TEXT.close;
/**
* Used to set the component's color scheme.
*/
@Prop({ reflect: true }) theme: CalciteTheme;
// --------------------------------------------------------------------------
//
// Private Properties
//
// --------------------------------------------------------------------------
@Element() el: HTMLCalcitePanelElement;
dismissButtonEl: HTMLCalciteActionElement;
containerEl: HTMLElement;
// --------------------------------------------------------------------------
//
// Events
//
// --------------------------------------------------------------------------
/**
* Emitted when the close button has been clicked.
*/
@Event() calcitePanelDismissedChange: EventEmitter;
/**
* Emitted when the content has been scrolled.
*/
@Event() calcitePanelScroll: EventEmitter;
// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------
panelKeyUpHandler = (event: KeyboardEvent): void => {
if (event.key === "Escape") {
this.dismiss();
}
};
dismiss = (): void => {
this.dismissed = true;
};
panelScrollHandler = (): void => {
this.calcitePanelScroll.emit();
};
// --------------------------------------------------------------------------
//
// Methods
//
// --------------------------------------------------------------------------
@Method()
async setFocus(focusId?: FocusId) {
if (focusId === "dismiss-button") {
this.dismissButtonEl?.setFocus();
return;
}
this.containerEl?.focus();
}
// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------
renderHeaderLeadingContent(): VNode {
const hasLeadingContent = getSlotted(this.el, SLOTS.headerLeadingContent).length;
return hasLeadingContent ? (
<div key="header-leading-content" class={CSS.headerLeadingContent}>
<slot name={SLOTS.headerLeadingContent} />
</div>
) : null;
}
renderHeaderContent(): VNode {
return (
<div key="header-content" class={CSS.headerContent}>
<slot name={SLOTS.headerContent} />
</div>
);
}
renderHeaderTrailingContent(): VNode {
const { dismiss, dismissible, textClose } = this;
const dismissibleNode = dismissible ? (
<calcite-action
ref={(dismissButtonEl) => (this.dismissButtonEl = dismissButtonEl)}
aria-label={textClose}
text={textClose}
onClick={dismiss}
>
<calcite-icon scale="s" icon={ICONS.close} />
</calcite-action>
) : null;
const slotNode = <slot name={SLOTS.headerTrailingContent} />;
return (
<div key="header-trailing-content" class={CSS.headerTrailingContent}>
{slotNode}
{dismissibleNode}
</div>
);
}
renderHeader(): VNode {
const headerLeadingContentNode = this.renderHeaderLeadingContent();
const headerContentNode = this.renderHeaderContent();
const headerTrailingContentNode = this.renderHeaderTrailingContent();
const canDisplayHeader =
headerContentNode || headerLeadingContentNode || headerTrailingContentNode;
return canDisplayHeader ? (
<header class={CSS.header}>
{headerLeadingContentNode}
{headerContentNode}
{headerTrailingContentNode}
</header>
) : null;
}
renderFooter(): VNode {
const { el } = this;
const hasFooter = el.querySelector(`[slot=${SLOTS.footer}]`);
return hasFooter ? (
<footer class={CSS.footer}>
<slot name={SLOTS.footer} />
</footer>
) : null;
}
renderContent(): VNode {
return (
<section class={CSS.contentContainer} onScroll={this.panelScrollHandler}>
<slot />
{this.renderFab()}
</section>
);
}
renderFab(): VNode {
const hasFab = this.el.querySelector(`[slot=${SLOTS.fab}]`);
return hasFab ? (
<div class={CSS.fabContainer}>
<slot name={SLOTS.fab} />
</div>
) : null;
}
render() {
const { dismissed, disabled, dismissible, el, loading, panelKeyUpHandler } = this;
const rtl = getElementDir(el) === "rtl";
return (
<Host>
<article
aria-busy={loading.toString()}
onKeyUp={panelKeyUpHandler}
tabIndex={dismissible ? 0 : -1}
hidden={dismissible && dismissed}
ref={(containerEl) => (this.containerEl = containerEl)}
class={classnames(CSS.container, {
[CSS_UTILITY.rtl]: rtl
})}
>
{this.renderHeader()}
{this.renderContent()}
{this.renderFooter()}
</article>
<CalciteScrim loading={loading} disabled={disabled} />
</Host>
);
}
}