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:
co-authored by
Claude Opus 5.5
parent
e53d09b720
commit
476c3ac1df
@@ -6,6 +6,7 @@ import { cloneElement, isValidElement, type ReactElement, type ReactNode } from
|
||||
|
||||
import { headingIds as computeHeadingIds, type HeadingLevel } from './headings.js'
|
||||
import { sanitizeRichTextLinks } from './sanitizeLinks.js'
|
||||
import { type RichTextLightboxOptions, withLightboxUploads } from './withLightboxUploads.js'
|
||||
|
||||
export type RenderRichTextProps = {
|
||||
/** Wrapper class (e.g. Tailwind prose classes). */
|
||||
@@ -31,6 +32,14 @@ export type RenderRichTextProps = {
|
||||
* converter still renders the heading; the id is added to what it returns.
|
||||
*/
|
||||
headingIds?: { className?: string; levels?: readonly HeadingLevel[] } | boolean
|
||||
/**
|
||||
* Click-to-enlarge on image uploads (opt-in): each image becomes a
|
||||
* <LightboxTrigger>, and the images browse as one gallery. `true` or
|
||||
* `{ group, image: { size }, triggerClassName }`. Your own `upload` converter
|
||||
* still renders the inline image. Inside a <LightboxProvider> the site's
|
||||
* viewer and texts are used; without one each image brings its own.
|
||||
*/
|
||||
lightbox?: boolean | RichTextLightboxOptions
|
||||
/**
|
||||
* Links whose URL isn't a safe target (`javascript:`, `data:`…) render as
|
||||
* '#'. Default true; `false` renders stored URLs as they are (only for
|
||||
@@ -103,8 +112,9 @@ function withHeadingIds(
|
||||
* <TableOfContents items={extractHeadings(page.content)} label={toc.label} /> // /client
|
||||
* <RenderRichText data={page.content} headingIds={{ className: 'scroll-mt-24' }} />
|
||||
*/
|
||||
export function RenderRichText({ className, converters, data: raw, headingIds, safeLinks = true }: RenderRichTextProps) {
|
||||
export function RenderRichText({ className, converters: own, data: raw, headingIds, lightbox, safeLinks = true }: RenderRichTextProps) {
|
||||
if (!raw) {return null}
|
||||
const converters = lightbox ? withLightboxUploads(own, lightbox === true ? {} : lightbox) : own
|
||||
// javascript:/data: link URLs from stored content never reach an href
|
||||
const data = safeLinks ? sanitizeRichTextLinks(raw) : raw
|
||||
if (headingIds) {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user