@akinon/pz-theme 2.0.159-beta.0 → 2.0.159-rc.16

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.
@@ -1,31 +1,16 @@
1
1
  # Custom blocks
2
2
 
3
- A brand developer writes a React component in their own repo and decides, at the
4
- point of use, what a merchant may change about it. The component appears in the
5
- theme editor's Add Block picker under **Brand blocks**, its declared fields are
6
- editable from the properties panel, and it can open a region the merchant fills
7
- with ordinary editor content.
8
-
9
- Nothing here is a schema kept alongside the component. The declaration *is* the
10
- usage.
11
-
12
- **Writing a whole area rather than one element inside it?** See
13
- [`README-sections.md`](./README-sections.md). A custom *section* owns its own
14
- band on the page: it gets the section root panel (geometry and content width),
15
- and it names the children that go inside it rather than opening a hole. A custom
16
- *block* is the right shape for one element the merchant drops into a band they
17
- composed.
18
-
19
- **Want a complete, commented example?** See `examples/header/` at the root of
20
- the pz-next repository: one header written at six levels, from basic to
21
- advanced, each building on the previous one. They are reference only — not
22
- shipped in the npm package and never imported by an app.
3
+ A brand developer writes a React component in their own repo and decides, at the point of use, what a merchant may change about it. The component appears in the theme editor's Add Block picker under **Brand blocks**, its declared fields are editable from the properties panel, and it can open a region the merchant fills with ordinary editor content.
4
+
5
+ Nothing here is a schema kept alongside the component. The declaration _is_ the usage.
6
+
7
+ **Writing a whole area rather than one element inside it?** See [`README-sections.md`](./README-sections.md). A custom _section_ owns its own band on the page: it gets the section root panel (geometry and content width), and it names the children that go inside it rather than opening a hole. A custom _block_ is the right shape for one element the merchant drops into a band they composed.
8
+
9
+ **Want a complete, commented example?** See `examples/header/` at the root of the pz-next repository: one header written at six levels, from basic to advanced, each building on the previous one. They are reference only — not shipped in the npm package and never imported by an app.
23
10
 
24
11
  ## The shape
25
12
 
26
- A "Complete the look" row for a product page — the merchant retypes its
27
- heading, chooses how many products it shows, drops a campaign banner beside it,
28
- and restyles the product cards. One block, all four mechanisms.
13
+ A "Complete the look" row for a product page — the merchant retypes its heading, chooses how many products it shows, drops a campaign banner beside it, and restyles the product cards. One block, all four mechanisms.
29
14
 
30
15
  ```tsx
31
16
  // src/theme-blocks/complete-the-look.tsx
@@ -73,19 +58,28 @@ function CompleteTheLook() {
73
58
  export default defineCustomBlock(CompleteTheLook, {
74
59
  label: 'Complete the look',
75
60
  settings: [
76
- { key: 'productCount', type: 'slider', label: 'How many products',
77
- defaultValue: 4, min: 2, max: 8 },
78
- { key: 'heading', type: 'text', label: 'Heading',
79
- defaultValue: 'Complete the look', editable: true }
61
+ {
62
+ key: 'productCount',
63
+ type: 'slider',
64
+ label: 'How many products',
65
+ defaultValue: 4,
66
+ min: 2,
67
+ max: 8
68
+ },
69
+ {
70
+ key: 'heading',
71
+ type: 'text',
72
+ label: 'Heading',
73
+ defaultValue: 'Complete the look',
74
+ editable: true
75
+ }
80
76
  ],
81
77
  slots: [{ name: 'campaign', label: 'Campaign banner' }],
82
78
  parts: { card: { states: ['hover'] }, price: {} }
83
79
  });
84
80
  ```
85
81
 
86
- The product data is the developer's business — it is fetched in the component
87
- and the merchant never sees it as a control. What the merchant gets is the
88
- heading, the count, the banner region and the card styling.
82
+ The product data is the developer's business — it is fetched in the component and the merchant never sees it as a control. What the merchant gets is the heading, the count, the banner region and the card styling.
89
83
 
90
84
  ```ts
91
85
  // src/theme-blocks/index.ts
@@ -101,51 +95,32 @@ import './configure-page-context';
101
95
  import '../../theme-blocks';
102
96
  ```
103
97
 
104
- The block name lives in the record passed to `registerCustomBlocks`, never in
105
- `Component.name`: a production build minifies function names, so a block
106
- authored as `CompleteTheLook` would register as `t` on the storefront and as
107
- `CompleteTheLook` in dev, and one composition would render in one build and be an
108
- unknown type in the other.
98
+ The block name lives in the record passed to `registerCustomBlocks`, never in `Component.name`: a production build minifies function names, so a block authored as `CompleteTheLook` would register as `t` on the storefront and as `CompleteTheLook` in dev, and one composition would render in one build and be an unknown type in the other.
109
99
 
