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.
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.
When to use
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.LightboxInfoDockwithLightboxInfoBar: a small floating bar near the bottom, onlayer-popoverwithshadow-float, holdingLightboxInfoTitle(the file name) andLightboxInfoActions(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, withradius-chip, and a 44px tall hit area on a touch screen that reaches only half the gap toward a neighbour (Shape).LightboxThumbnailStripwithLightboxThumbnails: 48px thumbnails along the bottom;activemarks the one shown.
LightboxToolbar: the attachment shell's top bar instead of the floating close button, onlayer-canvaswith aline-seamunder it. It holdsLightboxHeaderMeta(aLightboxHeaderLabelor Badge naming the preview type, thenLightboxTitle) andLightboxActions, withLightboxCloselast.LightboxBody: the scrolling body underLightboxToolbarin document mode, a ScrollArea that fills the rest of the content, onlayer-canvas, the toolbar's surface. ItsclassNamesets another surface (bg-layer-sidebar), and every ScrollArea prop,renderincluded, is the area's. Its content keeps clear of a phone's safe areas, andcontentClassNameandcontentStylestyle 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
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.
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 onlayer-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 (
openordefaultOpen), 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
LightboxBodyholds, 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
LightboxBodystays 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-nonein itsclassNamestops that too. - The keys stay in it as well. Focus lands on
LightboxBodywhen 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 ofLightboxBody, 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
inertor 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-nestedover 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
dialogrole, the focus trap and Esc; raft-ui adds the Cmd+W shortcut. LightboxTitleis plain text, not the dialog's name. Name the dialog yourself:aria-labelonLightboxContent, or arenderelement with one.- In document mode focus moves into the lightbox when it opens and goes back to
LightboxTriggerwhen it closes. It lands onLightboxBody, 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. AninitialFocusof 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 modeLightboxContentdoesn't move focus when it opens or return it when it closes (initialFocusandfinalFocusarefalse); 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, withoutLightboxTrigger(a click on an image in a message, a keyboard shortcut), has no trigger to go back to: passfinalFocus={openerRef}, a ref to what opened it, so focus returns there when it closes. - Focus is trapped in the lightbox in both
modaland document mode; the page under it is hidden from assistive technology (aria-hidden), not madeinert. - Give
LightboxClose, eachLightboxNavigationButtonand each icon-only action anaria-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
LightboxThumbnailanaria-label("Show product-shot.png").activechanges only how it looks: mark the shown one witharia-current="true". - Give the image an
altthat describes it, and aniframeatitle.
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
Lightbox
LightboxActions
Also takes the attributes of its element.
LightboxClose
Also takes the attributes of its element.
LightboxContent
Also takes the attributes of its element.
LightboxHeader, LightboxHeaderLabel, LightboxHeaderMeta, LightboxInfoActions, LightboxInfoBar, LightboxInfoDock, LightboxInfoTitle, LightboxMedia
Also takes the attributes of its element.
LightboxNavigationButton
Also takes the attributes of its element.
LightboxPortal
Also takes the attributes of its element.
LightboxStage
Also takes the attributes of its element.
LightboxThumbnail
Also takes the attributes of its element.
LightboxThumbnailStrip, LightboxTitle, LightboxToolbar
Also takes the attributes of its element.
LightboxTrigger
Also takes the attributes of its element.
LightboxBody
Also takes the attributes of its element.
Related
Changes
- 0.84.0 · AddedLightboxBody, the scrolling body of a document-mode lightbox (from
@botiverse/raft-ui/wip): a ScrollArea underLightboxToolbarthat fills the rest of the content (min-h-0 flex-1), takes the pointer back fromLightboxContent(pointer-events-auto), and never passes a scroll on to the page under the scrim (overscroll-contain), onlayer-canvas, the toolbar's surface.classNamesets another surface (bg-layer-sidebar); every other ScrollArea prop,renderincluded, is the area's, exceptoverscroll. It carriesdata-slot="lightbox-body". It is a tab stop even when it has nothing to scroll (tabIndex={0}), with the inset focus ring (a 2pxforegroundoutline inside its edge, after keyboard input only) instead of ScrollArea's ring outside it, which the window's edges hid. Its content is padded byenv(safe-area-inset-left|right|bottom), andcontentClassNameandcontentStylestyle 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 neitherinertnor hidden, so find in page reaches it. In document modeLightboxContenttakes Base UI's own focus defaults: focus moves into the lightbox when it opens and back toLightboxTriggerwhen it closes. A lightbox opened from state passesfinalFocus={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
initialFocusunset, a document lightbox with aLightboxBodyfocuses 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) scrollsLightboxBodyinstead, 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:
LightboxPortalis a stacking context of its own atz-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 insideLightboxContentin the lightbox's portal; now its ownz-50counts 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 drawslayer-backdrop-nested(data-nested), and a Dialog, AlertDialog or Drawer opened inside it draws the nested scrim over it. - 0.1.0 · AddedExtended
Lightboxwith thumbnail strip and related chrome parts; addedComboboxTriggerCountandContextMenuPopup.