@snowcone-app/ui 0.9.8 → 0.10.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/CHANGELOG.md +12 -0
- package/dist/index.cjs +202 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +49 -1
- package/dist/index.d.ts +49 -1
- package/dist/index.js +202 -15
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/composed/HeroProductImage.tsx +57 -1
- package/src/composed/carousels/MobileProductCarousel.tsx +3 -0
- package/src/patterns/RealtimeProvider.tsx +179 -11
- package/src/patterns/__tests__/RealtimeProvider.sessionShape.test.tsx +162 -0
- package/src/patterns/__tests__/sessionShape.test.ts +62 -0
- package/src/patterns/sessionShape.ts +82 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { ladderWidth, sameShape, sessionShape, type PictureView } from "../sessionShape";
|
|
3
|
+
|
|
4
|
+
const ALL = Array.from({ length: 31 }, (_, i) => `M${i}`); // the phone case has 31 pictures
|
|
5
|
+
const view = (inSet: boolean, onScreen: boolean, devicePx = 1026): PictureView => ({ inSet, onScreen, devicePx });
|
|
6
|
+
// iPhone 14 in the editor: slide M0 on screen (342 css px x 3 = 1026 device px), M1 its neighbour.
|
|
7
|
+
const phone = new Map<string, PictureView>(ALL.map((id, i) => [id, view(i <= 1, i === 0, i <= 1 ? 1026 : 0)]));
|
|
8
|
+
|
|
9
|
+
describe("sessionShape", () => {
|
|
10
|
+
it("no viewport policy (desktop): every picture at the page width, exactly as today", () => {
|
|
11
|
+
expect(sessionShape({ allMockupIds: ALL, views: new Map(), heroWidth: 1280, editorOpen: true, editLive: true }))
|
|
12
|
+
.toEqual({ mockupIds: ALL, width: 1280, phase: "no-policy" });
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("a live edit renders only the set in view, at the width the screen shows (1026 -> 1200)", () => {
|
|
16
|
+
expect(sessionShape({ allMockupIds: ALL, views: phone, heroWidth: 3000, editorOpen: true, editLive: true }))
|
|
17
|
+
.toEqual({ mockupIds: ["M0", "M1"], width: 1200, phase: "editing" });
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("when the edit stops: every picture, still at the editing width (the catch-up)", () => {
|
|
21
|
+
expect(sessionShape({ allMockupIds: ALL, views: phone, heroWidth: 3000, editorOpen: true, editLive: false }))
|
|
22
|
+
.toEqual({ mockupIds: ALL, width: 1200, phase: "catch-up" });
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("Done: the slide on screen at hero width", () => {
|
|
26
|
+
expect(sessionShape({ allMockupIds: ALL, views: phone, heroWidth: 3000, editorOpen: false, editLive: false }))
|
|
27
|
+
.toEqual({ mockupIds: ["M0"], width: 3000, phase: "done" });
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("width comes from the element, not a breakpoint: a tablet slide 700 css px at 2x asks 1600", () => {
|
|
31
|
+
const tablet = new Map<string, PictureView>([["M0", view(true, true, 1400)], ["M1", view(true, false, 1400)]]);
|
|
32
|
+
expect(sessionShape({ allMockupIds: ALL, views: tablet, heroWidth: 3000, editorOpen: true, editLive: true }).width).toBe(1600);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("keeps gallery order and never returns an empty set", () => {
|
|
36
|
+
const none = new Map<string, PictureView>([["M5", view(false, false, 0)]]);
|
|
37
|
+
expect(sessionShape({ allMockupIds: ALL, views: none, heroWidth: 3000, editorOpen: true, editLive: true }).mockupIds).toEqual(ALL);
|
|
38
|
+
expect(sessionShape({ allMockupIds: ALL, views: none, heroWidth: 3000, editorOpen: false, editLive: false }).mockupIds).toEqual(ALL);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe("ladderWidth / sameShape", () => {
|
|
43
|
+
it("rounds UP to a ladder step", () => {
|
|
44
|
+
expect(ladderWidth(1026)).toBe(1200);
|
|
45
|
+
expect(ladderWidth(1200)).toBe(1200);
|
|
46
|
+
expect(ladderWidth(5000)).toBe(4000);
|
|
47
|
+
});
|
|
48
|
+
it("compares like the server: sorted ids and width", () => {
|
|
49
|
+
expect(sameShape({ mockupIds: ["b", "a"], width: 1200 }, { mockupIds: ["a", "b"], width: 1200 })).toBe(true);
|
|
50
|
+
expect(sameShape({ mockupIds: ["a"], width: 1200 }, { mockupIds: ["a"], width: 3000 })).toBe(false);
|
|
51
|
+
expect(sameShape(null, { mockupIds: ["a"], width: 1 })).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("a mockup rendered twice (two elements) counts as on screen if either is", () => {
|
|
55
|
+
const twice = new Map<string, PictureView>([
|
|
56
|
+
["M0#a", { mockupId: "M0", inSet: true, onScreen: true, devicePx: 1026 }],
|
|
57
|
+
["M0#b", { mockupId: "M0", inSet: false, onScreen: false, devicePx: 0 }],
|
|
58
|
+
]);
|
|
59
|
+
expect(sessionShape({ allMockupIds: ALL, views: twice, heroWidth: 3000, editorOpen: false, editLive: false }).mockupIds).toEqual(["M0"]);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which pictures a realtime session renders, and at what width (lane-editor phone design, approved 2026-11-11).
|
|
3
|
+
*
|
|
4
|
+
* The server renders exactly the LAST config's mockupIds at its width. Today a phone renders every picture at 3000 px
|
|
5
|
+
* on every edit step while it shows one slide 1026 device px wide (phone case: 31 pictures, 3.57 MB a step; on a
|
|
6
|
+
* 1.6 Mbps link a drag never caught up in 90 s). The shape:
|
|
7
|
+
*
|
|
8
|
+
* - no viewport policy (desktop, or a gallery that did not opt in): every picture at the page's width (today);
|
|
9
|
+
* - editor open, edit LIVE: the pictures in view (+ neighbours) at the width the screen shows them;
|
|
10
|
+
* - editor open, edit STOPPED: every picture at that width (the catch-up; a new edit takes it back to the view set);
|
|
11
|
+
* - editor closed (Done): the pictures in view at the hero width; others when they come into view.
|
|
12
|
+
*
|
|
13
|
+
* Pure: the provider feeds it state and sends ONE config whenever the result changes.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** Widths a session asks for: the screen's need rounded UP to one of these (render tiers re-key any width anyway). */
|
|
17
|
+
export const WIDTH_LADDER = [400, 600, 800, 1000, 1200, 1600, 2000, 3000, 4000] as const;
|
|
18
|
+
|
|
19
|
+
export function ladderWidth(devicePx: number): number {
|
|
20
|
+
for (const w of WIDTH_LADDER) if (w >= devicePx) return w;
|
|
21
|
+
return WIDTH_LADDER[WIDTH_LADDER.length - 1];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface PictureView {
|
|
25
|
+
/** Optional when the map is keyed by mockup id; set when keyed per element (a mockup may render twice). */
|
|
26
|
+
mockupId?: string;
|
|
27
|
+
/** In view or a neighbour of the picture in view (the set an edit renders). */
|
|
28
|
+
inSet: boolean;
|
|
29
|
+
/** Actually on screen now (the set Done renders at hero width). */
|
|
30
|
+
onScreen: boolean;
|
|
31
|
+
/** Displayed width in device pixels (CSS width x devicePixelRatio). */
|
|
32
|
+
devicePx: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ShapeInput {
|
|
36
|
+
/** Every mockup of the product, in gallery order. */
|
|
37
|
+
allMockupIds: readonly string[];
|
|
38
|
+
/** Pictures that opted into the viewport policy (the phone gallery), keyed by element (or by mockup id). Empty = no policy. */
|
|
39
|
+
views: ReadonlyMap<string, PictureView>;
|
|
40
|
+
/** The page's width for its pictures outside the editor (the phone hero: 3000). */
|
|
41
|
+
heroWidth: number;
|
|
42
|
+
editorOpen: boolean;
|
|
43
|
+
editLive: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface SessionShape {
|
|
47
|
+
mockupIds: string[];
|
|
48
|
+
width: number;
|
|
49
|
+
/** Why, for traces and data attributes. */
|
|
50
|
+
phase: "no-policy" | "editing" | "catch-up" | "done";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function sessionShape(input: ShapeInput): SessionShape {
|
|
54
|
+
const { allMockupIds, views, heroWidth, editorOpen, editLive } = input;
|
|
55
|
+
if (views.size === 0) return { mockupIds: [...allMockupIds], width: heroWidth, phase: "no-policy" };
|
|
56
|
+
// Merge every element of a mockup (a gallery can render one twice): in set / on screen if ANY is, widest width.
|
|
57
|
+
const byMockup = new Map<string, PictureView>();
|
|
58
|
+
views.forEach((v, key) => {
|
|
59
|
+
const id = v.mockupId ?? key;
|
|
60
|
+
const prev = byMockup.get(id);
|
|
61
|
+
byMockup.set(id, prev
|
|
62
|
+
? { inSet: prev.inSet || v.inSet, onScreen: prev.onScreen || v.onScreen, devicePx: Math.max(prev.devicePx, v.devicePx) }
|
|
63
|
+
: { inSet: v.inSet, onScreen: v.onScreen, devicePx: v.devicePx });
|
|
64
|
+
});
|
|
65
|
+
const ordered = (pick: (v: PictureView) => boolean) => allMockupIds.filter((id) => { const v = byMockup.get(id); return v ? pick(v) : false; });
|
|
66
|
+
const inSet = ordered((v) => v.inSet);
|
|
67
|
+
const onScreen = ordered((v) => v.onScreen);
|
|
68
|
+
const shown = [...byMockup.values()].filter((v) => v.inSet && v.devicePx > 0).map((v) => v.devicePx);
|
|
69
|
+
const editingWidth = shown.length ? ladderWidth(Math.max(...shown)) : heroWidth;
|
|
70
|
+
if (!editorOpen) {
|
|
71
|
+
const set = onScreen.length ? onScreen : inSet;
|
|
72
|
+
return { mockupIds: set.length ? set : [...allMockupIds], width: heroWidth, phase: "done" };
|
|
73
|
+
}
|
|
74
|
+
if (editLive && inSet.length) return { mockupIds: inSet, width: editingWidth, phase: "editing" };
|
|
75
|
+
return { mockupIds: [...allMockupIds], width: editingWidth, phase: "catch-up" };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Same render request? (The server compares sorted mockupIds and width.) */
|
|
79
|
+
export function sameShape(a: Pick<SessionShape, "mockupIds" | "width"> | null, b: Pick<SessionShape, "mockupIds" | "width">): boolean {
|
|
80
|
+
if (!a) return false;
|
|
81
|
+
return a.width === b.width && [...a.mockupIds].sort().join(",") === [...b.mockupIds].sort().join(",");
|
|
82
|
+
}
|