@plusguard/reels 1.3.1 → 1.4.1

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.4.1/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.4.1/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.
@@ -205,6 +210,8 @@ in it, copies of the site's markup: the clip, the group's
205
210
  | `--pg-reels-ratio` | `9 / 16` | The card's shape |
206
211
  | `--pg-reels-duration` | `5s` | A picture's time |
207
212
  | `--pg-reels-panel` | 9:16 of its height, 20–30rem | The full view: the product panel's width on a computer |
213
+ | `--pg-reels-seen` | `all` | `all`: a group is seen once all its clips have been shown in the full view · `off`: never |
214
+ | `--pg-reels-seen-for` | `24h` | How long a group stays seen — `1h`, `30m`, `7d`, `forever` — then its ring is bright again |
208
215
  | `--pg-reels-peek` | `2rem` | The full view: how much of the next group shows under the video on a computer |
209
216
  | `--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
217
 
@@ -286,8 +293,8 @@ shows them. See [`examples/react/Example.tsx`](examples/react/Example.tsx)
286
293
  ## Example
287
294
 
288
295
  [`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
296
+ one to five clips, videos and a picture (its own time, 4s; `?duration=2s` for
297
+ pictures without one; `?seen=off`, `?seen-for=1h` for the rings), each clip with a product and its
291
298
  details: its category (a link), its name in the panel's head, a big
292
299
  picture at 4:3, small ones — nine for the first product, a row that
293
300
  scrolls — the price, a line about it, a link. Under the card the product
@@ -299,6 +306,7 @@ three clips from a food shop, the second five from a scarf shop, the
299
306
  third four from a pasta and mantı maker, the fourth four from a
300
307
  jeweller, the fifth four from a nut and sweet shop — a coffee with four
301
308
  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.
309
+ each listing a few of its products — the sixth three from a pastırma and
310
+ sucuk shop and a picture — each with its product. The food products'
311
+ further photos are from [Unsplash](https://unsplash.com/license) (free to
312
+ use).
package/dist/index.js CHANGED
@@ -72,10 +72,12 @@
72
72
  * on the group that plays; aria-current on its bar, data-pg-reels-done on
73
73
  * the bars before it, and --pg-reels-progress (0 to 1) on the bar playing.
74
74
  *
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.
75
+ * Seen: a group whose every clip has been shown in the full view is seen —
76
+ * data-pg-reels-seen="true" on it ("false" before), for its ring to go
77
+ * pale as stories' do; for --pg-reels-seen-for (24h by default), then
78
+ * bright again; --pg-reels-seen: off and never. With a data-pg-reels-id on
79
+ * the group it is remembered in this browser; forgetReelsSeen() forgets it
80
+ * all — every ring bright again, at once.
79
81
  *
80
82
  * CSS, per screen size: --pg-reels-show (cards across: 4; 1.5 on a phone
81
83
  * shows one and half the next), --pg-reels-gap, --pg-reels-duration (a
@@ -238,44 +240,80 @@ function show(g, i) {
238
240
  b.style.setProperty(PROGRESS, k < g.at ? "1" : "0");
239
241
  });
