@replohq/canopy-sdk 0.1.3 → 0.2.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.
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Slugified family name, matching how `fontNameToCssVariable` derives a CSS var
3
+ * (e.g. "DM Sans" → "dm-sans"), so a `--font-<slug>` var round-trips back to a
4
+ * catalog family.
5
+ */
6
+ export declare function slugifyFontFamily(family: string): string;
7
+ /**
8
+ * "DM Sans" → "--font-dm-sans". The CSS variable a `next/font` import exposes.
9
+ */
10
+ export declare function fontNameToCssVariable(fontName: string): string;
11
+ /**
12
+ * Wrap a family in single quotes for `font-family` when it contains whitespace
13
+ * (e.g. `Open Sans` → `'Open Sans'`); single-word families pass through.
14
+ */
15
+ export declare function quoteFontFamily(family: string): string;
16
+ /**
17
+ * "DM Sans" → "dmSans". The JS const name for a `next/font` declaration.
18
+ */
19
+ export declare function fontNameToVariableName(fontName: string): string;
20
+ export interface NextFontDeclaration {
21
+ /** Import name for `next/font/google`, e.g. "DM_Sans". */
22
+ importName: string;
23
+ /** JS const name, e.g. "dmSans". */
24
+ variableName: string;
25
+ /** CSS variable the import exposes, e.g. "--font-dm-sans". */
26
+ cssVar: string;
27
+ /** The full `const … = …({ … })` declaration. */
28
+ declaration: string;
29
+ }
30
+ /**
31
+ * Google Fonts CSS2 stylesheet URL for a family at the given weights (defaulting
32
+ * to 400), with `display=swap`.
33
+ */
34
+ export declare function buildGoogleFontsCssUrl(family: string, weights?: number[]): string;
@@ -0,0 +1,28 @@
1
+ // ../replo-utils/lib/google-fonts.ts
2
+ function slugifyFontFamily(family) {
3
+ return family.replace(/([a-z])([A-Z])/g, "$1-$2").split(/[\s_-]+/u).map((word) => word.toLowerCase()).join("-");
4
+ }
5
+ function fontNameToCssVariable(fontName) {
6
+ return `--font-${slugifyFontFamily(fontName)}`;
7
+ }
8
+ function quoteFontFamily(family) {
9
+ return /\s/u.test(family) ? `'${family}'` : family;
10
+ }
11
+ function fontNameToVariableName(fontName) {
12
+ const words = fontName.replace(/([a-z])([A-Z])/g, "$1 $2").split(/[\s_-]+/u);
13
+ return words.map((word, index) => {
14
+ return index === 0 ? word.toLowerCase() : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
15
+ }).join("");
16
+ }
17
+ function buildGoogleFontsCssUrl(family, weights) {
18
+ const requested = weights && weights.length > 0 ? Array.from(new Set(weights)) : [400];
19
+ const sortedWeights = requested.sort(
20
+ (leftWeight, rightWeight) => leftWeight - rightWeight
21
+ );
22
+ const familyParam = family.trim().replace(/\s+/gu, "+");
23
+ return `https://fonts.googleapis.com/css2?family=${familyParam}:wght@${sortedWeights.join(
24
+ ";"
25
+ )}&display=swap`;
26
+ }
27
+
28
+ export { buildGoogleFontsCssUrl, fontNameToCssVariable, fontNameToVariableName, quoteFontFamily, slugifyFontFamily };
@@ -327,7 +327,16 @@ export declare const globalsCssSnapshotSchema: z.ZodObject<{
327
327
  }, z.core.$strip>>;
328
328
  }, z.core.$strip>;
329
329
  export declare const DESIGN_LIBRARY_PREVIEW_MESSAGE_TYPE = "replo:design-library:applyOverrides";
330
+ export declare const designLibraryPreviewFontSchema: z.ZodObject<{
331
+ family: z.ZodString;
332
+ cssVar: z.ZodString;
333
+ }, z.core.$strip>;
330
334
  export declare const designLibraryPreviewMessageSchema: z.ZodObject<{
331
335
  type: z.ZodLiteral<"replo:design-library:applyOverrides">;
332
336
  cssVars: z.ZodRecord<z.ZodString, z.ZodString>;
337
+ fonts: z.ZodArray<z.ZodObject<{
338
+ family: z.ZodString;
339
+ cssVar: z.ZodString;
340
+ }, z.core.$strip>>;
341
+ defaultFontCssVar: z.ZodNullable<z.ZodString>;
333
342
  }, z.core.$strip>;
