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
+39
View File
@@ -0,0 +1,39 @@
import type { LightboxTexts } from './types.js'
/**
* English fallbacks, per field — the same philosophy as NOTIFICATION_FALLBACK.
* Editors translate them in Notifications → Lightbox (ipalKit `lightbox: true`).
*/
export const LIGHTBOX_FALLBACK: LightboxTexts = {
close: 'Close',
counter: '{current} / {total}',
dialog: 'Image viewer',
next: 'Next image',
open: 'Enlarge image',
previous: 'Previous image',
}
/** Merges partial texts over the fallback; empty strings fall back too. */
export function resolveLightboxTexts(texts?: null | Partial<Record<keyof LightboxTexts, null | string>>): LightboxTexts {
const out = { ...LIGHTBOX_FALLBACK }
for (const key of Object.keys(out) as (keyof LightboxTexts)[]) {
const value = texts?.[key]
if (typeof value === 'string' && value.trim()) {out[key] = value}
}
return out
}
/** "{current} / {total}" → "2 / 5". */
export const formatLightboxCounter = (template: string, current: number, total: number): string =>
template.replace('{current}', String(current)).replace('{total}', String(total))
/**
* The index after a step of `delta`. `loop` wraps around the ends; without it
* the index stops at the first and last image.
*/
export function lightboxIndex(index: number, delta: number, total: number, loop = true): number {
if (total <= 0) {return 0}
const next = index + delta
if (loop) {return ((next % total) + total) % total}
return Math.min(Math.max(next, 0), total - 1)
}