@c2n/tabs 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Nguyen Thai Vinh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # @c2n/tabs
2
+
3
+ Accessible tab strip built with Lit. `c2-tabs` shows one content panel at a time; each `c2-tab` names the panel it controls with `for`.
4
+
5
+ ```bash
6
+ npm install @c2n/tabs
7
+ ```
8
+
9
+ ```html
10
+ <script type="module">
11
+ import '@c2n/tabs' // registers c2-tabs and c2-tab
12
+ </script>
13
+
14
+ <c2-tabs selected-tab="activity">
15
+ <c2-tab for="overview">Overview</c2-tab>
16
+ <c2-tab for="activity">Activity</c2-tab>
17
+ <c2-tab for="settings" disabled>Settings</c2-tab>
18
+
19
+ <div id="overview">…</div>
20
+ <div id="activity">…</div>
21
+ <div id="settings">…</div>
22
+ </c2-tabs>
23
+ ```
24
+
25
+ - Children that are `<c2-tab>` become the header; every other child is a panel matched by `id` to a tab's `for`. Only the selected panel is rendered.
26
+ - Omit `selected-tab` to start on the first enabled tab. Set the `selectedTab` property (or the attribute) to switch programmatically.
27
+ - Keyboard: Left/Right arrows, Home and End move between tabs and select them (disabled tabs are skipped); Enter and Space activate the focused tab. The strip exposes `tablist` / `tab` / `tabpanel` roles with `aria-selected`, `aria-controls`, `aria-labelledby` and a roving `tabindex`.
28
+ - Events: `c2-tabs` fires `change` (`detail: { value }`) after a user selection. Each `c2-tab` fires a cancelable `tab-change` (`detail: for`) first; call `preventDefault()` on it to keep the current tab.
29
+ - The selection indicator follows the selected tab and re-measures on resize and label changes. Respects `prefers-reduced-motion`.
30
+
31
+ Theming goes through CSS custom properties (`--c2-tabs--*`, `--c2-tabs__tab--*`, `--c2-tabs__tab__{hover,selected,focus,disabled}--*`, `--c2-tabs__indicator--*`). The full list is in `custom-elements.json` and on the docs site.
@@ -0,0 +1,161 @@
1
+ import { t as selectedTabContext } from "./tab-context-C4m2EOBP.js";
2
+ import { LitElement, html, unsafeCSS } from "lit";
3
+ import { customElement, property } from "lit/decorators.js";
4
+ import { isServer } from "lit-html/is-server.js";
5
+ //#region ../../../node_modules/@lit/context/lib/context-request-event.js
6
+ /**
7
+ * @license
8
+ * Copyright 2021 Google LLC
9
+ * SPDX-License-Identifier: BSD-3-Clause
10
+ */
11
+ var s$1 = class extends Event {
12
+ constructor(s, t, e, o) {
13
+ super("context-request", {
14
+ bubbles: !0,
15
+ composed: !0
16
+ }), this.context = s, this.contextTarget = t, this.callback = e, this.subscribe = o ?? !1;
17
+ }
18
+ };
19
+ //#endregion
20
+ //#region ../../../node_modules/@lit/context/lib/controllers/context-consumer.js
21
+ /**
22
+ * @license
23
+ * Copyright 2021 Google LLC
24
+ * SPDX-License-Identifier: BSD-3-Clause
25
+ */ var s = class {
26
+ constructor(t, s, i, h) {
27
+ if (this.subscribe = !1, this.provided = !1, this.value = void 0, this.t = (t, s) => {
28
+ this.unsubscribe && (this.unsubscribe !== s && (this.provided = !1, this.unsubscribe()), this.subscribe || this.unsubscribe()), this.value = t, this.host.requestUpdate(), this.provided && !this.subscribe || (this.provided = !0, this.callback && this.callback(t, s)), this.unsubscribe = s;
29
+ }, this.host = t, void 0 !== s.context) {
30
+ const t = s;
31
+ this.context = t.context, this.callback = t.callback, this.subscribe = t.subscribe ?? !1;
32
+ } else this.context = s, this.callback = i, this.subscribe = h ?? !1;
33
+ this.host.addController(this);
34
+ }
35
+ hostConnected() {
36
+ this.dispatchRequest();
37
+ }
38
+ hostDisconnected() {
39
+ this.unsubscribe && (this.unsubscribe(), this.unsubscribe = void 0);
40
+ }
41
+ dispatchRequest() {
42
+ this.host.dispatchEvent(new s$1(this.context, this.host, this.t, this.subscribe));
43
+ }
44
+ };
45
+ //#endregion
46
+ //#region ../../../node_modules/@lit/context/lib/decorators/consume.js
47
+ /**
48
+ * @license
49
+ * Copyright 2022 Google LLC
50
+ * SPDX-License-Identifier: BSD-3-Clause
51
+ */ function c({ context: c, subscribe: e }) {
52
+ return (o, n) => {
53
+ "object" == typeof n ? n.addInitializer((function() {
54
+ new s(this, {
55
+ context: c,
56
+ callback: (t) => {
57
+ o.set.call(this, t);
58
+ },
59
+ subscribe: e
60
+ });
61
+ })) : o.constructor.addInitializer(((o) => {
62
+ new s(o, {
63
+ context: c,
64
+ callback: (t) => {
65
+ o[n] = t;
66
+ },
67
+ subscribe: e
68
+ });
69
+ }));
70
+ };
71
+ }
72
+ //#endregion
73
+ //#region src/tab.scss?inline
74
+ var tab_default = ":host {\n display: inline-flex;\n align-items: center;\n box-sizing: border-box;\n user-select: none;\n cursor: pointer;\n white-space: nowrap;\n}\n\n:host([disabled]) {\n cursor: default;\n}\n\n:host(:focus) {\n outline: none;\n}";
75
+ //#endregion
76
+ //#region \0@oxc-project+runtime@0.148.0/helpers/esm/decorate.js
77
+ function __decorate(decorators, target, key, desc) {
78
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
79
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
80
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
81
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
82
+ }
83
+ //#endregion
84
+ //#region src/tab.ts
85
+ var Tab = class Tab extends LitElement {
86
+ static {
87
+ this.styles = unsafeCSS(tab_default);
88
+ }
89
+ constructor() {
90
+ super();
91
+ this.label = "";
92
+ this.for = "";
93
+ this.disabled = false;
94
+ this.selected = false;
95
+ this.selectedTab = "";
96
+ this.handleClick = () => {
97
+ this.activate();
98
+ };
99
+ this.handleKeydown = (event) => {
100
+ if (event.key === "Enter" || event.key === " ") {
101
+ event.preventDefault();
102
+ this.activate();
103
+ }
104
+ };
105
+ if (!isServer) {
106
+ this.setAttribute("slot", "tab");
107
+ this.addEventListener("click", this.handleClick);
108
+ this.addEventListener("keydown", this.handleKeydown);
109
+ }
110
+ }
111
+ connectedCallback() {
112
+ super.connectedCallback();
113
+ if (!this.hasAttribute("role")) this.setAttribute("role", "tab");
114
+ if (!this.hasAttribute("tabindex")) this.tabIndex = -1;
115
+ }
116
+ updated(changed) {
117
+ if (changed.has("selected")) this.setAttribute("aria-selected", String(this.selected));
118
+ if (changed.has("disabled")) {
119
+ if (this.disabled) this.setAttribute("aria-disabled", "true");
120
+ else this.removeAttribute("aria-disabled");
121
+ }
122
+ if (changed.has("for")) {
123
+ if (this.for) this.setAttribute("aria-controls", this.for);
124
+ else this.removeAttribute("aria-controls");
125
+ }
126
+ }
127
+ activate() {
128
+ if (this.disabled || this.selectedTab === this.for) return;
129
+ this.dispatchEvent(new CustomEvent("tab-change", {
130
+ bubbles: true,
131
+ cancelable: true,
132
+ detail: this.for
133
+ }));
134
+ }
135
+ render() {
136
+ return html`<slot>${this.label}</slot>`;
137
+ }
138
+ };
139
+ __decorate([property({
140
+ type: String,
141
+ reflect: true
142
+ })], Tab.prototype, "label", void 0);
143
+ __decorate([property({
144
+ type: String,
145
+ reflect: true
146
+ })], Tab.prototype, "for", void 0);
147
+ __decorate([property({
148
+ type: Boolean,
149
+ reflect: true
150
+ })], Tab.prototype, "disabled", void 0);
151
+ __decorate([property({
152
+ type: Boolean,
153
+ reflect: true
154
+ })], Tab.prototype, "selected", void 0);
155
+ __decorate([c({
156
+ context: selectedTabContext,
157
+ subscribe: true
158
+ })], Tab.prototype, "selectedTab", void 0);
159
+ Tab = __decorate([customElement("c2-tab")], Tab);
160
+ //#endregion
161
+ export { __decorate as n, s$1 as r, Tab as t };
@@ -0,0 +1,14 @@
1
+ //#region ../../../node_modules/@lit/context/lib/create-context.js
2
+ /**
3
+ * @license
4
+ * Copyright 2021 Google LLC
5
+ * SPDX-License-Identifier: BSD-3-Clause
6
+ */
7
+ function n(n) {
8
+ return n;
9
+ }
10
+ //#endregion
11
+ //#region src/tab-context.ts
12
+ var selectedTabContext = n(Symbol("selected-tab-context"));
13
+ //#endregion
14
+ export { selectedTabContext as t };
@@ -0,0 +1,2 @@
1
+ import { t as selectedTabContext } from "./tab-context-C4m2EOBP.js";
2
+ export { selectedTabContext };
package/dist/tab.js ADDED
@@ -0,0 +1,2 @@
1
+ import { t as Tab } from "./tab-Cn3SGI4Q.js";
2
+ export { Tab };
package/dist/tabs.js ADDED
@@ -0,0 +1,291 @@
1
+ import { n as __decorate, r as s$1 } from "./tab-Cn3SGI4Q.js";
2
+ import { t as selectedTabContext } from "./tab-context-C4m2EOBP.js";
3
+ import { LitElement, html, unsafeCSS } from "lit";
4
+ import { customElement, property, query, state } from "lit/decorators.js";
5
+ import { classMap } from "lit/directives/class-map.js";
6
+ import { isServer } from "lit-html/is-server.js";
7
+ //#region ../../../node_modules/@lit/context/lib/value-notifier.js
8
+ /**
9
+ * @license
10
+ * Copyright 2021 Google LLC
11
+ * SPDX-License-Identifier: BSD-3-Clause
12
+ */
13
+ var s = class {
14
+ get value() {
15
+ return this.o;
16
+ }
17
+ set value(s) {
18
+ this.setValue(s);
19
+ }
20
+ setValue(s, t = !1) {
21
+ const i = t || !Object.is(s, this.o);
22
+ this.o = s, i && this.updateObservers();
23
+ }
24
+ constructor(s) {
25
+ this.subscriptions = /* @__PURE__ */ new Map(), this.updateObservers = () => {
26
+ for (const [s, { disposer: t }] of this.subscriptions) s(this.o, t);
27
+ }, void 0 !== s && (this.value = s);
28
+ }
29
+ addCallback(s, t, i) {
30
+ if (!i) return void s(this.value);
31
+ this.subscriptions.has(s) || this.subscriptions.set(s, {
32
+ disposer: () => {
33
+ this.subscriptions.delete(s);
34
+ },
35
+ consumerHost: t
36
+ });
37
+ const { disposer: h } = this.subscriptions.get(s);
38
+ s(this.value, h);
39
+ }
40
+ clearCallbacks() {
41
+ this.subscriptions.clear();
42
+ }
43
+ };
44
+ //#endregion
45
+ //#region ../../../node_modules/@lit/context/lib/controllers/context-provider.js
46
+ /**
47
+ * @license
48
+ * Copyright 2021 Google LLC
49
+ * SPDX-License-Identifier: BSD-3-Clause
50
+ */ var e$1 = class extends Event {
51
+ constructor(t, s) {
52
+ super("context-provider", {
53
+ bubbles: !0,
54
+ composed: !0
55
+ }), this.context = t, this.contextTarget = s;
56
+ }
57
+ };
58
+ var i = class extends s {
59
+ constructor(s, e, i) {
60
+ super(void 0 !== e.context ? e.initialValue : i), this.onContextRequest = (t) => {
61
+ if (t.context !== this.context) return;
62
+ const s = t.contextTarget ?? t.composedPath()[0];
63
+ s !== this.host && (t.stopPropagation(), this.addCallback(t.callback, s, t.subscribe));
64
+ }, this.onProviderRequest = (s) => {
65
+ if (s.context !== this.context) return;
66
+ if ((s.contextTarget ?? s.composedPath()[0]) === this.host) return;
67
+ const e = /* @__PURE__ */ new Set();
68
+ for (const [s, { consumerHost: i }] of this.subscriptions) e.has(s) || (e.add(s), i.dispatchEvent(new s$1(this.context, i, s, !0)));
69
+ s.stopPropagation();
70
+ }, this.host = s, void 0 !== e.context ? this.context = e.context : this.context = e, this.attachListeners(), this.host.addController?.(this);
71
+ }
72
+ attachListeners() {
73
+ this.host.addEventListener("context-request", this.onContextRequest), this.host.addEventListener("context-provider", this.onProviderRequest);
74
+ }
75
+ hostConnected() {
76
+ this.host.dispatchEvent(new e$1(this.context, this.host));
77
+ }
78
+ };
79
+ //#endregion
80
+ //#region ../../../node_modules/@lit/context/lib/decorators/provide.js
81
+ /**
82
+ * @license
83
+ * Copyright 2017 Google LLC
84
+ * SPDX-License-Identifier: BSD-3-Clause
85
+ */ function e({ context: e }) {
86
+ return (n, i$1) => {
87
+ const r = /* @__PURE__ */ new WeakMap();
88
+ if ("object" == typeof i$1) return {
89
+ get() {
90
+ return n.get.call(this);
91
+ },
92
+ set(t) {
93
+ return r.get(this).setValue(t), n.set.call(this, t);
94
+ },
95
+ init(n) {
96
+ return r.set(this, new i(this, {
97
+ context: e,
98
+ initialValue: n
99
+ })), n;
100
+ }
101
+ };
102
+ {
103
+ n.constructor.addInitializer(((n) => {
104
+ r.set(n, new i(n, { context: e }));
105
+ }));
106
+ const o = Object.getOwnPropertyDescriptor(n, i$1);
107
+ let s;
108
+ if (void 0 === o) {
109
+ const t = /* @__PURE__ */ new WeakMap();
110
+ s = {
111
+ get() {
112
+ return t.get(this);
113
+ },
114
+ set(e) {
115
+ r.get(this).setValue(e), t.set(this, e);
116
+ },
117
+ configurable: !0,
118
+ enumerable: !0
119
+ };
120
+ } else {
121
+ const t = o.set;
122
+ s = {
123
+ ...o,
124
+ set(e) {
125
+ r.get(this).setValue(e), t?.call(this, e);
126
+ }
127
+ };
128
+ }
129
+ Object.defineProperty(n, i$1, s);
130
+ return;
131
+ }
132
+ };
133
+ }
134
+ //#endregion
135
+ //#region src/tabs.scss?inline
136
+ var tabs_default = "/* ex : var((width: 24px), width, c2-checkbox) returns var(--c2-checkbox-width, 24px) */\n:host {\n display: block;\n}\n\n.c2-tabs {\n display: flex;\n flex-direction: column;\n}\n\n.c2-tabs-header {\n display: flex;\n align-items: flex-end;\n position: relative;\n box-sizing: border-box;\n gap: var(--c2-tabs--gap);\n box-shadow: var(--c2-tabs--box-shadow,inset 0px -2px 0px 0px rgb(213, 213, 213));\n justify-content: var(--c2-tabs--justify-content,flex-start);\n background-color: var(--c2-tabs--background-color);\n border-radius: var(--c2-tabs--border-radius);\n height: var(--c2-tabs--height);\n padding-top: var(--c2-tabs--padding-top);\n padding-right: var(--c2-tabs--padding-right);\n padding-left: var(--c2-tabs--padding-left);\n padding-bottom: var(--c2-tabs--padding-bottom,var(--c2-tabs__indicator--height,2px));\n}\n.c2-tabs-header .selection-indicator {\n position: absolute;\n left: 0;\n bottom: var(--c2-tabs__indicator--bottom,0px);\n z-index: 0;\n width: 0;\n pointer-events: none;\n will-change: transform, width;\n transform-origin: top left;\n background-color: var(--c2-tabs__indicator--color,var(--c2-tabs__tab__selected--color,var(--c2-tabs__tab__hover--color,rgb(109, 109, 109))));\n border-radius: var(--c2-tabs__indicator--border-radius);\n height: var(--c2-tabs__indicator--height,2px);\n transition: transform 150ms cubic-bezier(0.3, 0, 1, 1), width 150ms cubic-bezier(0.3, 0, 1, 1);\n}\n.c2-tabs-header .selection-indicator.first-position {\n transition: none;\n}\n.c2-tabs-header ::slotted(c2-tab) {\n position: relative;\n z-index: 1;\n gap: var(--c2-tabs__tab--gap,8px);\n flex: var(--c2-tabs__tab--flex);\n justify-content: var(--c2-tabs__tab--justify-content,center);\n padding-top: var(--c2-tabs__tab--padding-top,8px);\n padding-right: var(--c2-tabs__tab--padding-right,16px);\n padding-bottom: var(--c2-tabs__tab--padding-bottom,8px);\n padding-left: var(--c2-tabs__tab--padding-left,16px);\n font-weight: var(--c2-tabs__tab--font-weight);\n font-size: var(--c2-tabs__tab--font-size);\n font-style: var(--c2-tabs__tab--font-style);\n font-family: var(--c2-tabs__tab--font-family);\n color: var(--c2-tabs__tab--color);\n background-color: var(--c2-tabs__tab--background-color);\n border-top-left-radius: var(--c2-tabs__tab--border-top-left-radius);\n border-top-right-radius: var(--c2-tabs__tab--border-top-right-radius);\n border-bottom-left-radius: var(--c2-tabs__tab--border-bottom-left-radius);\n border-bottom-right-radius: var(--c2-tabs__tab--border-bottom-right-radius);\n transition: background-color 250ms cubic-bezier(0.2, 0, 0, 1), color 250ms cubic-bezier(0.2, 0, 0, 1);\n}\n.c2-tabs-header ::slotted(c2-tab:hover) {\n color: var(--c2-tabs__tab__hover--color,var(--c2-tabs__tab--color,rgb(2, 101, 220)));\n background-color: var(--c2-tabs__tab__hover--background-color,var(--c2-tabs__tab--background-color));\n}\n.c2-tabs-header ::slotted(c2-tab[selected]) {\n color: var(--c2-tabs__tab__selected--color,var(--c2-tabs__tab__hover--color,var(--c2-tabs__tab--color,rgb(2, 101, 220))));\n background-color: var(--c2-tabs__tab__selected--background-color,var(--c2-tabs__tab__hover--background-color,var(--c2-tabs__tab--background-color)));\n}\n.c2-tabs-header ::slotted(c2-tab:focus-visible) {\n outline: var(--c2-tabs__tab__focus--outline,2px solid rgba(2, 101, 220, 0.4));\n outline-offset: var(--c2-tabs__tab__focus--outline-offset,-2px);\n}\n.c2-tabs-header ::slotted(c2-tab[disabled]) {\n opacity: var(--c2-tabs__tab__disabled--opacity,0.38);\n pointer-events: none;\n}\n\n@media (prefers-reduced-motion: reduce) {\n .c2-tabs-header .selection-indicator,\n .c2-tabs-header ::slotted(c2-tab) {\n transition: none;\n }\n}";
137
+ //#endregion
138
+ //#region src/tabs.ts
139
+ var TAB_TAG = "c2-tab";
140
+ var Tabs = class Tabs extends LitElement {
141
+ constructor(..._args) {
142
+ super(..._args);
143
+ this.selectedTab = "";
144
+ this.indicatorStyle = "";
145
+ this.measured = false;
146
+ }
147
+ static {
148
+ this.styles = unsafeCSS(tabs_default);
149
+ }
150
+ connectedCallback() {
151
+ super.connectedCallback();
152
+ if (!isServer && typeof ResizeObserver !== "undefined" && !this.resizeObserver) this.resizeObserver = new ResizeObserver(() => this.updateIndicator());
153
+ }
154
+ disconnectedCallback() {
155
+ super.disconnectedCallback();
156
+ this.resizeObserver?.disconnect();
157
+ }
158
+ /** Slotted `<c2-tab>` elements, in DOM order. */
159
+ get tabs() {
160
+ return (this.tabsSlot?.assignedElements() ?? []).filter((el) => el.localName === TAB_TAG);
161
+ }
162
+ /** Selects a tab and notifies listeners. Ignores unknown or disabled tabs. */
163
+ select(value) {
164
+ if (value === this.selectedTab) return;
165
+ const tab = this.tabs.find((t) => t.for === value);
166
+ if (!tab || tab.disabled) return;
167
+ this.selectedTab = value;
168
+ this.dispatchEvent(new CustomEvent("change", {
169
+ detail: { value },
170
+ bubbles: true,
171
+ composed: true
172
+ }));
173
+ }
174
+ handleTabChange(event) {
175
+ const value = event.detail;
176
+ queueMicrotask(() => {
177
+ if (!event.defaultPrevented) this.select(value);
178
+ });
179
+ }
180
+ handleKeydown(event) {
181
+ const tabs = this.tabs.filter((t) => !t.disabled);
182
+ if (tabs.length === 0) return;
183
+ const current = tabs.indexOf(event.target);
184
+ if (current === -1) return;
185
+ const rtl = getComputedStyle(this).direction === "rtl";
186
+ let next;
187
+ switch (event.key) {
188
+ case "ArrowRight":
189
+ next = rtl ? current - 1 : current + 1;
190
+ break;
191
+ case "ArrowLeft":
192
+ next = rtl ? current + 1 : current - 1;
193
+ break;
194
+ case "Home":
195
+ next = 0;
196
+ break;
197
+ case "End":
198
+ next = tabs.length - 1;
199
+ break;
200
+ default: return;
201
+ }
202
+ event.preventDefault();
203
+ const tab = tabs[(next + tabs.length) % tabs.length];
204
+ tab.focus();
205
+ this.select(tab.for);
206
+ }
207
+ async handleTabSlotChange() {
208
+ await customElements.whenDefined(TAB_TAG);
209
+ const tabs = this.tabs;
210
+ await Promise.all(tabs.map((tab) => tab.updateComplete));
211
+ this.resizeObserver?.disconnect();
212
+ if (this.resizeObserver) {
213
+ this.resizeObserver.observe(this.header);
214
+ for (const tab of tabs) this.resizeObserver.observe(tab);
215
+ }
216
+ if (!this.selectedTab) {
217
+ const first = tabs.find((t) => !t.disabled);
218
+ if (first) this.selectedTab = first.for;
219
+ }
220
+ this.syncTabs();
221
+ this.updateIndicator();
222
+ }
223
+ /** Mirrors `selectedTab` onto the tabs (selected state, roving tabindex) and the panels (slot, ARIA). */
224
+ syncTabs() {
225
+ const tabs = this.tabs;
226
+ if (tabs.length === 0) return;
227
+ const selected = tabs.find((t) => t.for === this.selectedTab);
228
+ const focusable = selected && !selected.disabled ? selected : tabs.find((t) => !t.disabled);
229
+ for (const tab of tabs) {
230
+ tab.selected = tab === selected;
231
+ tab.tabIndex = tab === focusable ? 0 : -1;
232
+ }
233
+ for (const child of this.children) {
234
+ if (child.localName === TAB_TAG) continue;
235
+ if (child.id && child.id === this.selectedTab) {
236
+ child.setAttribute("slot", "tab-content");
237
+ if (!child.hasAttribute("role")) child.setAttribute("role", "tabpanel");
238
+ if (selected && !child.hasAttribute("aria-labelledby")) {
239
+ if (!selected.id) selected.id = `${child.id}-tab`;
240
+ child.setAttribute("aria-labelledby", selected.id);
241
+ }
242
+ } else if (child.getAttribute("slot") === "tab-content") child.removeAttribute("slot");
243
+ }
244
+ }
245
+ updateIndicator() {
246
+ const tab = this.tabs.find((t) => t.for === this.selectedTab);
247
+ if (!tab || !this.header) {
248
+ this.indicatorStyle = "width: 0px;";
249
+ return;
250
+ }
251
+ const tabRect = tab.getBoundingClientRect();
252
+ const headerRect = this.header.getBoundingClientRect();
253
+ const x = tabRect.left - headerRect.left + this.header.scrollLeft;
254
+ this.indicatorStyle = `transform: translateX(${x}px); width: ${tabRect.width}px;`;
255
+ if (!this.measured) this.updateComplete.then(() => this.measured = true);
256
+ }
257
+ willUpdate(changed) {
258
+ if (changed.has("selectedTab") && !this.selectedTab && this.hasUpdated) {
259
+ const first = this.tabs.find((t) => !t.disabled);
260
+ if (first) this.selectedTab = first.for;
261
+ }
262
+ }
263
+ updated(changed) {
264
+ if (changed.has("selectedTab")) {
265
+ this.syncTabs();
266
+ this.updateIndicator();
267
+ }
268
+ }
269
+ render() {
270
+ return html`
271
+ <div class="c2-tabs">
272
+ <div class="c2-tabs-header" role="tablist" @tab-change=${this.handleTabChange} @keydown=${this.handleKeydown}>
273
+ <slot name="tab" @slotchange=${this.handleTabSlotChange}></slot>
274
+ <div class="selection-indicator ${classMap({ "first-position": !this.measured })}" style=${this.indicatorStyle}></div>
275
+ </div>
276
+ <slot name="tab-content"></slot>
277
+ </div>
278
+ `;
279
+ }
280
+ };
281
+ __decorate([e({ context: selectedTabContext }), property({
282
+ type: String,
283
+ attribute: "selected-tab"
284
+ })], Tabs.prototype, "selectedTab", void 0);
285
+ __decorate([query(".c2-tabs-header")], Tabs.prototype, "header", void 0);
286
+ __decorate([query("slot[name=\"tab\"]")], Tabs.prototype, "tabsSlot", void 0);
287
+ __decorate([state()], Tabs.prototype, "indicatorStyle", void 0);
288
+ __decorate([state()], Tabs.prototype, "measured", void 0);
289
+ Tabs = __decorate([customElement("c2-tabs")], Tabs);
290
+ //#endregion
291
+ export { Tabs };
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@c2n/tabs",
3
+ "version": "0.0.6",
4
+ "type": "module",
5
+ "main": "dist/tabs.js",
6
+ "exports": {
7
+ ".": {
8
+ "default": "./dist/tabs.js",
9
+ "types": "./types/src/tabs.d.ts"
10
+ },
11
+ "./tab.js": {
12
+ "default": "./dist/tab.js",
13
+ "types": "./types/src/tab.d.ts"
14
+ },
15
+ "./tab-context.js": {
16
+ "default": "./dist/tab-context.js",
17
+ "types": "./types/src/tab-context.d.ts"
18
+ },
19
+ "./custom-elements.json": "./custom-elements.json"
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "types"
24
+ ],
25
+ "keywords": [
26
+ "tabs",
27
+ "web component",
28
+ "lit"
29
+ ],
30
+ "author": "code2nguyen@gmail.com",
31
+ "publishConfig": {
32
+ "registry": "https://registry.npmjs.org",
33
+ "access": "public"
34
+ },
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "https://github.com/code2nguyen/web-components.git"
38
+ },
39
+ "scripts": {
40
+ "dev": "vite",
41
+ "build": "wireit",
42
+ "type-check": "wireit"
43
+ },
44
+ "wireit": {
45
+ "type-check": {
46
+ "dependencies": [
47
+ "../../core:build"
48
+ ],
49
+ "command": "tsc -p tsconfig.lib.json --composite false"
50
+ },
51
+ "build": {
52
+ "dependencies": [
53
+ "type-check"
54
+ ],
55
+ "command": "vite build"
56
+ }
57
+ },
58
+ "dependencies": {
59
+ "@c2n/core": "0.0.6",
60
+ "@lit/context": "1.1.6",
61
+ "lit": "3.3.3"
62
+ },
63
+ "devDependencies": {
64
+ "@c2n/config": "*"
65
+ },
66
+ "customElements": "custom-elements.json",
67
+ "gitHead": "2921054bc75299da66dad11c1e374246ff88a51e"
68
+ }
@@ -0,0 +1,4 @@
1
+ export declare const selectedTabContext: {
2
+ __context__: string;
3
+ };
4
+ //# sourceMappingURL=tab-context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tab-context.d.ts","sourceRoot":"","sources":["../../src/tab-context.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,kBAAkB;;CAAwD,CAAA"}
@@ -0,0 +1,36 @@
1
+ import { LitElement, type PropertyValues } from 'lit';
2
+ /**
3
+ * A single tab inside `<c2-tabs>`. Its `for` attribute names the `id` of the panel it controls.
4
+ * The parent owns selection: it sets `selected` and the roving `tabindex`; this element exposes
5
+ * them as `role="tab"`, `aria-selected`, `aria-controls` and `aria-disabled`.
6
+ *
7
+ * @tag c2-tab
8
+ *
9
+ * @slot - Tab label. Defaults to the `label` attribute.
10
+ *
11
+ * @event {CustomEvent<string>} tab-change - Fired when the user activates this tab (click, Enter or Space). `detail` is the `for` value. Cancelable: `preventDefault()` keeps the current tab.
12
+ */
13
+ export declare class Tab extends LitElement {
14
+ static styles: import("lit").CSSResult;
15
+ /** Text label, used when no content is slotted. Reflected so SSR keeps it as an attribute. */
16
+ label: string;
17
+ /** `id` of the panel this tab controls. */
18
+ for: string;
19
+ disabled: boolean;
20
+ /** Set by the parent `<c2-tabs>`; do not set by hand. */
21
+ selected: boolean;
22
+ private selectedTab;
23
+ constructor();
24
+ connectedCallback(): void;
25
+ updated(changed: PropertyValues<this>): void;
26
+ private handleClick;
27
+ private handleKeydown;
28
+ private activate;
29
+ render(): import("lit-html").TemplateResult<1>;
30
+ }
31
+ declare global {
32
+ interface HTMLElementTagNameMap {
33
+ 'c2-tab': Tab;
34
+ }
35
+ }
36
+ //# sourceMappingURL=tab.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tab.d.ts","sourceRoot":"","sources":["../../src/tab.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAmB,KAAK,cAAc,EAAE,MAAM,KAAK,CAAA;AAOtE;;;;;;;;;;GAUG;AACH,qBACa,GAAI,SAAQ,UAAU;IACjC,OAAgB,MAAM,0BAAoB;IAE1C,8FAA8F;IACnD,KAAK,SAAK;IAErD,2CAA2C;IACA,GAAG,SAAK;IAEP,QAAQ,UAAQ;IAE5D,yDAAyD;IACb,QAAQ,UAAQ;IAG5D,OAAO,CAAC,WAAW,CAAa;;IAWvB,iBAAiB;IAMjB,OAAO,CAAC,OAAO,EAAE,cAAc,CAAC,IAAI,CAAC;IAY9C,OAAO,CAAC,WAAW,CAElB;IAED,OAAO,CAAC,aAAa,CAKpB;IAED,OAAO,CAAC,QAAQ;IAKP,MAAM;CAGhB;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,qBAAqB;QAC7B,QAAQ,EAAE,GAAG,CAAA;KACd;CACF"}
@@ -0,0 +1,99 @@
1
+ import { LitElement, type PropertyValues } from 'lit';
2
+ import './tab';
3
+ import type { Tab } from './tab';
4
+ /**
5
+ * Tab strip that shows one content panel at a time.
6
+ *
7
+ * Light-DOM children are split in two groups: every `<c2-tab for="…">` becomes a tab in the header
8
+ * (they slot themselves into `tab`), every other child is a panel whose `id` matches a tab's `for`.
9
+ * Only the panel of the selected tab is rendered. The header is a `tablist`: Arrow keys, Home and
10
+ * End move between tabs, Enter and Space activate the focused one.
11
+ *
12
+ * @tag c2-tabs
13
+ *
14
+ * @slot tab - The `<c2-tab>` elements (assigned automatically).
15
+ * @slot tab-content - The panel of the selected tab (assigned automatically from the child whose `id` matches `selected-tab`).
16
+ *
17
+ * @event {CustomEvent<{ value: string }>} change - Fired after the user selects another tab. `detail.value` is the new `selected-tab`. Not fired for programmatic changes.
18
+ *
19
+ * @cssproperty {box-shadow} [--c2-tabs--box-shadow=inset 0px -2px 0px 0px rgb(230, 230, 230)]
20
+ * @cssproperty {justify-content} [--c2-tabs--justify-content=flex-start]
21
+ * @cssproperty {background-color} --c2-tabs--background-color
22
+ * @cssproperty {pixel} --c2-tabs--height
23
+ * @cssproperty {pixel} --c2-tabs--gap
24
+ * @cssproperty {border-radius} --c2-tabs--border-radius
25
+ * @cssproperty {padding} --c2-tabs--padding-top
26
+ * @cssproperty {padding} --c2-tabs--padding-right
27
+ * @cssproperty {padding} [--c2-tabs--padding-bottom=var(--c2-tabs__indicator--height)]
28
+ * @cssproperty {padding} --c2-tabs--padding-left
29
+ *
30
+ * @cssproperty {pixel} [--c2-tabs__tab--gap=8px] - Space between an icon and the label inside a tab.
31
+ * @cssproperty {flex} --c2-tabs__tab--flex - Set `1 1 0` to make every tab grow to the same width.
32
+ * @cssproperty {justify-content} [--c2-tabs__tab--justify-content=center]
33
+ *
34
+ * @cssproperty {font-weight} --c2-tabs__tab--font-weight
35
+ * @cssproperty {font-size} --c2-tabs__tab--font-size
36
+ * @cssproperty {font-style} --c2-tabs__tab--font-style
37
+ * @cssproperty {font-family} --c2-tabs__tab--font-family
38
+ *
39
+ * @cssproperty {color} [--c2-tabs__indicator--color=rgb(109, 109, 109)]
40
+ * @cssproperty {border-radius} --c2-tabs__indicator--border-radius
41
+ * @cssproperty {pixel} [--c2-tabs__indicator--height=2px]
42
+ * @cssproperty {pixel} [--c2-tabs__indicator--bottom=0px]
43
+ *
44
+ * @cssproperty {padding} [--c2-tabs__tab--padding-top=8px]
45
+ * @cssproperty {padding} [--c2-tabs__tab--padding-right=16px]
46
+ * @cssproperty {padding} [--c2-tabs__tab--padding-bottom=8px]
47
+ * @cssproperty {padding} [--c2-tabs__tab--padding-left=16px]
48
+ *
49
+ * @cssproperty {color} --c2-tabs__tab--color
50
+ * @cssproperty {background-color} --c2-tabs__tab--background-color
51
+ *
52
+ * @cssproperty {border-radius} --c2-tabs__tab--border-top-left-radius
53
+ * @cssproperty {border-radius} --c2-tabs__tab--border-top-right-radius
54
+ * @cssproperty {border-radius} --c2-tabs__tab--border-bottom-left-radius
55
+ * @cssproperty {border-radius} --c2-tabs__tab--border-bottom-right-radius
56
+ *
57
+ * @cssproperty {color} [--c2-tabs__tab__hover--color=rgb(2, 101, 220)]
58
+ * @cssproperty {background-color} --c2-tabs__tab__hover--background-color
59
+ *
60
+ * @cssproperty {color} [--c2-tabs__tab__selected--color=rgb(2, 101, 220)]
61
+ * @cssproperty {background-color} --c2-tabs__tab__selected--background-color
62
+ *
63
+ * @cssproperty {outline} [--c2-tabs__tab__focus--outline=2px solid rgba(2, 101, 220, 0.4)]
64
+ * @cssproperty {pixel} [--c2-tabs__tab__focus--outline-offset=-2px]
65
+ *
66
+ * @cssproperty {opacity} [--c2-tabs__tab__disabled--opacity=0.38]
67
+ */
68
+ export declare class Tabs extends LitElement {
69
+ static styles: import("lit").CSSResult;
70
+ /** `for` value of the selected tab (and `id` of the visible panel). Falls back to the first enabled tab. */
71
+ selectedTab: string;
72
+ private header;
73
+ private tabsSlot;
74
+ private indicatorStyle;
75
+ /** False until the indicator has been placed once: the first placement must not animate. */
76
+ private measured;
77
+ private resizeObserver?;
78
+ connectedCallback(): void;
79
+ disconnectedCallback(): void;
80
+ /** Slotted `<c2-tab>` elements, in DOM order. */
81
+ get tabs(): Tab[];
82
+ /** Selects a tab and notifies listeners. Ignores unknown or disabled tabs. */
83
+ select(value: string): void;
84
+ private handleTabChange;
85
+ private handleKeydown;
86
+ private handleTabSlotChange;
87
+ /** Mirrors `selectedTab` onto the tabs (selected state, roving tabindex) and the panels (slot, ARIA). */
88
+ private syncTabs;
89
+ private updateIndicator;
90
+ willUpdate(changed: PropertyValues<this>): void;
91
+ updated(changed: PropertyValues<this>): void;
92
+ render(): import("lit-html").TemplateResult<1>;
93
+ }
94
+ declare global {
95
+ interface HTMLElementTagNameMap {
96
+ 'c2-tabs': Tabs;
97
+ }
98
+ }
99
+ //# sourceMappingURL=tabs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tabs.d.ts","sourceRoot":"","sources":["../../src/tabs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAmB,KAAK,cAAc,EAAE,MAAM,KAAK,CAAA;AAQtE,OAAO,OAAO,CAAA;AACd,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,OAAO,CAAA;AAIhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,qBACa,IAAK,SAAQ,UAAU;IAClC,OAAgB,MAAM,0BAAoB;IAE1C,4GAA4G;IAG5G,WAAW,SAAK;IAGhB,OAAO,CAAC,MAAM,CAAc;IAG5B,OAAO,CAAC,QAAQ,CAAkB;IAGlC,OAAO,CAAC,cAAc,CAAK;IAE3B,4FAA4F;IAE5F,OAAO,CAAC,QAAQ,CAAQ;IAExB,OAAO,CAAC,cAAc,CAAC,CAAgB;IAE9B,iBAAiB;IAOjB,oBAAoB;IAK7B,iDAAiD;IACjD,IAAI,IAAI,IAAI,GAAG,EAAE,CAEhB;IAED,8EAA8E;IAC9E,MAAM,CAAC,KAAK,EAAE,MAAM;IAQpB,OAAO,CAAC,eAAe;IAQvB,OAAO,CAAC,aAAa;YA8BP,mBAAmB;IAqBjC,yGAAyG;IACzG,OAAO,CAAC,QAAQ;IA2BhB,OAAO,CAAC,eAAe;IAgBd,UAAU,CAAC,OAAO,EAAE,cAAc,CAAC,IAAI,CAAC;IAQxC,OAAO,CAAC,OAAO,EAAE,cAAc,CAAC,IAAI,CAAC;IAOrC,MAAM;CAWhB;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,qBAAqB;QAC7B,SAAS,EAAE,IAAI,CAAA;KAChB;CACF"}