Shared image lightbox: LightboxProvider, LightboxTrigger, LightboxDialog, lightboxImage

Opt-in click-to-enlarge for small images (cards, galleries, rich text):
- /client: LightboxProvider (one viewer per site), LightboxTrigger (button
  around any thumbnail, works without a provider), LightboxDialog
  (controlled viewer), useLightbox / useOptionalLightbox
- accessible dialog: aria-modal, focus trap and return, Esc, arrows,
  Home/End, swipe, counter, caption, scroll lock, reduced motion
- lightboxImage builds src + srcSet from Payload image sizes
- labels: ipalKit({ lightbox: true }) adds Notifications -> Lightbox;
  getNotificationTexts().lightbox and getSiteShell().lightboxTexts,
  English fallbacks otherwise
- RenderRichText lightbox / withLightboxUploads wrap image uploads
- SiteProviders lightbox mounts the provider
- styling from --ipal-lightbox-* variables; unstyled + classNames,
  renderImage, renderDialog, icons, loop overrides
- tests with happy-dom (vitest's pinned optional peer)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-10-27 14:49:38 +01:00
co-authored by Claude Opus 5.5
parent e53d09b720
commit 476c3ac1df
23 changed files with 1203 additions and 15 deletions
+20 -1
View File
@@ -2,6 +2,7 @@ import type { ReactNode } from 'react'
import type { AnalyticsProps } from '../analytics/Analytics.js'
import type { ConsentCookieMap } from '../consent/useConsent.js'
import type { LightboxProviderProps } from '../lightbox/LightboxProvider.js'
import type { PageStatsProps } from '../stats/PageStats.js'
import type { SiteShell } from './createContentHelpers.js'
@@ -9,6 +10,7 @@ import { Analytics } from '../analytics/Analytics.js'
import { ConsentProvider } from '../consent/ConsentContext.js'
import { CookieBanner } from '../consent/CookieBanner.js'
import { CookieButton } from '../consent/CookieButton.js'
import { LightboxProvider } from '../lightbox/LightboxProvider.js'
import { JsonLdGraph } from '../seo/JsonLdGraph.js'
import { PageStats } from '../stats/PageStats.js'
import { MediaPreconnect } from '../storage/MediaPreconnect.js'
@@ -27,6 +29,14 @@ export type SiteProvidersProps = {
cookieMap?: ConsentCookieMap
/** More site-wide JSON-LD nodes next to shell.jsonLd (e.g. buildNavigationJsonLd). */
jsonLd?: (null | Record<string, unknown> | undefined)[]
/**
* One shared image lightbox for the site: mounts <LightboxProvider> around the
* page, so every <LightboxTrigger> and `RenderRichText lightbox` uses it.
* `true` (texts from shell.lightboxTexts = Notifications → Lightbox) or
* `{ texts, classNames, icons, loop, unstyled }`. Default off. For renderImage / renderDialog (functions) mount the provider
* yourself in a client component.
*/
lightbox?: boolean | Omit<LightboxProviderProps, 'children' | 'renderDialog' | 'renderImage'>
/** Consent log endpoint, e.g. '/api/ipal/consent' (ipalKit consentLog: true). */
logEndpoint?: string
/** <link rel=preconnect> to the media origin (R2_PUBLIC_URL). Default true. */
@@ -69,6 +79,7 @@ export function SiteProviders({
cookieButton,
cookieMap,
jsonLd = [],
lightbox = false,
logEndpoint,
preconnect = true,
shell,
@@ -81,7 +92,15 @@ export function SiteProviders({
{preconnect ? <MediaPreconnect /> : null}
<JsonLdGraph schemas={[...shell.jsonLd, ...jsonLd]} />
<ConsentProvider cookieMap={cookieMap} logEndpoint={logEndpoint} texts={shell.consent}>
<TurnstileProvider siteKey={shell.turnstileSiteKey}>{children}</TurnstileProvider>
<TurnstileProvider siteKey={shell.turnstileSiteKey}>
{lightbox ? (
<LightboxProvider texts={shell.lightboxTexts} {...(lightbox === true ? {} : lightbox)}>
{children}
</LightboxProvider>
) : (
children
)}
</TurnstileProvider>
{banner === undefined ? <CookieBanner /> : banner || null}
{cookieButton === undefined ? <CookieButton /> : cookieButton || null}
{analytics === false ? null : <Analytics {...shell.analytics} {...analytics} />}
+10 -7
View File
@@ -9,6 +9,7 @@ import type { ConstructionMode } from '../construction/types.js'
import type { PathVisibility } from '../construction/visibility.js'
import type { ArchiveEntries, ContentOption, ResolvedRoute } from '../content/index.js'
import type { I18nConfig } from '../i18n/index.js'
import type { LightboxTexts } from '../lightbox/types.js'
import type { LinkValue, NavItem, ResolvedLink, ResolveLinkHrefOptions, SystemLink } from '../links/index.js'
import type { CreateMcpRouteArgs } from '../mcp/createMcpRoute.js'
import type { FormNotificationTexts } from '../notifications/types.js'
@@ -795,23 +796,22 @@ export function createContentHelpers({
// ── Layout ────────────────────────────────────────────────────────────────
/** Notifications → form texts in this locale (BuilderForm / RenderForm `texts`). */
const getFormTexts = cache(async (locale: string): Promise<FormNotificationTexts> =>
(await getNotificationTexts({ locale, payload: await getCachedPayload() })).form,
)
const getNotifications = cache(async (locale: string) => getNotificationTexts({ locale, payload: await getCachedPayload() }))
const getFormTexts = cache(async (locale: string): Promise<FormNotificationTexts> => (await getNotifications(locale)).form)
/**
* Everything the [locale] layout reads, in one cached call: consent texts,
* analytics IDs, Turnstile site key, site JSON-LD, form texts and Site
* analytics IDs, Turnstile site key, site JSON-LD, form and lightbox texts and Site
* Settings. Feed it to <SiteProviders> (/rsc) — or use the pieces yourself.
*/
const getSiteShell = cache(async (locale: string): Promise<SiteShell> => {
const payload = await getCachedPayload()
const [consent, analytics, turnstile, settings, formTexts] = await Promise.all([
const [consent, analytics, turnstile, settings, notifications] = await Promise.all([
getConsentTexts({ config: needI18n('getSiteShell'), locale, payload, settingsSlug }),
getAnalyticsConfig(payload),
getTurnstileConfig(payload),
getSettings(locale) as Promise<Record<string, unknown>>,
getFormTexts(locale),
getNotifications(locale),
])
let jsonLd: Record<string, unknown>[] = []
if (typeof siteJsonLd === 'function') {
@@ -828,8 +828,9 @@ export function createContentHelpers({
return {
analytics,
consent,
formTexts,
formTexts: notifications.form,
jsonLd: jsonLd.map(({ '@context': _context, ...node }) => node),
lightboxTexts: notifications.lightbox,
settings: settings ?? {},
theme: themeConfigFrom(settings),
turnstileSiteKey: turnstile.siteKey,
@@ -910,6 +911,8 @@ export type SiteShell = {
formTexts: FormNotificationTexts
/** Site-wide nodes (WebSite, Organization…) for <JsonLdGraph>. */
jsonLd: Record<string, unknown>[]
/** Notifications → Lightbox (English fallbacks without ipalKit `lightbox`) — <SiteProviders lightbox> reads it. */
lightboxTexts?: LightboxTexts
settings: Record<string, unknown>
/** Site Settings → Theme, for <ThemeScript> / <SiteProviders theme> and <ThemeSwitcher label>. */
theme: ThemeConfig