@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.
@@ -5,6 +5,7 @@
5
5
  */
6
6
 
7
7
  import { useEffect, useState } from "react";
8
+ import type { PersonalizationField, PersonalizationTextField, PersonalizationValues } from "./types";
8
9
 
9
10
  /** Normalize any hex color to uppercase 6-digit form (#RGB → #RRGGBB) */
10
11
  export function normalizeHex(color: string): string {
@@ -29,6 +30,73 @@ export function isValidImageUrl(url: string): boolean {
29
30
  }
30
31
  }
31
32
 
33
+ /** Has the buyer typed into any TEXT field of the design (any non-blank text)? */
34
+ export function anyTyped(fields: readonly PersonalizationField[], values: PersonalizationValues): boolean {
35
+ return fields.some((f) => f.type === "text" && typeof values[f.name] === "string" && values[f.name].trim().length > 0);
36
+ }
37
+
38
+ /**
39
+ * What a text field's elements draw: its box's text, or null = the element's own (designed) text. A `sample` field draws
40
+ * its sample until anything is typed in any field, then exactly its box (empty = nothing). Any other field draws its
41
+ * box's text ("" until typed into, which the binder leaves alone).
42
+ */
43
+ export function textValueFor(field: PersonalizationTextField, values: PersonalizationValues, typed = false): string | null {
44
+ const v = values[field.name] ?? "";
45
+ if (field.sample) return typed ? v : null;
46
+ return v;
47
+ }
48
+
49
+ /** The font a text element draws with: the canvas's own CSS font shorthand (canvas TextMetrics buildFontString). */
50
+ export interface TextLineStyle {
51
+ fontSize: number;
52
+ fontFamily: string;
53
+ bold?: boolean;
54
+ italic?: boolean;
55
+ /** Extra advance after each character, px (canvas letterSpacing). */
56
+ letterSpacing?: number;
57
+ }
58
+
59
+ /** The canvas font shorthand for a style at a size, exactly as the canvas builds it ("italic bold 72px Oswald"). */
60
+ export function canvasFontString(style: TextLineStyle, fontSize = style.fontSize): string {
61
+ return [style.italic ? "italic" : "", style.bold ? "bold" : "", `${fontSize}px`, style.fontFamily].filter(Boolean).join(" ");
62
+ }
63
+
64
+ /** A share of the box kept free when fitting: measurement differs by a hair between renderers (browser, server raster). */
65
+ export const FIT_MARGIN = 0.03;
66
+
67
+ /**
68
+ * The font size one line of `text` is drawn at to fit `maxWidth`: `style.fontSize` when it already fits, else the
69
+ * largest smaller size that does (text width is linear in the font size, plus a fixed letter spacing per character).
70
+ * Never larger than the style's size; never below 1 px. `measure(text, fontSize)` is the line's width at a size.
71
+ */
72
+ export function shrinkFontSizeToFit(
73
+ text: string,
74
+ style: TextLineStyle,
75
+ maxWidth: number,
76
+ measure: (text: string, fontSize: number) => number,
77
+ ): number {
78
+ const limit = maxWidth * (1 - FIT_MARGIN);
79
+ if (!text || !(limit > 0)) return style.fontSize;
80
+ const at = measure(text, style.fontSize);
81
+ if (at <= limit) return style.fontSize;
82
+ const spacing = (style.letterSpacing ?? 0) * text.length;
83
+ let size = Math.max(1, (style.fontSize * Math.max(1, limit - spacing)) / Math.max(1, at - spacing));
84
+ // measurement rounding (hinting, kerning) can leave a hair over: step down until it truly fits
85
+ for (let i = 0; i < 40 && size > 1 && measure(text, size) > limit; i++) size *= 0.98;
86
+ return Math.max(1, Math.floor(size * 2) / 2);
87
+ }
88
+
89
+ let measureCtx: CanvasRenderingContext2D | null = null;
90
+
91
+ /** 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). */
92
+ export function measureTextLine(text: string, style: TextLineStyle, fontSize = style.fontSize): number {
93
+ if (typeof document === "undefined") return 0;
94
+ measureCtx ??= document.createElement("canvas").getContext("2d");
95
+ if (!measureCtx) return 0;
96
+ measureCtx.font = canvasFontString(style, fontSize);
97
+ return measureCtx.measureText(text).width + (style.letterSpacing ?? 0) * text.length;
98
+ }
99
+
32
100
  /** Debounce a string value — only emits after `delay` ms of inactivity */
33
101
  export function useDebouncedValue(value: string, delay: number): string {
34
102
  const [debounced, setDebounced] = useState(value);
@@ -1,50 +0,0 @@
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 useDebouncedValue(value, delay) {
23
- const [debounced, setDebounced] = useState(value);
24
- useEffect(() => {
25
- const timer = setTimeout(() => setDebounced(value), delay);
26
- return () => clearTimeout(timer);
27
- }, [value, delay]);
28
- return debounced;
29
- }
30
- function scrollInputAboveKeyboard(input) {
31
- const position = () => {
32
- const rect = input.getBoundingClientRect();
33
- const visibleBottom = window.visualViewport?.height || window.innerHeight * 0.5;
34
- const target = visibleBottom - rect.height - 20;
35
- const delta = rect.top - target;
36
- if (Math.abs(delta) > 10) {
37
- window.scrollBy({ top: delta, behavior: "smooth" });
38
- }
39
- };
40
- setTimeout(position, 400);
41
- window.visualViewport?.addEventListener("resize", position, { once: true });
42
- }
43
-
44
- export {
45
- normalizeHex,
46
- isValidImageUrl,
47
- useDebouncedValue,
48
- scrollInputAboveKeyboard
49
- };
50
- //# sourceMappingURL=chunk-WDOT4BST.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/personalization/utils.ts"],"sourcesContent":["\"use client\";\n\n/**\n * Personalization Utilities\n */\n\nimport { useEffect, useState } from \"react\";\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/** 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;AAG7B,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,SAAU,QAAO;AACxE,QAAI,CAAC,OAAO,SAAS,SAAS,GAAG,EAAG,QAAO;AAC3C,Q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