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]>
This commit is contained in:
Radosław Smoliński
2026-10-29 14:05:47 +01:00
co-authored by Claude Opus 5.5
parent 7918eaf6d4
commit 2d1cc0e77d
73 changed files with 5091 additions and 9 deletions
@@ -0,0 +1,72 @@
import type { LibraryBlockProps, TestimonialItem } from '../../context.js'
import type { BaseBlockData } from '../../data.js'
import { MEDIA_SIZES } from '../../../media/mediaProps.js'
import { blockTexts, fillText } from '../../texts.js'
import { Buttons, headingId } from '../../ui/Buttons.js'
import { cx, parts } from '../../ui/cx.js'
export type TestimonialsBlockData = { entries?: null | unknown[]; items?: null | TestimonialItem[]; source?: null | string } & BaseBlockData
export const TESTIMONIALS_PARTS = {
author: 'font-medium text-fg-strong',
figure: 'm-0 flex h-full flex-col gap-4 p-6',
grid: 'grid gap-gap md:grid-cols-2 lg:grid-cols-3',
photo: 'h-12 w-12 rounded-full object-cover',
quote: 'text-lead text-fg',
rating: 'text-accent',
role: 'text-sm text-fg-muted',
single: 'mx-auto grid max-w-narrow gap-gap',
}
const str = (v: unknown) => (typeof v === 'string' && v.trim() ? v : undefined)
/** Default mapping of a testimonials-collection document. */
export function defaultTestimonial(doc: Record<string, unknown>): null | TestimonialItem {
const quote = str(doc.quote) ?? str(doc.text)
const heading = str(doc.heading) ?? str(doc.author) ?? str(doc.name)
if (!quote || !heading) {return null}
return { id: str(doc.id), heading, media: doc.media ?? doc.photo, quote, rating: typeof doc.rating === 'number' ? doc.rating : null, text: str(doc.role) ?? str(doc.company) ?? null }
}
/** Quotes as <figure>/<blockquote>/<figcaption>; a rating has a text equivalent for screen readers. */
export function TestimonialsBlock(props: LibraryBlockProps<TestimonialsBlockData>) {
const { classNames, ctx, entries, header, isFirst, items, links, locale, settings, source, ui, variant } = props
const map = ctx.mapTestimonial ?? defaultTestimonial
const list =
source === 'collection'
? (entries ?? []).flatMap((e) => (e && typeof e === 'object' ? [map(e as Record<string, unknown>)] : [])).filter((x): x is TestimonialItem => !!x)
: (items ?? []).filter((i) => i.quote && i.heading)
if (!list.length) {return null}
const c = parts(TESTIMONIALS_PARTS, classNames)
const texts = blockTexts(locale, ctx.texts)
const id = headingId(props)
return (
<ui.Section block={props.blockType} labelledBy={id} settings={settings}>
<ui.SectionHeader align={variant === 'single' ? 'center' : 'start'} as={isFirst ? 'h1' : 'h2'} header={header} id={id} />
<ul className={variant === 'single' ? c.single : c.grid}>
{list.map((t, i) => (
<li key={t.id ?? i}>
<ui.Card as="figure" className={c.figure}>
{t.rating ? (
<p className={c.rating}>
<span aria-hidden>{'★'.repeat(Math.round(t.rating))}</span>
<span className="sr-only">{fillText(texts.rating, t.rating)}</span>
</p>
) : null}
<blockquote className={cx(c.quote, 'whitespace-pre-line')}>{t.quote}</blockquote>
<figcaption className="mt-auto flex items-center gap-3">
<ui.Media alt="" className={c.photo} resource={t.media as never} sizes={MEDIA_SIZES.thumb} />
<span className="flex flex-col">
<span className={c.author}>{t.heading}</span>
{t.text ? <span className={c.role}>{t.text}</span> : null}
</span>
</figcaption>
</ui.Card>
</li>
))}
</ul>
<Buttons CMSLink={ui.CMSLink} links={links} locale={locale} />
</ui.Section>
)
}
@@ -0,0 +1,70 @@
import type { Field } from 'payload'
import type { BlockOptions } from '../../types.js'
import { adminText, defineLibraryBlock, variantOption } from '../../define.js'
import { buildItemsField, buildMediaField } from '../../fields.js'
export const TESTIMONIALS_VARIANTS = [variantOption('grid', 'Grid', 'Siatka'), variantOption('single', 'One at a time', 'Pojedyncza opinia')] as const
export type TestimonialsOptions = {
/**
* A collection of testimonials to pick from, e.g. 'testimonials' — adds "From the list" as a source.
* Its documents need `quote` and `heading` (author); `text`, `media`, `rating` are optional
* (or map them with `mapTestimonial` in createBlockRegistry). Default none: inline only.
*/
collection?: string
} & BlockOptions
const ITEM_FIELDS: Field[] = [
{ name: 'quote', type: 'textarea', admin: { rows: 4 }, label: adminText('Quote', 'Cytat'), localized: true, required: true },
{
type: 'row',
fields: [
{ name: 'heading', type: 'text', admin: { width: '50%' }, label: adminText('Author', 'Autor'), localized: true, required: true },
{ name: 'text', type: 'text', admin: { width: '50%' }, label: adminText('Role or company', 'Stanowisko lub firma'), localized: true },
],
},
buildMediaField({ label: adminText('Photo', 'Zdjęcie') }),
{ name: 'rating', type: 'number', admin: { step: 1 }, label: adminText('Rating (1–5)', 'Ocena (1–5)'), max: 5, min: 1 },
]
/** Testimonials — quotes typed into the block, or picked from a testimonials collection. */
export const buildTestimonialsBlock = defineLibraryBlock<TestimonialsOptions>({
slug: 'testimonials',
fields: ({ collection, maxItems = 12 }) => {
const items = buildItemsField({
fields: ITEM_FIELDS,
labels: { plural: adminText('Testimonials', 'Opinie'), singular: adminText('Testimonial', 'Opinia') },
maxRows: maxItems,
})
if (!collection) {return [items]}
const fromList = (_: unknown, s: { source?: string } | undefined) => s?.source === 'collection'
return [
{
name: 'source',
type: 'radio',
defaultValue: 'inline',
label: adminText('Source', 'Źródło'),
options: [
{ label: adminText('Typed here', 'Wpisane tutaj'), value: 'inline' },
{ label: adminText('From the list', 'Z listy opinii'), value: 'collection' },
],
},
{ ...items, admin: { ...items.admin, condition: (d: unknown, s: { source?: string } | undefined) => !fromList(d, s) } },
{
name: 'entries',
type: 'relationship',
admin: { condition: fromList },
hasMany: true,
label: adminText('Testimonials', 'Opinie'),
maxRows: maxItems,
relationTo: collection,
},
]
},
interfaceName: 'TestimonialsBlock',
labels: { plural: adminText('Testimonials', 'Opinie'), singular: adminText('Testimonials', 'Opinie') },
links: { maxRows: 1 },
variants: TESTIMONIALS_VARIANTS,
})