NGU-Web/src/components/ContentBlocks.tsx
Zaldimmar 92e7938b64 Give blog posts an optional table of contents
Blog posts -> Show contents (migration 043, off by default): with two
or more headings, the post's headings sit in a sticky column beside
it with the one being read lit (an IntersectionObserver, not a scroll
listener), and fold out at the top on phones. Content-block headings
now carry anchor ids, so a link can point at one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 09:21:48 -05:00

234 lines
7.9 KiB
TypeScript

/* ═══════════════════════════════════════════════════════════════
CONTENT BLOCKS
Renders the `body` slot of content_blocks. Events, organizations,
teams, people and blog posts all own blocks and all render them
the same way, so this is written once and tinted by the caller's
accent. The admin's block editor previews each block through this
same component.
What each type reads (see 032_content_block_types.sql):
heading, subheading text
paragraph text (formatted), href makes it all a link
callout text (formatted), in a tinted box
quote text (formatted), cite is who said it
list items: text, detail, url
links items with a url, as outlined buttons
button text is the label, href where it goes
image media, alt, text is the caption, href
video href is the video, media a still, text
the caption; click to play
schedule items as entries in day tabs (day, time,
text, detail, url, location, person, and
a fold-out of description, media, map);
text is an optional intro. ScheduleBlock
divider nothing
"Formatted" is InlineText.tsx: **bold**, *italic*, [text](url),
line breaks, and a blank line for a new paragraph.
An unknown `type` renders its text as a paragraph rather than
disappearing. A block somebody typed into the admin should be
visible even if the renderer hasn't caught up with it.
═══════════════════════════════════════════════════════════════ */
import { mediaUrl } from '../lib/media.ts'
import type { ContentBlock } from '../lib/useContent.ts'
import { Anchor, Inline, Paragraphs } from './InlineText.tsx'
import VideoPlayer from './VideoPlayer.tsx'
import ScheduleBlock from './ScheduleBlock.tsx'
import { accentText, BRAND } from '../lib/color.ts'
import { headingAnchors } from '../lib/contents.ts'
const BODY = 'var(--color-muted)'
export default function ContentBlocks({
blocks,
accent = BRAND,
className = '',
}: {
blocks?: ContentBlock[] | null
accent?: string
className?: string
}) {
if (!blocks?.length) return null
// Headings get ids so a post's contents (PostContents) and a shared
// link (#getting-there) can point at them.
const { idAt } = headingAnchors(blocks)
return (
<div className={`space-y-6 ${className}`}>
{blocks.map((block, index) => (
<Block key={block.id ?? index} block={block} accent={accent} anchor={idAt.get(index)} />
))}
</div>
)
}
function Caption({ text }: { text?: string | null }) {
if (!text) return null
return (
<figcaption className="mt-2 text-sm opacity-70" style={{ color: BODY }}>
{text}
</figcaption>
)
}
// Clear of the fixed header when jumped to.
const ANCHOR_MARGIN = { scrollMarginTop: 'calc(var(--header-h, 5rem) + 1rem)' }
function Block({ block, accent, anchor }: { block: ContentBlock; accent: string; anchor?: string }) {
const text = block.text ?? ''
switch (block.type) {
case 'heading':
return (
<h3 id={anchor} className="text-2xl font-bold" style={{ color: accentText(accent), ...ANCHOR_MARGIN }}>
{text}
</h3>
)
case 'subheading':
return (
<h4 id={anchor} className="text-lg font-semibold" style={{ color: accentText(accent), ...ANCHOR_MARGIN }}>
{text}
</h4>
)
case 'list':
return (
<ul className="space-y-2">
{(block.items ?? []).map((item, index) => (
<li key={index} className="flex gap-3">
<span aria-hidden="true" style={{ color: accentText(accent) }}>
•
</span>
<span style={{ color: BODY }}>
{item.url ? (
<Anchor href={item.url} className="underline" style={{ color: accentText(accent) }}>
{item.text}
</Anchor>
) : (
<Inline text={item.text} accent={accent} />
)}
{item.detail && <span className="opacity-70">: {item.detail}</span>}
</span>
</li>
))}
</ul>
)
case 'links':
return (
<div className="flex flex-wrap gap-3">
{(block.items ?? [])
.filter((item) => item.url)
.map((item, index) => (
<Anchor
key={index}
href={item.url as string}
className="rounded-full border px-4 py-1.5 text-sm font-medium transition-colors hover:bg-tint"
style={{ borderColor: accent, color: accentText(accent) }}
>
{item.text}
</Anchor>
))}
</div>
)
case 'button':
if (!text || !block.href) return null
return (
<div>
<Anchor
href={block.href}
className="inline-block rounded-full px-6 py-2.5 font-semibold text-white transition-transform hover:scale-105"
style={{ background: accent }}
>
{text}
</Anchor>
</div>
)
case 'quote':
return (
<figure className="border-l-2 pl-5" style={{ borderColor: accent }}>
<blockquote className="text-lg italic" style={{ color: BODY }}>
<Inline text={text} accent={accent} />
</blockquote>
{block.cite && (
<figcaption className="mt-2 text-sm font-semibold not-italic" style={{ color: accentText(accent) }}>
{block.cite}
</figcaption>
)}
</figure>
)
case 'callout':
if (!text) return null
return (
<aside
className="space-y-3 rounded-2xl border-l-4 px-6 py-5 leading-relaxed"
style={{ borderColor: accent, background: `color-mix(in srgb, ${accent} 8%, var(--color-surface))`, color: 'var(--color-body)' }}
>
<Paragraphs text={text} accent={accent} />
</aside>
)
case 'image': {
const src = mediaUrl(block.media)
if (!src) return null
const img = (
<img
src={src}
// Alt describes the picture; the caption describes the
// moment. No alt means decorative, which alt="" says.
alt={block.alt ?? ''}
loading="lazy"
decoding="async"
className="w-full rounded-lg"
/>
)
return (
<figure>
{block.href ? <Anchor href={block.href}>{img}</Anchor> : img}
<Caption text={text} />
</figure>
)
}
case 'video':
if (!block.href) return null
return (
<figure>
<div className="overflow-hidden rounded-lg">
<VideoPlayer url={block.href} still={mediaUrl(block.media)} title={text || null} />
</div>
<Caption text={text} />
</figure>
)
case 'schedule':
if (!block.items?.length) return null
return <ScheduleBlock items={block.items} intro={block.text} accent={accent} />
case 'divider':
return <hr className="border-0 h-px" style={{ background: accent, opacity: 0.3 }} />
case 'paragraph':
default:
if (!text) return null
return block.href ? (
<p className="leading-relaxed" style={{ color: BODY }}>
<Anchor href={block.href} className="underline" style={{ color: accentText(accent) }}>
{text}
</Anchor>
</p>
) : (
<div className="space-y-4">
<Paragraphs text={text} accent={accent} className="leading-relaxed" style={{ color: BODY }} />
</div>
)
}
}