Raft Design System

Components · Overlays

Lightbox

A full-screen viewer over the scrim for images and attachment previews, with previous and next, thumbnails and one button to close.

StableAlso called Image viewer, Media viewer, Attachment preview
import { Lightbox } from "@botiverse/raft-ui";

A lightbox shows an image or a file preview at full size over the whole window: the image on a full-screen layer-backdrop, previous and next, and one icon button to close (Built from existing parts). Use it when someone opens an image or an attachment from a message. It is for media and previews only.

lightbox/compositions/image-lightbox-shell

When to use

SituationUse
Viewing an image, a screenshot or a video at full sizeLightbox, image shell
Previewing an attached file (an HTML page, a document) at full sizeLightbox, attachment shell
A file read top to bottom (a PDF, Markdown, a long diagram)Lightbox, attachment shell in document mode
A decision, a form or anything that is not mediaDialog
Details people read beside the pageDrawer
The attachment in the message itselfMessageAttachment

Anatomy

  • Lightbox: the root. It holds whether the lightbox is open. Required.
  • LightboxTrigger: what opens it, usually the image or the file card in the message.
  • LightboxContent: the full-screen layer, with the scrim behind it. Required. Popups opened inside it are drawn above it (Popups inside).
  • LightboxTitle: the file name.
  • LightboxClose: the close button. Required.
  • LightboxStage: the area that centers the media. Required.
    • LightboxMedia: the image or video, at most 80% of the window's height and 86% of its width.
    • LightboxNavigationButton: previous (direction="previous") and next (direction="next"), 44px buttons 32px from the left and right edges.
    • LightboxInfoDock with LightboxInfoBar: a small floating bar near the bottom, on layer-popover with shadow-float, holding LightboxInfoTitle (the file name) and LightboxInfoActions (such as Download). It is a floating toolbar, 36px tall at every input, the message hover toolbar's touch size: its buttons are 28px, 4px inside it and 4px apart, with radius-chip, and a 44px tall hit area on a touch screen that reaches only half the gap toward a neighbour (Shape).
    • LightboxThumbnailStrip with LightboxThumbnails: 48px thumbnails along the bottom; active marks the one shown.
  • LightboxToolbar: the attachment shell's top bar instead of the floating close button, on layer-canvas with a line-seam under it. It holds LightboxHeaderMeta (a LightboxHeaderLabel or Badge naming the preview type, then LightboxTitle) and LightboxActions, with LightboxClose last.
  • LightboxBody: the scrolling body under LightboxToolbar in document mode, a ScrollArea that fills the rest of the content, on layer-canvas, the toolbar's surface. Its className sets another surface (bg-layer-sidebar), and every ScrollArea prop, render included, is the area's. Its content keeps clear of a phone's safe areas, and contentClassName and contentStyle style a box of the caller's inside that, so its own padding adds to the safe area instead of replacing it. Experimental, from @botiverse/raft-ui/wip.
  • LightboxHeader: a plain top row for a title and actions, for shells that need neither toolbar nor floating close.

Variants

VariantUse for
Image shell: LightboxClose directly in LightboxContentImages and video. The close button floats at the top right; the image has the whole window
Attachment shell: LightboxToolbar above LightboxStageA file preview that fills the stage, such as an HTML page in an iframe. The toolbar names the file and holds its actions and the close button
lightbox/compositions/attachment-preview-shell

Document mode

A file read top to bottom, a PDF, a Markdown or text file or a long diagram, opens in the attachment shell with modal="trap-focus" on Lightbox, and the file's own body under the toolbar scrolls by itself in LightboxBody, a ScrollArea that fills the rest (min-h-0 flex-1). It is on layer-canvas, as the toolbar is; the app chooses another surface with className. LightboxContent lets the pointer through to the scrim, so the body takes it back with pointer-events-auto, as the toolbar does. The page's scroll isn't locked, so the body must not pass a scroll on to the page under the scrim: it takes overscroll-contain, since a ScrollArea stops a scroll only while it has something to scroll, and a short file's body would otherwise scroll the page behind. A body of your own in place of LightboxBody takes pointer-events-auto, min-h-0 flex-1 and overscroll-contain itself. Document mode is a value of Base UI's own modal prop rather than a prop of its own: it changes how the lightbox holds the page, not which parts it has, and a second switch beside modal would allow pairs that mean nothing, such as a document mode that isn't modal.

lightbox/compositions/document-preview-shell

