- buildRevalidateHook skips outside Next (no NEXT_RUNTIME) and with context.disableRevalidate - BuilderForm focuses only on a real step change (no scroll to the form on remount) - mediaImageProps: placeholder colour only for JPEG (placeholderColor); hook stores no colour for transparent images - buildLinkField: interface name per shape (CmsLink, CmsLinkPlain, CmsLinkNoLabel) + interfaceName option - Cookie Settings: buttons.save -> buttons.savePreferences (Mongoose reserved name) - ThemeScript, ThemeSwitcher, useTheme; Site Settings defaultTheme 'system' + themeToggleLabel; Turnstile follows page theme - extractHeadings, RenderRichText headingIds, TableOfContents, buildTableOfContentsField - createPageRoute onMissing: 'redirectHome' - Form builder: localized placeholder on text/email/textarea/number, new time field - ipal-audit: no false positive for object keys between JSX elements; /md route not reported as dynamic Co-Authored-By: Claude Opus 5.5 <[email protected]>
84 lines
2.8 KiB
TypeScript
84 lines
2.8 KiB
TypeScript
'use client'
|
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import type { HeadingItem } from './headings.js'
|
|
|
|
export type TableOfContentsClassNames = {
|
|
item?: string
|
|
label?: string
|
|
/** Every link. */
|
|
link?: string
|
|
/** Added to the link of the section being read. */
|
|
linkActive?: string
|
|
list?: string
|
|
nav?: string
|
|
}
|
|
|
|
export type TableOfContentsProps = {
|
|
classNames?: TableOfContentsClassNames
|
|
/** From extractHeadings — the same ids as the headings rendered with `headingIds`. */
|
|
items: readonly ({ level?: HeadingItem['level'] } & Pick<HeadingItem, 'id' | 'text'>)[]
|
|
/** Text above the list (from the panel). Also the nav's accessible name. Empty → none. */
|
|
label?: null | string
|
|
/**
|
|
* Where the "reading line" is, as a fraction of the viewport height: the
|
|
* current section is the last heading above it. Default 0.3.
|
|
*/
|
|
offset?: number
|
|
}
|
|
|
|
/**
|
|
* Table of contents with no styles: a <nav> with links to the headings. The
|
|
* server renders the full list (works without JS); in the browser the link of
|
|
* the section being read gets `aria-current="location"` and `classNames.linkActive`.
|
|
* Hooks for CSS: data-ipal="toc", data-level on each item.
|
|
*/
|
|
export function TableOfContents({ classNames: cn = {}, items, label, offset = 0.3 }: TableOfContentsProps) {
|
|
const [active, setActive] = useState<string | undefined>(items[0]?.id)
|
|
|
|
useEffect(() => {
|
|
const headings = items.map((item) => document.getElementById(item.id)).filter((el): el is HTMLElement => el !== null)
|
|
if (headings.length === 0) {return}
|
|
const update = () => {
|
|
const line = window.innerHeight * offset
|
|
let current = headings[0].id
|
|
for (const heading of headings) {
|
|
if (heading.getBoundingClientRect().top <= line) {current = heading.id}
|
|
}
|
|
setActive(current)
|
|
}
|
|
update()
|
|
window.addEventListener('scroll', update, { passive: true })
|
|
window.addEventListener('resize', update)
|
|
return () => {
|
|
window.removeEventListener('scroll', update)
|
|
window.removeEventListener('resize', update)
|
|
}
|
|
}, [items, offset])
|
|
|
|
if (items.length === 0) {return null}
|
|
const text = label?.trim()
|
|
return (
|
|
<nav aria-label={text || undefined} className={cn.nav} data-ipal="toc">
|
|
{text ? <p aria-hidden className={cn.label}>{text}</p> : null}
|
|
<ol className={cn.list}>
|
|
{items.map((item) => {
|
|
const current = item.id === active
|
|
return (
|
|
<li className={cn.item} data-level={item.level} key={item.id}>
|
|
<a
|
|
aria-current={current ? 'location' : undefined}
|
|
className={[cn.link, current ? cn.linkActive : undefined].filter(Boolean).join(' ') || undefined}
|
|
href={`#${item.id}`}
|
|
>
|
|
{item.text}
|
|
</a>
|
|
</li>
|
|
)
|
|
})}
|
|
</ol>
|
|
</nav>
|
|
)
|
|
}
|