/** * DOM side of the knob envelope: rebuild the real stack from the inputs. * * It uses the theme package's OWN components — ``, ``, * `IntentContext.Provider` — and never raw `PresetContext.Provider`, so a later * change to Preset's merge order or Surface's LC-78 clamp cannot desync the two * sides. Design: docs/design/node-graph-editor.md 6.1. */ import { type ReactNode, useMemo } from "react"; import { type TamaguiInternalConfig, TamaguiProvider, Theme } from "tamagui"; import type { ThemeName } from "@tamagui/web"; import { IntentContext, Preset, Surface } from "@multiplatform.one/theme"; import { type KnobEnvelope, knobsForEnvelope } from "./envelope"; export interface KnobEnvelopeProviderProps { readonly envelope: KnobEnvelope; /** * Only the DOM/WebView host passes a config: it is a fresh React root with no * provider above it. On web the graph renders inside the app's existing * `TamaguiProvider`, and mounting a second one there would give the subtree a * different token table from the page around it. */ readonly config?: TamaguiInternalConfig; readonly children?: ReactNode; } /** * The theme name this side can actually resolve. * * `envelope.themeName` is the HOST's, and across the bridge the two realms are * different Tamagui builds with different theme tables: the app's config knows * `light_blue`, the DOM bundle's knows whatever `@multiplatform.one/theme` * created. Tamagui does not degrade on a name it cannot find — it THROWS * `Missing theme.` — so an unportable name took the whole canvas down. * * Measured on the iPhone 16e simulator at task 2.7: `render: Missing theme.`, * caught by the entry's error boundary. Design 6.1 assumed the name crossed; * it does not, and the envelope already carries `scheme` for exactly the part * that must survive. * * On WEB there is no `config`, because the graph renders inside the app's own * provider and that provider owns the table — so the name is passed through * untouched and nothing changes for the platform that was already working. * * @returns the name to mount, or undefined to inherit. */ export function resolveEnvelopeThemeName( envelope: Pick, themes?: Readonly>, ): string | undefined { const wanted = envelope.themeName; if (!themes) return wanted || undefined; if (wanted && Object.hasOwn(themes, wanted)) return wanted; if (Object.hasOwn(themes, envelope.scheme)) return envelope.scheme; return undefined; } export function KnobEnvelopeProvider({ envelope, config, children }: KnobEnvelopeProviderProps) { const knobs = useMemo(() => knobsForEnvelope(envelope), [envelope]); const themeName = useMemo( () => resolveEnvelopeThemeName(envelope, config?.themes), [config, envelope], ); // `cascade={false}` starts from `defaultKnobs` and merges `overrides` // (Preset.tsx:57-76), which is exactly "replace the whole knob set with the // host's". A named preset still resolves through `getPreset`, which is what // carries its `intents` map across. let tree: ReactNode = ( {children} ); // An empty name is not a theme: `` would ask Tamagui to // resolve a theme that does not exist and silently inherit the parent. if (themeName) { tree = {tree}; } if (config) { tree = ( {tree} ); } return <>{tree}; }