States

  • First and last item: previous or next is disabled. It stays focusable (aria-disabled) and ignores clicks.
  • Thumbnails: the shown item has a solid edge; the others are dimmed with layer-backdrop-nested (40%), 28% on hover. The strip lies on layer-backdrop, dark in both modes, so the nested scrim dims a thumbnail the same way in light and dark mode (Surfaces).

Behavior

  • A lightbox appears and disappears without animation, like the other centered overlays (Motion).
  • Mounted already open (open or defaultOpen), it opens one commit after it mounted, before the first paint, as Dialog does, so overlays mounted open together still stack in their order.
  • Esc, the close button, a click on the scrim and Cmd+W (Ctrl+W) close it. Cmd+W closes the lightbox, not the browser tab; pass closeOnModW={false} to leave the shortcut to the browser.
  • With a popup or a dialog open inside the lightbox, Esc closes only that, and the next Esc the lightbox. Cmd+W closes the whole lightbox, whatever is open inside it.
  • The close button and the bars keep clear of the phone's safe areas. So does what LightboxBody holds, at the bottom and the sides (env(safe-area-inset-*) on its content, not on the body), while the body and its scrollbar reach the window's edges: on a phone held sideways a wide file doesn't run under the notch, and its end isn't under the home bar.
  • The caller keeps which item is shown. Previous, next, the thumbnails and the arrow keys all change that index.
  • In document mode (modal="trap-focus") focus stays trapped in the lightbox, but the page's scroll isn't locked: the document scrolls in its own body, and Base UI's lock would only rewrite the styles of the page's <html> and <body> under the scrim, again on every resize. The scrim still covers the page, so a press beside the file lands on the scrim and closes the lightbox, as in the default mode.
  • In document mode a scroll in LightboxBody stays in it, at its ends and when the file is too short to scroll: the page under the scrim never moves. A Mac trackpad or iOS still bounces the body at its ends; overscroll-none in its className stops that too.
  • The keys stay in it as well. Focus lands on LightboxBody when the lightbox opens (Accessibility), so the arrow keys, Page Up, Page Down, Space, Home and End scroll the file at once. Pressed anywhere else in the lightbox, on a toolbar button or on the lightbox itself after a tap, a key that would scroll the page under the scrim scrolls the body instead, by the browser's own steps; focus stays where it is. A key a control uses is the control's: a text field's arrows and Space, a button's Space, a menu's arrows. With a body of your own in place of LightboxBody, such a key does nothing rather than scroll the page.
  • The browser's find in page reaches the lightbox in every mode: its content is never inert or hidden while it is open. Focus that lands on something below the fold scrolls the body to it, as on a page.
  • A lightbox is drawn above dialogs and sheets (Layer order). It is a modal surface of the stack in every mode: one opened from a dialog draws the lighter layer-backdrop-nested over it and is the second of the two modal surfaces allowed, and a Dialog, AlertDialog or Drawer opened inside a lightbox draws the nested scrim over the lightbox. A dialog that opens a lightbox that opens a dialog would be three, which Elevation doesn't allow: replace the top one, or move the flow to a page.
  • Toasts are drawn above the lightbox, so a toast fired from inside it (Link copied, a failed download) is seen. While it is open, show only the toasts it causes, without an action, as for a dialog (Toast).

Popups inside

A Tooltip, Popover, menu and its submenus, Select, Combobox, PreviewCard, Dialog, AlertDialog or Drawer opened inside LightboxContent is drawn above the lightbox and works as it does anywhere: a press in it is a press inside the lightbox, Esc closes it first, focus stays trapped, and it is neither inert nor hidden from assistive technology. Base UI already puts a popup opened inside the lightbox in the lightbox's portal rather than on the body; the lightbox's portal is a stacking context of its own, at the lightbox's place in the layer order, so the popup's own order counts inside it, above the content. Without that context the popup, already in the lightbox's portal, would be stacked against the lightbox's scrim at the popup's own lower order: drawn under the scrim, with a press on it landing on the scrim and closing the lightbox. A lightbox opened from a phone sheet or a drawer is a surface of its own: its popups keep to the window, not to the sheet under it (Dialog).

A layer of your own (a popup that isn't raft-ui's, a portal you render yourself) gets the same only by being a Base UI popup rendered inside LightboxContent. One portaled to the body, with createPortal or a library's own portal, is drawn under the lightbox. Render it in place inside LightboxContent instead, with pointer-events-auto (the content lets the pointer through to the scrim): it is drawn in the lightbox in document order, above the parts before it, and needs no z-index.