110
100
  ## Import it from the client root only
111
101
 
112
- Not from the server layout as well. `registerCustomBlocks` is a client module
113
- because it reaches the renderer registry, which imports every block module — and
114
- most of those call hooks without a directive of their own, because the only path
115
- that has ever reached them is a client one. Importing it from a server layout
116
- fails the brand's build outright.
102
+ Not from the server layout as well. `registerCustomBlocks` is a client module because it reaches the renderer registry, which imports every block module — and most of those call hooks without a directive of their own, because the only path that has ever reached them is a client one. Importing it from a server layout fails the brand's build outright.
117
103
 
118
- Client-only registration loses nothing: `ThemeBlock` is the single production
119
- reader of the registry and is itself a client module, the server-side CSS
120
- generator never consults it, and SSR still resolves custom renderers because
121
- Next renders client components on the server through the client graph.
104
+ Client-only registration loses nothing: `ThemeBlock` is the single production reader of the registry and is itself a client module, the server-side CSS generator never consults it, and SSR still resolves custom renderers because Next renders client components on the server through the client graph.
122
105
 
123
- Use the deep path `@akinon/pz-theme/src/custom`, not the package root — the root
124
- also re-exports `ThemePlaceholder`, which imports `server-only`.
106
+ Use the deep path `@akinon/pz-theme/src/custom`, not the package root — the root also re-exports `ThemePlaceholder`, which imports `server-only`.
125
107
 
126
108
  ## `ThemeEditable`
127
109
 
128
110
  Makes one wrapped element merchant-editable. The wrapped tag decides the control:
129
111
 
130
112
  | Wrapped element | Control |
131
- |---|---|
113
+ | --- | --- |
132
114
  | `<img>` | image picker |
133
115
  | `<h1>`–`<h6>`, `<p>`, `<span>`, `<strong>` | text box |
134
116
  | `<a>` | two controls — link text and href |
135
117
  | `<video>` | file picker |
136
118
 
137
- The authored JSX is the default value: `<ThemeEditable name="heading"><h2>Complete
138
- the look</h2></ThemeEditable>` declares a text field whose default is
139
- `Complete the look`, because there is nowhere else for a developer to state it.
119
+ The authored JSX is the default value: `<ThemeEditable name="heading"><h2>Complete the look</h2></ThemeEditable>` declares a text field whose default is `Complete the look`, because there is nowhere else for a developer to state it.
140
120
 
141
- At render the merchant's value is applied by re-rendering the child through
142
- React — never by writing to the DOM. Three shapes are refused rather than
143
- guessed, and the message says which: a custom component instead of a host
144
- element, a bare expression with no element, and an ambiguous multi-child
145
- element. `type=` is the explicit escape.
121
+ At render the merchant's value is applied by re-rendering the child through React — never by writing to the DOM. Three shapes are refused rather than guessed, and the message says which: a custom component instead of a host element, a bare expression with no element, and an ambiguous multi-child element. `type=` is the explicit escape.
146
122
 
147
- The authored text is written in the store's default language. Its other
148
- languages go in `translations`:
123
+ The authored text is written in the store's default language. Its other languages go in `translations`:
149
124
 
150
125
  ```tsx
151
126
  <ThemeEditable name="label" translations={{ en: 'MENU' }}>
@@ -153,63 +128,33 @@ languages go in `translations`:
153
128
  </ThemeEditable>
154
129
  ```
155
130
 
156
- The editor is told `MENÜ` only. An English page shows `MENU` for as long as
157
- the field still says `MENÜ`, including the copy the editor seeds when the
158
- block is placed. Once the merchant types their own text, it shows as typed.
159
- This is the same rule as a per-language `useThemeSetting` default (below); a
160
- setting read for the same key, such as an accessible name, can share one
161
- constant with it.
131
+ The editor is told `MENÜ` only. An English page shows `MENU` for as long as the field still says `MENÜ`, including the copy the editor seeds when the block is placed. Once the merchant types their own text, it shows as typed. This is the same rule as a per-language `useThemeSetting` default (below); a setting read for the same key, such as an accessible name, can share one constant with it.
162
132
 
163
133
  ## Links and the page's language
164
134
 
