-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtab-group.ts
164 lines (142 loc) · 4.67 KB
/
tab-group.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
import { customElement, html, LitElement, property, PropertyValues, TemplateResult } from "lit-element";
import "../divider";
import { sharedStyles } from "../style/shared";
import { AriaRole } from "../util/aria";
import { cssResult } from "../util/css";
import { addListener, EventListenerSubscription, removeListeners } from "../util/event";
import { CAN_USE_RESIZE_OBSERVER, onSizeChanged } from "../util/resize";
import styles from "./tab-group.scss";
/**
* Properties of the tab.
*/
export interface ITabGroupProperties {
align: TabGroupAlignment;
filled: boolean;
vertical: boolean;
role: AriaRole;
}
export type TabGroupAlignment = "start" | "center" | "end" | "stretch";
/**
* Organize navigation between groups of content.
* @slot - Default content. Add wl-tab elements here.
* @cssprop --tab-group-color - Default color
* @cssprop --tab-group-bg - Default background
* @cssprop --tab-group-filled-color - Color when filled
* @cssprop --tab-group-filled-bg - Background when filled
* @cssprop --tab-group-indicator-size - Size of the indicator
* @cssprop --tab-group-indicator-scale - Scale of the indicator
* @cssprop --tab-group-indicator-bg - Background of the indicator
* @cssprop --tab-group-indicator-transition - Transition of the indicator
*/
@customElement("wl-tab-group")
export class TabGroup extends LitElement implements ITabGroupProperties {
static styles = [cssResult(styles), sharedStyles];
/**
* Alignment of the tabs.
* @attr
*/
@property({type: String, reflect: true}) align: TabGroupAlignment = "start";
/**
* Adds a filled color style to the tab.
* @attr
*/
@property({type: Boolean, reflect: true}) filled: boolean = false;
/**
* Makes the tabs vertical.
* @attr
*/
@property({type: Boolean, reflect: true}) vertical: boolean = false;
/**
* Role of the tab.
* @attr
*/
@property({type: String, reflect: true}) role: AriaRole = "tablist";
/**
* Returns the main slot element.
*/
get $slot (): HTMLSlotElement {
return this.shadowRoot!.querySelector<HTMLSlotElement>("slot")!;
}
/**
* Current event listeners.
*/
protected listeners: EventListenerSubscription[] = [];
/**
* Hooks up the element.
*/
connectedCallback () {
super.connectedCallback();
this.listeners.push(addListener(this, "change", this.updateIndicatorPosition.bind(this)));
}
/**
* Removes listeners.
*/
disconnectedCallback () {
super.disconnectedCallback();
removeListeners(this.listeners);
}
/**
* Hooks up the element.
* @param props
*/
protected firstUpdated (props: PropertyValues): void {
super.firstUpdated(props);
// We need to update the indicator position whenever the size of one of the tab changes.
// Either attach a resize observer or fallback to listening to window resizes
CAN_USE_RESIZE_OBSERVER
? onSizeChanged(this.$slot.parentElement!, this.updateIndicatorPosition.bind(this), {debounceMs: 100})
: addListener(window, "resize", this.updateIndicatorPosition.bind(this), {passive: true});
}
/**
* Updates the position and size of the indicator.
*/
protected updateIndicatorPosition () {
// Grab the nodes from the slot
const nodes = (Array.from(this.$slot.assignedNodes()
.filter(node => node.nodeType === 1)) as any) as HTMLElement[];
// Find the current checked node
let checkedNode: HTMLElement | null = null;
for (const node of nodes) {
if (node.hasAttribute("checked")) {
checkedNode = node;
break;
}
}
// Compute the size and offset of the indicator
const checkedIndex = checkedNode == null ? -1 : nodes.indexOf(checkedNode);
const checkedNodeSize = checkedNode == null ? 0 : this.getNodeSize(checkedNode);
const indicatorOffset = nodes
.filter(node => nodes.indexOf(node) < checkedIndex)
.map(this.getNodeSize.bind(this))
.reduce((acc: number, width: number) => acc + width, 0);
// Set the CSS variables that moves the indicator
this.style.setProperty("--_indicator-offset", `${indicatorOffset}px`);
this.style.setProperty("--_indicator-size", `${checkedNodeSize}px`);
}
/**
* Returns the size of the node depending on whether the tab group is vertical or horizontal.
* @param $node
*/
private getNodeSize ($node: HTMLElement) {
return this.vertical ? $node.offsetHeight : $node.offsetWidth;
}
/**
* Returns the template of the element.
*/
protected render (): TemplateResult {
return html`
<div id="tabs-container">
<div id="tabs">
<slot @slotchange="${this.updateIndicatorPosition}"></slot>
<div id="indicator"></div>
</div>
</div>
<wl-divider id="divider" ?vertical="${this.vertical}"></wl-divider>
`;
}
}
declare global {
interface HTMLElementTagNameMap {
"wl-tab-group": TabGroup;
}
}