Content

  • Title the lightbox with the file name. A long name truncates at the end.
  • Name the preview type in the toolbar's label in sentence case: HTML preview.

Accessibility

  • Lightbox wraps the Base UI Dialog. It provides the dialog role, the focus trap and Esc; raft-ui adds the Cmd+W shortcut.
  • LightboxTitle is plain text, not the dialog's name. Name the dialog yourself: aria-label on LightboxContent, or a render element with one.
  • In document mode focus moves into the lightbox when it opens and goes back to LightboxTrigger when it closes. It lands on LightboxBody, so a keyboard user reads and scrolls the file at once and Tab goes on from there; on the first control when there is no body; and after a tap on the lightbox itself, as Base UI does, so a phone shows no ring and opens no keyboard. An initialFocus of your own wins over all three. The body is a tab stop even when the file is too short to scroll, and draws the inset ring inside its edge, which is the window's, where a ring outside it couldn't be seen (Focus ring). In the default mode LightboxContent doesn't move focus when it opens or return it when it closes (initialFocus and finalFocus are false); pass them when keyboard users open the lightbox, so focus lands in it and goes back to the trigger. In either mode, a lightbox opened from state, without LightboxTrigger (a click on an image in a message, a keyboard shortcut), has no trigger to go back to: pass finalFocus={openerRef}, a ref to what opened it, so focus returns there when it closes.
  • Focus is trapped in the lightbox in both modal and document mode; the page under it is hidden from assistive technology (aria-hidden), not made inert.
  • Give LightboxClose, each LightboxNavigationButton and each icon-only action an aria-label: "Close", "Previous image", "Next image", "Download".
  • Bind the Left and Right arrow keys to previous and next while the lightbox is open.
  • Give each LightboxThumbnail an aria-label ("Show product-shot.png"). active changes only how it looks: mark the shown one with aria-current="true".
  • Give the image an alt that describes it, and an iframe a title.

Rules

  • Don't build a media viewer from a Dialog, and don't put anything other than media or a file preview in a Lightbox.
  • Give the lightbox one close button, at the top right (Navigation).
  • In the toolbar, a framed button ends its shape on the 16px margin; a frameless icon button, as the last action or the close, ends its icon box there and its hit area hangs past it (Alignment).
  • In Raft Chat every file opens in the attachment shell, images included, so each file type has the same toolbar (File viewer).
  • Don't give a lightbox or its parts a z-index of your own: raft-ui sets the order of the lightbox, the dialogs, the toasts and the popups inside it (Layer order), and a number of the app's own puts the lightbox above or below popups it doesn't know about.
  • Don't add colors or lines beyond the scrim, the info bar and the toolbar (Built from existing parts).

API

A full-screen viewer over the scrim for images and attachment previews, with previous and next, thumbnails and one button to close. Pass `modal="trap-focus"` for a file read top to bottom (document mode).
PropTypeDefault
childrenReactNode
closeOnModWWhether Cmd+W (Ctrl+W) closes the lightbox instead of the browser tab.booleantrue
modalHow the lightbox holds the page while it is open. - `true`: focus is trapped in it and the page's scroll is locked. For an image or a preview that fits the window. - `"trap-focus"`: document mode, for a file read top to bottom (a PDF, a document, a diagram). Focus is still trapped, but the page's scroll is not locked: the document's own body scrolls, and Base UI's lock would only rewrite the page's `<html>` and `<body>` styles under it, again on every resize. The scrim still covers the page. - `false`: nothing is held; focus can leave the lightbox.boolean | "trap-focus"true
defaultOpenWhether the dialog is initially open.booleanfalse
defaultTriggerIdID of the trigger that the dialog is associated with. This is useful in conjunction with the `defaultOpen` prop to create an initially open dialog.string
disablePointerDismissalWhether to prevent the dialog from closing on outside presses. For non-modal dialogs, this also prevents the dialog from closing when focus moves outside of it.booleanfalse
onOpenChangeEvent handler called when the dialog is opened or closed.(open: boolean, eventDetails: Dialog.Root.ChangeEventDetails) => void
onOpenChangeCompleteEvent handler called after any animations complete when the dialog is opened or closed.(open: boolean) => void
openWhether the dialog is currently open.boolean
triggerIdID of the trigger that the dialog is associated with. This is useful in conjunction with the `open` prop to create a controlled dialog. There's no need to specify this prop when the dialog is uncontrolled (that is, when the `open` prop is not set).string