165
- Storefront paths are stored and authored without a locale: `/yatak-odasi/`,
166
- never `/en/yatak-odasi/`. On an English page that bare path is the Turkish
167
- site, so a link that goes out as written takes the shopper out of English. A
168
- visible `next/link` also prefetches it, which makes the middleware set
169
- `pz-locale=tr`, and every request after that comes back Turkish. You do not
170
- write the locale yourself; there are three places it goes on for you:
135
+ Storefront paths are stored and authored without a locale: `/yatak-odasi/`, never `/en/yatak-odasi/`. On an English page that bare path is the Turkish site, so a link that goes out as written takes the shopper out of English. A visible `next/link` also prefetches it, which makes the middleware set `pz-locale=tr`, and every request after that comes back Turkish. You do not write the locale yourself; there are three places it goes on for you:
171
136
 
172
137
  | What you render | What to use |
173
- |---|---|
138
+ | --- | --- |
174
139
  | The `<a>` that `ThemeEditable` wraps | Nothing: the locale is added to its href, whether the merchant set it or you did |
175
140
  | Any other link | `ThemeLink` in place of `next/link`, with the same props |
176
141
  | A bare string: a plain `<a>`, `router.push`, a url read off commerce data | `const localize = useLocalizeHref();` then `localize(url)` |
177
142
 
178
- `ThemeLink` prefetches nothing unless you pass `prefetch`. Next otherwise
179
- fetches a link's page the moment it scrolls into view, and because the locale
180
- lives in a cookie that any request to an unprefixed path rewrites, one link
181
- that slips past these rules would switch the whole session's language without
182
- anyone clicking it.
183
-
184
- All three come from `@akinon/pz-theme/src/custom`. An `<a>` nested deeper
185
- inside a `ThemeEditable` is not touched — that one is a link of yours, so it
186
- takes the row it belongs to above.
187
-
188
- A full URL to this same storefront counts as a path, `www.` or not: merchants
189
- paste the address bar, and `https://www.zsazsazsu.com.tr/yeni-urunler/` on an
190
- English page becomes `/en/yeni-urunler/` — which also lets the router take it
191
- without a page load. Addresses with no localized copy stay as they are: `/api`
192
- and `/_next`, and anything ending in a file (`/files/kvkk.pdf`,
193
- `/manifest.json`). So do external, `#hash`, `?query` and empty hrefs, and the
194
- OAuth `/{provider}/login/` routes.
195
-
196
- No locale is added when the URL strategy hides the page's own: the default
197
- locale under `hide-default-locale`, every locale under `subdomain` and
198
- `hide-all-locales`. To compare an address with the page's own, localize the
199
- stored side rather than stripping the locale off the current one:
200
- `localize(node.url) === pathname`.
143
+ `ThemeLink` prefetches nothing unless you pass `prefetch`. Next otherwise fetches a link's page the moment it scrolls into view, and because the locale lives in a cookie that any request to an unprefixed path rewrites, one link that slips past these rules would switch the whole session's language without anyone clicking it.
144
+
145
+ All three come from `@akinon/pz-theme/src/custom`. An `<a>` nested deeper inside a `ThemeEditable` is not touched — that one is a link of yours, so it takes the row it belongs to above.
146
+
147
+ A full URL to this same storefront counts as a path, `www.` or not: merchants paste the address bar, and `https://www.zsazsazsu.com.tr/yeni-urunler/` on an English page becomes `/en/yeni-urunler/` — which also lets the router take it without a page load. Addresses with no localized copy stay as they are: `/api` and `/_next`, and anything ending in a file (`/files/kvkk.pdf`, `/manifest.json`). So do external, `#hash`, `?query` and empty hrefs, and the OAuth `/{provider}/login/` routes.
148
+
149
+ No locale is added when the URL strategy hides the page's own: the default locale under `hide-default-locale`, every locale under `subdomain` and `hide-all-locales`. To compare an address with the page's own, localize the stored side rather than stripping the locale off the current one: `localize(node.url) === pathname`.
201
150
 
202
151
  ## `useThemeSetting`
203
152
 
204
- For values with no on-screen representation — how many products to fetch, which
205
- category, whether a carousel auto-advances, which side an image sits on. Without
206
- it every variation becomes a separate block in the picker.
153
+ For values with no on-screen representation — how many products to fetch, which category, whether a carousel auto-advances, which side an image sits on. Without it every variation becomes a separate block in the picker.
207
154
 
208
- React's own rule that hooks are called unconditionally is doing useful work
209
- here: the language enforces that the declaration set is static.
155
+ React's own rule that hooks are called unconditionally is doing useful work here: the language enforces that the declaration set is static.
210
156
 
211
- A `text` default can be written per language, and the hook still returns one
212
- string, the entry for the page's locale:
157
+ A `text` default can be written per language, and the hook still returns one string, the entry for the page's locale:
213
158
 
214
159
  ```tsx
215
160
  const label = useThemeSetting('label', {
@@ -219,97 +164,83 @@ const label = useThemeSetting('label', {
219
164
  });
220
165
  ```
221
166
 
