-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
153 lines (144 loc) · 3.43 KB
/
main.js
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
import { html, css, define, observe, raw, state } from "./BlissElement";
const Tabs = {
styles: css`
:host nav {
display: inline-flex;
}
`,
attrs: {
activeTab: { type: Number },
},
// constructorCallback() {
// console.log("CONSTRUCTOR", this);
// debugger;
// },
// componentWillLoad() {
// return {
// promise: new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve("aoeu");
// }, 3000);
// }),
// placeholder: html`FOO BAR`,
// error: {
// message: html`This is an error`,
// callback: function () {
// console.log("ERROR", this);
// },
// },
// };
// },
// componentDidLoad() {
// debugger;
// },
connectedCallback() {
observe(() => {
this[state].activeTab = this[state].activeTab ?? 0;
});
},
render() {
return html`
<nav part="tabs">
<slot name="bliss-tab"></slot>
</nav>
<div part="content">
<slot name="bliss-tab-content"></slot>
</div>
`;
},
};
define("bliss-tabs", Tabs);
function tabbable(rootNode = "bliss-tabs") {
return {
attrs: {
active: { type: Boolean },
},
connectedCallback() {
this.tabs = this.getContext(rootNode);
const nodes = Array.from(
this.tabs.querySelectorAll(`:scope > ${this.tagName}`)
);
this[state].index = nodes.findIndex((node) => node === this);
// If this.active is true, then set tabs[state]activeTab to be this tab.
observe(() => {
if (this[state].active) this.tabs[state].activeTab = this[state].index;
});
// If tabs[state].activeTab is this tab, then set this tab's active prop to true.
observe(() => {
this[state].active = this.tabs[state].activeTab === this[state].index;
});
},
disconnectedCallback() {
if (this.tabs[state].activeTab === this[state].index)
this.tabs[state].activeTab = undefined;
},
};
}
const keyboardNavigable = {
attrs: { tabindex: { type: Number, default: 0 } },
connectedCallback() {
this.addEventListener("keypress", (e) => {
if (
e.target === this &&
!this[state].disabled &&
["Enter", " "].includes(e.key)
) {
this.click(e);
}
});
},
onclick(e) {
debugger;
},
};
const Tab = {
styles: css`
:host {
border-bottom: 2px solid transparent;
cursor: pointer;
}
:host([active]) {
border-bottom-color: blueviolet;
}
:host([disabled]) {
opacity: 0.5;
cursor: not-allowed;
}
:host(:not(:nth-of-type(1))) {
margin-left: 1rem;
}
`,
render() {
return html`<slot></slot>`;
},
onclick(e) {
if (!this[state].disabled) {
this.tabs[state].activeTab = this[state].index;
}
},
};
define("bliss-tab", Tab, {
mixins: [tabbable("bliss-tabs"), keyboardNavigable],
});
const TabContent = {
connectedCallback() {
observe(() => {
const activeIsNotHost = this.tabs[state].activeTab !== this[state].index;
this[state].disabled = activeIsNotHost;
this[state].hidden = activeIsNotHost;
});
},
render() {
return html`<slot></slot>`;
},
};
define("bliss-tab-content", TabContent, { mixins: tabbable("bliss-tabs") });
const AlertButton = {
onclick() {
alert("You have been alerted");
},
};
define("bliss-alert-button", AlertButton, {
base: HTMLButtonElement,
extend: "button",
});