LightboxActions

PropTypeDefault
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

LightboxClose

PropTypeDefault
nativeButtonWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`).booleantrue
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

LightboxContent

The full-screen layer, with the scrim behind it. Popups opened inside it are drawn above it (`LightboxPortal`). `initialFocus` and `finalFocus` are `false` by default, so focus doesn't move when it opens or closes, except in document mode (`modal="trap-focus"`): focus moves into the lightbox when it opens and back to `LightboxTrigger` when it closes. It lands on `LightboxBody`, so the keys that scroll scroll the document at once; on the first control when there is no body; and on the lightbox itself after a tap, so a phone shows no ring and opens no keyboard. A key that would scroll the page under the scrim scrolls the body instead.
PropTypeDefault
finalFocusDetermines the element to focus when the dialog is closed.boolean | RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | HTMLElement | null | void)
initialFocusDetermines the element to focus when the dialog is opened. By default, focus moves to the first tabbable element inside the popup, except when the dialog is opened by touch — then the popup itself is focused to avoid opening the virtual keyboard.boolean | RefObject<HTMLElement | null> | ((openType: InteractionType) => boolean | HTMLElement | null | void)
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

LightboxHeader, LightboxHeaderLabel, LightboxHeaderMeta, LightboxInfoActions, LightboxInfoBar, LightboxInfoDock, LightboxInfoTitle, LightboxMedia

PropTypeDefault
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

LightboxNavigationButton

PropTypeDefault
direction (required)"next" | "previous"
disabledboolean
loadingLabelThe spinner's accessible name while `loading`: say what is happening.string
focusableWhenDisabledWhether the button should be focusable when disabled.booleanfalse
nativeButtonWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`).booleantrue
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

LightboxPortal

The lightbox's own stacking context, at `z-lightbox` (70) above the page's dialogs and popups: the scrim, the content, and every Base UI popup opened from inside, which Base UI portals into this element by default, in DOM order. A popup's own `z-popup` then counts inside the lightbox, so it is drawn above the content instead of under the scrim, and a press on it is a press inside the lightbox.
PropTypeDefault
containerA parent element to render the portal element into.HTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null>
keepMountedWhether to keep the portal mounted in the DOM while the popup is hidden.booleanfalse
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

LightboxStage

PropTypeDefault
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

LightboxThumbnail

PropTypeDefault
activebooleanfalse
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

LightboxThumbnailStrip, LightboxTitle, LightboxToolbar

PropTypeDefault
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

LightboxTrigger