222
- The editor is only told the default language's entry, so a static `settings`
223
- declaration keeps that one plain string (`defaultValue: 'MENÜ'`). The
224
- translation applies for as long as the stored value is still that default,
225
- including the copy the editor seeds when the block is placed. Once the
226
- merchant types their own text, it shows as typed in every language. A text
227
- the merchant can translate themselves needs the `localetext` control, which a
228
- custom setting cannot declare yet: this package refuses the type (see
229
- `collect.ts`) because the editor stores such a value as a locale map with no
230
- breakpoint key, and the editor refuses it in a custom catalog for the same
231
- reason.
167
+ The editor is only told the default language's entry, so a static `settings` declaration keeps that one plain string (`defaultValue: 'MENÜ'`). The translation applies for as long as the stored value is still that default, including the copy the editor seeds when the block is placed. Once the merchant types their own text, it shows as typed in every language. A text the merchant can translate themselves needs the `localetext` control, which a custom setting cannot declare yet: this package refuses the type (see `collect.ts`) because the editor stores such a value as a locale map with no breakpoint key, and the editor refuses it in a custom catalog for the same reason.
168
+
169
+ A `richtext` setting gives the merchant the editor's rich text control and comes back as one HTML string for the page's language, already passed through `sanitizeRichText` (a DOM-free allowlist: paragraphs, headings, lists, emphasis, colour spans and safe links). The control stores the default language's text as a plain string and a locale map (`{ tr: '<p>…</p>', en: '<p>…</p>' }`) once another language has text of its own; the hook reads the page's language, then the default language, then the plain string, so a language the merchant has not written shows the default one. Render it with `dangerouslySetInnerHTML`; the default is written as HTML too:
170
+
171
+ ```tsx
172
+ const html = useThemeSetting('body', {
173
+ type: 'richtext',
174
+ label: 'Body',
175
+ default: '<p>Sipariş <strong>bugün</strong> kapınızda.</p>'
176
+ });
177
+
178
+ <div dangerouslySetInnerHTML={{ __html: html }} />;
179
+ ```
232
180
 
233
181
  ## `ThemeSlot`
234
182
 
235
- A merchant-fillable region. A block may open several, and each is filled
236
- independently.
237
-
238
- **Each slot is a real child node.** A declared slot materializes as one
239
- reserved-type child of the custom block (`custom-slot`, carrying its name in
240
- `properties.slotName`), and `<ThemeSlot name="campaign" />` renders THAT node's
241
- children. That is what makes several slots possible without touching how
242
- insertion is addressed: the editor already inserts at `(placeholderId,
243
- sectionId, parentBlockId, index)`, and a slot that is a node is simply the
244
- parent. A block declaring three slots gets three rows in the layer tree.
245
-
246
- `<ThemeSlot />` with no name opens the block's single slot whatever it is
247
- called, so every block written against the original single-slot API keeps
248
- working unchanged. A block declaring several NAMED slots has nothing for a bare
249
- slot to resolve to, and it is refused with the names listed. Two
250
- `<ThemeSlot name="campaign" />` in one component are a real duplicate — both resolve
251
- to the same node — so the second is refused: its children would render twice and
252
- two wrappers would answer to one block id.
253
-
254
- A slot whose node does not exist yet is not an error: it draws the empty band in
255
- the designer and renders nothing live. The editor creates the nodes when the
256
- block is inserted and again on every theme load, so a block already sitting in a
257
- composition gets the rows for a slot the component gained later — insert alone
258
- would leave those regions unreachable, because the merchant's only route into a
259
- region is the add-block affordance on its tree row.
260
-
261
- Children sitting directly on the block (the pre-node shape) are the unnamed
262
- slot's, and only the unnamed slot's. The node wins that lookup once it exists,
263
- so the editor MOVES those children into the unnamed node when it creates it
264
- rather than appending the node beside them — appending would leave live content
265
- saved, listed in the tree, and drawn nowhere. A block whose type declares only
266
- named slots has nothing to move them into, so they stay where they are and stop
267
- rendering, which is what changing the component means.
183
+ A merchant-fillable region. A block may open several, and each is filled independently.
184
+
185
+ **Each slot is a real child node.** A declared slot materializes as one reserved-type child of the custom block (`custom-slot`, carrying its name in `properties.slotName`), and `<ThemeSlot name="campaign" />` renders THAT node's children. That is what makes several slots possible without touching how insertion is addressed: the editor already inserts at `(placeholderId, sectionId, parentBlockId, index)`, and a slot that is a node is simply the parent. A block declaring three slots gets three rows in the layer tree.
186
+
187
+ `<ThemeSlot />` with no name opens the block's single slot whatever it is called, so every block written against the original single-slot API keeps working unchanged. A block declaring several NAMED slots has nothing for a bare slot to resolve to, and it is refused with the names listed. Two `<ThemeSlot name="campaign" />` in one component are a real duplicate — both resolve to the same node — so the second is refused: its children would render twice and two wrappers would answer to one block id.
188
+
189
+ A slot whose node does not exist yet is not an error: it draws the empty band in the designer and renders nothing live. The editor creates the nodes when the block is inserted and again on every theme load, so a block already sitting in a composition gets the rows for a slot the component gained later — insert alone would leave those regions unreachable, because the merchant's only route into a region is the add-block affordance on its tree row.
190
+
191
+ Children sitting directly on the block (the pre-node shape) are the unnamed slot's, and only the unnamed slot's. The node wins that lookup once it exists, so the editor MOVES those children into the unnamed node when it creates it rather than appending the node beside them — appending would leave live content saved, listed in the tree, and drawn nowhere. A block whose type declares only named slots has nothing to move them into, so they stay where they are and stop rendering, which is what changing the component means.
268
192
 
