// `stackable-components/studio` — registers this library's components with // Contentstack Studio, so a site can build Studio sections and templates out of // them. Its own entry: it imports `@contentstack/studio-react` (an optional // peer), and a site that doesn't use Studio never loads it. // // Registration needs two things the site already has: the Studio SDK // initialised (`studioSdk.init`, which the site does once at boot) and the // components loaded — importing the main entry registers every one of them in // this library's own registry, and this module looks them up there by name. // // What each registration says: `type` is the component's Contentstack global // field uid (what a Stackables site config keys components on), `component` is // the library export, `props` is the global field's schema mapped to Studio's // prop types (see ./props), `styles` the Design-panel groups every component // gets (see ./design), `sections` the palette group (" · Components"; // the library's atoms and layouts register beside them, see ./atoms and // ./layouts), `wrap: false` because every component's own wrapper // (withSectionWidth) already takes Studio's `studioAttributes`, `className` // and the text style groups. import React, { type ComponentType } from 'react'; import { registerComponents } from '@contentstack/studio-react'; import { getComponent } from '../registry'; import type { ContentModelField } from '../types'; import { STACKABLES_STYLE_GROUPS, type StackablesDesignInput, registerStackablesDesign } from './design'; import { STUDIO_ATOMS, type StudioPaletteEntry } from './atoms'; import { STUDIO_LAYOUTS } from './layouts'; import { studioPropsFromSchema } from './props'; export { studioPropsFromSchema } from './props'; export type { StudioProp, StudioProps } from './props'; export { STACKABLES_DESIGN_CLASSES, STACKABLES_STYLE_GROUPS, registerStackablesDesign, stackablesDesignTokens } from './design'; export type { StackablesDesignInput } from './design'; export { STUDIO_ATOMS } from './atoms'; export type { StudioPaletteEntry } from './atoms'; export { STUDIO_LAYOUTS } from './layouts'; /** One component to register — what a site knows about it from its catalog or its stack. */ export interface StudioComponentManifestEntry { /** Registered as Studio's `type`: the component's content model (global field) uid. */ id: string; /** The library export name (`Faq`, `HeroBanner`) — the site config's `components` value. */ importAs: string; /** Shown in Studio's component list; defaults to `importAs`. */ name?: string; /** Palette group; defaults to "Stackables". */ category?: string; description?: string; /** The global field's schema — its fields become the component's Studio props. */ schema?: ContentModelField[] | null; thumbnailUrl?: string; } export interface RegisterStackablesComponentsOptions { /** Components not exported by this library (a site's own, mapped in its overrides) — looked up here first, by export name. */ components?: Record; /** The same, keyed by content model uid (how a runtime site's overrides map them) — wins over everything. */ componentsByUid?: Record; /** * The site's theme and typography (what ThemeProvider renders with), for * Studio's design tokens: the Design panel then offers the site's colours * and fonts. Left out, the tokens are the library defaults — the design * classes and style groups are registered either way. */ design?: StackablesDesignInput | null; /** * The palette name — the project's, usually. Studio groups the component * list by it: " · Atoms", " · Layout" (the library's own, * see ./atoms and ./layouts) and " · Components" (this manifest's * components). Defaults to "Stackables". */ palette?: string; /** Register the library's atoms and layouts too (default true). */ atoms?: boolean; /** Called for a manifest entry no component exists for; defaults to a console warning. */ onMissing?: (entry: StudioComponentManifestEntry) => void; } const DEFAULT_PALETTE = 'Stackables'; /** A single style group for an atom or layout: the element itself, every section. */ const ELEMENT_STYLES = { default: { displayName: 'Element', styleSections: 'all' as const } }; function paletteConfigs(entries: StudioPaletteEntry[], section: string) { return entries.map((entry) => ({ type: entry.type, component: entry.component, displayName: entry.displayName, description: entry.description, sections: [section], thumbnailUrl: entry.thumbnailUrl, layerIcon: entry.layerIcon, wrap: false as const, props: entry.props, styles: ELEMENT_STYLES, })); } /** * Registers the library's atoms (heading, text, button, image…) and layouts * (container, stack, grid, columns…) with Studio, under " · Atoms" * and " · Layout". registerStackablesComponents does this on its * own; call it directly only for a site that registers no catalog components. */ export function registerStackablesPalette(palette: string = DEFAULT_PALETTE): void { registerComponents([ ...paletteConfigs(STUDIO_ATOMS, `${palette} · Atoms`), ...paletteConfigs(STUDIO_LAYOUTS, `${palette} · Layout`), ] as Parameters[0]); } /** * A file field is registered as an `imageurl` prop, so a bound asset reaches * the component as its URL string — but the component reads the asset object * a Stackables page gives it (`cover_image?.url`). This hands it `{ url }` for * a string, and leaves an object (a static asset picked in Studio) alone. */ function withAssetObjects(Component: ComponentType>, fileFields: string[]): ComponentType> { function WithAssetObjects(props: Record) { const next = { ...props }; for (const uid of fileFields) { const value = next[uid]; if (typeof value === 'string') next[uid] = value ? { url: value, title: '' } : null; } return React.createElement(Component, next); } WithAssetObjects.displayName = Component.displayName || Component.name; return WithAssetObjects; } /** * Registers every entry of `manifest` with Contentstack Studio, the site's * design tokens and classes, and the library's atoms and layouts with it. Entries whose component this library (or * `options.components`) doesn't have are skipped, and Studio itself skips a * `type` that is already registered, so calling this again with the same * manifest (a re-render, another route) is harmless. * * Returns the ids it registered. */ export function registerStackablesComponents( manifest: StudioComponentManifestEntry[], options: RegisterStackablesComponentsOptions = {}, ): string[] { registerStackablesDesign(options.design ?? {}); const palette = options.palette?.trim() || DEFAULT_PALETTE; if (options.atoms !== false) registerStackablesPalette(palette); const registered: string[] = []; const configs = []; for (const entry of manifest ?? []) { if (!entry?.id || !entry.importAs) continue; const own = (options.componentsByUid?.[entry.id] as never) ?? (options.components?.[entry.importAs] as never); const component = own ?? getComponent(entry.importAs); if (!component) { (options.onMissing ?? warnMissing)(entry); continue; } // A library component reads asset objects; a site's own component takes // the URL string its renderer hands it, which is what Studio binds too. const fileFields = own ? [] : (entry.schema ?? []).filter((field) => field.data_type === 'file' && !field.multiple).map((field) => field.uid); configs.push({ type: entry.id, component: fileFields.length ? withAssetObjects(component, fileFields) : component, displayName: entry.name || entry.importAs, description: entry.description, sections: [`${palette} · Components`], thumbnailUrl: entry.thumbnailUrl, wrap: false as const, props: studioPropsFromSchema(entry.schema), styles: STACKABLES_STYLE_GROUPS, }); registered.push(entry.id); } if (configs.length) registerComponents(configs as Parameters[0]); return registered; } function warnMissing(entry: StudioComponentManifestEntry): void { if (typeof console !== 'undefined') { console.warn(`[stackables/studio] no component named "${entry.importAs}" for "${entry.id}" — not registered with Studio`); } } /** * The manifest for a site that reads its composition from the stack at * runtime: the site config's component map (global field uid → export name) * joined with the stack's global fields (title and schema). A component in the * map without a global field in the stack is registered with no props. */ export function studioManifestFromSite( components: Record | null | undefined, globalFields: Array<{ uid: string; title?: string; description?: string; schema?: ContentModelField[] }> | null | undefined, ): StudioComponentManifestEntry[] { const byUid = new Map((globalFields ?? []).map((field) => [field.uid, field])); return Object.entries(components ?? {}).map(([id, importAs]) => { const field = byUid.get(id); return { id, importAs, name: field?.title || importAs, description: field?.description, schema: field?.schema ?? null }; }); }