create-marcapony-app 0.5.0 → 0.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-marcapony-app",
3
- "version": "0.5.0",
3
+ "version": "0.6.1",
4
4
  "description": "Scaffold a Marcapony app \u2014 a Worker backed by the Marcapony API, ready for `marcapony app link` and `marcapony app deploy`",
5
5
  "type": "module",
6
6
  "license": "SEE LICENSE IN LICENSE.md",
@@ -14,7 +14,7 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "hono": "^4.13.0",
17
- "marcapony-sdk": "^0.14.0",
17
+ "marcapony-sdk": "^0.15.0",
18
18
  "react": "^19.3.0",
19
19
  "react-dom": "^19.3.0"
20
20
  },
@@ -24,7 +24,7 @@
24
24
  "@types/react-dom": "^19.3.0",
25
25
  "@vitejs/plugin-react": "^5.2.0",
26
26
  "esbuild": "^0.28.0",
27
- "marcapony": "^0.16.0",
27
+ "marcapony": "^0.18.0",
28
28
  "typescript": "^5.9.0",
29
29
  "vite": "^7.3.0"
30
30
  }
@@ -53,6 +53,20 @@ export async function identityOf(
53
53
  return mp.identity(request)
54
54
  }
55
55
 
56
+ /**
57
+ * The website's theme for the interface (`/api/site-theme`), read with the app
58
+ * key so the browser never holds it. Null when the key lacks `site:read` or
59
+ * there is no key (local dev without `bun run link`): the interface then keeps
60
+ * its default look.
61
+ */
62
+ export async function siteThemeResponse(env: MarcaponyBindings): Promise<Response> {
63
+ try {
64
+ return Response.json(await marcaponyFromEnv(env).site.theme())
65
+ } catch {
66
+ return Response.json(null)
67
+ }
68
+ }
69
+
56
70
  /**
57
71
  * Turns a failed call into a JSON answer for the browser. A Marcapony error
58
72
  * keeps its status (401 not signed in, 403 not allowed, 404, 422 invalid
@@ -2,9 +2,11 @@ import { StrictMode } from 'react'
2
2
  import { createRoot } from 'react-dom/client'
3
3
  import { App } from './App'
4
4
  import { followPortalTheme } from './portal-theme'
5
+ import { followSiteTheme } from './site-theme'
5
6
  import './styles.css'
6
7
 
7
8
  followPortalTheme()
9
+ followSiteTheme()
8
10
 
9
11
  const root = document.getElementById('root')
10
12
  if (!root) throw new Error('#root is missing from index.html')
@@ -0,0 +1,71 @@
1
+ import type { SiteTheme } from 'marcapony-sdk'
2
+ import { api } from './api'
3
+
4
+ /**
5
+ * Follow the business's website theme when the app is NOT inside the customer
6
+ * portal (inside it, portal-theme.ts applies the portal's theme instead).
7
+ *
8
+ * The server reads the theme with the app key (`/api/site-theme`, which needs
9
+ * the `site:read` scope) and this writes it onto the same CSS variables
10
+ * `styles.css` reads. Without the scope, or locally without a key, the answer
11
+ * is null and the defaults in `styles.css` stay. Every value is checked before
12
+ * it reaches a style.
13
+ */
14
+
15
+ const COLORS: ReadonlyArray<readonly [keyof SiteTheme['light'], string]> = [
16
+ ['background', '--background'],
17
+ ['foreground', '--foreground'],
18
+ ['card', '--card'],
19
+ ['primary', '--primary'],
20
+ ['primaryForeground', '--primary-foreground'],
21
+ ['muted', '--muted'],
22
+ ['mutedForeground', '--muted-foreground'],
23
+ ['destructive', '--destructive'],
24
+ ['border', '--border'],
25
+ ['ring', '--ring'],
26
+ ]
27
+
28
+ const RGB = /^rgb\(\d{1,3} \d{1,3} \d{1,3}\)$/
29
+ const RADIUS = /^\d+(\.\d+)?(rem|px|em)$/
30
+ const FAMILY = /^[\w\s-]{1,80}$/
31
+ const GOOGLE_FONTS = 'https://fonts.googleapis.com/css2?'
32
+
33
+ export function applySiteTheme(
34
+ theme: SiteTheme,
35
+ root: HTMLElement = document.documentElement,
36
+ ): void {
37
+ const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches
38
+ // A theme with a dark palette follows the visitor; one without keeps its own look.
39
+ const dark = theme.dark ? prefersDark : theme.defaultAppearance === 'dark'
40
+ const palette = (dark ? theme.dark : null) ?? theme.light
41
+
42
+ for (const [key, variable] of COLORS) {
43
+ const value = palette[key]
44
+ if (typeof value === 'string' && RGB.test(value)) root.style.setProperty(variable, value)
45
+ }
46
+ if (theme.radius && RADIUS.test(theme.radius)) root.style.setProperty('--radius', theme.radius)
47
+
48
+ const body = theme.fonts.body
49
+ if (body && FAMILY.test(body)) {
50
+ const url = theme.fontStylesheetUrl
51
+ if (url?.startsWith(GOOGLE_FONTS)) {
52
+ const link = document.createElement('link')
53
+ link.rel = 'stylesheet'
54
+ link.href = url
55
+ document.head.append(link)
56
+ }
57
+ root.style.setProperty('--font-sans', `"${body}", ui-sans-serif, system-ui, sans-serif`)
58
+ }
59
+
60
+ root.dataset.appearance = dark ? 'dark' : 'light'
61
+ root.style.colorScheme = dark ? 'dark' : 'light'
62
+ }
63
+
64
+ export function followSiteTheme(): void {
65
+ if (window.parent !== window) return
66
+ api<SiteTheme | null>('/api/site-theme')
67
+ .then((theme) => {
68
+ if (theme) applySiteTheme(theme)
69
+ })
70
+ .catch(() => {})
71
+ }
@@ -1,8 +1,9 @@
1
1
  :root {
2
2
  color-scheme: light dark;
3
3
  /* Theme tokens. Inside the customer portal, portal-theme.ts replaces them
4
- with the portal's own colours, radius and font (light or dark). Style with
5
- these variables, never fixed colours, so the app follows the portal. */
4
+ with the portal's own colours, radius and font (light or dark); outside
5
+ it, site-theme.ts applies the website's theme. Style with these
6
+ variables, never fixed colours, so the app follows both. */
6
7
  --background: #ffffff;
7
8
  --foreground: #18181b;
8
9
  --card: #ffffff;
@@ -9,15 +9,21 @@ Create the app under **My Apps** with:
9
9
 
10
10
  - **Placement:** public (`placement` is not set in `marcapony.json`)
11
11
  - **Data access:** visitor
12
- - **Scopes:** `products:read`
12
+ - **Scopes:** `products:read`, `site:read`
13
13
  - **Permissions:** none
14
14
 
15
15
  ## Routes
16
16
 
17
- | Route | What it does |
18
- | ------------------- | ---------------------------------- |
19
- | `GET /api/health` | Answers `{ "ok": true }` |
20
- | `GET /api/products` | Up to 12 published products of the account |
17
+ | Route | What it does |
18
+ | --------------------- | ---------------------------------------------- |
19
+ | `GET /api/health` | Answers `{ "ok": true }` |
20
+ | `GET /api/site-theme` | The website's theme, or null without `site:read` |
21
+ | `GET /api/products` | Up to 12 published products of the account |
22
+
23
+ On the site, the app takes the website's colours, radius and font:
24
+ `src/ui/site-theme.ts` reads `/api/site-theme` and writes them onto the CSS
25
+ variables `src/ui/styles.css` uses. Inside the customer portal the portal's
26
+ own theme (`src/ui/portal-theme.ts`) applies instead.
21
27
 
22
28
  ## What to change first
23
29
 
@@ -1,5 +1,10 @@
1
1
  import { Hono } from 'hono'
2
- import { errorResponse, marcaponyFromEnv, type MarcaponyBindings } from './marcapony'
2
+ import {
3
+ errorResponse,
4
+ marcaponyFromEnv,
5
+ siteThemeResponse,
6
+ type MarcaponyBindings,
7
+ } from './marcapony'
3
8
 
4
9
  /**
5
10
  * The app's server. Only `/api/*` reaches this worker — everything else is
@@ -9,6 +14,7 @@ import { errorResponse, marcaponyFromEnv, type MarcaponyBindings } from './marca
9
14
  const app = new Hono<{ Bindings: MarcaponyBindings }>()
10
15
 
11
16
  app.get('/api/health', (c) => c.json({ ok: true }))
17
+ app.get('/api/site-theme', (c) => siteThemeResponse(c.env))
12
18
 
13
19
  /**
14
20
  * Example read. The app key never leaves the server: the browser calls this
@@ -4,6 +4,7 @@ import {
4
4
  errorResponse,
5
5
  identityOf,
6
6
  marcaponyFromEnv,
7
+ siteThemeResponse,
7
8
  type Marcapony,
8
9
  type MarcaponyBindings,
9
10
  } from './marcapony'
@@ -39,6 +40,7 @@ async function customerOf(
39
40
  }
40
41
 
41
42
  app.get('/api/health', (c) => c.json({ ok: true }))
43
+ app.get('/api/site-theme', (c) => siteThemeResponse(c.env))
42
44
 
43
45
  app.get('/api/me', async (c) => {
44
46
  const mp = marcaponyFromEnv(c.env)
@@ -1,6 +1,11 @@
1
1
  import { Hono } from 'hono'
2
2
  import { personasHandler } from 'marcapony-sdk'
3
- import { errorResponse, marcaponyFromEnv, type MarcaponyBindings } from './marcapony'
3
+ import {
4
+ errorResponse,
5
+ marcaponyFromEnv,
6
+ siteThemeResponse,
7
+ type MarcaponyBindings,
8
+ } from './marcapony'
4
9
 
5
10
  /**
6
11
  * A character from the business that chats with visitors. The browser talks
@@ -45,6 +50,7 @@ function personaSlug(env: Bindings): string {
45
50
  }
46
51
 
47
52
  app.get('/api/health', (c) => c.json({ ok: true }))
53
+ app.get('/api/site-theme', (c) => siteThemeResponse(c.env))
48
54
 
49
55
  /** The character the interface shows: name, greeting and starter questions. */
50
56
  app.get('/api/character', async (c) => {
@@ -1,5 +1,11 @@
1
1
  import { Hono } from 'hono'
2
- import { errorResponse, identityOf, marcaponyFromEnv, type MarcaponyBindings } from './marcapony'
2
+ import {
3
+ errorResponse,
4
+ identityOf,
5
+ marcaponyFromEnv,
6
+ siteThemeResponse,
7
+ type MarcaponyBindings,
8
+ } from './marcapony'
3
9
 
4
10
  /**
5
11
  * A game anyone can play; a signed-in customer keeps their best score. Only
@@ -20,6 +26,7 @@ const BEST = 'best'
20
26
  const MAX_SCORE = 500
21
27
 
22
28
  app.get('/api/health', (c) => c.json({ ok: true }))
29
+ app.get('/api/site-theme', (c) => siteThemeResponse(c.env))
23
30
 
24
31
  app.get('/api/score', async (c) => {
25
32
  const mp = marcaponyFromEnv(c.env)
@@ -1,6 +1,11 @@
1
1
  import { Hono, type Context } from 'hono'
2
2
  import type { PaymentLinkLineItem, Product } from 'marcapony-sdk'
3
- import { errorResponse, marcaponyFromEnv, type MarcaponyBindings } from './marcapony'
3
+ import {
4
+ errorResponse,
5
+ marcaponyFromEnv,
6
+ siteThemeResponse,
7
+ type MarcaponyBindings,
8
+ } from './marcapony'
4
9
 
5
10
  /**
6
11
  * A small public shop: the catalog, a cart kept in the browser, and a checkout
@@ -61,6 +66,7 @@ async function cartFingerprint(quantities: Map<string, number>): Promise<string>
61
66
  }
62
67
 
63
68
  app.get('/api/health', (c) => c.json({ ok: true }))
69
+ app.get('/api/site-theme', (c) => siteThemeResponse(c.env))
64
70
 
65
71
  app.get('/api/catalog', async (c) => {
66
72
  const mp = marcaponyFromEnv(c.env)
@@ -4,6 +4,7 @@ import {
4
4
  errorResponse,
5
5
  identityOf,
6
6
  marcaponyFromEnv,
7
+ siteThemeResponse,
7
8
  type Marcapony,
8
9
  type MarcaponyBindings,
9
10
  } from './marcapony'
@@ -51,6 +52,7 @@ async function memberOf(
51
52
  }
52
53
 
53
54
  app.get('/api/health', (c) => c.json({ ok: true }))
55
+ app.get('/api/site-theme', (c) => siteThemeResponse(c.env))
54
56
 
55
57
  app.get('/api/me', async (c) => {
56
58
  const mp = marcaponyFromEnv(c.env)