269
193
  ### Narrowing what a slot takes
270
194
 
271
195
  ```ts
272
196
  slots: [
273
- { name: 'campaign', label: 'Campaign banner', accepts: ['image', 'text'], max: 2 },
197
+ {
198
+ name: 'campaign',
199
+ label: 'Campaign banner',
200
+ accepts: ['image', 'text'],
201
+ max: 2
202
+ },
274
203
  { name: 'sizeGuide', label: 'Size guide' }
275
- ]
204
+ ];
276
205
  ```
277
206
 
278
- `accepts` limits the block types the merchant may put in the region and `max`
279
- limits how many; omit either for "anything" and "as many as you like". The
280
- editor enforces both where a block is WRITTEN into the composition, not only
281
- where it is offered, so a drag from elsewhere in the tree obeys them too.
207
+ `accepts` limits the block types the merchant may put in the region and `max` limits how many; omit either for "anything" and "as many as you like". The editor enforces both where a block is WRITTEN into the composition, not only where it is offered, so a drag from elsewhere in the tree obeys them too.
282
208
 
283
- Neither is read by pz-theme — the renderer draws whatever the composition holds
284
- — but both are checked at declare time, because they are refused on arrival and
285
- the editor refuses the WHOLE catalog rather than one slot. `accepts: []` (a
286
- region nothing could ever go in) and a `max` that is not a positive whole number
287
- would otherwise empty a brand's entire palette, with the only explanation in a
288
- repo the developer is not working in.
209
+ Neither is read by pz-theme — the renderer draws whatever the composition holds — but both are checked at declare time, because they are refused on arrival and the editor refuses the WHOLE catalog rather than one slot. `accepts: []` (a region nothing could ever go in) and a `max` that is not a positive whole number would otherwise empty a brand's entire palette, with the only explanation in a repo the developer is not working in.
289
210
 
290
211
  ## Detecting the editor canvas
291
212
 
292
- `useCustomBlockContext()` returns the context the block or section renders in,
293
- or `null` outside one. Its `isDesigner` is what to branch on when the canvas
294
- needs something the live site must not do — keep a menu open so the merchant
295
- can style it, or stop a link from navigating on click.
213
+ `useCustomBlockContext()` returns the context the block or section renders in, or `null` outside one. Its `isDesigner` is what to branch on when the canvas needs something the live site must not do — keep a menu open so the merchant can style it, or stop a link from navigating on click.
296
214
 
297
215
  ```ts
298
216
  const isDesigner = useCustomBlockContext()?.isDesigner ?? false;
299
217
  ```
300
218
 
301
- `isDesigner` means "the editing affordances are on", not "the editor is
302
- attached". The editor's **Use preview** toggle turns it off while the editor
303
- stays open, so the canvas behaves like the live site: whatever you held open
304
- closes and links navigate again.
219
+ `isDesigner` means "the editing affordances are on", not "the editor is attached". The editor's **Use preview** toggle turns it off while the editor stays open, so the canvas behaves like the live site: whatever you held open closes and links navigate again.
220
+
221
+ ## Reading the row a block is placed in
222
+
223
+ A block dropped into a list's row layout (the child a designer marked as the
224
+ row template of an order list, a product grid, an address list, …) renders once
225
+ per row. The product lists open the product scope `useThemeProduct()` reads;
226
+ every list also opens a row scope that carries the row itself:
227
+
228
+ ```ts
229
+ import { useThemeRowScope } from '@akinon/pz-theme/src/blocks/row-scope';
230
+
231
+ const scope = useThemeRowScope();
232
+ ```
233
+
234
+ Inside a row it returns `{ listType, row, index }`, for example
235
+ `listType: 'order-list'` with the order's `number` and `items` on `row`; anywhere
236
+ else it returns `null`. `row` is the same record the row's bindings resolve
237
+ against, so whatever a text block can bind to (`item.number`), code can read.
238
+ Import it from that deep path for the same reason as `useThemePageContext`: the
239
+ package root is not importable from a client module.
305
240
 
