@sdxc/ui 2026.11.18 → 2026.11.26
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/README.md +133 -1
- package/dist/app/index.d.ts +1 -0
- package/dist/app/index.js +1 -0
- package/dist/app/mixins/command-palette.js +10 -2
- package/dist/app/mixins/log-viewer.js +17 -14
- package/dist/app/mixins/route-dialog.d.ts +17 -0
- package/dist/app/mixins/route-dialog.js +53 -0
- package/dist/app/optimistic-toggle.js +6 -0
- package/dist/app/route-dialog.d.ts +38 -0
- package/dist/app/route-dialog.js +49 -0
- package/dist/audio/equalizer.d.ts +24 -0
- package/dist/audio/equalizer.js +57 -0
- package/dist/audio/index.d.ts +16 -0
- package/dist/audio/index.js +16 -0
- package/dist/audio/media-tile.d.ts +38 -0
- package/dist/audio/media-tile.js +98 -0
- package/dist/audio/player-bar.d.ts +108 -0
- package/dist/audio/player-bar.js +338 -0
- package/dist/audio/queue-list.d.ts +56 -0
- package/dist/audio/queue-list.js +102 -0
- package/dist/audio/track-list.d.ts +91 -0
- package/dist/audio/track-list.js +226 -0
- package/dist/audio/utils/media-session.d.ts +44 -0
- package/dist/audio/utils/media-session.js +87 -0
- package/dist/audio/utils/play-queue.d.ts +98 -0
- package/dist/audio/utils/play-queue.js +149 -0
- package/dist/audio/utils/track-time.d.ts +28 -0
- package/dist/audio/utils/track-time.js +36 -0
- package/dist/chat/composer-format.d.ts +35 -0
- package/dist/chat/composer-format.js +84 -0
- package/dist/chat/index.d.ts +6 -0
- package/dist/chat/index.js +6 -0
- package/dist/chat/mention-menu.d.ts +43 -0
- package/dist/chat/mention-menu.js +70 -0
- package/dist/chat/message-group.d.ts +92 -0
- package/dist/chat/message-group.js +222 -0
- package/dist/chat/mixins/composer-format.d.ts +28 -0
- package/dist/chat/mixins/composer-format.js +80 -0
- package/dist/chat/mixins/composer-send.d.ts +2 -2
- package/dist/chat/mixins/composer-send.js +4 -2
- package/dist/chat/mixins/mention-menu.d.ts +41 -0
- package/dist/chat/mixins/mention-menu.js +213 -0
- package/dist/chat/mixins/reaction-sync.d.ts +47 -0
- package/dist/chat/mixins/reaction-sync.js +172 -0
- package/dist/chat/reaction-bar.d.ts +70 -0
- package/dist/chat/reaction-bar.js +108 -0
- package/dist/chat/typing-indicator.d.ts +42 -0
- package/dist/chat/typing-indicator.js +68 -0
- package/dist/chat/utils/composer-text.d.ts +38 -0
- package/dist/chat/utils/composer-text.js +86 -0
- package/dist/chat/utils/markdown-format.d.ts +30 -0
- package/dist/chat/utils/markdown-format.js +158 -0
- package/dist/chat/utils/mention-query.d.ts +55 -0
- package/dist/chat/utils/mention-query.js +60 -0
- package/dist/commerce/cart-drawer.d.ts +69 -0
- package/dist/commerce/cart-drawer.js +163 -0
- package/dist/commerce/cart-line.d.ts +32 -0
- package/dist/commerce/cart-line.js +43 -0
- package/dist/commerce/checkout-layout.d.ts +32 -0
- package/dist/commerce/checkout-layout.js +51 -0
- package/dist/commerce/facet-filters.d.ts +121 -0
- package/dist/commerce/facet-filters.js +318 -0
- package/dist/commerce/fulfilment-note.d.ts +28 -0
- package/dist/commerce/fulfilment-note.js +26 -0
- package/dist/commerce/index.d.ts +27 -0
- package/dist/commerce/index.js +27 -0
- package/dist/commerce/item-options-dialog.d.ts +67 -0
- package/dist/commerce/item-options-dialog.js +150 -0
- package/dist/commerce/menu-item-card.d.ts +37 -0
- package/dist/commerce/menu-item-card.js +96 -0
- package/dist/commerce/option-group.d.ts +62 -0
- package/dist/commerce/option-group.js +121 -0
- package/dist/commerce/order-summary.d.ts +42 -0
- package/dist/commerce/order-summary.js +76 -0
- package/dist/commerce/order-tracker.d.ts +80 -0
- package/dist/commerce/order-tracker.js +178 -0
- package/dist/commerce/price.d.ts +52 -0
- package/dist/commerce/price.js +108 -0
- package/dist/commerce/product-card.d.ts +53 -0
- package/dist/commerce/product-card.js +127 -0
- package/dist/commerce/product-gallery.d.ts +70 -0
- package/dist/commerce/product-gallery.js +186 -0
- package/dist/commerce/promo-code.d.ts +56 -0
- package/dist/commerce/promo-code.js +101 -0
- package/dist/commerce/quantity-stepper.d.ts +42 -0
- package/dist/commerce/quantity-stepper.js +53 -0
- package/dist/commerce/question-list.d.ts +53 -0
- package/dist/commerce/question-list.js +92 -0
- package/dist/commerce/shipping-estimate.d.ts +53 -0
- package/dist/commerce/shipping-estimate.js +39 -0
- package/dist/commerce/sort-select.d.ts +38 -0
- package/dist/commerce/sort-select.js +38 -0
- package/dist/commerce/variant-picker.d.ts +75 -0
- package/dist/commerce/variant-picker.js +135 -0
- package/dist/components/amount.d.ts +46 -0
- package/dist/components/amount.js +34 -0
- package/dist/components/autocomplete.d.ts +82 -0
- package/dist/components/autocomplete.js +114 -0
- package/dist/components/carousel.d.ts +31 -1
- package/dist/components/carousel.js +66 -3
- package/dist/components/index.d.ts +11 -0
- package/dist/components/index.js +11 -0
- package/dist/components/letterbox.d.ts +47 -0
- package/dist/components/letterbox.js +56 -0
- package/dist/components/lightbox.d.ts +88 -0
- package/dist/components/lightbox.js +224 -0
- package/dist/components/media-grid.d.ts +53 -0
- package/dist/components/media-grid.js +89 -0
- package/dist/components/range-months.d.ts +100 -0
- package/dist/components/range-months.js +274 -0
- package/dist/components/rating-input.d.ts +33 -0
- package/dist/components/rating-input.js +63 -0
- package/dist/components/rating.d.ts +38 -0
- package/dist/components/rating.js +66 -0
- package/dist/components/review-card.d.ts +39 -0
- package/dist/components/review-card.js +59 -0
- package/dist/components/section-bar.d.ts +57 -0
- package/dist/components/section-bar.js +140 -0
- package/dist/components/slider.js +7 -0
- package/dist/components/step-indicator.d.ts +46 -0
- package/dist/components/step-indicator.js +96 -0
- package/dist/finance/index.d.ts +10 -0
- package/dist/finance/index.js +10 -0
- package/dist/finance/ledger.d.ts +60 -0
- package/dist/finance/ledger.js +93 -0
- package/dist/finance/payment-card.d.ts +49 -0
- package/dist/finance/payment-card.js +115 -0
- package/dist/map/engine.d.ts +49 -0
- package/dist/map/engine.js +591 -0
- package/dist/map/index.d.ts +12 -0
- package/dist/map/index.js +12 -0
- package/dist/map/map-canvas.d.ts +101 -0
- package/dist/map/map-canvas.js +275 -0
- package/dist/map/map.d.ts +217 -0
- package/dist/map/map.js +598 -0
- package/dist/map/mixins/map-link.d.ts +30 -0
- package/dist/map/mixins/map-link.js +116 -0
- package/dist/map/utils/basemap.d.ts +88 -0
- package/dist/map/utils/basemap.js +84 -0
- package/dist/map/utils/cluster.d.ts +43 -0
- package/dist/map/utils/cluster.js +80 -0
- package/dist/map/utils/mercator.d.ts +66 -0
- package/dist/map/utils/mercator.js +92 -0
- package/dist/map/utils/stage.d.ts +24 -0
- package/dist/map/utils/stage.js +49 -0
- package/dist/mixins/autocomplete.d.ts +28 -0
- package/dist/mixins/autocomplete.js +283 -0
- package/dist/mixins/carousel-controls.d.ts +14 -3
- package/dist/mixins/carousel-controls.js +89 -30
- package/dist/mixins/index.d.ts +2 -0
- package/dist/mixins/index.js +2 -0
- package/dist/mixins/lightbox-controls.d.ts +34 -0
- package/dist/mixins/lightbox-controls.js +176 -0
- package/dist/schedule/agenda.d.ts +63 -0
- package/dist/schedule/agenda.js +221 -0
- package/dist/schedule/day-strip.d.ts +50 -0
- package/dist/schedule/day-strip.js +136 -0
- package/dist/schedule/free-busy.d.ts +96 -0
- package/dist/schedule/free-busy.js +285 -0
- package/dist/schedule/index.d.ts +15 -0
- package/dist/schedule/index.js +15 -0
- package/dist/schedule/mixins/day-strip-scroll.d.ts +16 -0
- package/dist/schedule/mixins/day-strip-scroll.js +40 -0
- package/dist/schedule/mixins/time-grid-drag.d.ts +24 -0
- package/dist/schedule/mixins/time-grid-drag.js +532 -0
- package/dist/schedule/month-view.d.ts +68 -0
- package/dist/schedule/month-view.js +301 -0
- package/dist/schedule/time-grid.d.ts +168 -0
- package/dist/schedule/time-grid.js +417 -0
- package/dist/schedule/utils/time-grid-layout.d.ts +50 -0
- package/dist/schedule/utils/time-grid-layout.js +93 -0
- package/dist/schedule/year-view.d.ts +46 -0
- package/dist/schedule/year-view.js +142 -0
- package/dist/styles/index.d.ts +1 -0
- package/dist/styles/index.js +1 -0
- package/dist/styles/typewriter-text.d.ts +20 -0
- package/dist/styles/typewriter-text.js +23 -0
- package/dist/test/map.d.ts +18 -0
- package/dist/test/map.js +96 -0
- package/dist/test/reels.d.ts +30 -0
- package/dist/test/reels.js +41 -0
- package/dist/test/stories.d.ts +31 -0
- package/dist/test/stories.js +66 -0
- package/dist/travel/boarding-pass.d.ts +107 -0
- package/dist/travel/boarding-pass.js +293 -0
- package/dist/travel/car-offer.d.ts +70 -0
- package/dist/travel/car-offer.js +127 -0
- package/dist/travel/fare-calendar.d.ts +85 -0
- package/dist/travel/fare-calendar.js +233 -0
- package/dist/travel/fare-options.d.ts +84 -0
- package/dist/travel/fare-options.js +142 -0
- package/dist/travel/flight-itinerary.d.ts +84 -0
- package/dist/travel/flight-itinerary.js +112 -0
- package/dist/travel/flight-option.d.ts +58 -0
- package/dist/travel/flight-option.js +111 -0
- package/dist/travel/flight-segment.d.ts +105 -0
- package/dist/travel/flight-segment.js +174 -0
- package/dist/travel/index.d.ts +20 -0
- package/dist/travel/index.js +20 -0
- package/dist/travel/package-savings.d.ts +74 -0
- package/dist/travel/package-savings.js +128 -0
- package/dist/travel/price-breakdown.d.ts +66 -0
- package/dist/travel/price-breakdown.js +102 -0
- package/dist/travel/rating-breakdown.d.ts +81 -0
- package/dist/travel/rating-breakdown.js +132 -0
- package/dist/travel/seat-map.d.ts +108 -0
- package/dist/travel/seat-map.js +371 -0
- package/dist/travel/trip-timeline.d.ts +84 -0
- package/dist/travel/trip-timeline.js +212 -0
- package/dist/utils/gamepad-presses.d.ts +21 -0
- package/dist/utils/gamepad-presses.js +28 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.js +5 -0
- package/dist/utils/move-focus.d.ts +20 -0
- package/dist/utils/move-focus.js +28 -0
- package/dist/utils/range-months.d.ts +54 -0
- package/dist/utils/range-months.js +98 -0
- package/dist/utils/swipe-direction.d.ts +23 -0
- package/dist/utils/swipe-direction.js +27 -0
- package/dist/utils/typewriter.d.ts +49 -0
- package/dist/utils/typewriter.js +171 -0
- package/dist/video/index.d.ts +6 -2
- package/dist/video/index.js +6 -2
- package/dist/video/mixins/reel-feed-sync.d.ts +41 -0
- package/dist/video/mixins/reel-feed-sync.js +430 -0
- package/dist/video/mixins/story-viewer-sync.d.ts +31 -0
- package/dist/video/mixins/story-viewer-sync.js +391 -0
- package/dist/video/reel-feed.d.ts +95 -0
- package/dist/video/reel-feed.js +124 -0
- package/dist/video/story-ring.d.ts +32 -0
- package/dist/video/story-ring.js +42 -0
- package/dist/video/story-viewer.d.ts +85 -0
- package/dist/video/story-viewer.js +250 -0
- package/package.json +21 -2
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reveals rendered text character by character without touching its DOM: the unrevealed rest
|
|
3
|
+
* is a range in the `ui-typewriter` CSS highlight, which `typewriterText()` paints transparent,
|
|
4
|
+
* so the text keeps its final layout and a screen reader gets all of it at once.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
/** The CSS highlight holding every typewriter's unrevealed text. */
|
|
10
|
+
export const TYPEWRITER_HIGHLIGHT = "ui-typewriter";
|
|
11
|
+
/** Marks an element where typing holds; its value is the hold in milliseconds. */
|
|
12
|
+
export const TYPEWRITER_PAUSE_ATTRIBUTE = "data-typewriter-pause";
|
|
13
|
+
/** Multiplies the typing speed of the text inside an element; `0.5` types at half speed. */
|
|
14
|
+
export const TYPEWRITER_SPEED_ATTRIBUTE = "data-typewriter-speed";
|
|
15
|
+
/** How long a pause element holds the typing when its value is empty or not a positive number. */
|
|
16
|
+
const DEFAULT_PAUSE_MS = 400;
|
|
17
|
+
/**
|
|
18
|
+
* The shared highlight the unrevealed ranges go in, registered on first use.
|
|
19
|
+
*
|
|
20
|
+
* @returns The highlight, or `undefined` where the browser lacks the Custom Highlight API, which
|
|
21
|
+
* leaves the text fully painted from the start.
|
|
22
|
+
*/
|
|
23
|
+
function unrevealed() {
|
|
24
|
+
if (typeof CSS === "undefined" || !("highlights" in CSS))
|
|
25
|
+
return undefined;
|
|
26
|
+
let existing = CSS.highlights.get(TYPEWRITER_HIGHLIGHT);
|
|
27
|
+
if (existing !== undefined)
|
|
28
|
+
return existing;
|
|
29
|
+
let created = new Highlight();
|
|
30
|
+
CSS.highlights.set(TYPEWRITER_HIGHLIGHT, created);
|
|
31
|
+
return created;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The speed multiplier around a text node: the product of every speed attribute from the node
|
|
35
|
+
* up to `root`, so nested speeds compound.
|
|
36
|
+
*
|
|
37
|
+
* @param node - A text node inside `root`.
|
|
38
|
+
* @param root - The element being typed, where the search stops.
|
|
39
|
+
* @returns The multiplier; `0.5` types at half speed.
|
|
40
|
+
*/
|
|
41
|
+
function speedAt(node, root) {
|
|
42
|
+
let rate = 1;
|
|
43
|
+
let element = node.parentElement;
|
|
44
|
+
while (element !== null && element !== root.parentElement) {
|
|
45
|
+
let value = Number(element.getAttribute(TYPEWRITER_SPEED_ATTRIBUTE));
|
|
46
|
+
if (Number.isFinite(value) && value > 0)
|
|
47
|
+
rate *= value;
|
|
48
|
+
if (element === root)
|
|
49
|
+
break;
|
|
50
|
+
element = element.parentElement;
|
|
51
|
+
}
|
|
52
|
+
return rate;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* When each character of `root` appears, honoring speed and pause markup.
|
|
56
|
+
*
|
|
57
|
+
* @param root - The element being typed.
|
|
58
|
+
* @param charactersPerSecond - The base typing speed.
|
|
59
|
+
* @returns One step per character, in reading order.
|
|
60
|
+
*/
|
|
61
|
+
function schedule(root, charactersPerSecond) {
|
|
62
|
+
let steps = [];
|
|
63
|
+
let time = 0;
|
|
64
|
+
let walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT | NodeFilter.SHOW_ELEMENT);
|
|
65
|
+
let interval = 1000 / charactersPerSecond;
|
|
66
|
+
for (let node = walker.nextNode(); node !== null; node = walker.nextNode()) {
|
|
67
|
+
if (node instanceof Element && node.hasAttribute(TYPEWRITER_PAUSE_ATTRIBUTE)) {
|
|
68
|
+
let pause = Number(node.getAttribute(TYPEWRITER_PAUSE_ATTRIBUTE));
|
|
69
|
+
time += Number.isFinite(pause) && pause > 0 ? pause : DEFAULT_PAUSE_MS;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (!(node instanceof Text))
|
|
73
|
+
continue;
|
|
74
|
+
let step = interval / speedAt(node, root);
|
|
75
|
+
for (let offset = 0; offset < node.data.length; offset++) {
|
|
76
|
+
time += step;
|
|
77
|
+
steps.push({ node, offset, at: time });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return steps;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Types out the text of one element. Construct it once the element is in the document, which
|
|
84
|
+
* hides the text, call `start()` to type, and `dispose()` when the element leaves. Paint the
|
|
85
|
+
* host with `typewriterText()` from `@sdxc/ui/styles` so the hidden text stays invisible.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* let writer = new Typewriter(paragraph);
|
|
89
|
+
* writer.start(40);
|
|
90
|
+
* await writer.done;
|
|
91
|
+
*/
|
|
92
|
+
export class Typewriter {
|
|
93
|
+
#root;
|
|
94
|
+
#range;
|
|
95
|
+
#steps = [];
|
|
96
|
+
#shown = 0;
|
|
97
|
+
#start = 0;
|
|
98
|
+
#timer;
|
|
99
|
+
#complete = false;
|
|
100
|
+
#resolve = () => undefined;
|
|
101
|
+
/** Settles once the whole text is visible, typed out or finished early. */
|
|
102
|
+
done;
|
|
103
|
+
/**
|
|
104
|
+
* Hides the whole text, ready to type.
|
|
105
|
+
*
|
|
106
|
+
* @param root - The element whose text is typed.
|
|
107
|
+
*/
|
|
108
|
+
constructor(root) {
|
|
109
|
+
this.#root = root;
|
|
110
|
+
this.#range = document.createRange();
|
|
111
|
+
this.#range.selectNodeContents(root);
|
|
112
|
+
unrevealed()?.add(this.#range);
|
|
113
|
+
this.done = new Promise((resolve) => {
|
|
114
|
+
this.#resolve = resolve;
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/** How many characters are visible. */
|
|
118
|
+
get shown() {
|
|
119
|
+
return this.#shown;
|
|
120
|
+
}
|
|
121
|
+
/** True once the text is fully visible. */
|
|
122
|
+
get finished() {
|
|
123
|
+
return this.#complete;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Starts typing.
|
|
127
|
+
*
|
|
128
|
+
* @param charactersPerSecond - The base typing speed; `0` or less shows the text at once.
|
|
129
|
+
*/
|
|
130
|
+
start(charactersPerSecond) {
|
|
131
|
+
if (charactersPerSecond <= 0) {
|
|
132
|
+
this.finish();
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
this.#steps = schedule(this.#root, charactersPerSecond);
|
|
136
|
+
if (this.#steps.length === 0) {
|
|
137
|
+
this.finish();
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
this.#start = performance.now();
|
|
141
|
+
this.#tick();
|
|
142
|
+
}
|
|
143
|
+
/** Shows the rest of the text at once, as a click during typing usually asks. */
|
|
144
|
+
finish() {
|
|
145
|
+
clearTimeout(this.#timer);
|
|
146
|
+
this.#complete = true;
|
|
147
|
+
this.#shown = this.#steps.length;
|
|
148
|
+
unrevealed()?.delete(this.#range);
|
|
149
|
+
this.#resolve();
|
|
150
|
+
}
|
|
151
|
+
/** Stops typing and releases the highlight range, leaving `done` unsettled. */
|
|
152
|
+
dispose() {
|
|
153
|
+
clearTimeout(this.#timer);
|
|
154
|
+
unrevealed()?.delete(this.#range);
|
|
155
|
+
}
|
|
156
|
+
/** Reveals every character whose moment has passed, and waits for the next one. */
|
|
157
|
+
#tick() {
|
|
158
|
+
let elapsed = performance.now() - this.#start;
|
|
159
|
+
while (this.#shown < this.#steps.length && (this.#steps[this.#shown]?.at ?? 0) <= elapsed) {
|
|
160
|
+
this.#shown++;
|
|
161
|
+
}
|
|
162
|
+
let next = this.#steps[this.#shown];
|
|
163
|
+
if (next === undefined) {
|
|
164
|
+
this.finish();
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
this.#range.setStart(next.node, next.offset);
|
|
168
|
+
this.#range.setEnd(this.#root, this.#root.childNodes.length);
|
|
169
|
+
this.#timer = window.setTimeout(() => this.#tick(), Math.max(0, next.at - elapsed));
|
|
170
|
+
}
|
|
171
|
+
}
|
package/dist/video/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Barrel for `@sdxc/ui/video`: a video player and its parts,
|
|
3
|
-
*
|
|
2
|
+
* Barrel for `@sdxc/ui/video`: a video player and its parts, the catalogue pieces a video
|
|
3
|
+
* library browses with (cards, shelves, heroes, episode lists), and the short-video pieces a
|
|
4
|
+
* social app plays with (a reel feed, a story viewer and its rings), with their helpers.
|
|
4
5
|
* Each module also imports on its own, as `@sdxc/ui/video/video-player`.
|
|
5
6
|
*
|
|
6
7
|
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
@@ -9,6 +10,9 @@
|
|
|
9
10
|
export * from "./mixins/video-player-sync.js";
|
|
10
11
|
export * from "./utils/timeline.js";
|
|
11
12
|
export * from "./episode-list.js";
|
|
13
|
+
export * from "./reel-feed.js";
|
|
14
|
+
export * from "./story-ring.js";
|
|
15
|
+
export * from "./story-viewer.js";
|
|
12
16
|
export * from "./title-hero.js";
|
|
13
17
|
export * from "./video-card.js";
|
|
14
18
|
export * from "./video-player.js";
|
package/dist/video/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Barrel for `@sdxc/ui/video`: a video player and its parts,
|
|
3
|
-
*
|
|
2
|
+
* Barrel for `@sdxc/ui/video`: a video player and its parts, the catalogue pieces a video
|
|
3
|
+
* library browses with (cards, shelves, heroes, episode lists), and the short-video pieces a
|
|
4
|
+
* social app plays with (a reel feed, a story viewer and its rings), with their helpers.
|
|
4
5
|
* Each module also imports on its own, as `@sdxc/ui/video/video-player`.
|
|
5
6
|
*
|
|
6
7
|
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
@@ -9,6 +10,9 @@
|
|
|
9
10
|
export * from "./mixins/video-player-sync.js";
|
|
10
11
|
export * from "./utils/timeline.js";
|
|
11
12
|
export * from "./episode-list.js";
|
|
13
|
+
export * from "./reel-feed.js";
|
|
14
|
+
export * from "./story-ring.js";
|
|
15
|
+
export * from "./story-viewer.js";
|
|
12
16
|
export * from "./title-hero.js";
|
|
13
17
|
export * from "./video-card.js";
|
|
14
18
|
export * from "./video-player.js";
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why JS: picking which reel is on screen, loading only the ones around it, pausing the rest,
|
|
3
|
+
* and stepping one reel per key press or wheel flick all need the scroller's live position.
|
|
4
|
+
* No-JS baseline: the feed scrolls and snaps by touch, wheel and keys, and the first reel autoplays.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { MixinFactory } from "remix/component";
|
|
10
|
+
/** Set on the feed once its island runs. */
|
|
11
|
+
export declare const REEL_FEED_READY = "data-reel-feed-ready";
|
|
12
|
+
/** Set on the reel on screen, which plays while every other reel pauses. */
|
|
13
|
+
export declare const REEL_ACTIVE = "data-reel-active";
|
|
14
|
+
/** Set on a reel the visitor paused by tapping it. */
|
|
15
|
+
export declare const REEL_PAUSED = "data-reel-paused";
|
|
16
|
+
/** Dispatched, bubbling, from the feed whenever another reel comes on screen. */
|
|
17
|
+
export declare const REEL_CHANGE_EVENT = "ui:reel-change";
|
|
18
|
+
/** What {@link REEL_CHANGE_EVENT} carries. */
|
|
19
|
+
export interface ReelChangeDetail {
|
|
20
|
+
/** The reel's position in the feed, from 0. */
|
|
21
|
+
index: number;
|
|
22
|
+
/** The reel's own address. */
|
|
23
|
+
href: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The `preload` a reel's video gets by its distance from the reel on screen: the current and the
|
|
27
|
+
* next load whole, the one after loads its metadata, and every other one waits.
|
|
28
|
+
*
|
|
29
|
+
* @param offset The reel's index minus the current reel's index.
|
|
30
|
+
* @returns The value for the video's `preload` attribute.
|
|
31
|
+
* @example reelPreload(1) // "auto"
|
|
32
|
+
*/
|
|
33
|
+
export declare function reelPreload(offset: number): "auto" | "metadata" | "none";
|
|
34
|
+
/**
|
|
35
|
+
* Runs the feed it sits in: watches which reel fills the scroller, slides the preload window,
|
|
36
|
+
* plays that reel and pauses the rest, follows its address, prefetches the next reel's page,
|
|
37
|
+
* and steps one reel at a time for the arrow keys, Page keys, `j`/`k`, the wheel and any
|
|
38
|
+
* `data-reel-step` control inside the feed. A looping feed runs on past the last reel back to
|
|
39
|
+
* the first.
|
|
40
|
+
*/
|
|
41
|
+
export declare const reelFeedSync: MixinFactory<HTMLElement>;
|
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why JS: picking which reel is on screen, loading only the ones around it, pausing the rest,
|
|
3
|
+
* and stepping one reel per key press or wheel flick all need the scroller's live position.
|
|
4
|
+
* No-JS baseline: the feed scrolls and snaps by touch, wheel and keys, and the first reel autoplays.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { createMixin, prefetch } from "remix/component";
|
|
10
|
+
/** Set on the feed once its island runs. */
|
|
11
|
+
export const REEL_FEED_READY = "data-reel-feed-ready";
|
|
12
|
+
/** Set on the reel on screen, which plays while every other reel pauses. */
|
|
13
|
+
export const REEL_ACTIVE = "data-reel-active";
|
|
14
|
+
/** Set on a reel the visitor paused by tapping it. */
|
|
15
|
+
export const REEL_PAUSED = "data-reel-paused";
|
|
16
|
+
/** Dispatched, bubbling, from the feed whenever another reel comes on screen. */
|
|
17
|
+
export const REEL_CHANGE_EVENT = "ui:reel-change";
|
|
18
|
+
/** How long the wheel must rest before another flick steps again, so one flick moves one reel. */
|
|
19
|
+
const WHEEL_REST_MS = 220;
|
|
20
|
+
/** The smallest wheel travel that counts as a flick, which drops a trackpad's stray nudges. */
|
|
21
|
+
const WHEEL_THRESHOLD = 12;
|
|
22
|
+
/**
|
|
23
|
+
* How long the feed holds still before it counts as at rest, for the browsers and touch flings
|
|
24
|
+
* that reach the end of the feed without a `scrollend`.
|
|
25
|
+
*/
|
|
26
|
+
const SCROLL_REST_MS = 150;
|
|
27
|
+
/**
|
|
28
|
+
* The `preload` a reel's video gets by its distance from the reel on screen: the current and the
|
|
29
|
+
* next load whole, the one after loads its metadata, and every other one waits.
|
|
30
|
+
*
|
|
31
|
+
* @param offset The reel's index minus the current reel's index.
|
|
32
|
+
* @returns The value for the video's `preload` attribute.
|
|
33
|
+
* @example reelPreload(1) // "auto"
|
|
34
|
+
*/
|
|
35
|
+
export function reelPreload(offset) {
|
|
36
|
+
if (offset === 0 || offset === 1)
|
|
37
|
+
return "auto";
|
|
38
|
+
return offset === 2 ? "metadata" : "none";
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Runs the feed it sits in: watches which reel fills the scroller, slides the preload window,
|
|
42
|
+
* plays that reel and pauses the rest, follows its address, prefetches the next reel's page,
|
|
43
|
+
* and steps one reel at a time for the arrow keys, Page keys, `j`/`k`, the wheel and any
|
|
44
|
+
* `data-reel-step` control inside the feed. A looping feed runs on past the last reel back to
|
|
45
|
+
* the first.
|
|
46
|
+
*/
|
|
47
|
+
export const reelFeedSync = createMixin((handle) => {
|
|
48
|
+
let controller;
|
|
49
|
+
handle.addEventListener("insert", (event) => {
|
|
50
|
+
controller?.abort();
|
|
51
|
+
let feed = event.node.closest("[data-slot=reel-feed]");
|
|
52
|
+
let scroller = feed?.querySelector("[data-slot=reel-feed-scroller]");
|
|
53
|
+
if (!feed || !scroller)
|
|
54
|
+
return;
|
|
55
|
+
controller = new AbortController();
|
|
56
|
+
wireFeed(feed, scroller, controller.signal);
|
|
57
|
+
});
|
|
58
|
+
handle.addEventListener("remove", () => {
|
|
59
|
+
controller?.abort();
|
|
60
|
+
controller = undefined;
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
/** The reels in feed order. */
|
|
64
|
+
function reelsOf(scroller) {
|
|
65
|
+
return [...scroller.querySelectorAll(":scope > [data-slot=reel-feed-item]")];
|
|
66
|
+
}
|
|
67
|
+
/** The video a reel plays, if it has one. */
|
|
68
|
+
function videoOf(reel) {
|
|
69
|
+
return reel?.querySelector("video[data-reel-video]") ?? null;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Plays a muted video, ignoring the refusal a browser gives when the page has not been
|
|
73
|
+
* interacted with or the video was paused again before it could start.
|
|
74
|
+
*/
|
|
75
|
+
function play(video) {
|
|
76
|
+
video.muted = true;
|
|
77
|
+
video.play().catch(() => undefined);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Wires one feed until `signal` aborts.
|
|
81
|
+
*
|
|
82
|
+
* @param feed The feed's root, which carries its options as data attributes.
|
|
83
|
+
* @param scroller The element that scrolls and snaps.
|
|
84
|
+
* @param signal Ends the wiring.
|
|
85
|
+
*/
|
|
86
|
+
function wireFeed(feed, scroller, signal) {
|
|
87
|
+
let reels = reelsOf(scroller);
|
|
88
|
+
let current = Math.max(0, reels.findIndex((reel) => reel.hasAttribute(REEL_ACTIVE)));
|
|
89
|
+
let follow = feed.hasAttribute("data-follow-address");
|
|
90
|
+
let advance = feed.hasAttribute("data-auto-advance");
|
|
91
|
+
let loop = feed.hasAttribute("data-loop");
|
|
92
|
+
let clone = loop ? appendLoopClone(scroller, reels[0]) : null;
|
|
93
|
+
let target = feed.dataset.prefetchTarget;
|
|
94
|
+
let wheelLocked = false;
|
|
95
|
+
let wheelTimer;
|
|
96
|
+
let hint = null;
|
|
97
|
+
let touchedAt = null;
|
|
98
|
+
let fingerDown = false;
|
|
99
|
+
let restTimer;
|
|
100
|
+
/** Moves the preload window, the playing reel and the address to the reel at `index`. */
|
|
101
|
+
function activate(index, announce) {
|
|
102
|
+
current = index;
|
|
103
|
+
reels.forEach((reel, position) => {
|
|
104
|
+
let offset = position - index;
|
|
105
|
+
let video = videoOf(reel);
|
|
106
|
+
reel.toggleAttribute(REEL_ACTIVE, offset === 0);
|
|
107
|
+
if (offset !== 0)
|
|
108
|
+
reel.removeAttribute(REEL_PAUSED);
|
|
109
|
+
if (video === null)
|
|
110
|
+
return;
|
|
111
|
+
let preload = reelPreload(offset);
|
|
112
|
+
if (video.preload !== preload)
|
|
113
|
+
video.preload = preload;
|
|
114
|
+
if (offset === 0) {
|
|
115
|
+
if (!reel.hasAttribute(REEL_PAUSED))
|
|
116
|
+
play(video);
|
|
117
|
+
}
|
|
118
|
+
else if (!video.paused) {
|
|
119
|
+
video.pause();
|
|
120
|
+
}
|
|
121
|
+
if (preload !== "none" && video.readyState === 0 && video.networkState !== 2)
|
|
122
|
+
video.load();
|
|
123
|
+
});
|
|
124
|
+
syncSteps(index);
|
|
125
|
+
let reel = reels[index];
|
|
126
|
+
let next = reels[index + 1]?.dataset.href;
|
|
127
|
+
if (next !== undefined) {
|
|
128
|
+
hint = movePrefetchHint(feed, hint, next);
|
|
129
|
+
if (target !== undefined)
|
|
130
|
+
prefetch(new URL(next, location.href).href, { target });
|
|
131
|
+
}
|
|
132
|
+
let href = reel?.dataset.href;
|
|
133
|
+
if (!announce || reel === undefined || href === undefined)
|
|
134
|
+
return;
|
|
135
|
+
let url = new URL(href, location.href);
|
|
136
|
+
if (follow && `${url.pathname}${url.search}` !== `${location.pathname}${location.search}`) {
|
|
137
|
+
history.replaceState(history.state, "", href);
|
|
138
|
+
}
|
|
139
|
+
feed.dispatchEvent(new CustomEvent(REEL_CHANGE_EVENT, {
|
|
140
|
+
bubbles: true,
|
|
141
|
+
detail: { index, href },
|
|
142
|
+
}));
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Points each `data-reel-step` control at the reel it would move to from the one on screen,
|
|
146
|
+
* and marks it `aria-disabled` at the end it points past, so an arrow is live in both
|
|
147
|
+
* directions whichever reel the page opened on.
|
|
148
|
+
*/
|
|
149
|
+
function syncSteps(index) {
|
|
150
|
+
for (let control of feed.querySelectorAll("[data-reel-step]")) {
|
|
151
|
+
let forward = Number(control.dataset.reelStep) >= 0;
|
|
152
|
+
let target = reels[index + (forward ? 1 : -1)] ?? (forward && clone !== null ? reels[0] : undefined);
|
|
153
|
+
if (target === undefined) {
|
|
154
|
+
control.setAttribute("aria-disabled", "true");
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
control.removeAttribute("aria-disabled");
|
|
158
|
+
let href = target.dataset.href;
|
|
159
|
+
if (control instanceof HTMLAnchorElement && href !== undefined)
|
|
160
|
+
control.href = href;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/** Scrolls to the reel `step` places away, clamped to the feed's ends. */
|
|
164
|
+
function go(step) {
|
|
165
|
+
if (clone !== null && step > 0 && current === reels.length - 1) {
|
|
166
|
+
scroller.scrollTo({ top: clone.offsetTop, behavior: scrollBehavior() });
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
let index = Math.min(reels.length - 1, Math.max(0, current + step));
|
|
170
|
+
let reel = reels[index];
|
|
171
|
+
if (reel === undefined || index === current)
|
|
172
|
+
return;
|
|
173
|
+
scroller.scrollTo({ top: reel.offsetTop, behavior: scrollBehavior() });
|
|
174
|
+
activate(index, true);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Whether a key press is the feed's: one from inside it, or any while the feed fills most of
|
|
178
|
+
* the window and is the page's main content; a small feed on a longer page leaves the page's keys alone.
|
|
179
|
+
*/
|
|
180
|
+
function ownsKeys(event) {
|
|
181
|
+
if (event.target instanceof Node && feed.contains(event.target))
|
|
182
|
+
return true;
|
|
183
|
+
return feed.getBoundingClientRect().height >= window.innerHeight * 0.8;
|
|
184
|
+
}
|
|
185
|
+
/** Steps for the keys a visitor reads a vertical feed with, unless they are typing. */
|
|
186
|
+
function onKey(event) {
|
|
187
|
+
if (event.defaultPrevented || event.altKey || event.ctrlKey || event.metaKey)
|
|
188
|
+
return;
|
|
189
|
+
let typing = event.target instanceof Element &&
|
|
190
|
+
event.target.closest("input, textarea, select, [contenteditable]");
|
|
191
|
+
if (typing || !ownsKeys(event))
|
|
192
|
+
return;
|
|
193
|
+
let step = { ArrowDown: 1, PageDown: 1, j: 1, ArrowUp: -1, PageUp: -1, k: -1 }[event.key];
|
|
194
|
+
if (step === undefined)
|
|
195
|
+
return;
|
|
196
|
+
event.preventDefault();
|
|
197
|
+
go(step);
|
|
198
|
+
}
|
|
199
|
+
/** Turns each flick of the wheel into one step, however many events its momentum sends. */
|
|
200
|
+
function onWheel(event) {
|
|
201
|
+
if (event.ctrlKey || Math.abs(event.deltaY) < Math.abs(event.deltaX))
|
|
202
|
+
return;
|
|
203
|
+
event.preventDefault();
|
|
204
|
+
clearTimeout(wheelTimer);
|
|
205
|
+
wheelTimer = setTimeout(() => (wheelLocked = false), WHEEL_REST_MS);
|
|
206
|
+
if (wheelLocked || Math.abs(event.deltaY) < WHEEL_THRESHOLD)
|
|
207
|
+
return;
|
|
208
|
+
wheelLocked = true;
|
|
209
|
+
go(event.deltaY > 0 ? 1 : -1);
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Steps for a control marked `data-reel-step` (`1` or `-1`) inside the feed, such as a link
|
|
213
|
+
* to the next reel's page, which then moves the feed in place.
|
|
214
|
+
*/
|
|
215
|
+
function onStep(event) {
|
|
216
|
+
let control = event.target instanceof Element
|
|
217
|
+
? event.target.closest("[data-reel-step]")
|
|
218
|
+
: null;
|
|
219
|
+
if (control === null || !feed.contains(control))
|
|
220
|
+
return;
|
|
221
|
+
if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)
|
|
222
|
+
return;
|
|
223
|
+
event.preventDefault();
|
|
224
|
+
go(Number(control.dataset.reelStep) < 0 ? -1 : 1);
|
|
225
|
+
}
|
|
226
|
+
/** Pauses or resumes the reel whose stage was pressed. */
|
|
227
|
+
function onToggle(event) {
|
|
228
|
+
let button = event.target instanceof Element ? event.target.closest("[data-reel-toggle]") : null;
|
|
229
|
+
let reel = button?.closest("[data-slot=reel-feed-item]");
|
|
230
|
+
let video = videoOf(reel ?? undefined);
|
|
231
|
+
if (!reel || !video)
|
|
232
|
+
return;
|
|
233
|
+
let paused = !reel.hasAttribute(REEL_PAUSED);
|
|
234
|
+
reel.toggleAttribute(REEL_PAUSED, paused);
|
|
235
|
+
button?.setAttribute("aria-pressed", paused ? "true" : "false");
|
|
236
|
+
if (paused)
|
|
237
|
+
video.pause();
|
|
238
|
+
else
|
|
239
|
+
play(video);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Draws the reel's progress line and, at the reel's own length, moves on to the next reel
|
|
243
|
+
* when the feed advances by itself; the last reel, or any reel in a feed that does not, loops.
|
|
244
|
+
*/
|
|
245
|
+
function onTime(event) {
|
|
246
|
+
let video = event.target;
|
|
247
|
+
if (!(video instanceof HTMLVideoElement) || !video.hasAttribute("data-reel-video"))
|
|
248
|
+
return;
|
|
249
|
+
let length = Number(video.dataset.loopAt) || video.duration;
|
|
250
|
+
if (!Number.isFinite(length) || length <= 0)
|
|
251
|
+
return;
|
|
252
|
+
if (video.currentTime >= length) {
|
|
253
|
+
let reel = video.closest("[data-slot=reel-feed-item]");
|
|
254
|
+
let index = reel === null ? -1 : reels.indexOf(reel);
|
|
255
|
+
if (advance && index === current && (clone !== null || index < reels.length - 1)) {
|
|
256
|
+
video.pause();
|
|
257
|
+
go(1);
|
|
258
|
+
}
|
|
259
|
+
video.currentTime = 0;
|
|
260
|
+
}
|
|
261
|
+
let reel = video.closest("[data-slot=reel-feed-item]");
|
|
262
|
+
reel?.style.setProperty("--ui-reel-progress", String(Math.min(1, video.currentTime / length)));
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Settles a touch gesture on the reel next to the one it started from, so a long fling
|
|
266
|
+
* moves one reel even where a browser carries its momentum past a snap stop.
|
|
267
|
+
*
|
|
268
|
+
* @returns Whether it scrolled again, which ends in another `scrollend`.
|
|
269
|
+
*/
|
|
270
|
+
function settleTouch() {
|
|
271
|
+
if (touchedAt === null)
|
|
272
|
+
return false;
|
|
273
|
+
let height = scroller.clientHeight;
|
|
274
|
+
if (height === 0)
|
|
275
|
+
return false;
|
|
276
|
+
let landed = Math.round(scroller.scrollTop / height);
|
|
277
|
+
let step = Math.sign(landed - touchedAt);
|
|
278
|
+
let last = clone === null ? reels.length - 1 : reels.length;
|
|
279
|
+
let allowed = Math.min(last, Math.max(0, touchedAt + step));
|
|
280
|
+
touchedAt = null;
|
|
281
|
+
let reel = allowed === reels.length ? clone : reels[allowed];
|
|
282
|
+
if (Math.abs(landed - allowed) === 0 || reel === undefined || reel === null)
|
|
283
|
+
return false;
|
|
284
|
+
scroller.scrollTo({ top: reel.offsetTop, behavior: scrollBehavior() });
|
|
285
|
+
if (reel !== clone)
|
|
286
|
+
activate(allowed, true);
|
|
287
|
+
return true;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Moves the feed from the loop clone to the real first reel it copies, in place. Hiding the
|
|
291
|
+
* overflow for a frame ends any fling still running, which would carry on from the top.
|
|
292
|
+
*/
|
|
293
|
+
function wrapToFirst() {
|
|
294
|
+
touchedAt = null;
|
|
295
|
+
scroller.style.overflowY = "hidden";
|
|
296
|
+
scroller.scrollTo({ top: 0, behavior: "instant" });
|
|
297
|
+
requestAnimationFrame(() => scroller.style.removeProperty("overflow-y"));
|
|
298
|
+
if (current !== 0)
|
|
299
|
+
activate(0, true);
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Wraps from the loop clone once the feed rests on it with no finger down, so no momentum
|
|
303
|
+
* left from a fling carries on past the first reel after the jump.
|
|
304
|
+
*/
|
|
305
|
+
function wrapAtRest() {
|
|
306
|
+
if (fingerDown || clone === null)
|
|
307
|
+
return;
|
|
308
|
+
if (Math.abs(scroller.scrollTop - clone.offsetTop) <= 1)
|
|
309
|
+
wrapToFirst();
|
|
310
|
+
}
|
|
311
|
+
/** Settles the gesture, then wraps if it came to rest on the loop clone. */
|
|
312
|
+
function onScrollEnd() {
|
|
313
|
+
clearTimeout(restTimer);
|
|
314
|
+
if (settleTouch())
|
|
315
|
+
return;
|
|
316
|
+
wrapAtRest();
|
|
317
|
+
}
|
|
318
|
+
/** Restarts the rest timer, which stands in for a `scrollend` the browser never sends. */
|
|
319
|
+
function onScroll() {
|
|
320
|
+
if (clone === null)
|
|
321
|
+
return;
|
|
322
|
+
clearTimeout(restTimer);
|
|
323
|
+
restTimer = setTimeout(wrapAtRest, SCROLL_REST_MS);
|
|
324
|
+
}
|
|
325
|
+
let observer = new IntersectionObserver((entries) => {
|
|
326
|
+
for (let entry of entries) {
|
|
327
|
+
if (!entry.isIntersecting || entry.intersectionRatio < 0.6)
|
|
328
|
+
continue;
|
|
329
|
+
if (entry.target === clone) {
|
|
330
|
+
activate(0, true);
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
let index = reels.indexOf(entry.target);
|
|
334
|
+
if (index !== -1 && index !== current)
|
|
335
|
+
activate(index, true);
|
|
336
|
+
}
|
|
337
|
+
}, { root: scroller, threshold: [0.6] });
|
|
338
|
+
let start = reels[current];
|
|
339
|
+
if (start !== undefined && Math.abs(scroller.scrollTop - start.offsetTop) > 1) {
|
|
340
|
+
scroller.scrollTo({ top: start.offsetTop, behavior: "instant" });
|
|
341
|
+
}
|
|
342
|
+
activate(current, false);
|
|
343
|
+
for (let reel of reels)
|
|
344
|
+
observer.observe(reel);
|
|
345
|
+
if (clone !== null)
|
|
346
|
+
observer.observe(clone);
|
|
347
|
+
feed.setAttribute(REEL_FEED_READY, "");
|
|
348
|
+
document.addEventListener("keydown", onKey, { signal });
|
|
349
|
+
scroller.addEventListener("wheel", onWheel, { passive: false, signal });
|
|
350
|
+
scroller.addEventListener("click", onToggle, { signal });
|
|
351
|
+
feed.addEventListener("click", onStep, { signal });
|
|
352
|
+
scroller.addEventListener("touchstart", () => {
|
|
353
|
+
touchedAt = current;
|
|
354
|
+
fingerDown = true;
|
|
355
|
+
}, { passive: true, signal });
|
|
356
|
+
scroller.addEventListener("touchend", () => (fingerDown = false), { passive: true, signal });
|
|
357
|
+
scroller.addEventListener("touchcancel", () => (fingerDown = false), { passive: true, signal });
|
|
358
|
+
scroller.addEventListener("scroll", onScroll, { passive: true, signal });
|
|
359
|
+
scroller.addEventListener("scrollend", onScrollEnd, { signal });
|
|
360
|
+
scroller.addEventListener("timeupdate", onTime, { capture: true, signal });
|
|
361
|
+
document.addEventListener("visibilitychange", () => {
|
|
362
|
+
let video = videoOf(reels[current]);
|
|
363
|
+
if (video === null)
|
|
364
|
+
return;
|
|
365
|
+
if (document.hidden)
|
|
366
|
+
video.pause();
|
|
367
|
+
else if (!reels[current]?.hasAttribute(REEL_PAUSED))
|
|
368
|
+
play(video);
|
|
369
|
+
}, { signal });
|
|
370
|
+
signal.addEventListener("abort", () => {
|
|
371
|
+
observer.disconnect();
|
|
372
|
+
clearTimeout(wheelTimer);
|
|
373
|
+
clearTimeout(restTimer);
|
|
374
|
+
hint?.remove();
|
|
375
|
+
clone?.remove();
|
|
376
|
+
feed.removeAttribute(REEL_FEED_READY);
|
|
377
|
+
for (let reel of reels)
|
|
378
|
+
videoOf(reel)?.pause();
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Appends an inert copy of the first reel after the last, so a swipe, key or flick past the end
|
|
383
|
+
* lands on something that looks like the first reel, from which the feed jumps to the real one.
|
|
384
|
+
* The copy carries no ids, loads no video and is hidden from assistive tech.
|
|
385
|
+
*
|
|
386
|
+
* @returns The copy, or `null` for a feed with no reels.
|
|
387
|
+
*/
|
|
388
|
+
function appendLoopClone(scroller, first) {
|
|
389
|
+
if (first === undefined)
|
|
390
|
+
return null;
|
|
391
|
+
let clone = first.cloneNode(true);
|
|
392
|
+
clone.removeAttribute(REEL_ACTIVE);
|
|
393
|
+
clone.removeAttribute("data-href");
|
|
394
|
+
clone.setAttribute("data-reel-clone", "");
|
|
395
|
+
clone.setAttribute("aria-hidden", "true");
|
|
396
|
+
clone.inert = true;
|
|
397
|
+
for (let element of [clone, ...clone.querySelectorAll("[id]")])
|
|
398
|
+
element.removeAttribute("id");
|
|
399
|
+
for (let video of clone.querySelectorAll("video")) {
|
|
400
|
+
video.removeAttribute("autoplay");
|
|
401
|
+
video.preload = "none";
|
|
402
|
+
video.removeAttribute("data-reel-video");
|
|
403
|
+
}
|
|
404
|
+
for (let hint of clone.querySelectorAll("link"))
|
|
405
|
+
hint.remove();
|
|
406
|
+
scroller.appendChild(clone);
|
|
407
|
+
return clone;
|
|
408
|
+
}
|
|
409
|
+
/** Smooth steps, or instant ones for a visitor who asked for reduced motion. */
|
|
410
|
+
function scrollBehavior() {
|
|
411
|
+
return matchMedia("(prefers-reduced-motion: reduce)").matches ? "instant" : "smooth";
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Points the feed's document prefetch hint at the next reel's page, so following a shared link
|
|
415
|
+
* to it, or a reload there, finds the page already in the HTTP cache.
|
|
416
|
+
*
|
|
417
|
+
* @returns The hint, created on first use inside the feed.
|
|
418
|
+
*/
|
|
419
|
+
function movePrefetchHint(feed, hint, href) {
|
|
420
|
+
let link = hint ?? feed.querySelector("link[data-reel-next]");
|
|
421
|
+
if (link === null) {
|
|
422
|
+
link = document.createElement("link");
|
|
423
|
+
link.rel = "prefetch";
|
|
424
|
+
link.setAttribute("data-reel-next", "");
|
|
425
|
+
feed.appendChild(link);
|
|
426
|
+
}
|
|
427
|
+
if (link.getAttribute("href") !== href)
|
|
428
|
+
link.setAttribute("href", href);
|
|
429
|
+
return link;
|
|
430
|
+
}
|