Files
ipal-kit/src/modules/block-library/registry.tsx
T
Radosław SmolińskiandClaude Opus 5.5 2d1cc0e77d Block library: 15 blocks, registry, primitives, templates, manifest, eject
- @intecion/ipal-kit/blocks: libraryBlocks() opt-in, one builder per block, field
  builders, option merging, blockLibraryManifest, faqItemsFromLayout
- @intecion/ipal-kit/blocks/rsc: createBlockRegistry, the block components and UI
  primitives (token utilities only; next/image and next/link passed in)
- @intecion/ipal-kit/templates: 10 page templates as createSeeder data
- createSeeder: { $form: title } references
- ipal-blocks eject <block>
- tests: options, rendering and a11y per block, Payload + seeding, GraphQL, admin
  wording, schema snapshot (versioning), eject imports
- shorter admin labels: link "New tab", table of contents "Show contents"

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-29 14:05:47 +01:00

110 lines
5.3 KiB
TypeScript

import type { ComponentType } from 'react'
import type { BlockComponentMap } from '../blocks/types.js'
import type { BlockContent, BlockContext, BlockUi } from './context.js'
import type { LibraryBlockSlug } from './types.js'
import type { CMSLinkProps } from './ui/CMSLink.js'
import type { PartClasses } from './ui/cx.js'
import type { MediaProps } from './ui/Media.js'
import type { SectionProps } from './ui/Section.js'
import { ContactBlock } from './blocks/contact/Component.js'
import { CtaBlock } from './blocks/cta/Component.js'
import { EntryListBlock } from './blocks/entryList/Component.js'
import { FaqBlock } from './blocks/faq/Component.js'
import { FeatureGridBlock } from './blocks/featureGrid/Component.js'
import { GalleryBlock } from './blocks/gallery/Component.js'
import { HeroBlock } from './blocks/hero/Component.js'
import { LogosBlock } from './blocks/logos/Component.js'
import { PricingBlock } from './blocks/pricing/Component.js'
import { RichTextBlock } from './blocks/richText/Component.js'
import { StatsBlock } from './blocks/stats/Component.js'
import { StepsBlock } from './blocks/steps/Component.js'
import { TeamBlock } from './blocks/team/Component.js'
import { TestimonialsBlock } from './blocks/testimonials/Component.js'
import { TextMediaBlock } from './blocks/textMedia/Component.js'
import { buttonClass } from './ui/Button.js'
import { Card } from './ui/Card.js'
import { CMSLink } from './ui/CMSLink.js'
import { Container } from './ui/Container.js'
import { Media } from './ui/Media.js'
import { Section, SECTION_BACKGROUNDS } from './ui/Section.js'
import { SectionHeader } from './ui/SectionHeader.js'
/** The plugin's component for each library block, by default slug. */
export const LIBRARY_BLOCK_COMPONENTS = {
contact: ContactBlock,
cta: CtaBlock,
entryList: EntryListBlock,
faq: FaqBlock,
featureGrid: FeatureGridBlock,
gallery: GalleryBlock,
hero: HeroBlock,
logos: LogosBlock,
pricing: PricingBlock,
richText: RichTextBlock,
stats: StatsBlock,
steps: StepsBlock,
team: TeamBlock,
testimonials: TestimonialsBlock,
textMedia: TextMediaBlock,
} as const
export type BlockRegistryOptions = {
/** Section backgrounds (`settings.background` values) → classes, merged over the defaults. */
backgrounds?: Record<string, string>
/** Classes per block part, replacing that part's defaults: `{ faq: { item: 'border-b border-border' } }`. */
classNames?: Partial<Record<LibraryBlockSlug, PartClasses>>
/** Replace a block's component. It gets the same props (data, locale, isFirst, ui, ctx, classNames). */
components?: Partial<Record<LibraryBlockSlug, ComponentType<never>>>
/** The project's own blocks, in the same registry. */
extra?: BlockComponentMap
/** Turn on only these library blocks. Default: all 15 (an unused entry costs nothing). */
only?: readonly LibraryBlockSlug[]
/** Renamed slugs (BlockOptions `slug`), so the registry key matches the stored blockType. */
slugs?: Partial<Record<LibraryBlockSlug, string>>
/** Swap UI primitives for every library block (e.g. the project's own Media). */
ui?: Partial<BlockUi>
} & Omit<BlockContext, 'content' | 'lightbox'> & Partial<Pick<BlockContext, 'lightbox'>>
/**
* The `blockType → component` map for RenderBlocks / createPageRoute, with the library
* blocks bound to the project's lib/content and options:
*
* // src/blocks/registry.ts
* export const blockRegistry = createBlockRegistry(content, { sendForm, extra: { timeline: Timeline } })
*
* Everything is bound on the server, so no function crosses to the client.
*/
export function createBlockRegistry(content: BlockContent, options: BlockRegistryOptions = {}): BlockComponentMap {
const { backgrounds, classNames = {}, components = {}, extra = {}, only, slugs = {}, ui: own = {}, ...rest } = options
const ctx: BlockContext = { ...rest, content, lightbox: rest.lightbox ?? true }
const ui = buildUi(ctx, own, backgrounds)
const map: BlockComponentMap = {}
for (const slug of only ?? (Object.keys(LIBRARY_BLOCK_COMPONENTS) as LibraryBlockSlug[])) {
const Block = (components[slug] ?? LIBRARY_BLOCK_COMPONENTS[slug]) as ComponentType<Record<string, unknown>>
const bound = (props: Record<string, unknown>) => <Block {...props} classNames={classNames[slug]} ctx={ctx} ui={ui} />
bound.displayName = `Library(${slug})`
map[slugs[slug] ?? slug] = bound
}
return { ...map, ...extra }
}
/** The plugin's primitives, with lib/content's resolveLink and the project's buttons and backgrounds bound in. */
function buildUi(ctx: BlockContext, own: Partial<BlockUi>, backgrounds?: Record<string, string>): BlockUi {
const buttons = own.buttonClass ?? buttonClass
const sectionBackgrounds = { ...SECTION_BACKGROUNDS, ...backgrounds }
const BoundLink = (props: CMSLinkProps) => <CMSLink {...props} buttonClass={buttons} Link={ctx.Link} resolve={ctx.content.resolveLink} />
const BoundMedia = (props: MediaProps) => <Media {...props} Image={ctx.Image} />
const BoundSection = (props: SectionProps) => <Section backgrounds={sectionBackgrounds} {...props} />
return {
buttonClass: buttons,
Card: own.Card ?? Card,
CMSLink: own.CMSLink ?? BoundLink,
Container: own.Container ?? Container,
Media: own.Media ?? BoundMedia,
Section: own.Section ?? BoundSection,
SectionHeader: own.SectionHeader ?? SectionHeader,
}
}