@plusguard/reels 1.3.1 → 1.5.0

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 CHANGED
@@ -23,9 +23,9 @@ From npm, through jsDelivr — the npm package is the code alone (about
23
23
  jsDelivr's GitHub route.
24
24
 
25
25
  ```html
26
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@plusguard/reels@1.3.1/index.css" />
26
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@plusguard/reels@1.5.0/index.css" />
27
27
  <script type="module">
28
- import { initReels } from "https://cdn.jsdelivr.net/npm/@plusguard/reels@1.3.1/dist/index.js";
28
+ import { initReels } from "https://cdn.jsdelivr.net/npm/@plusguard/reels@1.5.0/dist/index.js";
29
29
  initReels();
30
30
  </script>
31
31
  ```
@@ -107,11 +107,16 @@ below).
107
107
  event `pg-reels:open` on the section, with `detail.group` and
108
108
  `detail.clip`; `preventDefault()` on it and the site opens its own.
109
109
  - **Reduced motion:** nothing plays by itself; a bar pressed shows its clip.
110
- - **Seen.** A group shown in the full view is seen:
111
- `data-pg-reels-seen="true"` on it (`"false"` until then), for the ring
112
- round its avatar to go pale — stories' rings, in the site's CSS. With a
113
- `data-pg-reels-id` it is remembered in this browser (`localStorage`,
114
- `pg-reels-seen`); without, for this visit. In the full view the head's
110
+ - **Seen.** A group is seen once **every one of its clips** has been shown
111
+ in the full view — one clip watched, and it is not; the rest watched
112
+ later in the same visit, and it is: `data-pg-reels-seen="true"` on it
113
+ (`"false"` until then), for the ring round its avatar to go pale —
114
+ stories' rings, in the site's CSS. It stays seen for
115
+ `--pg-reels-seen-for` (`24h` by default; `1h`, `30m`, `7d`, or `forever`)
116
+ and is bright again after; `--pg-reels-seen: off` and the rings never go
117
+ pale. With a `data-pg-reels-id` it is remembered in this browser
118
+ (`localStorage`, `pg-reels-seen`, with the time); without, for this
119
+ visit. In the full view the head's
115
120
  copy keeps the ring as it was when the group came in, through all its
116
121
  clips. `forgetReelsSeen()` forgets it all — every ring bright again, at
117
122
  once, no reload.
@@ -134,14 +139,16 @@ in it, copies of the site's markup: the clip, the group's
134
139
  `data-pg-reels-head`, the clip's product and its details.
135
140
 
136
141
  - **Within a group:** the bars on top (none for a single clip), a press on
137
- the video's right or left third, → and ←. Its clips play on, a video to
142
+ the video's right or left third, the ‹ › on either side of the video on
143
+ a computer — none before the very first clip, none after the very last
144
+ — → and ←. Its clips play on, a video to
138
145
  its end, a picture for its time; after the group's last clip, the next
139
146
  group — after the last group, the first.
140
147
  - **Between groups:** a finger — or the mouse — dragging the video up or
141
148
  down, at once or after holding it: it follows, the next group coming in under it (the one before
142
149
  above it), and let go it goes on if pulled a fifth of its height or
143
150
  flicked, or back if not; past the first or the last it gives a little
