@numueg/theme-sdk 0.3.2 → 0.5.0
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/dist/index.cjs +521 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +98 -221
- package/dist/index.d.ts +98 -221
- package/dist/index.mjs +508 -6
- package/dist/index.mjs.map +1 -1
- package/dist/mount-BN2mz_wx.d.ts +225 -0
- package/dist/mount-DH9dz3dq.d.mts +225 -0
- package/dist/validation.cjs +408 -0
- package/dist/validation.cjs.map +1 -0
- package/dist/validation.d.mts +89 -0
- package/dist/validation.d.ts +89 -0
- package/dist/validation.mjs +397 -0
- package/dist/validation.mjs.map +1 -0
- package/dist/verify.cjs +200 -0
- package/dist/verify.cjs.map +1 -0
- package/dist/verify.d.mts +65 -0
- package/dist/verify.d.ts +65 -0
- package/dist/verify.mjs +191 -0
- package/dist/verify.mjs.map +1 -0
- package/package.json +11 -1
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ReactNode, ReactElement } from 'react';
|
|
3
|
+
import { C as Cart, b as Collection, c as Customer, P as Page, e as Product, i as Store } from './entities-6MGANln7.js';
|
|
4
|
+
import { T as ThemeSettingsV3, M as MountResult } from './theme-D0QybTQS.js';
|
|
5
|
+
|
|
6
|
+
interface LocalizationState {
|
|
7
|
+
locale: string;
|
|
8
|
+
direction: "ltr" | "rtl";
|
|
9
|
+
translations: Record<string, string>;
|
|
10
|
+
formatMoney: (amount: number, currency?: string) => string;
|
|
11
|
+
formatDate: (date: string | Date) => string;
|
|
12
|
+
/**
|
|
13
|
+
* Phase 3.7 — locale-aware number formatter. Routes to either
|
|
14
|
+
* Western (1234) or Arab-Indic (١٢٣٤) digits depending on
|
|
15
|
+
* `store.settings.numerals`. Themes calling formatMoney get the
|
|
16
|
+
* same digit choice automatically; this is for raw counts ("12 items").
|
|
17
|
+
*/
|
|
18
|
+
formatNumber: (n: number, options?: Intl.NumberFormatOptions) => string;
|
|
19
|
+
/**
|
|
20
|
+
* Phase 3.6 — switch the active locale.
|
|
21
|
+
*
|
|
22
|
+
* Sets the `numu_locale` cookie and triggers a full page reload so
|
|
23
|
+
* the server-rendered layout picks up the new locale (the storefront
|
|
24
|
+
* resolves locale at SSR time from cookie/query). Returns once the
|
|
25
|
+
* cookie is written; the page navigation cancels any pending React
|
|
26
|
+
* work so callers don't need to await.
|
|
27
|
+
*/
|
|
28
|
+
setLocale: (next: string) => void;
|
|
29
|
+
/**
|
|
30
|
+
* Phase 3.6 — list of locales the store advertises. Empty when the
|
|
31
|
+
* store hasn't configured a multi-locale catalog. Themes use this
|
|
32
|
+
* to decide whether to render the LocaleSwitcher at all.
|
|
33
|
+
*/
|
|
34
|
+
availableLocales: string[];
|
|
35
|
+
}
|
|
36
|
+
declare const ShopContext: react.Context<Store | null>;
|
|
37
|
+
declare const ProductContext: react.Context<Product | null>;
|
|
38
|
+
declare const CollectionContext: react.Context<Collection | null>;
|
|
39
|
+
declare const CartContext: react.Context<{
|
|
40
|
+
cart: Cart;
|
|
41
|
+
addItem: (productId: string, variantId?: string, quantity?: number) => Promise<void>;
|
|
42
|
+
removeItem: (itemId: string) => Promise<void>;
|
|
43
|
+
updateQuantity: (itemId: string, quantity: number) => Promise<void>;
|
|
44
|
+
applyDiscount: (code: string) => Promise<void>;
|
|
45
|
+
removeDiscount: () => Promise<void>;
|
|
46
|
+
updateNote: (note: string) => Promise<void>;
|
|
47
|
+
clearCart: () => Promise<void>;
|
|
48
|
+
loading: boolean;
|
|
49
|
+
} | null>;
|
|
50
|
+
declare const CustomerContext: react.Context<Customer | null>;
|
|
51
|
+
declare const ThemeSettingsContext: react.Context<ThemeSettingsV3 | null>;
|
|
52
|
+
declare const LocalizationContext: react.Context<LocalizationState | null>;
|
|
53
|
+
declare const PageContext: react.Context<Page | null>;
|
|
54
|
+
/**
|
|
55
|
+
* A merchant-managed navigation menu item, exactly as the storefront
|
|
56
|
+
* menus resolver returns it (`GET /storefront/store/{id}/menus`):
|
|
57
|
+
* bilingual `label`, a pre-resolved `url`, and nested `children`.
|
|
58
|
+
*
|
|
59
|
+
* This is the RAW shape the host injects via `NuMuProvider`'s
|
|
60
|
+
* `navigation` prop. `useNavigation(handle)` localizes it to the
|
|
61
|
+
* display-ready `NavigationItem` (a single `title` string for the
|
|
62
|
+
* active locale).
|
|
63
|
+
*/
|
|
64
|
+
interface MenuItemData {
|
|
65
|
+
id: string;
|
|
66
|
+
label: Record<string, string>;
|
|
67
|
+
url: string;
|
|
68
|
+
type?: string | null;
|
|
69
|
+
resource_id?: string | null;
|
|
70
|
+
/**
|
|
71
|
+
* §5 hide-page → hide-nav-link. `false` when the item targets a CMS page
|
|
72
|
+
* (`/pages/<handle>`) that is currently unpublished or deleted. The backend
|
|
73
|
+
* menus resolver annotates it; absent/`true` means visible (back-compat).
|
|
74
|
+
*/
|
|
75
|
+
target_visible?: boolean;
|
|
76
|
+
children?: MenuItemData[];
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Phase 2.4 — navigation menus keyed by handle (`main-menu`, `footer`,
|
|
80
|
+
* plus custom), injected by the host from the storefront resolver so a
|
|
81
|
+
* theme's `useNavigation(handle)` resolves without a client round-trip.
|
|
82
|
+
*
|
|
83
|
+
* Defaults to `{}` — an empty map signals "host provided no menus", at
|
|
84
|
+
* which point `useNavigation` falls back to its own fetch / a theme's
|
|
85
|
+
* `DEFAULT_NAV`. A present-but-handle-absent map means the menu simply
|
|
86
|
+
* doesn't exist (render nothing / fallback), no fetch attempted.
|
|
87
|
+
*/
|
|
88
|
+
declare const NavigationContext: react.Context<Record<string, MenuItemData[]>>;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* `mountTheme(el, ctx, renderApp)` — the canonical V3 bundle entry helper.
|
|
92
|
+
*
|
|
93
|
+
* ## Why this exists
|
|
94
|
+
*
|
|
95
|
+
* Every theme bundle exports `mount(el, ctx): MountResult`. Historically each
|
|
96
|
+
* theme hand-wrote that function, and the wiring it needs is non-trivial and
|
|
97
|
+
* easy to get wrong:
|
|
98
|
+
*
|
|
99
|
+
* 1. **Forward the real catalog.** The host ships the page's products /
|
|
100
|
+
* collections in `ctx.page.data` (home + listing routes) and the product
|
|
101
|
+
* in `ctx.page.data.product` (PDP). A theme that doesn't pass these into
|
|
102
|
+
* `NuMuProvider` (+ wrap the PDP in `<ProductProvider>`) sees
|
|
103
|
+
* `useProducts()` / `useProductOptional()` return empty on a REAL store —
|
|
104
|
+
* so product sections render "No products yet" or fall back to demo data
|
|
105
|
+
* on a stocked merchant. This was the single most common BYOT bug:
|
|
106
|
+
* only bon-younes wired it; the other 13 themes dropped the catalog.
|
|
107
|
+
*
|
|
108
|
+
* 2. **Apply global style tokens.** Merchant-chosen colors/fonts live in
|
|
109
|
+
* `themeSettings.global_settings`; they only paint if the bundle calls
|
|
110
|
+
* `applyGlobalStyleTokens` on its mount root (and resolves font tokens
|
|
111
|
+
* to real stacks + injects the webfont link). Themes that skipped this
|
|
112
|
+
* ignored every color/font picker.
|
|
113
|
+
*
|
|
114
|
+
* 3. **Forward navigation.** `useNavigation(handle)` only resolves the
|
|
115
|
+
* header/footer menus the host pre-resolved if the bundle passes
|
|
116
|
+
* `ctx.navigation` into `NuMuProvider`.
|
|
117
|
+
*
|
|
118
|
+
* 4. **Live-preview + lifecycle.** The customizer streams draft settings via
|
|
119
|
+
* the host's `applyDraft`; the bundle must hold them in state and re-paint
|
|
120
|
+
* the style tokens on every draft. And it must return a `MountResult`
|
|
121
|
+
* (`cleanup` + `applyDraft`) the host's `ByotThemeBoundary` understands.
|
|
122
|
+
*
|
|
123
|
+
* `mountTheme` does all of that once, so a theme's `main.tsx` collapses to:
|
|
124
|
+
*
|
|
125
|
+
* ```tsx
|
|
126
|
+
* import { mountTheme } from "@numueg/theme-sdk";
|
|
127
|
+
* export function mount(el, ctx) {
|
|
128
|
+
* return mountTheme(el, ctx, ({ currentTemplate }) =>
|
|
129
|
+
* <ThemeApp currentTemplate={currentTemplate} />,
|
|
130
|
+
* );
|
|
131
|
+
* }
|
|
132
|
+
* ```
|
|
133
|
+
*
|
|
134
|
+
* The theme owns only its section list (`ThemeApp`). Everything in the list
|
|
135
|
+
* above is handled here — fix it once, every theme benefits.
|
|
136
|
+
*
|
|
137
|
+
* ## Both ctx shapes
|
|
138
|
+
*
|
|
139
|
+
* The host (numu-storefront `ByotThemeBoundary`) passes
|
|
140
|
+
* `{ themeSettings, storeData, page, locale, demo, navigation }`. Older / dev
|
|
141
|
+
* contexts used `{ store, currentTemplate }`. We normalise both so a bundle
|
|
142
|
+
* built against this helper works regardless of which host calls it.
|
|
143
|
+
*/
|
|
144
|
+
|
|
145
|
+
/** Minimal page descriptor the host forwards in the mount context. */
|
|
146
|
+
interface ThemeMountPage {
|
|
147
|
+
type?: string;
|
|
148
|
+
handle?: string;
|
|
149
|
+
title?: string;
|
|
150
|
+
data?: Record<string, unknown>;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* The mount context a host (or dev harness) passes to a bundle's `mount`.
|
|
154
|
+
* Accepts both the current storefront shape (`storeData`/`page`) and the
|
|
155
|
+
* legacy/dev shape (`store`/`currentTemplate`); `mountTheme` normalises them.
|
|
156
|
+
*/
|
|
157
|
+
interface ThemeMountContext {
|
|
158
|
+
storeData?: Store;
|
|
159
|
+
page?: ThemeMountPage;
|
|
160
|
+
store?: Store;
|
|
161
|
+
currentTemplate?: string;
|
|
162
|
+
themeSettings: ThemeSettingsV3;
|
|
163
|
+
initialCart?: Cart;
|
|
164
|
+
customer?: Customer | null;
|
|
165
|
+
locale?: string;
|
|
166
|
+
translations?: Record<string, string>;
|
|
167
|
+
/** AUTHORITATIVE marketplace-preview flag from the host (true only for the
|
|
168
|
+
* catalog "Try theme" preview). Themes with demo-image fallbacks gate on
|
|
169
|
+
* it so a real installed store never shows demo imagery. */
|
|
170
|
+
demo?: boolean;
|
|
171
|
+
/** Store navigation menus keyed by handle, resolved server-side. */
|
|
172
|
+
navigation?: Record<string, MenuItemData[]>;
|
|
173
|
+
/**
|
|
174
|
+
* Host signal that the container already holds server-rendered HTML for
|
|
175
|
+
* this exact ctx (produced via `createApp` from `defineThemeEntry`).
|
|
176
|
+
* `mountTheme` then adopts it with `hydrateRoot` instead of re-rendering
|
|
177
|
+
* from scratch. Ignored when the container is empty, so a host can pass
|
|
178
|
+
* it optimistically and still get a plain client mount on SSR failure.
|
|
179
|
+
*/
|
|
180
|
+
hydrate?: boolean;
|
|
181
|
+
[extra: string]: unknown;
|
|
182
|
+
}
|
|
183
|
+
/** Arguments handed to a theme's render callback on every (re)render. */
|
|
184
|
+
interface ThemeRenderArgs {
|
|
185
|
+
/** Active template key — "home" | "product" | "collection" | "cart" | … */
|
|
186
|
+
currentTemplate: string;
|
|
187
|
+
/** Marketplace-preview flag (see ThemeMountContext.demo). */
|
|
188
|
+
demo: boolean;
|
|
189
|
+
/** The raw host page descriptor (type/handle/data), or null. */
|
|
190
|
+
page: ThemeMountPage | null;
|
|
191
|
+
/** Normalised store record (never undefined). */
|
|
192
|
+
store: Store;
|
|
193
|
+
/** Live theme settings (reflects customizer drafts via applyDraft). */
|
|
194
|
+
themeSettings: ThemeSettingsV3;
|
|
195
|
+
}
|
|
196
|
+
interface DraftHandle {
|
|
197
|
+
applyDraft: (next: ThemeSettingsV3) => void;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Build the canonical theme element tree for a ctx. BOTH render paths go
|
|
201
|
+
* through here — `mountTheme` (client mount/hydrate) and `createApp`
|
|
202
|
+
* (host-side `renderToString`) — so the server markup and the hydration
|
|
203
|
+
* tree are the same React tree by construction. `mountEl` is a prop, not
|
|
204
|
+
* DOM output, so it differing between server (null) and client (the
|
|
205
|
+
* container) cannot cause a hydration mismatch.
|
|
206
|
+
*/
|
|
207
|
+
declare function buildThemeElement(ctx: ThemeMountContext, mountEl: HTMLElement | null, renderApp: (args: ThemeRenderArgs) => ReactNode, ref?: (h: DraftHandle | null) => void): ReactElement;
|
|
208
|
+
/**
|
|
209
|
+
* Mount a V3 theme. Owns the React root, the provider stack (catalog + nav +
|
|
210
|
+
* style tokens), and the live-preview draft cycle. Returns the host-contract
|
|
211
|
+
* `MountResult` (`cleanup` + `applyDraft`).
|
|
212
|
+
*
|
|
213
|
+
* When the host passes `ctx.hydrate === true` and the container already
|
|
214
|
+
* holds server-rendered HTML (produced by this theme's `createApp` with the
|
|
215
|
+
* identical ctx), the tree is adopted via `hydrateRoot` — no re-render, no
|
|
216
|
+
* flash. An empty container downgrades to a plain client mount so hosts can
|
|
217
|
+
* pass the flag optimistically.
|
|
218
|
+
*
|
|
219
|
+
* @param el the host-supplied container element
|
|
220
|
+
* @param ctx the mount context (either host or legacy/dev shape)
|
|
221
|
+
* @param renderApp returns the theme's section tree for the current args
|
|
222
|
+
*/
|
|
223
|
+
declare function mountTheme(el: HTMLElement, ctx: ThemeMountContext, renderApp: (args: ThemeRenderArgs) => ReactNode): MountResult;
|
|
224
|
+
|
|
225
|
+
export { CartContext as C, type LocalizationState as L, type MenuItemData as M, NavigationContext as N, PageContext as P, ShopContext as S, type ThemeMountContext as T, type ThemeRenderArgs as a, CollectionContext as b, CustomerContext as c, LocalizationContext as d, ProductContext as e, type ThemeMountPage as f, ThemeSettingsContext as g, buildThemeElement as h, mountTheme as m };
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ReactNode, ReactElement } from 'react';
|
|
3
|
+
import { C as Cart, b as Collection, c as Customer, P as Page, e as Product, i as Store } from './entities-6MGANln7.mjs';
|
|
4
|
+
import { T as ThemeSettingsV3, M as MountResult } from './theme-D0QybTQS.mjs';
|
|
5
|
+
|
|
6
|
+
interface LocalizationState {
|
|
7
|
+
locale: string;
|
|
8
|
+
direction: "ltr" | "rtl";
|
|
9
|
+
translations: Record<string, string>;
|
|
10
|
+
formatMoney: (amount: number, currency?: string) => string;
|
|
11
|
+
formatDate: (date: string | Date) => string;
|
|
12
|
+
/**
|
|
13
|
+
* Phase 3.7 — locale-aware number formatter. Routes to either
|
|
14
|
+
* Western (1234) or Arab-Indic (١٢٣٤) digits depending on
|
|
15
|
+
* `store.settings.numerals`. Themes calling formatMoney get the
|
|
16
|
+
* same digit choice automatically; this is for raw counts ("12 items").
|
|
17
|
+
*/
|
|
18
|
+
formatNumber: (n: number, options?: Intl.NumberFormatOptions) => string;
|
|
19
|
+
/**
|
|
20
|
+
* Phase 3.6 — switch the active locale.
|
|
21
|
+
*
|
|
22
|
+
* Sets the `numu_locale` cookie and triggers a full page reload so
|
|
23
|
+
* the server-rendered layout picks up the new locale (the storefront
|
|
24
|
+
* resolves locale at SSR time from cookie/query). Returns once the
|
|
25
|
+
* cookie is written; the page navigation cancels any pending React
|
|
26
|
+
* work so callers don't need to await.
|
|
27
|
+
*/
|
|
28
|
+
setLocale: (next: string) => void;
|
|
29
|
+
/**
|
|
30
|
+
* Phase 3.6 — list of locales the store advertises. Empty when the
|
|
31
|
+
* store hasn't configured a multi-locale catalog. Themes use this
|
|
32
|
+
* to decide whether to render the LocaleSwitcher at all.
|
|
33
|
+
*/
|
|
34
|
+
availableLocales: string[];
|
|
35
|
+
}
|
|
36
|
+
declare const ShopContext: react.Context<Store | null>;
|
|
37
|
+
declare const ProductContext: react.Context<Product | null>;
|
|
38
|
+
declare const CollectionContext: react.Context<Collection | null>;
|
|
39
|
+
declare const CartContext: react.Context<{
|
|
40
|
+
cart: Cart;
|
|
41
|
+
addItem: (productId: string, variantId?: string, quantity?: number) => Promise<void>;
|
|
42
|
+
removeItem: (itemId: string) => Promise<void>;
|
|
43
|
+
updateQuantity: (itemId: string, quantity: number) => Promise<void>;
|
|
44
|
+
applyDiscount: (code: string) => Promise<void>;
|
|
45
|
+
removeDiscount: () => Promise<void>;
|
|
46
|
+
updateNote: (note: string) => Promise<void>;
|
|
47
|
+
clearCart: () => Promise<void>;
|
|
48
|
+
loading: boolean;
|
|
49
|
+
} | null>;
|
|
50
|
+
declare const CustomerContext: react.Context<Customer | null>;
|
|
51
|
+
declare const ThemeSettingsContext: react.Context<ThemeSettingsV3 | null>;
|
|
52
|
+
declare const LocalizationContext: react.Context<LocalizationState | null>;
|
|
53
|
+
declare const PageContext: react.Context<Page | null>;
|
|
54
|
+
/**
|
|
55
|
+
* A merchant-managed navigation menu item, exactly as the storefront
|
|
56
|
+
* menus resolver returns it (`GET /storefront/store/{id}/menus`):
|
|
57
|
+
* bilingual `label`, a pre-resolved `url`, and nested `children`.
|
|
58
|
+
*
|
|
59
|
+
* This is the RAW shape the host injects via `NuMuProvider`'s
|
|
60
|
+
* `navigation` prop. `useNavigation(handle)` localizes it to the
|
|
61
|
+
* display-ready `NavigationItem` (a single `title` string for the
|
|
62
|
+
* active locale).
|
|
63
|
+
*/
|
|
64
|
+
interface MenuItemData {
|
|
65
|
+
id: string;
|
|
66
|
+
label: Record<string, string>;
|
|
67
|
+
url: string;
|
|
68
|
+
type?: string | null;
|
|
69
|
+
resource_id?: string | null;
|
|
70
|
+
/**
|
|
71
|
+
* §5 hide-page → hide-nav-link. `false` when the item targets a CMS page
|
|
72
|
+
* (`/pages/<handle>`) that is currently unpublished or deleted. The backend
|
|
73
|
+
* menus resolver annotates it; absent/`true` means visible (back-compat).
|
|
74
|
+
*/
|
|
75
|
+
target_visible?: boolean;
|
|
76
|
+
children?: MenuItemData[];
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Phase 2.4 — navigation menus keyed by handle (`main-menu`, `footer`,
|
|
80
|
+
* plus custom), injected by the host from the storefront resolver so a
|
|
81
|
+
* theme's `useNavigation(handle)` resolves without a client round-trip.
|
|
82
|
+
*
|
|
83
|
+
* Defaults to `{}` — an empty map signals "host provided no menus", at
|
|
84
|
+
* which point `useNavigation` falls back to its own fetch / a theme's
|
|
85
|
+
* `DEFAULT_NAV`. A present-but-handle-absent map means the menu simply
|
|
86
|
+
* doesn't exist (render nothing / fallback), no fetch attempted.
|
|
87
|
+
*/
|
|
88
|
+
declare const NavigationContext: react.Context<Record<string, MenuItemData[]>>;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* `mountTheme(el, ctx, renderApp)` — the canonical V3 bundle entry helper.
|
|
92
|
+
*
|
|
93
|
+
* ## Why this exists
|
|
94
|
+
*
|
|
95
|
+
* Every theme bundle exports `mount(el, ctx): MountResult`. Historically each
|
|
96
|
+
* theme hand-wrote that function, and the wiring it needs is non-trivial and
|
|
97
|
+
* easy to get wrong:
|
|
98
|
+
*
|
|
99
|
+
* 1. **Forward the real catalog.** The host ships the page's products /
|
|
100
|
+
* collections in `ctx.page.data` (home + listing routes) and the product
|
|
101
|
+
* in `ctx.page.data.product` (PDP). A theme that doesn't pass these into
|
|
102
|
+
* `NuMuProvider` (+ wrap the PDP in `<ProductProvider>`) sees
|
|
103
|
+
* `useProducts()` / `useProductOptional()` return empty on a REAL store —
|
|
104
|
+
* so product sections render "No products yet" or fall back to demo data
|
|
105
|
+
* on a stocked merchant. This was the single most common BYOT bug:
|
|
106
|
+
* only bon-younes wired it; the other 13 themes dropped the catalog.
|
|
107
|
+
*
|
|
108
|
+
* 2. **Apply global style tokens.** Merchant-chosen colors/fonts live in
|
|
109
|
+
* `themeSettings.global_settings`; they only paint if the bundle calls
|
|
110
|
+
* `applyGlobalStyleTokens` on its mount root (and resolves font tokens
|
|
111
|
+
* to real stacks + injects the webfont link). Themes that skipped this
|
|
112
|
+
* ignored every color/font picker.
|
|
113
|
+
*
|
|
114
|
+
* 3. **Forward navigation.** `useNavigation(handle)` only resolves the
|
|
115
|
+
* header/footer menus the host pre-resolved if the bundle passes
|
|
116
|
+
* `ctx.navigation` into `NuMuProvider`.
|
|
117
|
+
*
|
|
118
|
+
* 4. **Live-preview + lifecycle.** The customizer streams draft settings via
|
|
119
|
+
* the host's `applyDraft`; the bundle must hold them in state and re-paint
|
|
120
|
+
* the style tokens on every draft. And it must return a `MountResult`
|
|
121
|
+
* (`cleanup` + `applyDraft`) the host's `ByotThemeBoundary` understands.
|
|
122
|
+
*
|
|
123
|
+
* `mountTheme` does all of that once, so a theme's `main.tsx` collapses to:
|
|
124
|
+
*
|
|
125
|
+
* ```tsx
|
|
126
|
+
* import { mountTheme } from "@numueg/theme-sdk";
|
|
127
|
+
* export function mount(el, ctx) {
|
|
128
|
+
* return mountTheme(el, ctx, ({ currentTemplate }) =>
|
|
129
|
+
* <ThemeApp currentTemplate={currentTemplate} />,
|
|
130
|
+
* );
|
|
131
|
+
* }
|
|
132
|
+
* ```
|
|
133
|
+
*
|
|
134
|
+
* The theme owns only its section list (`ThemeApp`). Everything in the list
|
|
135
|
+
* above is handled here — fix it once, every theme benefits.
|
|
136
|
+
*
|
|
137
|
+
* ## Both ctx shapes
|
|
138
|
+
*
|
|
139
|
+
* The host (numu-storefront `ByotThemeBoundary`) passes
|
|
140
|
+
* `{ themeSettings, storeData, page, locale, demo, navigation }`. Older / dev
|
|
141
|
+
* contexts used `{ store, currentTemplate }`. We normalise both so a bundle
|
|
142
|
+
* built against this helper works regardless of which host calls it.
|
|
143
|
+
*/
|
|
144
|
+
|
|
145
|
+
/** Minimal page descriptor the host forwards in the mount context. */
|
|
146
|
+
interface ThemeMountPage {
|
|
147
|
+
type?: string;
|
|
148
|
+
handle?: string;
|
|
149
|
+
title?: string;
|
|
150
|
+
data?: Record<string, unknown>;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* The mount context a host (or dev harness) passes to a bundle's `mount`.
|
|
154
|
+
* Accepts both the current storefront shape (`storeData`/`page`) and the
|
|
155
|
+
* legacy/dev shape (`store`/`currentTemplate`); `mountTheme` normalises them.
|
|
156
|
+
*/
|
|
157
|
+
interface ThemeMountContext {
|
|
158
|
+
storeData?: Store;
|
|
159
|
+
page?: ThemeMountPage;
|
|
160
|
+
store?: Store;
|
|
161
|
+
currentTemplate?: string;
|
|
162
|
+
themeSettings: ThemeSettingsV3;
|
|
163
|
+
initialCart?: Cart;
|
|
164
|
+
customer?: Customer | null;
|
|
165
|
+
locale?: string;
|
|
166
|
+
translations?: Record<string, string>;
|
|
167
|
+
/** AUTHORITATIVE marketplace-preview flag from the host (true only for the
|
|
168
|
+
* catalog "Try theme" preview). Themes with demo-image fallbacks gate on
|
|
169
|
+
* it so a real installed store never shows demo imagery. */
|
|
170
|
+
demo?: boolean;
|
|
171
|
+
/** Store navigation menus keyed by handle, resolved server-side. */
|
|
172
|
+
navigation?: Record<string, MenuItemData[]>;
|
|
173
|
+
/**
|
|
174
|
+
* Host signal that the container already holds server-rendered HTML for
|
|
175
|
+
* this exact ctx (produced via `createApp` from `defineThemeEntry`).
|
|
176
|
+
* `mountTheme` then adopts it with `hydrateRoot` instead of re-rendering
|
|
177
|
+
* from scratch. Ignored when the container is empty, so a host can pass
|
|
178
|
+
* it optimistically and still get a plain client mount on SSR failure.
|
|
179
|
+
*/
|
|
180
|
+
hydrate?: boolean;
|
|
181
|
+
[extra: string]: unknown;
|
|
182
|
+
}
|
|
183
|
+
/** Arguments handed to a theme's render callback on every (re)render. */
|
|
184
|
+
interface ThemeRenderArgs {
|
|
185
|
+
/** Active template key — "home" | "product" | "collection" | "cart" | … */
|
|
186
|
+
currentTemplate: string;
|
|
187
|
+
/** Marketplace-preview flag (see ThemeMountContext.demo). */
|
|
188
|
+
demo: boolean;
|
|
189
|
+
/** The raw host page descriptor (type/handle/data), or null. */
|
|
190
|
+
page: ThemeMountPage | null;
|
|
191
|
+
/** Normalised store record (never undefined). */
|
|
192
|
+
store: Store;
|
|
193
|
+
/** Live theme settings (reflects customizer drafts via applyDraft). */
|
|
194
|
+
themeSettings: ThemeSettingsV3;
|
|
195
|
+
}
|
|
196
|
+
interface DraftHandle {
|
|
197
|
+
applyDraft: (next: ThemeSettingsV3) => void;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Build the canonical theme element tree for a ctx. BOTH render paths go
|
|
201
|
+
* through here — `mountTheme` (client mount/hydrate) and `createApp`
|
|
202
|
+
* (host-side `renderToString`) — so the server markup and the hydration
|
|
203
|
+
* tree are the same React tree by construction. `mountEl` is a prop, not
|
|
204
|
+
* DOM output, so it differing between server (null) and client (the
|
|
205
|
+
* container) cannot cause a hydration mismatch.
|
|
206
|
+
*/
|
|
207
|
+
declare function buildThemeElement(ctx: ThemeMountContext, mountEl: HTMLElement | null, renderApp: (args: ThemeRenderArgs) => ReactNode, ref?: (h: DraftHandle | null) => void): ReactElement;
|
|
208
|
+
/**
|
|
209
|
+
* Mount a V3 theme. Owns the React root, the provider stack (catalog + nav +
|
|
210
|
+
* style tokens), and the live-preview draft cycle. Returns the host-contract
|
|
211
|
+
* `MountResult` (`cleanup` + `applyDraft`).
|
|
212
|
+
*
|
|
213
|
+
* When the host passes `ctx.hydrate === true` and the container already
|
|
214
|
+
* holds server-rendered HTML (produced by this theme's `createApp` with the
|
|
215
|
+
* identical ctx), the tree is adopted via `hydrateRoot` — no re-render, no
|
|
216
|
+
* flash. An empty container downgrades to a plain client mount so hosts can
|
|
217
|
+
* pass the flag optimistically.
|
|
218
|
+
*
|
|
219
|
+
* @param el the host-supplied container element
|
|
220
|
+
* @param ctx the mount context (either host or legacy/dev shape)
|
|
221
|
+
* @param renderApp returns the theme's section tree for the current args
|
|
222
|
+
*/
|
|
223
|
+
declare function mountTheme(el: HTMLElement, ctx: ThemeMountContext, renderApp: (args: ThemeRenderArgs) => ReactNode): MountResult;
|
|
224
|
+
|
|
225
|
+
export { CartContext as C, type LocalizationState as L, type MenuItemData as M, NavigationContext as N, PageContext as P, ShopContext as S, type ThemeMountContext as T, type ThemeRenderArgs as a, CollectionContext as b, CustomerContext as c, LocalizationContext as d, ProductContext as e, type ThemeMountPage as f, ThemeSettingsContext as g, buildThemeElement as h, mountTheme as m };
|