240
242
  }
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() {
243
+ /* ---------- Seen, kept in this browser ----------
244
+ A group is seen once every one of its clips has been shown in the full
245
+ view. Set on the section or above:
246
+ --pg-reels-seen all (default) · off: the rings never go pale
247
+ --pg-reels-seen-for how long it stays seen: 24h (default), 1h, 30m,
248
+ 7d — then bright again; forever: until forgotten */
249
+ // Groups with a data-pg-reels-id: in localStorage, as { id: when }.
250
+ // Without: this visit only.
251
+ const seenHere = new WeakMap();
252
+ const SEEN_FOR_DEFAULT = 24 * 60 * 60 * 1000;
253
+ function seenMap() {
245
254
  try {
246
- return new Set(JSON.parse(localStorage.getItem(SEEN_KEY) || "[]"));
255
+ const kept = JSON.parse(localStorage.getItem(SEEN_KEY) || "{}");
256
+ // An older list of ids: seen now.
257
+ if (Array.isArray(kept))
258
+ return Object.fromEntries(kept.map((id) => [String(id), Date.now()]));
259
+ return kept && typeof kept === "object" ? kept : {};
247
260
  }
248
261
  catch {
249
- return new Set();
262
+ return {};
250
263
  }
251
264
  }
252
- function isSeen(el, set = seenSet()) {
265
+ /* "24h", "30m", "90s", "500ms", "7d" → ms; "forever" → Infinity. */
266
+ function spanOf(raw) {
267
+ const text = raw.trim().toLowerCase();
268
+ if (!text)
269
+ return SEEN_FOR_DEFAULT;
270
+ if (text === "forever" || text === "none")
271
+ return Infinity;
272
+ const m = /^(-?[\d.]+)\s*(ms|s|m|h|d)?$/.exec(text);
273
+ if (!m)
274
+ return SEEN_FOR_DEFAULT;
275
+ const n = Number(m[1]);
276
+ const unit = { ms: 1, s: 1000, m: 60000, h: 3600000, d: 86400000 }[(m[2] ?? "ms")];
277
+ return Number.isFinite(n) ? Math.max(0, n * unit) : SEEN_FOR_DEFAULT;
278
+ }
279
+ function seenRules(el) {
280
+ const cs = getComputedStyle(el);
281
+ return {
282
+ off: cs.getPropertyValue("--pg-reels-seen").trim() === "off",
283
+ span: spanOf(cs.getPropertyValue("--pg-reels-seen-for")),
284
+ };
285
+ }
286
+ function isSeen(el, map = seenMap()) {
287
+ const { off, span } = seenRules(el);
288
+ if (off)
289
+ return false;
253
290
  const id = el.getAttribute(ID);
254
- return id ? set.has(id) : seenHere.has(el);
291
+ const when = id ? map[id] : seenHere.get(el);
292
+ return typeof when === "number" && Date.now() - when < span;
255
293
  }
256
294
  function markSeen(el) {
295
+ if (seenRules(el).off)
296
+ return;
257
297
  const id = el.getAttribute(ID);
258
298
  if (!id)
259
- seenHere.add(el);
299
+ seenHere.set(el, Date.now());
260
300
  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
- }
301
+ const map = seenMap();
302
+ map[id] = Date.now();
303
+ try {
304
+ localStorage.setItem(SEEN_KEY, JSON.stringify(map));
305
+ }
306
+ catch {
307
+ // Private mode, or storage full: seen for this visit only.
308
+ seenHere.set(el, Date.now());
271
309
  }
272
310
  }
273
311
  paintSeen();
274
312
  }
275
313
  function paintSeen() {
276
- const set = seenSet();
314
+ const map = seenMap();
277
315
  document.querySelectorAll(`[${GROUP}]`).forEach((el) => {
278
- const value = String(isSeen(el, set));
316
+ const value = String(isSeen(el, map));
279
317
  if (el.getAttribute(SEEN) !== value)
280
318
  el.setAttribute(SEEN, value);
281
319
  });
@@ -290,8 +328,11 @@ export function forgetReelsSeen() {
290
328
  // Storage blocked: nothing kept, nothing to forget.
291
329
  }
292
330
  document.querySelectorAll(`[${GROUP}]`).forEach((el) => seenHere.delete(el));
331
+ watched = new WeakMap();
293
332
  paintSeen();
294
333
  }
334
+ // The clips of each group shown in the full view so far, this visit.
335
+ let watched = new WeakMap();
295
336
  /* ---------- Everything on the page ---------- */
296
337
  let live = null;
