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:
co-authored by
Claude Opus 5.5
parent
7918eaf6d4
commit
2d1cc0e77d
@@ -0,0 +1,20 @@
|
||||
import type { ComponentType, ReactNode } from 'react'
|
||||
|
||||
/** next/link, passed in by the project (the plugin never imports next/*). */
|
||||
export type LinkComponent = ComponentType<{ children?: ReactNode; className?: string; href: string; rel?: string; target?: string }>
|
||||
|
||||
/** An internal link through `Link` when the project passed one, otherwise a plain <a>. */
|
||||
export function Anchor({ children, className, href, internal, Link, targetProps }: { children: ReactNode; className?: string; href: string; internal: boolean; Link?: LinkComponent; targetProps: Record<string, string> }) {
|
||||
if (internal && Link) {
|
||||
return (
|
||||
<Link className={className} href={href} {...targetProps}>
|
||||
{children}
|
||||
</Link>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<a className={className} href={href} {...targetProps}>
|
||||
{children}
|
||||
</a>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Button looks by link appearance — the one place buttons are styled. Token utilities only.
|
||||
* Swap it with createBlockRegistry({ ui: { buttonClass } }) or extend BUTTON_CLASSES' keys
|
||||
* with the project's own appearance names.
|
||||
*/
|
||||
const BASE =
|
||||
'inline-flex items-center justify-center gap-2 rounded-control px-5 py-3 font-medium leading-none transition-colors duration-fast ease-out focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-focus'
|
||||
|
||||
export const BUTTON_CLASSES: Record<string, string> = {
|
||||
link: 'font-medium text-link underline underline-offset-4 hover:text-link-hover focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-focus',
|
||||
primary: `${BASE} bg-primary text-on-primary hover:bg-primary-hover`,
|
||||
secondary: `${BASE} border border-border-strong bg-transparent text-fg hover:bg-hover`,
|
||||
}
|
||||
|
||||
/** Classes for a link appearance; an unknown name gets the primary look. No appearance → ''. */
|
||||
export function buttonClass(appearance?: null | string): string {
|
||||
if (!appearance) {return ''}
|
||||
return BUTTON_CLASSES[appearance] ?? BUTTON_CLASSES.primary
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import type { BlockUi } from '../context.js'
|
||||
import type { LinksData } from '../data.js'
|
||||
|
||||
import { cx } from './cx.js'
|
||||
|
||||
/** The section's buttons in a row; nothing when there are none. */
|
||||
export function Buttons({ className, CMSLink, links, locale }: { className?: string; CMSLink: BlockUi['CMSLink']; links?: LinksData; locale: string }) {
|
||||
const rows = (links ?? []).filter((row) => row.link)
|
||||
if (!rows.length) {return null}
|
||||
return (
|
||||
<div className={cx('mt-8 flex flex-wrap items-center gap-3', className)}>
|
||||
{rows.map((row, i) => (
|
||||
<CMSLink key={row.id ?? i} link={row.link} locale={locale} />
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** The id of a block's heading (aria-labelledby), when it has one. */
|
||||
export function headingId(block: { blockType: string; header?: { heading?: null | string } | null; id?: null | string }): string | undefined {
|
||||
return block.header?.heading && block.id ? `${block.blockType}-${block.id}-heading` : undefined
|
||||
}
|
||||
|
||||
/** Grid columns from the `columns` select. */
|
||||
export const GRID_COLUMNS: Record<string, string> = {
|
||||
four: 'sm:grid-cols-2 lg:grid-cols-4',
|
||||
three: 'sm:grid-cols-2 lg:grid-cols-3',
|
||||
two: 'sm:grid-cols-2',
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
import type { ResolvedLink } from '../../links/resolveNav.js'
|
||||
import type { LinkData } from '../data.js'
|
||||
import type { LinkComponent } from './Anchor.js'
|
||||
|
||||
import { Anchor } from './Anchor.js'
|
||||
import { buttonClass as defaultButtonClass } from './Button.js'
|
||||
import { cx } from './cx.js'
|
||||
|
||||
export type ResolveLink = (link: LinkData, locale: string) => null | Promise<null | ResolvedLink> | ResolvedLink
|
||||
|
||||
export type CMSLinkProps = {
|
||||
/** For a link that wraps something (a card, an image): the content instead of the label. */
|
||||
children?: ReactNode
|
||||
className?: string
|
||||
link: LinkData
|
||||
locale: string
|
||||
}
|
||||
|
||||
/** The plugin's link: `resolve` (lib/content `resolveLink`), `buttonClass` and `Link` (next/link) are bound by createBlockRegistry. */
|
||||
export async function CMSLink({
|
||||
buttonClass = defaultButtonClass,
|
||||
children,
|
||||
className,
|
||||
link,
|
||||
Link,
|
||||
locale,
|
||||
resolve,
|
||||
}: { buttonClass?: (appearance?: null | string) => string; Link?: LinkComponent; resolve: ResolveLink } & CMSLinkProps) {
|
||||
const l = await resolve(link, locale)
|
||||
const content = children ?? l?.label
|
||||
if (!l || !content) {return null}
|
||||
const cls = cx(buttonClass(l.appearance), className) || undefined
|
||||
return (
|
||||
<Anchor className={cls} href={l.href} internal={l.internal} Link={Link} targetProps={l.targetProps}>
|
||||
{content}
|
||||
</Anchor>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import type { ElementType, ReactNode } from 'react'
|
||||
|
||||
import { cx } from './cx.js'
|
||||
|
||||
export const CARD_CLASS = 'flex flex-col rounded-card border border-border bg-surface text-fg shadow-sm'
|
||||
export const CARD_INTERACTIVE_CLASS = 'relative transition-shadow duration-fast ease-out hover:shadow-md focus-within:shadow-md'
|
||||
|
||||
export type CardProps = { as?: ElementType; children: ReactNode; className?: string; interactive?: boolean }
|
||||
|
||||
/** A card surface. `interactive` = the whole card is a link (hover and focus lift). */
|
||||
export function Card({ as: As = 'article', children, className, interactive = false }: CardProps) {
|
||||
return <As className={cx(CARD_CLASS, interactive && CARD_INTERACTIVE_CLASS, className)}>{children}</As>
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
import { cx } from './cx.js'
|
||||
|
||||
export type ContainerWidth = 'default' | 'narrow' | 'wide'
|
||||
|
||||
export const CONTAINER_WIDTHS: Record<ContainerWidth, string> = {
|
||||
default: 'max-w-container',
|
||||
narrow: 'max-w-narrow',
|
||||
wide: 'max-w-container-wide',
|
||||
}
|
||||
|
||||
export type ContainerProps = { children: ReactNode; className?: string; width?: ContainerWidth }
|
||||
|
||||
/** The page column: centred, the site's max width, side gutters. */
|
||||
export function Container({ children, className, width = 'default' }: ContainerProps) {
|
||||
return <div className={cx('mx-auto w-full px-gutter', CONTAINER_WIDTHS[width], className)}>{children}</div>
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import type { ComponentType } from 'react'
|
||||
|
||||
import type { MediaImageProps, VideoMode } from '../../media/mediaProps.js'
|
||||
import type { MediaValue } from '../data.js'
|
||||
|
||||
import { MEDIA_SIZES, mediaImageProps, videoProps } from '../../media/mediaProps.js'
|
||||
|
||||
/** next/image, passed in by the project (the plugin never imports next/*). */
|
||||
export type ImageComponent = ComponentType<{ className?: string } & MediaImageProps>
|
||||
|
||||
export type MediaProps = {
|
||||
/** Override the document's alt text — `''` for a decorative image. */
|
||||
alt?: string
|
||||
className?: string
|
||||
poster?: MediaValue
|
||||
/** Only the first visible image of the page (the LCP image). */
|
||||
priority?: boolean
|
||||
resource: MediaValue
|
||||
/** The width on screen (MEDIA_SIZES.*). Without it the full width downloads. */
|
||||
sizes?: string
|
||||
videoMode?: VideoMode
|
||||
}
|
||||
|
||||
/**
|
||||
* Every image or video of the library blocks. With `Image` (next/image, bound by createBlockRegistry)
|
||||
* images are optimized with blur-up; without it a plain <img> with width, height, lazy loading and focal point.
|
||||
*/
|
||||
export function Media({ alt, className, Image, poster, priority = false, resource, sizes = MEDIA_SIZES.full, videoMode = 'player' }: { Image?: ImageComponent } & MediaProps) {
|
||||
const video = videoProps(resource, { mode: videoMode, poster })
|
||||
if (video) {
|
||||
// Captions are not stored with media uploads; a project that needs them swaps Media (ui option).
|
||||
// eslint-disable-next-line jsx-a11y/media-has-caption
|
||||
return <video {...video} aria-hidden={videoMode === 'background' ? true : undefined} className={className} />
|
||||
}
|
||||
const img = mediaImageProps(resource, { priority, sizes, ...(alt !== undefined ? { alt } : {}) })
|
||||
if (!img) {return null}
|
||||
if (Image) {return <Image {...img} className={className} />}
|
||||
const { blurDataURL: _blur, placeholder: _placeholder, preload: _preload, ...plain } = img
|
||||
return <img {...plain} alt={plain.alt} className={className} decoding="async" />
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
import type { SectionSettingsData } from '../data.js'
|
||||
import type { ContainerWidth } from './Container.js'
|
||||
|
||||
import { Container } from './Container.js'
|
||||
import { cx } from './cx.js'
|
||||
|
||||
/** Section backgrounds (`settings.background`) → token utilities. Add project values with createBlockRegistry({ backgrounds }). */
|
||||
export const SECTION_BACKGROUNDS: Record<string, string> = {
|
||||
default: 'bg-bg text-fg',
|
||||
inverted: 'bg-inverse text-on-inverse',
|
||||
muted: 'bg-surface-muted text-fg',
|
||||
}
|
||||
|
||||
/** `data-surface` per background — the token preset re-maps text, links, borders and focus inside `inverse`. */
|
||||
const SURFACES: Record<string, string> = { inverted: 'inverse', muted: 'muted' }
|
||||
|
||||
export type SectionProps = {
|
||||
backgrounds?: Record<string, string>
|
||||
/** The block's slug — `data-ipal-block` for project CSS and tests. */
|
||||
block: string
|
||||
children: ReactNode
|
||||
className?: string
|
||||
/** Smaller vertical spacing (CTA band, logos). */
|
||||
compact?: boolean
|
||||
/** The heading's id, for aria-labelledby. */
|
||||
labelledBy?: string
|
||||
settings?: null | SectionSettingsData
|
||||
width?: ContainerWidth
|
||||
}
|
||||
|
||||
/** One <section> per block: anchor id, background, spacing, the page column. */
|
||||
export function Section({ backgrounds = SECTION_BACKGROUNDS, block, children, className, compact = false, labelledBy, settings, width }: SectionProps) {
|
||||
const background = settings?.background ?? 'default'
|
||||
return (
|
||||
<section
|
||||
aria-labelledby={labelledBy}
|
||||
className={cx(compact ? 'py-section-sm' : 'py-section', backgrounds[background] ?? backgrounds.default, className)}
|
||||
data-ipal-block={block}
|
||||
data-surface={SURFACES[background]}
|
||||
id={settings?.anchor || undefined}
|
||||
>
|
||||
<Container width={width}>{children}</Container>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import type { SectionHeaderData } from '../data.js'
|
||||
|
||||
import { cx } from './cx.js'
|
||||
|
||||
export type HeadingLevel = 'h1' | 'h2'
|
||||
|
||||
export type SectionHeaderProps = {
|
||||
align?: 'center' | 'start'
|
||||
/** h1 only for the first section of the page (`isFirst`). */
|
||||
as?: HeadingLevel
|
||||
className?: string
|
||||
/** Larger heading for a hero. */
|
||||
display?: boolean
|
||||
header?: null | SectionHeaderData
|
||||
/** The heading's id — the section's aria-labelledby. */
|
||||
id?: string
|
||||
}
|
||||
|
||||
const SIZES: Record<HeadingLevel, string> = { h1: 'text-h1', h2: 'text-h2' }
|
||||
|
||||
/** Eyebrow, heading and introduction from the panel. Nothing renders when all three are empty. */
|
||||
export function SectionHeader({ id, align = 'start', as: H = 'h2', className, display = false, header }: SectionHeaderProps) {
|
||||
if (!header?.heading && !header?.eyebrow && !header?.intro) {return null}
|
||||
return (
|
||||
<header className={cx('mb-10 flex max-w-measure flex-col gap-3', align === 'center' && 'mx-auto items-center text-center', className)}>
|
||||
{header.eyebrow ? <p className="text-sm font-medium uppercase tracking-heading text-fg-muted">{header.eyebrow}</p> : null}
|
||||
{header.heading ? (
|
||||
<H className={cx('font-heading leading-heading tracking-heading text-fg-strong', display ? 'text-display' : SIZES[H])} id={id}>
|
||||
{header.heading}
|
||||
</H>
|
||||
) : null}
|
||||
{header.intro ? <p className="text-lead text-fg-muted">{header.intro}</p> : null}
|
||||
</header>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
import type { LinkData } from '../data.js'
|
||||
import type { LinkComponent } from './Anchor.js'
|
||||
import type { ResolveLink } from './CMSLink.js'
|
||||
|
||||
import { Anchor } from './Anchor.js'
|
||||
|
||||
/**
|
||||
* A card title that links when its link leads somewhere, and stays plain text when it doesn't
|
||||
* (a deleted page, an entry without an archive). `stretch` makes the whole card the click area.
|
||||
*/
|
||||
export async function TitleLink({ children, link, Link, locale, resolve, stretch = true }: { children: ReactNode; link: LinkData; Link?: LinkComponent; locale: string; resolve: ResolveLink; stretch?: boolean }) {
|
||||
const l = link ? await resolve(link, locale) : null
|
||||
if (!l) {return <>{children}</>}
|
||||
const className = `${stretch ? 'after:absolute after:inset-0 ' : ''}hover:text-link focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-focus`
|
||||
return (
|
||||
<Anchor className={className} href={l.href} internal={l.internal} Link={Link} targetProps={l.targetProps}>
|
||||
{children}
|
||||
</Anchor>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
/** Joins class names, skipping empty ones. */
|
||||
export function cx(...classes: (false | null | string | undefined)[]): string {
|
||||
return classes.filter(Boolean).join(' ')
|
||||
}
|
||||
|
||||
/** A block's classes by part; the project's `classNames` replace a part's defaults. */
|
||||
export type PartClasses<P extends string = string> = Partial<Record<P, string>>
|
||||
|
||||
export function parts<P extends string>(defaults: Record<P, string>, overrides?: PartClasses): Record<P, string> {
|
||||
return overrides ? ({ ...defaults, ...overrides } as Record<P, string>) : defaults
|
||||
}
|
||||
Reference in New Issue
Block a user