@@ -100,9 +100,15 @@ var globalsCssSnapshotSchema = z.object({
100
100
  scopes: z.array(globalsCssScopeSchema)
101
101
  }).describe("GlobalsCssSnapshot");
102
102
  var DESIGN_LIBRARY_PREVIEW_MESSAGE_TYPE = "replo:design-library:applyOverrides";
103
+ var designLibraryPreviewFontSchema = z.object({
104
+ family: z.string(),
105
+ cssVar: z.string()
106
+ }).describe("DesignLibraryPreviewFont");
103
107
  var designLibraryPreviewMessageSchema = z.object({
104
108
  type: z.literal(DESIGN_LIBRARY_PREVIEW_MESSAGE_TYPE),
105
- cssVars: z.record(z.string(), z.string())
109
+ cssVars: z.record(z.string(), z.string()),
110
+ fonts: z.array(designLibraryPreviewFontSchema),
111
+ defaultFontCssVar: z.string().nullable()
106
112
  }).describe("DesignLibraryPreviewMessage");
107
113
 
108
- export { DESIGN_LIBRARY_PREVIEW_MESSAGE_TYPE, designLibraryPreviewMessageSchema, designLibrarySavedStyleMetadataSchema, designLibrarySavedStylesSchema, designLibrarySchema, globalsCssScopeSchema, globalsCssSnapshotSchema, globalsCssTokenSchema, savedColorStyleSchema, savedTextStyleSchema };
114
+ export { DESIGN_LIBRARY_PREVIEW_MESSAGE_TYPE, designLibraryPreviewFontSchema, designLibraryPreviewMessageSchema, designLibrarySavedStyleMetadataSchema, designLibrarySavedStylesSchema, designLibrarySchema, globalsCssScopeSchema, globalsCssSnapshotSchema, globalsCssTokenSchema, savedColorStyleSchema, savedTextStyleSchema };
@@ -0,0 +1,24 @@
1
+ export type ReploScriptType = "GA4" | "GoogleTagManager" | "Meta" | "TikTok" | "Pinterest" | "Reddit" | "Snapchat" | "Hotjar" | "MicrosoftClarity" | "Triplewhale" | "Segment" | "custom";
2
+ export type ConsentCategory = "necessary" | "analytics" | "marketing" | "preferences" | "sale_of_data";
3
+ export type ConsentMode = "off" | "simple" | "per-category";
4
+ export type ScriptTagDescriptor = {
5
+ kind: "external";
6
+ src: string;
7
+ attributes?: {
8
+ [x: string]: string;
9
+ };
10
+ } | {
11
+ kind: "inline";
12
+ body: string;
13
+ };
14
+ export type ReploScriptEntry = {
15
+ type: "GA4" | "GoogleTagManager" | "Meta" | "TikTok" | "Pinterest" | "Reddit" | "Snapchat" | "Hotjar" | "MicrosoftClarity" | "Triplewhale" | "Segment";
16
+ identifier: string;
17
+ requiredConsent?: ConsentCategory[];
18
+ } | {
19
+ type: "custom";
20
+ id: string;
21
+ requiredConsent: ConsentCategory[];
22
+ src?: string;
23
+ body?: string;
24
+ };
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -1,22 +1,57 @@
1
1
  "use client";
2
2
  import { useEffect, useRef } from "react";
3
+ import { buildGoogleFontsCssUrl } from "../../_vendor/replo-utils/lib/google-fonts.mjs";
3
4
  import { designLibraryPreviewMessageSchema } from "../../_vendor/schemas/designLibrary.mjs";