297
338
  /* Start watching every reels section on the page, those rendered later
@@ -660,7 +701,12 @@ export function initReels() {
660
701
  headOf = g;
661
702
  part("view-head").setAttribute(SEEN, String(isSeen(g.el)));
662
703
  }
663
- markSeen(g.el);
704
+ // This clip shown; every clip of the group shown: seen.
705
+ const shown = watched.get(g.el) ?? new Set();
706
+ shown.add(view.ci);
707
+ watched.set(g.el, shown);
708
+ if (shown.size >= n)
709
+ markSeen(g.el);
664
710
  viewBars.replaceChildren(...g.clips.map((_, i) => {
665
711
  const bar = document.createElement("button");
666
712
  bar.type = "button";
@@ -905,6 +951,7 @@ export function initReels() {
905
951
  view.pushed = false;
906
952
  choose();
907
953
  applySound();
954
+ paintSeen();
908
955
  };
909
956
  const onViewClick = (event) => {
910
957
  if (!(event.target instanceof Element))
@@ -1137,6 +1184,9 @@ export function initReels() {
1137
1184
  return;
1138
1185
  }
1139
1186
  playing.forEach(document.hidden ? stop : start);
1187
+ // Back after a while: what was seen may have run out.
1188
+ if (!document.hidden)
1189
+ paintSeen();
1140
1190
  };
1141
1191
  document.addEventListener("visibilitychange", onHidden);
1142
1192
  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)
package/index.ts CHANGED
@@ -72,10 +72,12 @@
72
72
  * on the group that plays; aria-current on its bar, data-pg-reels-done on
73
73
  * the bars before it, and --pg-reels-progress (0 to 1) on the bar playing.
74
74
  *
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.
75
+ * Seen: a group whose every clip has been shown in the full view is seen —
76
+ * data-pg-reels-seen="true" on it ("false" before), for its ring to go
77
+ * pale as stories' do; for --pg-reels-seen-for (24h by default), then
78
+ * bright again; --pg-reels-seen: off and never. With a data-pg-reels-id on
79
+ * the group it is remembered in this browser; forgetReelsSeen() forgets it
80
+ * all — every ring bright again, at once.
79
81
  *
80
82
  * CSS, per screen size: --pg-reels-show (cards across: 4; 1.5 on a phone
81
83
  * shows one and half the next), --pg-reels-gap, --pg-reels-duration (a
@@ -265,46 +267,78 @@ function show(g: Group, i: number): void {
265
267
  });
266
268
  }
267
269
 
268
- /* ---------- Seen, kept in this browser ---------- */
270
+ /* ---------- Seen, kept in this browser ----------
271
+ A group is seen once every one of its clips has been shown in the full
272
+ view. Set on the section or above:
273
+ --pg-reels-seen all (default) · off: the rings never go pale
274
+ --pg-reels-seen-for how long it stays seen: 24h (default), 1h, 30m,
275
+ 7d — then bright again; forever: until forgotten */
269
276
 
270
- // Groups with a data-pg-reels-id: in localStorage. Without: this visit.
271
- const seenHere = new WeakSet<Element>();
277
+ // Groups with a data-pg-reels-id: in localStorage, as { id: when }.
278
+ // Without: this visit only.
279
+ const seenHere = new WeakMap<Element, number>();
280
+ const SEEN_FOR_DEFAULT = 24 * 60 * 60 * 1000;
272
281
 
