@snowcone-app/ui 0.12.2 → 0.13.2
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 +22 -0
- package/dist/chunk-GY6TC6M6.js +86 -0
- package/dist/chunk-GY6TC6M6.js.map +1 -0
- package/dist/index.cjs +225 -51
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +45 -3
- package/dist/index.d.ts +45 -3
- package/dist/index.js +194 -52
- package/dist/index.js.map +1 -1
- package/dist/personalization/bridge.cjs.map +1 -1
- package/dist/personalization/bridge.d.cts +1 -1
- package/dist/personalization/bridge.d.ts +1 -1
- package/dist/personalization/bridge.js +1 -1
- package/dist/{types-CNJT8FUd.d.cts → types-BObPMwP5.d.cts} +17 -1
- package/dist/{types-CNJT8FUd.d.ts → types-BObPMwP5.d.ts} +17 -1
- package/package.json +1 -1
- package/src/composed/HeroProductImage.tsx +137 -64
- package/src/composed/__tests__/HeroProductImage.mountsOnLiveFrame.test.tsx +147 -0
- package/src/composed/__tests__/HeroProductImage.swap.test.tsx +105 -0
- package/src/patterns/RealtimeProvider.tsx +32 -1
- package/src/patterns/__tests__/RealtimeProvider.blobLifecycle.test.tsx +73 -0
- package/src/personalization/PersonalizationInputs.tsx +2 -0
- package/src/personalization/PersonalizationProvider.tsx +95 -14
- package/src/personalization/__tests__/PersonalizationProvider.last-keystroke.test.tsx +81 -0
- package/src/personalization/__tests__/fit.test.ts +75 -0
- package/src/personalization/index.ts +2 -1
- package/src/personalization/types.ts +16 -0
- package/src/personalization/utils.ts +68 -0
- package/dist/chunk-WDOT4BST.js +0 -50
- package/dist/chunk-WDOT4BST.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.13.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#6692](https://github.com/snowcone-app/snowcone-monorepo/pull/6692) [`96079cb`](https://github.com/snowcone-app/snowcone-monorepo/commit/96079cb697b9c2051f64e39370ff1f3fc2d7c6ec) Thanks [@kevinsproles](https://github.com/kevinsproles)! - Gallery pictures that mount after their live frames arrived (a phone carousel slide swiped to after an edit): the provider's replayed frame is kept too (not only a `realtimeUrl` handed in), while a non-live URL handed in at mount (a static picture of an older variant) is still replaced. `data-render-version` / `data-render-state` now describe what the element paints: a non-live picture puts the version back to 0 (stale), never "current". A picture whose newest frame can no longer be loaded is forgotten and re-rendered alone (`E_FRAME_LOST`), once until it shows a picture again.
|
|
8
|
+
|
|
9
|
+
## 0.13.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#6688](https://github.com/snowcone-app/snowcone-monorepo/pull/6688) [`27379aa`](https://github.com/snowcone-app/snowcone-monorepo/commit/27379aab04800d7b9b6e07915b9a320cc763994c) Thanks [@kevinsproles](https://github.com/kevinsproles)! - HeroProductImage double-buffers its picture: the next one loads hidden in its own `<img>` above the shown one and shows once decoded (at once for a live frame, a fade for another picture), so typing never paints a frame without a picture (`data-hero-incoming`). A picture mounted with a live frame (the phone carousel's slide 3) keeps it instead of its static picture. PersonalizationProvider exports 250 ms after the last keystroke (was 100 ms) and always exports the last keystroke once typing settles (the canvas auto-export could drop it: "Hello Kev!" rendered and printed as "Hello Kev").
|
|
14
|
+
|
|
15
|
+
## 0.13.0
|
|
16
|
+
|
|
17
|
+
### Minor Changes
|
|
18
|
+
|
|
19
|
+
- [#6680](https://github.com/snowcone-app/snowcone-monorepo/pull/6680) [`608bc6c`](https://github.com/snowcone-app/snowcone-monorepo/commit/608bc6c885e21739bd1e0fed30ed711bad6d0cbe) Thanks [@kevinsproles](https://github.com/kevinsproles)! - Template text fields: `fit: "shrink"` sets the buyer's text at the element's own size and makes it smaller (never larger)
|
|
20
|
+
until one line fits the element's box width, so a long name never runs past the design's edge; `sample: true` marks the
|
|
21
|
+
element's own text as a sample that shows only until the buyer types into any field, after which an empty field draws
|
|
22
|
+
nothing. The ui personalization inputs take the field's `maxLength` and carry its label as their accessible name; the
|
|
23
|
+
fit helpers (`shrinkFontSizeToFit`, `measureTextLine`, `anyTyped`, `textValueFor`) are exported for custom inputs.
|
|
24
|
+
|
|
3
25
|
## 0.12.2
|
|
4
26
|
|
|
5
27
|
### Patch Changes
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// src/personalization/utils.ts
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
function normalizeHex(color) {
|
|
4
|
+
let hex = color.trim().toUpperCase();
|
|
5
|
+
if (hex.length === 4 && hex.startsWith("#")) {
|
|
6
|
+
hex = "#" + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3];
|
|
7
|
+
}
|
|
8
|
+
return hex;
|
|
9
|
+
}
|
|
10
|
+
function isValidImageUrl(url) {
|
|
11
|
+
if (!url || url.length < 10) return false;
|
|
12
|
+
try {
|
|
13
|
+
const parsed = new URL(url);
|
|
14
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return false;
|
|
15
|
+
if (!parsed.hostname.includes(".")) return false;
|
|
16
|
+
if (parsed.pathname.length <= 1) return false;
|
|
17
|
+
return true;
|
|
18
|
+
} catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function anyTyped(fields, values) {
|
|
23
|
+
return fields.some((f) => f.type === "text" && typeof values[f.name] === "string" && values[f.name].trim().length > 0);
|
|
24
|
+
}
|
|
25
|
+
function textValueFor(field, values, typed = false) {
|
|
26
|
+
const v = values[field.name] ?? "";
|
|
27
|
+
if (field.sample) return typed ? v : null;
|
|
28
|
+
return v;
|
|
29
|
+
}
|
|
30
|
+
function canvasFontString(style, fontSize = style.fontSize) {
|
|
31
|
+
return [style.italic ? "italic" : "", style.bold ? "bold" : "", `${fontSize}px`, style.fontFamily].filter(Boolean).join(" ");
|
|
32
|
+
}
|
|
33
|
+
var FIT_MARGIN = 0.03;
|
|
34
|
+
function shrinkFontSizeToFit(text, style, maxWidth, measure) {
|
|
35
|
+
const limit = maxWidth * (1 - FIT_MARGIN);
|
|
36
|
+
if (!text || !(limit > 0)) return style.fontSize;
|
|
37
|
+
const at = measure(text, style.fontSize);
|
|
38
|
+
if (at <= limit) return style.fontSize;
|
|
39
|
+
const spacing = (style.letterSpacing ?? 0) * text.length;
|
|
40
|
+
let size = Math.max(1, style.fontSize * Math.max(1, limit - spacing) / Math.max(1, at - spacing));
|
|
41
|
+
for (let i = 0; i < 40 && size > 1 && measure(text, size) > limit; i++) size *= 0.98;
|
|
42
|
+
return Math.max(1, Math.floor(size * 2) / 2);
|
|
43
|
+
}
|
|
44
|
+
var measureCtx = null;
|
|
45
|
+
function measureTextLine(text, style, fontSize = style.fontSize) {
|
|
46
|
+
if (typeof document === "undefined") return 0;
|
|
47
|
+
measureCtx ??= document.createElement("canvas").getContext("2d");
|
|
48
|
+
if (!measureCtx) return 0;
|
|
49
|
+
measureCtx.font = canvasFontString(style, fontSize);
|
|
50
|
+
return measureCtx.measureText(text).width + (style.letterSpacing ?? 0) * text.length;
|
|
51
|
+
}
|
|
52
|
+
function useDebouncedValue(value, delay) {
|
|
53
|
+
const [debounced, setDebounced] = useState(value);
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
const timer = setTimeout(() => setDebounced(value), delay);
|
|
56
|
+
return () => clearTimeout(timer);
|
|
57
|
+
}, [value, delay]);
|
|
58
|
+
return debounced;
|
|
59
|
+
}
|
|
60
|
+
function scrollInputAboveKeyboard(input) {
|
|
61
|
+
const position = () => {
|
|
62
|
+
const rect = input.getBoundingClientRect();
|
|
63
|
+
const visibleBottom = window.visualViewport?.height || window.innerHeight * 0.5;
|
|
64
|
+
const target = visibleBottom - rect.height - 20;
|
|
65
|
+
const delta = rect.top - target;
|
|
66
|
+
if (Math.abs(delta) > 10) {
|
|
67
|
+
window.scrollBy({ top: delta, behavior: "smooth" });
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
setTimeout(position, 400);
|
|
71
|
+
window.visualViewport?.addEventListener("resize", position, { once: true });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export {
|
|
75
|
+
normalizeHex,
|
|
76
|
+
isValidImageUrl,
|
|
77
|
+
anyTyped,
|
|
78
|
+
textValueFor,
|
|
79
|
+
canvasFontString,
|
|
80
|
+
FIT_MARGIN,
|
|
81
|
+
shrinkFontSizeToFit,
|
|
82
|
+
measureTextLine,
|
|
83
|
+
useDebouncedValue,
|
|
84
|
+
scrollInputAboveKeyboard
|
|
85
|
+
};
|
|
86
|
+
//# sourceMappingURL=chunk-GY6TC6M6.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/personalization/utils.ts"],"sourcesContent":["\"use client\";\n\n/**\n * Personalization Utilities\n */\n\nimport { useEffect, useState } from \"react\";\nimport type { PersonalizationField, PersonalizationTextField, PersonalizationValues } from \"./types\";\n\n/** Normalize any hex color to uppercase 6-digit form (#RGB → #RRGGBB) */\nexport function normalizeHex(color: string): string {\n let hex = color.trim().toUpperCase();\n if (hex.length === 4 && hex.startsWith(\"#\")) {\n hex = \"#\" + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3];\n }\n return hex;\n}\n\n/** Check if a string looks like a complete, loadable URL */\nexport function isValidImageUrl(url: string): boolean {\n if (!url || url.length < 10) return false;\n try {\n const parsed = new URL(url);\n if (parsed.protocol !== \"http:\" && parsed.protocol !== \"https:\") return false;\n if (!parsed.hostname.includes(\".\")) return false;\n if (parsed.pathname.length <= 1) return false;\n return true;\n } catch {\n return false;\n }\n}\n\n/** Has the buyer typed into any TEXT field of the design (any non-blank text)? */\nexport function anyTyped(fields: readonly PersonalizationField[], values: PersonalizationValues): boolean {\n return fields.some((f) => f.type === \"text\" && typeof values[f.name] === \"string\" && values[f.name].trim().length > 0);\n}\n\n/**\n * What a text field's elements draw: its box's text, or null = the element's own (designed) text. A `sample` field draws\n * its sample until anything is typed in any field, then exactly its box (empty = nothing). Any other field draws its\n * box's text (\"\" until typed into, which the binder leaves alone).\n */\nexport function textValueFor(field: PersonalizationTextField, values: PersonalizationValues, typed = false): string | null {\n const v = values[field.name] ?? \"\";\n if (field.sample) return typed ? v : null;\n return v;\n}\n\n/** The font a text element draws with: the canvas's own CSS font shorthand (canvas TextMetrics buildFontString). */\nexport interface TextLineStyle {\n fontSize: number;\n fontFamily: string;\n bold?: boolean;\n italic?: boolean;\n /** Extra advance after each character, px (canvas letterSpacing). */\n letterSpacing?: number;\n}\n\n/** The canvas font shorthand for a style at a size, exactly as the canvas builds it (\"italic bold 72px Oswald\"). */\nexport function canvasFontString(style: TextLineStyle, fontSize = style.fontSize): string {\n return [style.italic ? \"italic\" : \"\", style.bold ? \"bold\" : \"\", `${fontSize}px`, style.fontFamily].filter(Boolean).join(\" \");\n}\n\n/** A share of the box kept free when fitting: measurement differs by a hair between renderers (browser, server raster). */\nexport const FIT_MARGIN = 0.03;\n\n/**\n * The font size one line of `text` is drawn at to fit `maxWidth`: `style.fontSize` when it already fits, else the\n * largest smaller size that does (text width is linear in the font size, plus a fixed letter spacing per character).\n * Never larger than the style's size; never below 1 px. `measure(text, fontSize)` is the line's width at a size.\n */\nexport function shrinkFontSizeToFit(\n text: string,\n style: TextLineStyle,\n maxWidth: number,\n measure: (text: string, fontSize: number) => number,\n): number {\n const limit = maxWidth * (1 - FIT_MARGIN);\n if (!text || !(limit > 0)) return style.fontSize;\n const at = measure(text, style.fontSize);\n if (at <= limit) return style.fontSize;\n const spacing = (style.letterSpacing ?? 0) * text.length;\n let size = Math.max(1, (style.fontSize * Math.max(1, limit - spacing)) / Math.max(1, at - spacing));\n // measurement rounding (hinting, kerning) can leave a hair over: step down until it truly fits\n for (let i = 0; i < 40 && size > 1 && measure(text, size) > limit; i++) size *= 0.98;\n return Math.max(1, Math.floor(size * 2) / 2);\n}\n\nlet measureCtx: CanvasRenderingContext2D | null = null;\n\n/** One line's width in a browser, with the canvas's own font shorthand (0 where no 2D canvas exists, e.g. on a server). */\nexport function measureTextLine(text: string, style: TextLineStyle, fontSize = style.fontSize): number {\n if (typeof document === \"undefined\") return 0;\n measureCtx ??= document.createElement(\"canvas\").getContext(\"2d\");\n if (!measureCtx) return 0;\n measureCtx.font = canvasFontString(style, fontSize);\n return measureCtx.measureText(text).width + (style.letterSpacing ?? 0) * text.length;\n}\n\n/** Debounce a string value — only emits after `delay` ms of inactivity */\nexport function useDebouncedValue(value: string, delay: number): string {\n const [debounced, setDebounced] = useState(value);\n useEffect(() => {\n const timer = setTimeout(() => setDebounced(value), delay);\n return () => clearTimeout(timer);\n }, [value, delay]);\n return debounced;\n}\n\n/**\n * Scroll an input to sit just above the iOS keyboard after it appears.\n * Uses visualViewport.height to determine the visible area above the keyboard.\n */\nexport function scrollInputAboveKeyboard(input: HTMLElement) {\n const position = () => {\n const rect = input.getBoundingClientRect();\n const visibleBottom =\n window.visualViewport?.height || window.innerHeight * 0.5;\n const target = visibleBottom - rect.height - 20;\n const delta = rect.top - target;\n if (Math.abs(delta) > 10) {\n window.scrollBy({ top: delta, behavior: \"smooth\" });\n }\n };\n setTimeout(position, 400);\n window.visualViewport?.addEventListener(\"resize\", position, { once: true });\n}\n"],"mappings":";AAMA,SAAS,WAAW,gBAAgB;AAI7B,SAAS,aAAa,OAAuB;AAClD,MAAI,MAAM,MAAM,KAAK,EAAE,YAAY;AACnC,MAAI,IAAI,WAAW,KAAK,IAAI,WAAW,GAAG,GAAG;AAC3C,UAAM,MAAM,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC;AAAA,EAChE;AACA,SAAO;AACT;AAGO,SAAS,gBAAgB,KAAsB;AACpD,MAAI,CAAC,OAAO,IAAI,SAAS,GAAI,QAAO;AACpC,MAAI;AACF,UAAM,SAAS,IAAI,IAAI,GAAG;AAC1B,QAAI,OAAO,aAAa,WAAW,OAAO,aAAa,S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