@xoong/controls 0.0.0-stage → 0.0.35

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) 2026 Duong Tran Xuan
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 CHANGED
@@ -1,3 +1,29 @@
1
- # Temporary Holding Version
1
+ # @xoong/controls
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Accessible controls for xoong as Light DOM custom elements: tabs, menu, tooltip, combobox, tags and carousel. Only depends on `@xoong/core`.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @xoong/controls
9
+ ```
10
+
11
+ ## Use
12
+
13
+ ```js
14
+ import '@xoong/controls'; // everything, or one: '@xoong/controls/carousel'
15
+ ```
16
+ ```html
17
+ <xo-tabs label="Page"><section xo-tab="General">…</section><section xo-tab="SEO">…</section></xo-tabs>
18
+ <xo-combobox name="author" multiple><option value="1">Ada</option></xo-combobox>
19
+ <xo-carousel label="Services" items="3" dots counter>…</xo-carousel>
20
+ ```
21
+
22
+ ## More
23
+
24
+ * Documentation and demos: https://github.com/fpt-duongtx/xoong#readme
25
+ * Source: https://github.com/fpt-duongtx/xoong/tree/main/packages/controls
26
+ * All packages share one version number and are released together: https://github.com/fpt-duongtx/xoong/releases
27
+ * xoong is a working prototype (0.0.x): the HTML syntax is still Draft.
28
+
29
+ MIT licence.
package/package.json CHANGED
@@ -1,6 +1,58 @@
1
1
  {
2
2
  "name": "@xoong/controls",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.35",
4
+ "description": "xoong form and page controls: tabs, popover, tooltip, menu, combobox, chips, tag input, carousel. Light DOM, accessible, zero dependencies besides @xoong/core.",
5
+ "type": "module",
6
+ "main": "src/index.js",
7
+ "exports": {
8
+ ".": {
9
+ "default": "./src/index.js"
10
+ },
11
+ "./carousel": "./src/carousel.js",
12
+ "./combobox": "./src/combobox.js",
13
+ "./copy": "./src/copy.js",
14
+ "./date": "./src/date.js",
15
+ "./password": "./src/password.js",
16
+ "./slider": "./src/slider.js",
17
+ "./stepper": "./src/stepper.js",
18
+ "./controls.css": "./src/controls.css",
19
+ "./menu": "./src/menu.js",
20
+ "./switch": "./src/switch.js",
21
+ "./tabs": "./src/tabs.js",
22
+ "./time": "./src/time.js",
23
+ "./tooltip": "./src/tooltip.js"
24
+ },
25
+ "sideEffects": true,
26
+ "files": [
27
+ "src"
28
+ ],
29
+ "keywords": [
30
+ "xoong",
31
+ "tabs",
32
+ "combobox",
33
+ "menu",
34
+ "tooltip",
35
+ "a11y",
36
+ "carousel"
37
+ ],
38
+ "license": "MIT",
39
+ "author": "Duong Tran Xuan <duongtx1505@icloud.com>",
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "git+https://github.com/fpt-duongtx/xoong.git",
43
+ "directory": "packages/controls"
44
+ },
45
+ "bugs": {
46
+ "url": "https://github.com/fpt-duongtx/xoong/issues"
47
+ },
48
+ "homepage": "https://github.com/fpt-duongtx/xoong#readme",
49
+ "dependencies": {
50
+ "@xoong/core": "0.0.35"
51
+ },
52
+ "publishConfig": {
53
+ "access": "public"
54
+ },
55
+ "engines": {
56
+ "node": ">=22.12"
57
+ }
58
+ }
@@ -0,0 +1,248 @@
1
+ // @ts-check
2
+ /**
3
+ * <xo-carousel> is a row of slides you move through: cards, a banner, testimonials, a gallery. The slides are its children.
4
+ *
5
+ * <xo-carousel label="Services" items="3" counter>
6
+ * <article>...</article>
7
+ * <article>...</article>
8
+ * </xo-carousel>
9
+ *
10
+ * It is made on the platform: the slides sit in a scroll container with CSS scroll snap, so touch swiping, the mouse wheel, momentum and
11
+ * keyboard scrolling are the browser's own, and the buttons only ask the container to scroll. The markup follows the WAI-ARIA carousel
12
+ * pattern: a region with `aria-roledescription="carousel"`, slides as groups ("2 of 5"), previous and next buttons, and when it plays by
13
+ * itself a pause button, a pause on hover and on focus, and no automatic movement under `prefers-reduced-motion`.
14
+ *
15
+ * - `items` slides are visible at once (default 1; `1.15` lets the next one peek). Set it in CSS for breakpoints:
16
+ * `xo-carousel { --xo-carousel-items: 2 } @media (min-width: 60rem) { xo-carousel { --xo-carousel-items: 4 } }`. The gap is `--xo-carousel-gap`.
17
+ * - `dots` adds a button per position (a slide with `data-thumb="url"` shows that picture instead: a gallery with thumbnails), `counter` shows
18
+ * "1 / 5", `no-arrows` hides the arrows, `loop` makes Next at the end return to the first slide, `autoplay="5000"` moves on by itself every 5 seconds.
19
+ * - Slides outside the view are `inert`, so the Tab key never lands on something that is not on screen. A slide that is partly visible stays usable.
20
+ * - Slides may come from data: `<xo-carousel><article xo-each="items">` inside an `<xo-view>`; the carousel follows its children.
21
+ * - API: `index`, `count`, `next()`, `prev()`, `goTo(i)`, `play()`, `pause()`; `xo-carousel-change` fires with `{ index, count }`.
22
+ * `index` is the first visible slide.
23
+ */
24
+ import { XoElement, define, text } from '@xoong/core';
25
+
26
+ class XoCarousel extends XoElement {
27
+ static props = { label: String, items: Number, autoplay: Number, loop: Boolean, dots: Boolean, counter: Boolean, noArrows: Boolean };
28
+
29
+ /** @type {HTMLElement | null} */ #track = null;
30
+ /** @type {HTMLButtonElement | null} */ #prev = null;
31
+ /** @type {HTMLButtonElement | null} */ #next = null;
32
+ /** @type {HTMLButtonElement | null} */ #toggle = null;
33
+ /** @type {HTMLElement | null} */ #dots = null;
34
+ /** @type {HTMLElement | null} */ #count = null;
35
+ /** @type {MutationObserver | null} */ #watch = null;
36
+ /** @type {ResizeObserver | null} */ #size = null;
37
+ #index = 0;
38
+ #positions = 1;
39
+ #settle = 0;
40
+ #timer = 0;
41
+ #held = false; // the pointer or the focus is inside
42
+ #stopped = false; // the person pressed pause
43
+
44
+ get index() { return this.#index; }
45
+ get count() { return this.#slides.length; }
46
+ get #slides() { return /** @type {HTMLElement[]} */ ([...(this.#track?.children ?? [])]); }
47
+ get #reduced() { return !!globalThis.matchMedia?.('(prefers-reduced-motion: reduce)').matches; }
48
+
49
+ connected() {
50
+ this.effect(() => { const n = this.items; if (n > 0) this.style.setProperty('--xo-carousel-items', String(n)); else this.style.removeProperty('--xo-carousel-items'); });
51
+ this.effect(() => { this.label; this.dots; this.counter; this.noArrows; this.autoplay; this.#refresh(true); }); // attributes may arrive after the build
52
+ queueMicrotask(() => this.#build());
53
+ }
54
+
55
+ disconnected() {
56
+ clearTimeout(this.#timer);
57
+ clearTimeout(this.#settle);
58
+ this.#watch?.disconnect();
59
+ this.#size?.disconnect();
60
+ }
61
+
62
+ #button(/** @type {string} */ cls, /** @type {string} */ glyph, /** @type {string} */ label, /** @type {() => void} */ run) {
63
+ const b = document.createElement('button');
64
+ b.type = 'button';
65
+ b.className = cls;
66
+ b.setAttribute('aria-label', label);
67
+ const g = document.createElement('span');
68
+ g.setAttribute('aria-hidden', 'true');
69
+ g.textContent = glyph;
70
+ b.append(g);
71
+ b.addEventListener('click', run);
72
+ return b;
73
+ }
74
+
75
+ #build() {
76
+ if (this.#track || !this.isConnected) return;
77
+ const old = this.querySelector(':scope > .xo-carousel-track'); // a copy of a carousel that was already built (a view clones its content): take it apart, then build again
78
+ if (old) { old.replaceWith(...old.childNodes); this.querySelector(':scope > .xo-carousel-controls')?.remove(); }
79
+ this.setAttribute('role', 'region');
80
+ this.setAttribute('aria-roledescription', 'carousel');
81
+ const track = document.createElement('div');
82
+ track.className = 'xo-carousel-track';
83
+ track.tabIndex = 0;
84
+ track.setAttribute('role', 'group');
85
+ track.setAttribute('aria-label', text('xoong.carousel.slides', 'Slides'));
86
+ track.append(...this.childNodes);
87
+ const controls = document.createElement('div');
88
+ controls.className = 'xo-carousel-controls';
89
+ const nav = document.createElement('div');
90
+ nav.className = 'xo-carousel-nav';
91
+ this.#toggle = this.#button('xo-carousel-toggle', '', '', () => { this.#stopped = !this.#stopped; this.#refresh(true); });
92
+ this.#prev = this.#button('xo-carousel-prev', '←', text('xoong.carousel.previous', 'Previous slide'), () => this.prev());
93
+ this.#next = this.#button('xo-carousel-next', '→', text('xoong.carousel.next', 'Next slide'), () => this.next());
94
+ nav.append(this.#toggle, this.#prev, this.#next);
95
+ this.#dots = document.createElement('div');
96
+ this.#dots.className = 'xo-carousel-dots';
97
+ this.#dots.setAttribute('role', 'group');
98
+ this.#dots.setAttribute('aria-label', text('xoong.carousel.choose', 'Choose a slide'));
99
+ this.#count = document.createElement('span');
100
+ this.#count.className = 'xo-carousel-count';
101
+ controls.append(nav, this.#dots, this.#count);
102
+ this.append(track, controls);
103
+ this.#track = track;
104
+
105
+ track.addEventListener('scroll', () => { clearTimeout(this.#settle); this.#settle = /** @type {any} */ (setTimeout(() => this.#refresh(), 90)); }, { passive: true });
106
+ track.addEventListener('scrollend', () => { clearTimeout(this.#settle); this.#refresh(); }); // where the browser says the scrolling ended; the timer above is the fallback
107
+ track.addEventListener('keydown', (event) => {
108
+ if (event.target !== track || event.altKey || event.ctrlKey || event.metaKey) return;
109
+ const rtl = getComputedStyle(this).direction === 'rtl';
110
+ if (event.key === 'ArrowRight') this[rtl ? 'prev' : 'next']();
111
+ else if (event.key === 'ArrowLeft') this[rtl ? 'next' : 'prev']();
112
+ else if (event.key === 'Home') this.goTo(0);
113
+ else if (event.key === 'End') this.goTo(this.#positions - 1);
114
+ else return;
115
+ event.preventDefault();
116
+ });
117
+ for (const [on, off] of [['pointerenter', 'pointerleave'], ['focusin', 'focusout']]) {
118
+ this.addEventListener(on, () => { this.#held = true; this.#schedule(); });
119
+ this.addEventListener(off, () => { this.#held = this.matches(':hover') || this.contains(document.activeElement); this.#schedule(); });
120
+ }
121
+ this.#watch = new MutationObserver(() => {
122
+ for (const node of [...this.childNodes]) if (node !== track && node !== controls) track.append(node); // slides added to the element, not to the track
123
+ this.#refresh(true);
124
+ });
125
+ this.#watch.observe(this, { childList: true });
126
+ this.#watch.observe(track, { childList: true });
127
+ if (typeof ResizeObserver !== 'undefined') { this.#size = new ResizeObserver(() => this.#refresh()); this.#size.observe(track); }
128
+ this.#refresh(true);
129
+ }
130
+
131
+ /** The distance between the start of one slide and the next, and how many fit in the view. */
132
+ #measure() {
133
+ const track = /** @type {HTMLElement} */ (this.#track);
134
+ const slides = this.#slides;
135
+ const step = slides.length > 1 ? Math.abs(slides[1].offsetLeft - slides[0].offsetLeft) : track.clientWidth;
136
+ const gap = slides.length > 1 ? Math.max(0, step - slides[0].offsetWidth) : 0;
137
+ const view = step > 0 ? Math.max(1, Math.floor((track.clientWidth + gap + 1) / step)) : 1;
138
+ return { step, view, laidOut: track.clientWidth > 0 && step > 0 };
139
+ }
140
+
141
+ /** @param {boolean} [rebuild] */
142
+ #refresh(rebuild = false) {
143
+ const track = this.#track;
144
+ if (!track) return;
145
+ const slides = this.#slides;
146
+ const n = slides.length;
147
+ const { step, view, laidOut } = this.#measure();
148
+ this.#positions = Math.max(1, n - view + 1);
149
+ if (laidOut) {
150
+ const atEnd = Math.abs(track.scrollLeft) >= track.scrollWidth - track.clientWidth - 2;
151
+ this.#index = Math.min(this.#positions - 1, atEnd && n > view ? this.#positions - 1 : Math.round(Math.abs(track.scrollLeft) / step));
152
+ } else this.#index = Math.min(this.#index, this.#positions - 1);
153
+ const index = this.#index;
154
+ slides.forEach((slide, i) => {
155
+ if (!slide.hasAttribute('aria-label') || slide.hasAttribute('data-xo-label')) { slide.setAttribute('aria-label', text('xoong.carousel.slide', '{n} of {count}', { n: i + 1, count: n })); slide.setAttribute('data-xo-label', ''); }
156
+ slide.setAttribute('role', 'group');
157
+ slide.setAttribute('aria-roledescription', 'slide');
158
+ if (laidOut) { // a slide completely out of view is not reachable by Tab or by a reader's browse mode; a peeking one is
159
+ const a = slide.getBoundingClientRect(), b = track.getBoundingClientRect();
160
+ slide.inert = a.right <= b.left + 1 || a.left >= b.right - 1;
161
+ }
162
+ });
163
+ if (this.label) this.setAttribute('aria-label', this.label); else this.removeAttribute('aria-label');
164
+ const wraps = this.loop && this.#positions > 1;
165
+ /** @type {HTMLButtonElement} */ (this.#prev).disabled = !wraps && index <= 0;
166
+ /** @type {HTMLButtonElement} */ (this.#next).disabled = !wraps && index >= this.#positions - 1;
167
+ /** @type {HTMLButtonElement} */ (this.#prev).hidden = /** @type {HTMLButtonElement} */ (this.#next).hidden = this.noArrows || this.#positions < 2;
168
+ /** @type {HTMLElement} */ (this.#count).hidden = !this.counter || n === 0;
169
+ /** @type {HTMLElement} */ (this.#count).textContent = `${index + 1} / ${n}`;
170
+ const dots = /** @type {HTMLElement} */ (this.#dots);
171
+ dots.hidden = !this.dots || this.#positions < 2;
172
+ if (!dots.hidden) {
173
+ if (rebuild || dots.children.length !== this.#positions) {
174
+ dots.replaceChildren(...Array.from({ length: this.#positions }, (_, i) => {
175
+ const dot = document.createElement('button');
176
+ dot.type = 'button';
177
+ dot.setAttribute('aria-label', text('xoong.carousel.goto', 'Go to slide {n}', { n: i + 1 }));
178
+ const thumb = slides[i]?.getAttribute('data-thumb');
179
+ if (thumb) { const img = document.createElement('img'); img.src = thumb; img.alt = ''; dot.append(img); dot.classList.add('xo-carousel-thumb'); }
180
+ dot.addEventListener('click', () => this.goTo(i));
181
+ return dot;
182
+ }));
183
+ }
184
+ [...dots.children].forEach((dot, i) => dot.setAttribute('aria-current', String(i === index)));
185
+ }
186
+ const playing = this.#playing;
187
+ const toggle = /** @type {HTMLButtonElement} */ (this.#toggle);
188
+ toggle.hidden = !(this.autoplay > 0);
189
+ const label = this.#stopped ? text('xoong.carousel.play', 'Start automatic slide show') : text('xoong.carousel.pause', 'Pause automatic slide show');
190
+ toggle.setAttribute('aria-label', label);
191
+ toggle.dataset.state = this.#stopped ? 'paused' : 'playing'; // a theme can draw its own icons from this
192
+ /** @type {HTMLElement} */ (toggle.firstElementChild).textContent = this.#stopped ? '▶' : '❚❚';
193
+ track.setAttribute('aria-live', this.autoplay > 0 && playing ? 'off' : 'polite');
194
+ this.#announce();
195
+ this.#schedule();
196
+ }
197
+ #announced = 0;
198
+
199
+ #announce() {
200
+ if (this.#index === this.#announced) return;
201
+ this.#announced = this.#index;
202
+ this.dispatchEvent(new CustomEvent('xo-carousel-change', { bubbles: true, detail: { index: this.#index, count: this.#slides.length } }));
203
+ }
204
+
205
+ get #playing() { return !this.#stopped && !this.#held && !this.#reduced && this.autoplay > 0; }
206
+
207
+ #schedule() {
208
+ clearTimeout(this.#timer);
209
+ if (!this.#playing || this.#positions < 2) return;
210
+ this.#timer = /** @type {any} */ (setTimeout(() => { this.#step(1, true); }, this.autoplay));
211
+ }
212
+
213
+ #step(/** @type {number} */ delta, /** @type {boolean} */ wrap) {
214
+ const to = this.#index + delta;
215
+ if (to < 0 || to >= this.#positions) { if (wrap || this.loop) this.goTo(to < 0 ? this.#positions - 1 : 0); return; }
216
+ this.goTo(to);
217
+ }
218
+
219
+ next() { this.#step(1, false); }
220
+ prev() { this.#step(-1, false); }
221
+
222
+ /** Scroll so that slide `i` is the first visible one. */
223
+ goTo(/** @type {number} */ i) {
224
+ const track = this.#track;
225
+ const slides = this.#slides;
226
+ if (!track || !slides.length) return;
227
+ this.#index = Math.max(0, Math.min(this.#positions - 1, i));
228
+ const left = slides[this.#index].offsetLeft - slides[0].offsetLeft;
229
+ const dir = getComputedStyle(track).direction === 'rtl' ? -1 : 1;
230
+ if (track.scrollTo) track.scrollTo({ left: left * dir, behavior: this.#reduced ? 'auto' : 'smooth' }); else track.scrollLeft = left * dir;
231
+ this.#refreshLabels();
232
+ }
233
+
234
+ /** The buttons and dots answer at once; the scroll position settles a moment later and corrects them. */
235
+ #refreshLabels() {
236
+ const index = this.#index;
237
+ /** @type {HTMLButtonElement} */ (this.#prev).disabled = !this.loop && index <= 0;
238
+ /** @type {HTMLButtonElement} */ (this.#next).disabled = !this.loop && index >= this.#positions - 1;
239
+ /** @type {HTMLElement} */ (this.#count).textContent = `${index + 1} / ${this.#slides.length}`;
240
+ [.../** @type {HTMLElement} */ (this.#dots).children].forEach((dot, i) => dot.setAttribute('aria-current', String(i === index)));
241
+ this.#announce();
242
+ this.#schedule();
243
+ }
244
+
245
+ play() { this.#stopped = false; this.#refresh(); }
246
+ pause() { this.#stopped = true; this.#refresh(); }
247
+ }
248
+ define('xo-carousel', XoCarousel);