PropTypeDefault
handleA handle to associate the trigger with a dialog. Can be created with the Dialog.createHandle() method.Dialog.Handle<unknown>
idID of the trigger. In addition to being forwarded to the rendered element, it is also used to specify the active trigger for the dialog in controlled mode (with the DialogRoot `triggerId` prop).string
nativeButtonWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`).booleantrue
payloadA payload to pass to the dialog when it is opened.{}
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

LightboxBody

ExperimentalThe scrolling body of a document-mode lightbox (`modal="trap-focus"`), under `LightboxToolbar`: a ScrollArea that fills the rest of the content (`min-h-0 flex-1`) on `layer-canvas`, the toolbar's surface. Pass a `className` for another surface (`bg-layer-sidebar`). It takes the pointer back from `LightboxContent`, which lets it through to the scrim, and never passes a scroll on to the page under the scrim (`overscroll-contain`, also while it has nothing to scroll; `overscroll-none` in `className` also stops the bounce). Every other ScrollArea prop, `render` included, is the area's.
PropTypeDefault
actionsRefMethods to scroll from code that the container doesn't have natively.((instance: ScrollAreaActions | null) => void | (() => void | { [UNDEFINED_VOID_ONLY]: never; }) | DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES[keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES]) | RefObject<ScrollAreaActions | null>
childrenThe scrolling content, in ScrollArea's own container.ReactNode
contentClassNameClass of a content element around `children`, inside the container: put padding and the content's own layout (grid, flex, `gap`, `stack-y-*`) here, so the scrollbars stay at the edges and the gutters layer is never one of the laid-out children. Only with it, `contentStyle` or `fade` is there one.string
contentRefAn element whose size is the content's, watched for size changes (such as a hidden mirror of a textarea's text). Without it each element child of the container is watched, and children added or removed.RefObject<HTMLElement | null>
contentStyleStyle of the content element; renders it, as `contentClassName` does.CSSProperties
fadeFade the content out toward an edge that has more past it. The scrollbars don't fade.boolean
hideDelayHow long the scrollbars stay after scrolling stops, in ms. At least 100 with `trigger="scroll"`.number
onEdgeChangeCalled when the area reaches or leaves an edge, and after mounting when the content overflows.(edges: ScrollAreaEdges) => void
onScrollThe container's scroll events.UIEventHandler<HTMLDivElement>
orientationThe directions the area scrolls in. Content too wide for a vertical area is cut off.ScrollAreaOrientation
railInsetRoom in px the scrollbars keep clear at each end, such as a sticky header's height, so they don't run over it. Also settable in CSS, on the root: `--r-scroll-area-rail-inset-{top,bottom,left,right}`, which don't inherit.ScrollAreaRailInset
renderAnother element in place of ScrollArea's own `<div>`, as the container (an element or a function that gets the props). It must hold children: the gutters are its first child.ReactElement | ((props, state) => ReactElement)
sizeThumb thickness: `small` is 5px, `medium` 7px; both grow while pointed at or dragged.ScrollAreaSize
tabIndexThe container's: by default `0` while there is something to scroll.number
triggerWhen the scrollbars show. They always show while scrolling or dragging.ScrollAreaTrigger

Also takes the attributes of its element.

Changes

  • 0.84.0 · AddedLightboxBody, the scrolling body of a document-mode lightbox (from @botiverse/raft-ui/wip): a ScrollArea under LightboxToolbar that fills the rest of the content (min-h-0 flex-1), takes the pointer back from LightboxContent (pointer-events-auto), and never passes a scroll on to the page under the scrim (overscroll-contain), on layer-canvas, the toolbar's surface. className sets another surface (bg-layer-sidebar); every other ScrollArea prop, render included, is the area's, except overscroll. It carries data-slot="lightbox-body". It is a tab stop even when it has nothing to scroll (tabIndex={0}), with the inset focus ring (a 2px foreground outline inside its edge, after keyboard input only) instead of ScrollArea's ring outside it, which the window's edges hid. Its content is padded by env(safe-area-inset-left|right|bottom), and contentClassName and contentStyle style a box of the caller's inside that (data-slot="lightbox-body-content"), so a phone held sideways doesn't put a wide file under the notch or its end under the home bar, and the scrollbar stays at the window's edge.
  • 0.84.0 · AddedLightbox document mode. <Lightbox modal="trap-focus"> traps focus without locking the page's scroll, for a file read top to bottom whose own body scrolls (a PDF, Markdown, a diagram); the content stays neither inert nor hidden, so find in page reaches it. In document mode LightboxContent takes Base UI's own focus defaults: focus moves into the lightbox when it opens and back to LightboxTrigger when it closes. A lightbox opened from state passes finalFocus={openerRef} to send focus back to what opened it.
  • 0.84.0 · FixedLightbox document mode: the keys scroll the document, never the page under the scrim: with initialFocus unset, a document lightbox with a LightboxBody focuses the body when opened by a click or a key, so Page Down, the arrow keys and Space scroll the file at once; before, focus landed on the first toolbar control (Close), and those keys scrolled the page under the scrim while the file stayed put. A tap still focuses the lightbox itself, and a lightbox without a body still focuses its first control. A key that would scroll the page from elsewhere in the lightbox (a toolbar button, the lightbox itself after a tap) scrolls LightboxBody instead, by the browser's own steps, and does nothing with a body of the caller's own; keys a control uses (a text field's, a button's Space, a menu's arrows) are left to it.
  • 0.84.0 · FixedWhat opens inside a lightbox is drawn above it, and a lightbox opened from a dialog has its scrim: LightboxPortal is a stacking context of its own at z-70 (relative isolate z-70), with the scrim and the content inside it in document order. Base UI already put a Tooltip, Popover, menu and its submenus, Select, Combobox, PreviewCard, Dialog, AlertDialog or Drawer opened inside LightboxContent in the lightbox's portal; now its own z-50 counts inside the lightbox, above the content. The lightbox's scrim is always rendered (forceRender), and the lightbox joins the stack of modal surfaces: opened from a dialog it draws layer-backdrop-nested (data-nested), and a Dialog, AlertDialog or Drawer opened inside it draws the nested scrim over it.
  • 0.1.0 · AddedExtended Lightbox with thumbnail strip and related chrome parts; added ComboboxTriggerCount and ContextMenuPopup.