5
+ const FONT_LINK_DATA_KEY = "reploDesignLibraryFont";
4
6
  function DesignLibraryPreview() {
5
7
  const ownedKeys = useRef(null);
8
+ const ownedFontLinks = useRef(null);
9
+ const ownsDefaultFont = useRef(false);
6
10
  useEffect(() => {
7
11
  const handleMessage = (event) => {
8
12
  const parsed = designLibraryPreviewMessageSchema.safeParse(event.data);
9
13
  if (!parsed.success) return;
10
- const next = parsed.data.cssVars;
14
+ const { cssVars, fonts, defaultFontCssVar } = parsed.data;
11
15
  const root = document.documentElement;
16
+ const nextVars = { ...cssVars };
17
+ for (const font of fonts) {
18
+ nextVars[font.cssVar] = `'${font.family}'`;
19
+ }
12
20
  const owned = ownedKeys.current ?? /* @__PURE__ */ new Set();
13
21
  for (const key of owned) {
14
- if (!(key in next)) root.style.removeProperty(key);
22
+ if (!(key in nextVars)) root.style.removeProperty(key);
15
23
  }
16
- ownedKeys.current = new Set(Object.keys(next));
17
- for (const [key, value] of Object.entries(next)) {
24
+ ownedKeys.current = new Set(Object.keys(nextVars));
25
+ for (const [key, value] of Object.entries(nextVars)) {
18
26
  root.style.setProperty(key, value);
19
27
  }
28
+ const links = ownedFontLinks.current ?? /* @__PURE__ */ new Map();
29
+ const nextHrefs = new Set(
30
+ fonts.map((font) => buildGoogleFontsCssUrl(font.family))
31
+ );
32
+ for (const [href, link] of links) {
33
+ if (!nextHrefs.has(href)) {
34
+ link.remove();
35
+ links.delete(href);
36
+ }
37
+ }
38
+ for (const href of nextHrefs) {
39
+ if (links.has(href)) continue;
40
+ const link = document.createElement("link");
41
+ link.rel = "stylesheet";
42
+ link.href = href;
43
+ link.dataset[FONT_LINK_DATA_KEY] = "";
44
+ document.head.append(link);
45
+ links.set(href, link);
46
+ }
47
+ ownedFontLinks.current = links;
48
+ if (defaultFontCssVar) {
49
+ root.style.setProperty("font-family", `var(${defaultFontCssVar})`);
50
+ ownsDefaultFont.current = true;
51
+ } else if (ownsDefaultFont.current) {
52
+ root.style.removeProperty("font-family");
53
+ ownsDefaultFont.current = false;
54
+ }
20
55
  };
21
56
  window.addEventListener("message", handleMessage);
22
57
  return () => {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../editor/design-library-preview/DesignLibraryPreview.tsx"],
4
- "sourcesContent": ["\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nimport { designLibraryPreviewMessageSchema } from \"schemas/designLibrary\";\n\nexport function DesignLibraryPreview() {\n // NOTE (Ben, 2026-06-03): The custom-property keys we set last time, so on the\n // next message we remove only the ones we own that the new override set drops.\n const ownedKeys = useRef<Set<string> | null>(null);\n\n // eslint-disable-next-line replo/no-use-effect -- Subscribes to window message events from the parent editor.\n useEffect(() => {\n const handleMessage = (event: MessageEvent) => {\n const parsed = designLibraryPreviewMessageSchema.safeParse(event.data);\n if (!parsed.success) return;\n const next = parsed.data.cssVars;\n const root = document.documentElement;\n const owned = ownedKeys.current ?? new Set();\n for (const key of owned) {\n if (!(key in next)) root.style.removeProperty(key);\n }\n ownedKeys.current = new Set(Object.keys(next));\n for (const [key, value] of Object.entries(next)) {\n root.style.setProperty(key, value);\n }\n };\n window.addEventListener(\"message\", handleMessage);\n return () => {\n window.removeEventListener(\"message\", handleMessage);\n };\n }, []);\n\n return null;\n}\n"],
5
- "mappings": ";AAEA,SAAS,WAAW,cAAc;AAElC,SAAS,yCAAyC;AAE3C,SAAS,uBAAuB;AAGrC,QAAM,YAAY,OAA2B,IAAI;AAGjD,YAAU,MAAM;AACd,UAAM,gBAAgB,CAAC,UAAwB;AAC7C,YAAM,SAAS,kCAAkC,UAAU,MAAM,IAAI;AACrE,UAAI,CAAC,OAAO,QAAS;AACrB,YAAM,OAAO,OAAO,KAAK;AACzB,YAAM,OAAO,SAAS;AACtB,YAAM,QAAQ,UAAU,WAAW,oBAAI,IAAI;AAC3C,iBAAW,OAAO,OAAO;AACvB,YAAI,EAAE,OAAO,MAAO,MAAK,MAAM,eAAe,GAAG;AAAA,MACnD;AACA,gBAAU,UAAU,IAAI,IAAI,OAAO,KAAK,IAAI,CAAC;AAC7C,iBAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,IAAI,GAAG;AAC/C,aAAK,MAAM,YAAY,KAAK,KAAK;AAAA,MACnC;AAAA,IACF;AACA,WAAO,iBAAiB,WAAW,aAAa;AAChD,WAAO,MAAM;AACX,aAAO,oBAAoB,WAAW,aAAa;AAAA,IACrD;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AACT;",
4
+ "sourcesContent": ["\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nimport { buildGoogleFontsCssUrl } from \"replo-utils/lib/google-fonts\";\nimport { designLibraryPreviewMessageSchema } from \"schemas/designLibrary\";\n\nconst FONT_LINK_DATA_KEY = \"reploDesignLibraryFont\";\n\nexport function DesignLibraryPreview() {\n // NOTE (Ben, 2026-06-03): The custom-property keys we set last time, so on the\n // next message we remove only the ones we own that the new override set drops.\n const ownedKeys = useRef<Set<string> | null>(null);\n // NOTE (Ben, 2026-06-05): The Google Font <link> hrefs we injected, keyed by\n // href so we de-dupe and remove only our own when the override set changes.\n const ownedFontLinks = useRef<Map<string, HTMLLinkElement> | null>(null);\n // NOTE (Ben, 2026-06-05): Whether we set the document default font-family, so\n // we can clear just that.\n const ownsDefaultFont = useRef(false);\n\n // eslint-disable-next-line replo/no-use-effect -- Subscribes to window message events from the parent editor.\n useEffect(() => {\n const handleMessage = (event: MessageEvent) => {\n const parsed = designLibraryPreviewMessageSchema.safeParse(event.data);\n if (!parsed.success) return;\n const { cssVars, fonts, defaultFontCssVar } = parsed.data;\n const root = document.documentElement;\n\n // --- CSS variable overrides (colors + font role vars) ---\n const nextVars: Record<string, string> = { ...cssVars };\n for (const font of fonts) {\n nextVars[font.cssVar] = `'${font.family}'`;\n }\n const owned = ownedKeys.current ?? new Set();\n for (const key of owned) {\n if (!(key in nextVars)) root.style.removeProperty(key);\n }\n ownedKeys.current = new Set(Object.keys(nextVars));\n for (const [key, value] of Object.entries(nextVars)) {\n root.style.setProperty(key, value);\n }\n\n // --- Google Font stylesheets ---\n const links = ownedFontLinks.current ?? new Map();\n const nextHrefs = new Set(\n fonts.map((font) => buildGoogleFontsCssUrl(font.family)),\n );\n for (const [href, link] of links) {\n if (!nextHrefs.has(href)) {\n link.remove();\n links.delete(href);\n }\n }\n for (const href of nextHrefs) {\n if (links.has(href)) continue;\n const link = document.createElement(\"link\");\n link.rel = \"stylesheet\";\n link.href = href;\n link.dataset[FONT_LINK_DATA_KEY] = \"\";\n document.head.append(link);\n links.set(href, link);\n }\n ownedFontLinks.current = links;\n\n // --- Document default font ---\n if (defaultFontCssVar) {\n root.style.setProperty(\"font-family\", `var(${defaultFontCssVar})`);\n ownsDefaultFont.current = true;\n } else if (ownsDefaultFont.current) {\n root.style.removeProperty(\"font-family\");\n ownsDefaultFont.current = false;\n }\n };\n window.addEventListener(\"message\", handleMessage);\n return () => {\n window.removeEventListener(\"message\", handleMessage);\n };\n }, []);\n\n return null;\n}\n"],
5
+ "mappings": ";AAEA,SAAS,WAAW,cAAc;AAElC,SAAS,8BAA8B;AACvC,SAAS,yCAAyC;AAElD,MAAM,qBAAqB;AAEpB,SAAS,uBAAuB;AAGrC,QAAM,YAAY,OAA2B,IAAI;AAGj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AACA,qBAAe,UAAU;AAGzB,UAAI,mBAAmB;AACrB,aAAK,MAAM,YAAY,eAAe,OAAO,iBAAiB,GAAG;AACjE,wBAAgB,UAAU;AAAA,MAC5B,WAAW,gBAAgB,SAAS;AAClC,aAAK,MAAM,eAAe,aAAa;AACvC,wBAAgB,UAAU;AAAA,MAC5B;AAAA,IACF;AACA,WAAO,iBAAiB,WAAW,aAAa;AAChD,WAAO,MAAM;AACX,aAAO,oBAAoB,WAAW,aAAa;AAAA,IACrD;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AACT;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@replohq/canopy-sdk",
3
- "version": "0.1.3",
3
+ "version": "0.2.1",
4
4
  "description": "Replo Canopy SDK — cart, analytics, data loaders, and editor runtime for agent-built Next.js sites.",
5
5
  "type": "module",
6
6
  "sideEffects": false,