import type { ComponentType } from 'react'; // Every content component in this library, keyed by the name it is exported // under — the same name the Stackables catalog stores as `react_component`, // and the name a Stackables site config maps each content type / global field // uid onto (see renderer/siteConfig.ts). // // The registry fills itself: every exported content component goes through // `withSectionWidth`, which registers the wrapped component under the display // name it is given, so adding a component to index.ts is all it takes to make // it renderable by name. Layout primitives (PageLayout, PageSection, // ThemeProvider) are not content and never appear here. type AnyComponent = ComponentType; // One registry per page, not per bundle. The `studio` entry is built as its // own bundle and would otherwise get a private copy of this module — an empty // Map that never sees what the main entry registered (the runtime site's // register-components saw every component as missing). Keying it on // globalThis makes every copy of this module, bundled or vendored, share it. const REGISTRY_KEY = Symbol.for('stackable-components.registry'); type RegistryHost = { [REGISTRY_KEY]?: Map }; const host = globalThis as RegistryHost; const registry: Map = host[REGISTRY_KEY] ?? (host[REGISTRY_KEY] = new Map()); export function registerComponent(name: string, component: AnyComponent): void { registry.set(name, component); } /** The component exported under `name`, or undefined when the library has none by that name. */ export function getComponent(name: string | null | undefined): AnyComponent | undefined { return name ? registry.get(name) : undefined; } export function hasComponent(name: string | null | undefined): boolean { return !!name && registry.has(name); } /** Every registered component name, sorted — handy for diagnostics and admin tooling. */ export function componentNames(): string[] { return [...registry.keys()].sort(); } /** A plain-object snapshot of the registry (name → component). */ export function componentRegistry(): Record { const snapshot: Record = {}; registry.forEach((component, name) => { snapshot[name] = component; }); return snapshot; }