306
241
  ## Reporting what happened
307
242
 
308
- A custom unit can report a moment — a strip was seen, a lookbook was opened, a
309
- tab was changed — and the brand decides in its own repo what that moment means
310
- to GA4, Insider or its own service. The theme never names an analytics event and
311
- never pushes to a tag manager, which is why one renderer can serve brands whose
312
- schemas disagree.
243
+ A custom unit can report a moment — a strip was seen, a lookbook was opened, a tab was changed — and the brand decides in its own repo what that moment means to GA4, Insider or its own service. The theme never names an analytics event and never pushes to a tag manager, which is why one renderer can serve brands whose schemas disagree.
313
244
 
314
245
  ```tsx
315
246
  import { useThemeSignal } from '@akinon/pz-theme/src/custom';
@@ -327,9 +258,7 @@ const LookbookStrip = () => {
327
258
  };
328
259
  ```
329
260
 
330
- For the "first time it becomes true" case — a section seen, a promo shown — use
331
- the latching form. A `null` key means "not ready yet" and reports nothing, which
332
- is what stops a unit reporting before the data it describes has resolved:
261
+ For the "first time it becomes true" case — a section seen, a promo shown — use the latching form. A `null` key means "not ready yet" and reports nothing, which is what stops a unit reporting before the data it describes has resolved:
333
262
 
334
263
  ```tsx
335
264
  useThemeSignalOnce({
@@ -339,8 +268,7 @@ useThemeSignalOnce({
339
268
  });
340
269
  ```
341
270
 
342
- Outside React — a module-level bus, a delegated document listener — call the
343
- primitive directly:
271
+ Outside React — a module-level bus, a delegated document listener — call the primitive directly:
344
272
 
345
273
  ```ts
346
274
  import { emitThemeSignal } from '@akinon/pz-theme/src/custom';
@@ -365,69 +293,42 @@ useEffect(
365
293
 
366
294
  Four things the emitter does so a call site does not have to:
367
295
 
368
- - **It is silent while the editor is attached, browse mode included.** It reads
369
- the editor handshake flag itself, so do NOT wrap a report in an `isDesigner`
370
- check — that prop means "the editing affordances are on", goes false under
371
- **Use preview**, and a report guarded on it reports a designer's clicks as a
372
- shopper's.
373
- - **`{ key, once: true }` is the latch**, reset on client-side navigation. A
374
- repeat inside the same page view is a no-op.
375
- - **A ceiling** — 24 per key, 200 per page view. An autoplaying carousel would
376
- otherwise emit continuously, billed to whatever the brand forwards to.
377
- - **The payload is closed to flat scalars** and sanitized at emit. A product
378
- object, a DOM node or shopper-typed text is dropped with a dev warning, which
379
- is how the no-PII rule is enforced rather than reviewed.
380
-
381
- A brand's own moment must be namespaced `custom.<something>`, so it can never
382
- collide with a name this package adds later and a bridge can tell the two apart.
383
- The package's own names are listed in `THEME_SIGNAL_NAMES`.
296
+ - **It is silent while the editor is attached, browse mode included.** It reads the editor handshake flag itself, so do NOT wrap a report in an `isDesigner` check — that prop means "the editing affordances are on", goes false under **Use preview**, and a report guarded on it reports a designer's clicks as a shopper's.
297
+ - **`{ key, once: true }` is the latch**, reset on client-side navigation. A repeat inside the same page view is a no-op.
298
+ - **A ceiling** — 24 per key, 200 per page view. An autoplaying carousel would otherwise emit continuously, billed to whatever the brand forwards to.
299
+ - **The payload is closed to flat scalars** and sanitized at emit. A product object, a DOM node or shopper-typed text is dropped with a dev warning, which is how the no-PII rule is enforced rather than reviewed.
300
+
301
+ A brand's own moment must be namespaced `custom.<something>`, so it can never collide with a name this package adds later and a bridge can tell the two apart. The package's own names are listed in `THEME_SIGNAL_NAMES`.
384
302
 
385
303
  ## Styling parts
386
304
 
387
- The `field-<key>` parts come free from `ThemeEditable`. Everything else a block
388
- draws is offered to the properties panel only if it is DECLARED:
305
+ The `field-<key>` parts come free from `ThemeEditable`. Everything else a block draws is offered to the properties panel only if it is DECLARED:
389
306
 
390
307
  ```ts