144
- and comes back. The ↑ ↓ buttons beside the video (round, their name
151
+ and comes back. The ↑ ↓ buttons left of the video (round, their name
145
152
  beside them under the mouse), a wheel or a trackpad, ↑ and ↓ slide it
146
153
  the same way. On a computer the next group's top shows under the video
147
154
  (`--pg-reels-peek`); pressed, it is the next. The product panel changes
@@ -205,6 +212,8 @@ in it, copies of the site's markup: the clip, the group's
205
212
  | `--pg-reels-ratio` | `9 / 16` | The card's shape |
206
213
  | `--pg-reels-duration` | `5s` | A picture's time |
207
214
  | `--pg-reels-panel` | 9:16 of its height, 20–30rem | The full view: the product panel's width on a computer |
215
+ | `--pg-reels-seen` | `all` | `all`: a group is seen once all its clips have been shown in the full view · `off`: never |
216
+ | `--pg-reels-seen-for` | `24h` | How long a group stays seen — `1h`, `30m`, `7d`, `forever` — then its ring is bright again |
208
217
  | `--pg-reels-peek` | `2rem` | The full view: how much of the next group shows under the video on a computer |
209
218
  | `--pg-reels-band` | — | Written by the script on the view: the band's height at a phone's foot (0 on a computer) — to keep a caption above it |
210
219
 
@@ -286,8 +295,8 @@ shows them. See [`examples/react/Example.tsx`](examples/react/Example.tsx)
286
295
  ## Example
287
296
 
288
297
  [`examples/static/index.html`](examples/static/index.html) — six groups of
289
- one to five clips, videos and pictures (two with their own time — the
290
- jellyfish 8s, the mist 3s; `?duration=2s` for the rest), each clip with a product and its
298
+ three to five clips, videos (`?duration=2s` sets a picture's time; `?seen=off`,
299
+ `?seen-for=1h` for the rings), each clip with a product and its
291
300
  details: its category (a link), its name in the panel's head, a big
292
301
  picture at 4:3, small ones — nine for the first product, a row that
293
302
  scrolls — the price, a line about it, a link. Under the card the product
@@ -299,6 +308,7 @@ three clips from a food shop, the second five from a scarf shop, the
299
308
  third four from a pasta and mantı maker, the fourth four from a
300
309
  jeweller, the fifth four from a nut and sweet shop — a coffee with four
301
310
  weights (variants, each its pictures and price) and three categories,
302
- each listing a few of its products — each with its product; the food products' further photos are from
303
- [Unsplash](https://unsplash.com/license) (free to use). The other pictures
304
- and the bubbles video are from stories' example.
311
+ each listing a few of its products — the sixth three from a pastırma and
312
+ sucuk shop — each with its product. The food products'
313
+ further photos are from [Unsplash](https://unsplash.com/license) (free to
314
+ use).
package/dist/index.js CHANGED
@@ -48,7 +48,8 @@
48
48
  * The full view — a <dialog>, built here on <body>, its contents copies:
49
49
  * the clip playing, the group's data-pg-reels-head, the clip's product.
50
50
  * Within a group: the bars on top, a press on the video's right or left,
51
- * → and ←. Between groups: a finger (or the mouse) dragging the video up
51
+ * the ‹ › beside it (none before the first clip or after the last), → and
52
+ * ←. Between groups: a finger (or the mouse) dragging the video up
52
53
  * or down — it follows, the next group coming in under it, and let go
53
54
  * goes on or back — its ↑ and ↓ buttons, a wheel, ↑ and ↓; after a
54
55
  * group's last clip, the next group. The product changes once it has
@@ -72,10 +73,12 @@
72
73
  * on the group that plays; aria-current on its bar, data-pg-reels-done on
73
74
  * the bars before it, and --pg-reels-progress (0 to 1) on the bar playing.
74
75
  *
75
- * Seen: a group shown in the full view is seen — data-pg-reels-seen="true"
76
- * on it ("false" before), for its ring to go grey as stories' do. With a
77
- * data-pg-reels-id on the group it is remembered in this browser;
78
- * forgetReelsSeen() forgets it all — every ring bright again, at once.
76
+ * Seen: a group whose every clip has been shown in the full view is seen —
77
+ * data-pg-reels-seen="true" on it ("false" before), for its ring to go
78
+ * pale as stories' do; for --pg-reels-seen-for (24h by default), then
79
+ * bright again; --pg-reels-seen: off and never. With a data-pg-reels-id on
80
+ * the group it is remembered in this browser; forgetReelsSeen() forgets it
81
+ * all — every ring bright again, at once.
79
82
  *
80
83
  * CSS, per screen size: --pg-reels-show (cards across: 4; 1.5 on a phone
81
84
  * shows one and half the next), --pg-reels-gap, --pg-reels-duration (a
@@ -186,6 +189,8 @@ const ICONS = {
186
189
  up: '<path d="M12 19V5M5 12l7-7 7 7"/>',
187
190
  down: '<path d="M12 5v14M19 12l-7 7-7-7"/>',
188
191
  chevron: '<path d="m6 15 6-6 6 6"/>',
192
+ prev: '<path d="m15 6-6 6 6 6"/>',
193
+ next: '<path d="m9 6 6 6-6 6"/>',
189
194
  };
190
195
  /* ---------- One group ---------- */
191
196
  function partsOf(el, root) {
@@ -238,44 +243,80 @@ function show(g, i) {
238
243
  b.style.setProperty(PROGRESS, k < g.at ? "1" : "0");
239
244
  });
240
245
  }
241
- /* ---------- Seen, kept in this browser ---------- */
242
- // Groups with a data-pg-reels-id: in localStorage. Without: this visit.
243
- const seenHere = new WeakSet();
244
- function seenSet() {
246
+ /* ---------- Seen, kept in this browser ----------
247
+ A group is seen once every one of its clips has been shown in the full
248
+ view. Set on the section or above:
249
+ --pg-reels-seen all (default) · off: the rings never go pale
250
+ --pg-reels-seen-for how long it stays seen: 24h (default), 1h, 30m,
251
+ 7d — then bright again; forever: until forgotten */
252
+ // Groups with a data-pg-reels-id: in localStorage, as { id: when }.
253
+ // Without: this visit only.
254
+ const seenHere = new WeakMap();
255
+ const SEEN_FOR_DEFAULT = 24 * 60 * 60 * 1000;
256
+ function seenMap() {
245
257
  try {
246
- return new Set(JSON.parse(localStorage.getItem(SEEN_KEY) || "[]"));
258
+ const kept = JSON.parse(localStorage.getItem(SEEN_KEY) || "{}");
259
+ // An older list of ids: seen now.
260
+ if (Array.isArray(kept))
261
+ return Object.fromEntries(kept.map((id) => [String(id), Date.now()]));
262
+ return kept && typeof kept === "object" ? kept : {};
247
263
  }
248
264
  catch {
249
- return new Set();
265
+ return {};
250
266
  }
251
267
  }
252
- function isSeen(el, set = seenSet()) {
268
+ /* "24h", "30m", "90s", "500ms", "7d" → ms; "forever" → Infinity. */
269
+ function spanOf(raw) {
270
+ const text = raw.trim().toLowerCase();
271
+ if (!text)
272
+ return SEEN_FOR_DEFAULT;
273
+ if (text === "forever" || text === "none")
274
+ return Infinity;
275
+ const m = /^(-?[\d.]+)\s*(ms|s|m|h|d)?$/.exec(text);
276
+ if (!m)
277
+ return SEEN_FOR_DEFAULT;
278
+ const n = Number(m[1]);
279
+ const unit = { ms: 1, s: 1000, m: 60000, h: 3600000, d: 86400000 }[(m[2] ?? "ms")];
280
+ return Number.isFinite(n) ? Math.max(0, n * unit) : SEEN_FOR_DEFAULT;
281
+ }
282
+ function seenRules(el) {
283
+ const cs = getComputedStyle(el);
284
+ return {
285
+ off: cs.getPropertyValue("--pg-reels-seen").trim() === "off",
286
+ span: spanOf(cs.getPropertyValue("--pg-reels-seen-for")),
287
+ };
288
+ }
289
+ function isSeen(el, map = seenMap()) {
290
+ const { off, span } = seenRules(el);
291
+ if (off)
292
+ return false;
253
293
  const id = el.getAttribute(ID);
254
- return id ? set.has(id) : seenHere.has(el);
294
+ const when = id ? map[id] : seenHere.get(el);
295
+ return typeof when === "number" && Date.now() - when < span;
255
296
  }
256
297
  function markSeen(el) {
298
+ if (seenRules(el).off)
299
+ return;
257
300
  const id = el.getAttribute(ID);
258
301
  if (!id)
259
- seenHere.add(el);
302
+ seenHere.set(el, Date.now());
260
303
  else {
261
- const set = seenSet();
262
- if (!set.has(id)) {
263
- set.add(id);
264
- try {
265
- localStorage.setItem(SEEN_KEY, JSON.stringify([...set]));
266
- }
267
- catch {
268
- // Private mode, or storage full: seen for this visit only.
269
- seenHere.add(el);
270
- }
304
+ const map = seenMap();
305
+ map[id] = Date.now();
306
+ try {
307
+ localStorage.setItem(SEEN_KEY, JSON.stringify(map));
308
+ }
309
+ catch {
310
+ // Private mode, or storage full: seen for this visit only.
311
+ seenHere.set(el, Date.now());
271
312
  }
272
313
  }
273
314
  paintSeen();
274
315
  }
275
316
  function paintSeen() {
276
- const set = seenSet();
317
+ const map = seenMap();
277
318
  document.querySelectorAll(`[${GROUP}]`).forEach((el) => {
278
- const value = String(isSeen(el, set));
319
+ const value = String(isSeen(el, map));
279
320
  if (el.getAttribute(SEEN) !== value)
280
321
  el.setAttribute(SEEN, value);
281
322
  });
@@ -290,8 +331,11 @@ export function forgetReelsSeen() {
290
331
  // Storage blocked: nothing kept, nothing to forget.
291
332
  }
292
333
  document.querySelectorAll(`[${GROUP}]`).forEach((el) => seenHere.delete(el));
334
+ watched = new WeakMap();
293
335
  paintSeen();
294
336
  }
337
+ // The clips of each group shown in the full view so far, this visit.
338
+ let watched = new WeakMap();
295
339
  /* ---------- Everything on the page ---------- */
296
340
  let live = null;
297
341
  /* Start watching every reels section on the page, those rendered later
@@ -553,6 +597,12 @@ export function initReels() {
553
597
  dialog.tabIndex = -1;
554
598
  dialog.innerHTML = `
555
599
  <div data-pg-reels-stage>
600
+ <div data-pg-reels-view-steps>
601
+ <button type="button" data-pg-reels-view-up aria-label="Previous reel">${svg(ICONS.up)}</button>
602
+ <button type="button" data-pg-reels-view-down aria-label="Next reel">${svg(ICONS.down)}</button>
603
+ </div>
604
+ <div data-pg-reels-column>
605
+ <button type="button" data-pg-reels-view-side="prev" aria-label="Previous">${svg(ICONS.prev)}</button>
556
606
  <div data-pg-reels-feed>
557
607
  <div data-pg-reels-view-peek="before" aria-hidden="true" hidden></div>
558
608
  <div data-pg-reels-screen>
@@ -574,9 +624,7 @@ export function initReels() {
574
624
  </div>
575
625
  <button type="button" data-pg-reels-view-peek="after" tabindex="-1" aria-label="Next reel" hidden></button>
576
626
  </div>
577
- <div data-pg-reels-view-steps>
578
- <button type="button" data-pg-reels-view-up aria-label="Previous reel">${svg(ICONS.up)}</button>
579
- <button type="button" data-pg-reels-view-down aria-label="Next reel">${svg(ICONS.down)}</button>
627
+ <button type="button" data-pg-reels-view-side="next" aria-label="Next">${svg(ICONS.next)}</button>
580
628
  </div>
581
629
  <aside data-pg-reels-panel>
582
630
  <div data-pg-reels-panel-head>
@@ -600,6 +648,8 @@ export function initReels() {
600
648
  const viewBars = part("view-bars");
601
649
  const playButton = part("view-play");
602
650
  const upButton = part("view-up");
651
+ const sidePrev = dialog.querySelector('[data-pg-reels-view-side="prev"]');
652
+ const sideNext = dialog.querySelector('[data-pg-reels-view-side="next"]');
603
653
  const downButton = part("view-down");
604
654
  const toggle = part("panel-toggle");
605
655
  document.body.append(dialog);
@@ -660,7 +710,12 @@ export function initReels() {
660
710
  headOf = g;
661
711
  part("view-head").setAttribute(SEEN, String(isSeen(g.el)));
662
712
  }
663
- markSeen(g.el);
713
+ // This clip shown; every clip of the group shown: seen.
714
+ const shown = watched.get(g.el) ?? new Set();
715
+ shown.add(view.ci);
716
+ watched.set(g.el, shown);
717
+ if (shown.size >= n)
718
+ markSeen(g.el);
664
719
  viewBars.replaceChildren(...g.clips.map((_, i) => {
665
720
  const bar = document.createElement("button");
666
721
  bar.type = "button";
@@ -711,6 +766,10 @@ export function initReels() {
711
766
  dialog.toggleAttribute(NO_PRODUCT, !product);
712
767
  upButton.disabled = view.gi === 0;
713
768
  downButton.disabled = view.gi === view.list.length - 1;
769
+ // The arrows beside the video: none before the very first clip,
770
+ // none after the very last.
771
+ sidePrev.hidden = view.gi === 0 && view.ci === 0;
772
+ sideNext.hidden = view.gi === view.list.length - 1 && view.ci === n - 1;
714
773
  view.shown = 0;
715
774
  view.since = performance.now();
716
775
  playView();
@@ -905,6 +964,7 @@ export function initReels() {
905
964
  view.pushed = false;
906
965
  choose();
907
966
  applySound();
967
+ paintSeen();
908
968
  };
909
969
  const onViewClick = (event) => {
910
970
  if (!(event.target instanceof Element))
@@ -922,9 +982,9 @@ export function initReels() {
922
982
  return closeView();
923
983
  if (sliding)
924
984
  return;
925
- if (t.closest("[data-pg-reels-view-on]"))
985
+ if (t.closest('[data-pg-reels-view-on], [data-pg-reels-view-side="next"]'))
926
986
  return nextClip();
927
- if (t.closest("[data-pg-reels-view-back]"))
987
+ if (t.closest('[data-pg-reels-view-back], [data-pg-reels-view-side="prev"]'))
928
988
  return prevClip();
929
989
  if (t.closest("[data-pg-reels-view-up]"))
930
990
  return prevGroup();
@@ -1137,6 +1197,9 @@ export function initReels() {
1137
1197
  return;
1138
1198
  }
1139
1199
  playing.forEach(document.hidden ? stop : start);
1200
+ // Back after a while: what was seen may have run out.
1201
+ if (!document.hidden)
1202
+ paintSeen();
1140
1203
  };
1141
1204
  document.addEventListener("visibilitychange", onHidden);
1142
1205
  look();
package/index.css CHANGED
@@ -10,6 +10,10 @@
10
10
  * --pg-reels-gap between the cards (default 1rem)
11
11
  * --pg-reels-ratio the card's shape (default 9 / 16)
12
12
  * --pg-reels-duration a picture's time (default 5s)
13
+ * --pg-reels-seen all (default): a group is seen once all its
14
+ * clips have been shown in the full view · off
15
+ * --pg-reels-seen-for how long it stays seen (default 24h; 1h, 30m,
16
+ * 7d, forever)
13
17
  * --pg-reels-panel the full view: the product panel's width on a
14
18
  * computer (default: 9:16 of its height, as the
15
19
  * video; 20rem to 30rem)
@@ -122,7 +126,7 @@ html:has(dialog[data-pg-reels-viewer][open]) {
122
126
  dialog[data-pg-reels-viewer] {
123
127
  /* The panel 9:16, as the video: its width from its height — but at
124
128
  most half the room beside the arrows, and 20rem to 30rem. */
125
- --pg-reels-panel: clamp(20rem, min(calc((100dvh - 2rem) * 9 / 16), calc((100vw - 11rem) / 2)), 30rem);
129
+ --pg-reels-panel: clamp(20rem, min(calc((100dvh - 2rem) * 9 / 16), calc((100vw - 17rem) / 2)), 30rem);
126
130
  --pg-reels-peek: 2rem;
127
131
  position: fixed;
128
132
  inset: 0;
@@ -156,7 +160,7 @@ dialog[data-pg-reels-viewer] svg {
156
160
  it (--pg-reels-peek) — and low enough to leave room for the panel. */
157
161
  [data-pg-reels-stage] {
158
162
  --gap: 1rem;
159
- --h: min(calc(100dvh - 2 * var(--gap) - var(--pg-reels-peek)), 56rem, calc((100vw - var(--pg-reels-panel) - 11rem) * 16 / 9));
163
+ --h: min(calc(100dvh - 2 * var(--gap) - var(--pg-reels-peek)), 56rem, calc((100vw - var(--pg-reels-panel) - 17rem) * 16 / 9));
160
164
  display: flex;
161
165
  align-items: flex-start;
162
166
  justify-content: center;
@@ -166,6 +170,49 @@ dialog[data-pg-reels-viewer] svg {
166
170
  box-sizing: border-box;
167
171
  }
168
172
 
173
+ /* The video's column: the feed, and the ‹ › on either side of the
174
+ video's middle — the clip before, the next; none before the very first
175
+ or after the very last. */
176
+ [data-pg-reels-column] {
177
+ position: relative;
178
+ flex: none;
179
+ margin-inline: 3.25rem;
180
+ }
181
+ dialog[data-pg-reels-viewer] [data-pg-reels-view-side] {
182
+ position: absolute;
183
+ z-index: 2;
184
+ top: calc(var(--h) / 2);
185
+ translate: 0 -50%;
186
+ display: grid;
187
+ place-items: center;
188
+ width: 2.25rem;
189
+ height: 2.25rem;
190
+ padding: 0;
191
+ border: 0;
192
+ border-radius: 50%;
193
+ background: rgb(255 255 255 / 0.92);
194
+ color: #111;
195
+ box-shadow: 0 0.125rem 0.5rem rgb(0 0 0 / 0.3);
196
+ cursor: pointer;
197
+ transition: background-color 0.15s;
198
+ }
199
+ dialog[data-pg-reels-viewer] [data-pg-reels-view-side]:hover {
200
+ background: #fff;
201
+ }
202
+ [data-pg-reels-view-side="prev"] {
203
+ right: calc(100% + 0.75rem);
204
+ }
205
+ [data-pg-reels-view-side="next"] {
206
+ left: calc(100% + 0.75rem);
207
+ }
208
+ dialog[data-pg-reels-viewer] [data-pg-reels-view-side][hidden] {
209
+ display: none;
210
+ }
211
+ dialog[data-pg-reels-viewer] [data-pg-reels-view-side] svg {
212
+ width: 1.125rem;
213
+ height: 1.125rem;
214
+ }
215
+
169
216
  /* The video, the group before it out of sight above, the next one's top
170
217
  under it: a feed that a finger drags, or the ↑ ↓ slide, by a video and
171
218
  its gap. */
@@ -353,8 +400,8 @@ dialog[data-pg-reels-viewer] svg {
353
400
  display: block;
354
401
  }
355
402
 
356
- /* ↑ ↓: the group before, the next — beside the video's middle, round
357
- and grey, their name beside them under the mouse. */
403
+ /* ↑ ↓: the group before, the next — left of the video, at its middle,
404
+ round and grey, their name beside them under the mouse. */
358
405
  [data-pg-reels-view-steps] {
359
406
  display: flex;
360
407
  flex-direction: column;
@@ -480,6 +527,13 @@ dialog[data-pg-reels-viewer] [data-pg-reels-panel-close] svg {
480
527
  display: block;
481
528
  padding: 0;
482
529
  }
530
+ [data-pg-reels-column] {
531
+ position: static;
532
+ margin: 0;
533
+ }
534
+ dialog[data-pg-reels-viewer] [data-pg-reels-view-side] {
535
+ display: none;
536
+ }
483
537
  [data-pg-reels-feed] {
484
538
  position: absolute;
485
539
  inset: 0;
package/index.ts CHANGED
@@ -48,7 +48,8 @@
48
48
  * The full view — a <dialog>, built here on <body>, its contents copies:
49
49
  * the clip playing, the group's data-pg-reels-head, the clip's product.
50
50
  * Within a group: the bars on top, a press on the video's right or left,
51
- * → and ←. Between groups: a finger (or the mouse) dragging the video up
51
+ * the ‹ › beside it (none before the first clip or after the last), → and
52
+ * ←. Between groups: a finger (or the mouse) dragging the video up
52
53
  * or down — it follows, the next group coming in under it, and let go
53
54
  * goes on or back — its ↑ and ↓ buttons, a wheel, ↑ and ↓; after a
54
55
  * group's last clip, the next group. The product changes once it has
@@ -72,10 +73,12 @@
72
73
  * on the group that plays; aria-current on its bar, data-pg-reels-done on
73
74
  * the bars before it, and --pg-reels-progress (0 to 1) on the bar playing.
74
75
  *
75
- * Seen: a group shown in the full view is seen — data-pg-reels-seen="true"
76
- * on it ("false" before), for its ring to go grey as stories' do. With a
77
- * data-pg-reels-id on the group it is remembered in this browser;
78
- * forgetReelsSeen() forgets it all — every ring bright again, at once.
76
+ * Seen: a group whose every clip has been shown in the full view is seen —
77
+ * data-pg-reels-seen="true" on it ("false" before), for its ring to go
78
+ * pale as stories' do; for --pg-reels-seen-for (24h by default), then
79
+ * bright again; --pg-reels-seen: off and never. With a data-pg-reels-id on
80
+ * the group it is remembered in this browser; forgetReelsSeen() forgets it
81
+ * all — every ring bright again, at once.
79
82
  *
80
83
  * CSS, per screen size: --pg-reels-show (cards across: 4; 1.5 on a phone
81
84
  * shows one and half the next), --pg-reels-gap, --pg-reels-duration (a
@@ -210,6 +213,8 @@ const ICONS = {
210
213
  up: '<path d="M12 19V5M5 12l7-7 7 7"/>',
211
214
  down: '<path d="M12 5v14M19 12l-7 7-7-7"/>',
212
215
  chevron: '<path d="m6 15 6-6 6 6"/>',
216
+ prev: '<path d="m15 6-6 6 6 6"/>',
217
+ next: '<path d="m9 6 6 6-6 6"/>',
213
218
  };
214
219
 
215
220
  /* ---------- One group ---------- */
@@ -265,46 +270,78 @@ function show(g: Group, i: number): void {
265
270
  });
266
271
  }
267
272
 
268
- /* ---------- Seen, kept in this browser ---------- */
273
+ /* ---------- Seen, kept in this browser ----------
274
+ A group is seen once every one of its clips has been shown in the full
275
+ view. Set on the section or above:
276
+ --pg-reels-seen all (default) · off: the rings never go pale
277
+ --pg-reels-seen-for how long it stays seen: 24h (default), 1h, 30m,
278
+ 7d — then bright again; forever: until forgotten */
269
279
 
270
- // Groups with a data-pg-reels-id: in localStorage. Without: this visit.
271
- const seenHere = new WeakSet<Element>();
280
+ // Groups with a data-pg-reels-id: in localStorage, as { id: when }.
281
+ // Without: this visit only.
282
+ const seenHere = new WeakMap<Element, number>();
283
+ const SEEN_FOR_DEFAULT = 24 * 60 * 60 * 1000;
272
284
 
273
- function seenSet(): Set<string> {
285
+ function seenMap(): Record<string, number> {
274
286
  try {
275
- return new Set(JSON.parse(localStorage.getItem(SEEN_KEY) || "[]") as string[]);
287
+ const kept = JSON.parse(localStorage.getItem(SEEN_KEY) || "{}") as unknown;
288
+ // An older list of ids: seen now.
289
+ if (Array.isArray(kept)) return Object.fromEntries(kept.map((id) => [String(id), Date.now()]));
290
+ return kept && typeof kept === "object" ? (kept as Record<string, number>) : {};
276
291
  } catch {
277
- return new Set();
292
+ return {};
278
293
  }
279
294
  }
280
295
 
281
- function isSeen(el: Element, set = seenSet()): boolean {
296
+ /* "24h", "30m", "90s", "500ms", "7d" → ms; "forever" → Infinity. */
297
+ function spanOf(raw: string): number {
298
+ const text = raw.trim().toLowerCase();
299
+ if (!text) return SEEN_FOR_DEFAULT;
300
+ if (text === "forever" || text === "none") return Infinity;
301
+ const m = /^(-?[\d.]+)\s*(ms|s|m|h|d)?$/.exec(text);
302
+ if (!m) return SEEN_FOR_DEFAULT;
303
+ const n = Number(m[1]);
304
+ const unit = { ms: 1, s: 1000, m: 60000, h: 3600000, d: 86400000 }[(m[2] ?? "ms") as "ms" | "s" | "m" | "h" | "d"];
305
+ return Number.isFinite(n) ? Math.max(0, n * unit) : SEEN_FOR_DEFAULT;
306
+ }
307
+
308
+ function seenRules(el: Element): { off: boolean; span: number } {
309
+ const cs = getComputedStyle(el);
310
+ return {
311
+ off: cs.getPropertyValue("--pg-reels-seen").trim() === "off",
312
+ span: spanOf(cs.getPropertyValue("--pg-reels-seen-for")),
313
+ };
314
+ }
315
+
316
+ function isSeen(el: Element, map = seenMap()): boolean {
317
+ const { off, span } = seenRules(el);
318
+ if (off) return false;
282
319
  const id = el.getAttribute(ID);
283
- return id ? set.has(id) : seenHere.has(el);
320
+ const when = id ? map[id] : seenHere.get(el);
321
+ return typeof when === "number" && Date.now() - when < span;
284
322
  }
285
323
 
286
324
  function markSeen(el: Element): void {
325
+ if (seenRules(el).off) return;
287
326
  const id = el.getAttribute(ID);
288
- if (!id) seenHere.add(el);
327
+ if (!id) seenHere.set(el, Date.now());
289
328
  else {
290
- const set = seenSet();
291
- if (!set.has(id)) {
292
- set.add(id);
293
- try {
294
- localStorage.setItem(SEEN_KEY, JSON.stringify([...set]));
295
- } catch {
296
- // Private mode, or storage full: seen for this visit only.
297
- seenHere.add(el);
298
- }
329
+ const map = seenMap();
330
+ map[id] = Date.now();
331
+ try {
332
+ localStorage.setItem(SEEN_KEY, JSON.stringify(map));
333
+ } catch {
334
+ // Private mode, or storage full: seen for this visit only.
335
+ seenHere.set(el, Date.now());
299
336
  }
300
337
  }
301
338
  paintSeen();
302
339
  }
303
340
 
304
341
  function paintSeen(): void {
305
- const set = seenSet();
342
+ const map = seenMap();
306
343
  document.querySelectorAll<HTMLElement>(`[${GROUP}]`).forEach((el) => {
307
- const value = String(isSeen(el, set));
344
+ const value = String(isSeen(el, map));
308
345
  if (el.getAttribute(SEEN) !== value) el.setAttribute(SEEN, value);
309
346
  });
310
347
  }
@@ -318,9 +355,13 @@ export function forgetReelsSeen(): void {
318
355
  // Storage blocked: nothing kept, nothing to forget.
319
356
  }
320
357
  document.querySelectorAll(`[${GROUP}]`).forEach((el) => seenHere.delete(el));
358
+ watched = new WeakMap();
321
359
  paintSeen();
322
360
  }
323
361
 
362
+ // The clips of each group shown in the full view so far, this visit.
363
+ let watched = new WeakMap<Element, Set<number>>();
364
+
324
365
  /* ---------- Everything on the page ---------- */
325
366
 
326
367
  let live: object | null = null;
@@ -574,6 +615,12 @@ export function initReels(): () => void {
574
615
  dialog.tabIndex = -1;
575
616
  dialog.innerHTML = `
576
617
  <div data-pg-reels-stage>
618
+ <div data-pg-reels-view-steps>
619
+ <button type="button" data-pg-reels-view-up aria-label="Previous reel">${svg(ICONS.up)}</button>
620
+ <button type="button" data-pg-reels-view-down aria-label="Next reel">${svg(ICONS.down)}</button>
621
+ </div>
622
+ <div data-pg-reels-column>
623
+ <button type="button" data-pg-reels-view-side="prev" aria-label="Previous">${svg(ICONS.prev)}</button>
577
624
  <div data-pg-reels-feed>
578
625
  <div data-pg-reels-view-peek="before" aria-hidden="true" hidden></div>
579
626
  <div data-pg-reels-screen>
@@ -595,9 +642,7 @@ export function initReels(): () => void {
595
642
  </div>
596
643
  <button type="button" data-pg-reels-view-peek="after" tabindex="-1" aria-label="Next reel" hidden></button>
597
644
  </div>
598
- <div data-pg-reels-view-steps>
599
- <button type="button" data-pg-reels-view-up aria-label="Previous reel">${svg(ICONS.up)}</button>
600
- <button type="button" data-pg-reels-view-down aria-label="Next reel">${svg(ICONS.down)}</button>
645
+ <button type="button" data-pg-reels-view-side="next" aria-label="Next">${svg(ICONS.next)}</button>
601
646
  </div>
602
647
  <aside data-pg-reels-panel>
603
648
  <div data-pg-reels-panel-head>
@@ -621,6 +666,8 @@ export function initReels(): () => void {
621
666
  const viewBars = part("view-bars");
622
667
  const playButton = part("view-play");
623
668
  const upButton = part("view-up") as HTMLButtonElement;
669
+ const sidePrev = dialog.querySelector<HTMLElement>('[data-pg-reels-view-side="prev"]')!;
670
+ const sideNext = dialog.querySelector<HTMLElement>('[data-pg-reels-view-side="next"]')!;
624
671
  const downButton = part("view-down") as HTMLButtonElement;
625
672
  const toggle = part("panel-toggle");
626
673
  document.body.append(dialog);
@@ -686,7 +733,11 @@ export function initReels(): () => void {
686
733
  headOf = g;
687
734
  part("view-head").setAttribute(SEEN, String(isSeen(g.el)));
688
735
  }
689
- markSeen(g.el);
736
+ // This clip shown; every clip of the group shown: seen.
737
+ const shown = watched.get(g.el) ?? new Set<number>();
738
+ shown.add(view.ci);
739
+ watched.set(g.el, shown);
740
+ if (shown.size >= n) markSeen(g.el);
690
741
 
691
742
  viewBars.replaceChildren(
692
743
  ...g.clips.map((_, i) => {
@@ -741,6 +792,10 @@ export function initReels(): () => void {
741
792
 
742
793
  upButton.disabled = view.gi === 0;
743
794
  downButton.disabled = view.gi === view.list.length - 1;
795
+ // The arrows beside the video: none before the very first clip,
796
+ // none after the very last.
797
+ sidePrev.hidden = view.gi === 0 && view.ci === 0;
798
+ sideNext.hidden = view.gi === view.list.length - 1 && view.ci === n - 1;
744
799
  view.shown = 0;
745
800
  view.since = performance.now();
746
801
  playView();
@@ -919,6 +974,7 @@ export function initReels(): () => void {
919
974
  view.pushed = false;
920
975
  choose();
921
976
  applySound();
977
+ paintSeen();
922
978
  };
923
979
 
924
980
  const onViewClick = (event: MouseEvent): void => {
@@ -933,8 +989,8 @@ export function initReels(): () => void {
933
989
  if (t === dialog || t.hasAttribute("data-pg-reels-stage")) return closeView();
934
990
  if (t.closest("[data-pg-reels-view-close], [data-pg-reels-panel-close]")) return closeView();
935
991
  if (sliding) return;
936
- if (t.closest("[data-pg-reels-view-on]")) return nextClip();
937
- if (t.closest("[data-pg-reels-view-back]")) return prevClip();
992
+ if (t.closest('[data-pg-reels-view-on], [data-pg-reels-view-side="next"]')) return nextClip();
993
+ if (t.closest('[data-pg-reels-view-back], [data-pg-reels-view-side="prev"]')) return prevClip();
938
994
  if (t.closest("[data-pg-reels-view-up]")) return prevGroup();
939
995
  if (t.closest('[data-pg-reels-view-down], [data-pg-reels-view-peek="after"]')) return nextGroup();
940
996
  const bar = t.closest<HTMLElement>(`[${VIEW_BAR}]`);
@@ -1122,6 +1178,8 @@ export function initReels(): () => void {
1122
1178
  return;
1123
1179
  }
1124
1180
  playing.forEach(document.hidden ? stop : start);
1181
+ // Back after a while: what was seen may have run out.
1182
+ if (!document.hidden) paintSeen();
1125
1183
  };
1126
1184
  document.addEventListener("visibilitychange", onHidden);
1127
1185
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plusguard/reels",
3
- "version": "1.3.1",
3
+ "version": "1.5.0",
4
4
  "description": "Shoppable reels for a store: a strip of video cards — each a group of clips, each clip with its own product — that opens into a full view. Up and down between groups, left and right within one, the product beside the video on a computer and under it on a phone. No dependencies.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",