Files
ipal-kit/src/modules/richtext/withLightboxUploads.tsx
T
Radosław SmolińskiandClaude Opus 5.5 476c3ac1df 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]>
2026-10-27 14:49:38 +01:00

56 lines
2.7 KiB
TypeScript

import type { JSXConverters, JSXConvertersFunction } from '@payloadcms/richtext-lexical/react'
import type { ReactNode } from 'react'
import type { LightboxImageOptions } from '../lightbox/lightboxImage.js'
import { lightboxImage } from '../lightbox/lightboxImage.js'
import { LightboxTrigger } from '../lightbox/LightboxProvider.js'
export type RichTextLightboxOptions = {
/**
* Images of one rich text browse together under this group. Default
* 'richtext' — every rich text on the page is one gallery; pass a different
* group per field to keep them apart, or `false` to open each image alone.
*/
group?: false | string
/** Passed to lightboxImage: `size` (src fallback), `sizes`, `srcSet`. */
image?: Pick<LightboxImageOptions, 'size' | 'sizes' | 'srcSet'>
/** Class on each trigger button (e.g. Tailwind `my-6 block`). */
triggerClassName?: string
}
type UploadNode = { fields?: { alt?: unknown; caption?: unknown } | null; value?: unknown }
/**
* Wraps every image upload of a rich text in <LightboxTrigger>, on top of the
* project's converters: your own `upload` converter still renders the inline
* image (Payload's default otherwise); only the click-to-enlarge button is
* added around it. Files that aren't images (PDF, video) are left as they are.
*
* <RenderRichText data={post.content} lightbox /> // the shortcut
* <RichText data={…} converters={withLightboxUploads(myConverters)} /> // your own renderer
*/
export function withLightboxUploads(converters?: JSXConvertersFunction, options: RichTextLightboxOptions = {}): JSXConvertersFunction {
const group = options.group === false ? undefined : (options.group ?? 'richtext')
return (args) => {
const base: JSXConverters = converters ? converters(args) : args.defaultConverters
const inner = base.upload
return {
...base,
upload: (props: { node: UploadNode }) => {
const rendered = typeof inner === 'function' ? (inner as (p: typeof props) => ReactNode)(props) : null
const { fields, value } = props.node
const alt = typeof fields?.alt === 'string' && fields.alt ? fields.alt : undefined
const caption = typeof fields?.caption === 'string' && fields.caption ? fields.caption : undefined
const image = lightboxImage(value, { ...options.image, ...(alt ? { alt } : {}), ...(caption ? { caption } : {}) })
if (!image || rendered === null || rendered === undefined) {return rendered}
return (
<LightboxTrigger className={options.triggerClassName} {...(group ? { group } : {})} image={image}>
{rendered}
</LightboxTrigger>
)
},
} as JSXConverters
}
}