391
- parts: ['row', 'tile'] // the cheap form
392
- parts: { row: { states: ['hover', 'open'] } } // when appearance depends on state
308
+ parts: ['row', 'tile']; // the cheap form
309
+ parts: {
310
+ row: {
311
+ states: ['hover', 'open'];
312
+ }
313
+ } // when appearance depends on state
393
314
  ```
394
315
 
395
- Declare the same name you stamp with `partAttrs('row')`, or the panel offers a
396
- control nothing on the page answers to. Only states the renderer can actually
397
- match are accepted (`PART_STYLE_STATES` in `utils/part-styles.ts`) — a state
398
- with no selector emits CSS that can never apply, so it is refused with the
399
- available list. The `field-` prefix is reserved and cannot be declared.
316
+ Declare the same name you stamp with `partAttrs('row')`, or the panel offers a control nothing on the page answers to. Only states the renderer can actually match are accepted (`PART_STYLE_STATES` in `utils/part-styles.ts`) — a state with no selector emits CSS that can never apply, so it is refused with the available list. The `field-` prefix is reserved and cannot be declared.
400
317
 
401
318
  ## Declare `settings` and `slots`
402
319
 
403
- Runtime collection exists as a fallback, but it only runs on a type's first
404
- render and never re-announces — so a block that declares nothing reaches the
405
- editor with an empty panel until the editor is reloaded. Declare them.
320
+ Runtime collection exists as a fallback, but it only runs on a type's first render and never re-announces — so a block that declares nothing reaches the editor with an empty panel until the editor is reloaded. Declare them.
406
321
 
407
322
  ## What the merchant still cannot restyle
408
323
 
409
- A field's own `field-<key>` part carries no states: declaring one is refused,
410
- because the `field-` prefix belongs to `ThemeEditable`. To give a field element
411
- a hover appearance, wrap it in an element you stamp and declare yourself.
324
+ A field's own `field-<key>` part carries no states: declaring one is refused, because the `field-` prefix belongs to `ThemeEditable`. To give a field element a hover appearance, wrap it in an element you stamp and declare yourself.
412
325
 
413
326
  ## Limits
414
327
 
415
- Forty types per build, twenty-four settings, eight slots and thirty-two parts
416
- per type, and a 256 KB catalog. The whole catalog is posted to the editor on
417
- every placeholder announce, which is what the caps are protecting.
328
+ Forty types per build, twenty-four settings, eight slots and thirty-two parts per type, and a 256 KB catalog. The whole catalog is posted to the editor on every placeholder announce, which is what the caps are protecting.
418
329
 
419
- Every one of them is refused at declare time, because the editor's answer to an
420
- over-cap catalog is to drop ALL of it — a single block over the parts cap costs
421
- the build every block in the palette. The one cap a declaration cannot fully
422
- guarantee is the parts cap: an undeclared `ThemeEditable` discovered at render
423
- time adds a `field-<key>` part after registration counted them, and if that
424
- would cross the cap the part is dropped with a warning rather than the catalog
425
- being put over. The field keeps its control in the Content tab and loses only
426
- its styling group.
330
+ Every one of them is refused at declare time, because the editor's answer to an over-cap catalog is to drop ALL of it — a single block over the parts cap costs the build every block in the palette. The one cap a declaration cannot fully guarantee is the parts cap: an undeclared `ThemeEditable` discovered at render time adds a `field-<key>` part after registration counted them, and if that would cross the cap the part is dropped with a warning rather than the catalog being put over. The field keeps its control in the Content tab and loses only its styling group.
427
331
 
428
332
  ## Failure behaviour
429
333
 
430
- A composition naming a type this build does not register renders nothing on the
431
- live site and a labelled card in the designer, and the editor shows the block as
432
- an orphan row rather than dropping it — its stored properties survive a save
433
- untouched, so a type that comes back finds its content where it left it.
334
+ A composition naming a type this build does not register renders nothing on the live site and a labelled card in the designer, and the editor shows the block as an orphan row rather than dropping it — its stored properties survive a save untouched, so a type that comes back finds its content where it left it.
@@ -367,3 +367,100 @@ describe('useThemeSetting — invalid declarations throw in development', () =>
367
367
  ).toThrow(/not one of the editor's control types/);
368
368
  });
369
369
  });
370
+
371
+ describe('useThemeSetting — richtext', () => {
372
+ it('reads the breakpoint’s html and strips what the allowlist refuses', () => {
373
+ const { result } = renderHook(
374
+ () =>
375
+ useThemeSetting('body', {
376
+ type: 'richtext',
377
+ label: 'Body',
378
+ default: '<p>default</p>'
379
+ }),
380
+ {
381
+ wrapper: wrapperFor(
382
+ contextValue({
383
+ currentBreakpoint: 'mobile',
384
+ properties: {
385
+ body: {
386
+ desktop: '<p>desktop</p>',
387
+ mobile:
388
+ '<p onclick="x()">mobile <script>x</script><b>bold</b></p>'
389
+ }
390
+ }
391
+ })
392
+ )
393
+ }
394
+ );
395
+
396
+ expect(result.current).toBe('<p>mobile <b>bold</b></p>');
397
+ });
398
+
399
+ it('sanitizes the authored default too', () => {
400
+ const { result } = renderHook(
401
+ () =>
402
+ useThemeSetting('body', {
403
+ type: 'richtext',
404
+ label: 'Body',
405
+ default: '<p><img src=x onerror=alert(1)>safe</p>'
406
+ }),
407
+ { wrapper: wrapperFor(contextValue()) }
408
+ );
409
+
410
+ expect(result.current).toBe('<p>safe</p>');
411
+ });
412
+
413
+ describe('written per language by the rich text control', () => {
414
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
415
+ const hooks = require('@akinon/next/hooks');
416
+ let localizationSpy: jest.SpyInstance;
417
+
418
+ const onPage = (locale: string) => {
419
+ const original = hooks.useLocalization;
420
+ localizationSpy = jest
421
+ .spyOn(hooks, 'useLocalization')
422
+ .mockImplementation(() => ({
423
+ ...original(),
424
+ locale,
425
+ defaultLocaleValue: 'tr',
426
+ locales: [{ value: 'tr' }, { value: 'en' }]
427
+ }));
428
+ };
429
+
430
+ afterEach(() => localizationSpy?.mockRestore());
431
+
432
+ const read = (body: unknown) =>
433
+ renderHook(
434
+ () =>
435
+ useThemeSetting('body', {
436
+ type: 'richtext',
437
+ label: 'Body',
438
+ default: '<p>default</p>'
439
+ }),
440
+ { wrapper: wrapperFor(contextValue({ properties: { body } })) }
441
+ ).result.current;
442
+
443
+ it('reads the page’s language out of the locale map and sanitizes it', () => {
444
+ onPage('tr');
445
+
446
+ expect(
447
+ read({
448
+ en: '<p>old</p>',
449
+ tr: '<p>sss <b>bold</b><script>x()</script></p>'
450
+ })
451
+ ).toBe('<p>sss <b>bold</b></p>');
452
+ });
453
+
454
+ it('falls back to the default language’s entry', () => {
455
+ onPage('en');
456
+
457
+ expect(read({ tr: '<p>türkçe</p>' })).toBe('<p>türkçe</p>');
458
+ });
459
+
460
+ it('still reads a breakpoint map written in the default language', () => {
461
+ onPage('tr');
462
+
463
+ expect(read({ desktop: '<p>desktop</p>' })).toBe('<p>desktop</p>');
464
+ });
465
+ });
466
+ });
@@ -42,9 +42,7 @@ import type { CustomSettingDescriptor, CustomSlotDescriptor } from './types';
42
42
  * through the one function below.
43
43
  */
44
44
  export type ThemeSettingChoice =
45
- | string
46
- | number
47
- | { label?: string; value: unknown };
45
+ string | number | { label?: string; value: unknown };
48
46
 
49
47
  /**
50
48
  * Normalizes the shorthand into the `{label, value}` pairs the editor's select
@@ -80,8 +78,8 @@ export const normalizeSettingChoices = (
80
78
 
81
79
  /**
82
80
  * Why a control type is refused, named per type. "Unsupported" tells a
83
- * developer nothing actionable; each of these three fails for its own reason
84
- * and each has a different way out.
81
+ * developer nothing actionable; each of these fails for its own reason and
82
+ * each has a different way out.
85
83
  */
86
84
  const UNSUPPORTED_TYPE_REASON: Record<string, string> = {
87
85
  multiselect:
@@ -90,10 +88,7 @@ const UNSUPPORTED_TYPE_REASON: Record<string, string> = {
90
88
  'Use "select" and store one value until a per-type resolver exists',
91
89
  localetext:
92
90
  'the editor stores a localetext value as a locale map with no "desktop" key, so getResponsiveValue ' +
93
- 'falls back and the block would read its authored default forever. Use "text" and localize in the component',
94
- richtext:
95
- 'there is no safe renderer for it here — the sanitizer this repo owns parses by assigning to innerHTML ' +
96
- 'on a live element, and pz-theme renders on the server where there is no document at all. Use "text"'
91
+ 'falls back and the block would read its authored default forever. Use "text" and localize in the component'
97
92
  };
98
93
 
99
94
  /** The reason a setting key is refused, or null. Shared by the declared and the collected path. */