273
- function seenSet(): Set<string> {
282
+ function seenMap(): Record<string, number> {
274
283
  try {
275
- return new Set(JSON.parse(localStorage.getItem(SEEN_KEY) || "[]") as string[]);
284
+ const kept = JSON.parse(localStorage.getItem(SEEN_KEY) || "{}") as unknown;
285
+ // An older list of ids: seen now.
286
+ if (Array.isArray(kept)) return Object.fromEntries(kept.map((id) => [String(id), Date.now()]));
287
+ return kept && typeof kept === "object" ? (kept as Record<string, number>) : {};
276
288
  } catch {
277
- return new Set();
289
+ return {};
278
290
  }
279
291
  }
280
292
 
281
- function isSeen(el: Element, set = seenSet()): boolean {
293
+ /* "24h", "30m", "90s", "500ms", "7d" → ms; "forever" → Infinity. */
294
+ function spanOf(raw: string): number {
295
+ const text = raw.trim().toLowerCase();
296
+ if (!text) return SEEN_FOR_DEFAULT;
297
+ if (text === "forever" || text === "none") return Infinity;
298
+ const m = /^(-?[\d.]+)\s*(ms|s|m|h|d)?$/.exec(text);
299
+ if (!m) return SEEN_FOR_DEFAULT;
300
+ const n = Number(m[1]);
301
+ const unit = { ms: 1, s: 1000, m: 60000, h: 3600000, d: 86400000 }[(m[2] ?? "ms") as "ms" | "s" | "m" | "h" | "d"];
302
+ return Number.isFinite(n) ? Math.max(0, n * unit) : SEEN_FOR_DEFAULT;
303
+ }
304
+
305
+ function seenRules(el: Element): { off: boolean; span: number } {
306
+ const cs = getComputedStyle(el);
307
+ return {
308
+ off: cs.getPropertyValue("--pg-reels-seen").trim() === "off",
309
+ span: spanOf(cs.getPropertyValue("--pg-reels-seen-for")),
310
+ };
311
+ }
312
+
313
+ function isSeen(el: Element, map = seenMap()): boolean {
314
+ const { off, span } = seenRules(el);
315
+ if (off) return false;
282
316
  const id = el.getAttribute(ID);
283
- return id ? set.has(id) : seenHere.has(el);
317
+ const when = id ? map[id] : seenHere.get(el);
318
+ return typeof when === "number" && Date.now() - when < span;
284
319
  }
285
320
 
286
321
  function markSeen(el: Element): void {
322
+ if (seenRules(el).off) return;
287
323
  const id = el.getAttribute(ID);
288
- if (!id) seenHere.add(el);
324
+ if (!id) seenHere.set(el, Date.now());
289
325
  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
- }
326
+ const map = seenMap();
327
+ map[id] = Date.now();
328
+ try {
329
+ localStorage.setItem(SEEN_KEY, JSON.stringify(map));
330
+ } catch {
331
+ // Private mode, or storage full: seen for this visit only.
332
+ seenHere.set(el, Date.now());
299
333
  }
300
334
  }
301
335
  paintSeen();
302
336
  }
303
337
 
304
338
  function paintSeen(): void {
305
- const set = seenSet();
339
+ const map = seenMap();
306
340
  document.querySelectorAll<HTMLElement>(`[${GROUP}]`).forEach((el) => {
307
- const value = String(isSeen(el, set));
341
+ const value = String(isSeen(el, map));
308
342
  if (el.getAttribute(SEEN) !== value) el.setAttribute(SEEN, value);
309
343
  });
310
344
  }
@@ -318,9 +352,13 @@ export function forgetReelsSeen(): void {
318
352
  // Storage blocked: nothing kept, nothing to forget.
319
353
  }
320
354
  document.querySelectorAll(`[${GROUP}]`).forEach((el) => seenHere.delete(el));
355
+ watched = new WeakMap();
321
356
  paintSeen();
322
357
  }
323
358
 
359
+ // The clips of each group shown in the full view so far, this visit.
360
+ let watched = new WeakMap<Element, Set<number>>();
361
+
324
362
  /* ---------- Everything on the page ---------- */
325
363
 
326
364
  let live: object | null = null;
@@ -686,7 +724,11 @@ export function initReels(): () => void {
686
724
  headOf = g;
687
725
  part("view-head").setAttribute(SEEN, String(isSeen(g.el)));
688
726
  }
689
- markSeen(g.el);
727
+ // This clip shown; every clip of the group shown: seen.
728
+ const shown = watched.get(g.el) ?? new Set<number>();
729
+ shown.add(view.ci);
730
+ watched.set(g.el, shown);
731
+ if (shown.size >= n) markSeen(g.el);
690
732
 
691
733
  viewBars.replaceChildren(
692
734
  ...g.clips.map((_, i) => {
@@ -919,6 +961,7 @@ export function initReels(): () => void {
919
961
  view.pushed = false;
920
962
  choose();
921
963
  applySound();
964
+ paintSeen();
922
965
  };
923
966
 
924
967
  const onViewClick = (event: MouseEvent): void => {
@@ -1122,6 +1165,8 @@ export function initReels(): () => void {
1122
1165
  return;
1123
1166
  }
1124
1167
  playing.forEach(document.hidden ? stop : start);
1168
+ // Back after a while: what was seen may have run out.
1169
+ if (!document.hidden) paintSeen();
1125
1170
  };
1126
1171
  document.addEventListener("visibilitychange", onHidden);
1127
1172
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plusguard/reels",
3
- "version": "1.3.1",
3
+ "version": "1.4.1",
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",