Toolbar
Toolbar
Paging, the counter and zoom in one floating toolbar, as in a photos app.
A real lightbox, open inside the frame. Use ←→ to page and Esc to close it.
import { createLightbox } from "@reuse-sh/lightbox";
import "@reuse-sh/lightbox/styles.css";
import "./lightbox-style.css";
import { addChrome } from "./lightbox-chrome.js";
const lightbox = createLightbox({
className: "lightbox-style",
});
lightbox.open(photos, 0);
addChrome(lightbox, photos);import { useLightbox } from "@reuse-sh/lightbox/react";
import { useRef } from "react";
import "@reuse-sh/lightbox/styles.css";
import "./lightbox-style.css";
import { addChrome } from "./lightbox-chrome.js";
export function Gallery({ photos }) {
const lightbox = useLightbox({
className: "lightbox-style",
});
const root = useRef(null);
const open = (index) => {
const images = root.current.querySelectorAll("img");
const items = photos.map((photo, i) => ({ ...photo, origin: images[i] }));
lightbox.open(items, index);
addChrome(lightbox, items);
};
return (
<div ref={root}>
{photos.map((photo, index) => (
<button key={photo.src} onClick={() => open(index)}>
<img src={photo.src} alt={photo.alt} />
</button>
))}
</div>
);
}lightbox-style.css
.lightbox-style {
--lightbox-pad: 16px;
--lightbox-side: 0px;
--lightbox-caption: 0px;
--lightbox-strip: 0px;
--lightbox-bar: 0px;
--lightbox-under: 0px;
--lightbox-bottom: calc(var(--lightbox-caption) + var(--lightbox-strip) + var(--lightbox-bar) + var(--lightbox-under));
--lightbox-ring: 0 0 0 0.5px rgb(0 0 0 / 10%), 0 1px 2px rgb(0 0 0 / 6%), 0 2px 6px rgb(0 0 0 / 4%);
--reuse-lightbox-background: #fcfcfc;
--reuse-lightbox-foreground: #1f1f1f;
--reuse-lightbox-control: #fff;
}
.lightbox-style *, .lightbox-style *::before, .lightbox-style *::after { box-sizing: border-box; }
.lightbox-style .reuse-lightbox-viewport {
width: calc(100% - var(--lightbox-side));
height: calc(100% - var(--lightbox-bottom));
}
.lightbox-style .reuse-lightbox-button {
width: 40px;
height: 40px;
padding: 0;
color: inherit;
box-shadow: var(--lightbox-ring);
backdrop-filter: none;
transition: background-color 150ms ease, transform 160ms cubic-bezier(0.23, 1, 0.32, 1), opacity 300ms ease;
}
.lightbox-style .reuse-lightbox-button svg { width: 18px; height: 18px; }
.lightbox-style .reuse-lightbox-button:hover { background: #f4f4f4; }
.lightbox-style .reuse-lightbox-button:active:not(:disabled) { transform: scale(0.95); }
.lightbox-style .reuse-lightbox-button:disabled { opacity: 0.3; }
.lightbox-style .reuse-lightbox-close, .lightbox-style .reuse-lightbox-zoom { top: var(--lightbox-pad); }
.lightbox-style .reuse-lightbox-close { right: var(--lightbox-pad); }
.lightbox-style .reuse-lightbox-zoom { right: calc(var(--lightbox-pad) + 48px); }
.lightbox-style .reuse-lightbox-footer {
right: calc(var(--lightbox-side) + var(--lightbox-pad));
bottom: calc(var(--lightbox-under) + var(--lightbox-pad));
left: var(--lightbox-pad);
gap: 10px;
}
.lightbox-style .reuse-lightbox-counter {
padding: 0;
background: none;
backdrop-filter: none;
font-size: 13px;
}
.lightbox-style { --lightbox-caption: 30px; }
.lightbox-style .reuse-lightbox-caption {
order: 1;
padding: 0;
background: none;
color: #4a4a4a;
font-size: 14px;
line-height: 20px;
}
.lightbox-style { --lightbox-bar: 58px; }
.lightbox-style-toolbar {
order: 3;
display: flex;
align-items: center;
gap: 2px;
height: 48px;
padding: 4px;
border-radius: 999px;
background: #fff;
box-shadow: 0 0 0 0.5px rgb(0 0 0 / 10%), 0 2px 4px rgb(0 0 0 / 5%), 0 8px 24px rgb(0 0 0 / 7%);
pointer-events: auto;
}
.lightbox-style-toolbar .reuse-lightbox-button { position: static; box-shadow: none; background: transparent; }
.lightbox-style-toolbar .reuse-lightbox-button:hover { background: rgb(0 0 0 / 5%); }
.lightbox-style-toolbar .reuse-lightbox-counter { min-width: 52px; padding: 0 6px; font-weight: 500; text-align: center; }
.lightbox-style-toolbar-divider { width: 1px; height: 20px; margin: 0 4px; background: rgb(0 0 0 / 10%); }
@media (max-width: 559px) {
.lightbox-style { --lightbox-pad: 12px; }
}
@media (prefers-reduced-motion: reduce) {
.lightbox-style, .lightbox-style * { transition-duration: 0ms !important; }
}lightbox-chrome.js
export function addChrome(lightbox, items) {
const dialog = document.querySelector("dialog.lightbox-style");
const part = (name) => dialog.querySelector(`.reuse-lightbox-${name}`);
const syncs = [];
const cleanups = [];
const bar = document.createElement("div");
bar.className = "lightbox-style-toolbar";
bar.setAttribute("role", "group");
bar.setAttribute("aria-label", "Photo controls");
bar.append(part("prev"), part("counter"), part("next"));
const divider = document.createElement("span");
divider.className = "lightbox-style-toolbar-divider";
divider.setAttribute("aria-hidden", "true");
bar.append(divider, part("zoom"));
part("footer").append(bar);
const sync = (state) => syncs.forEach((fn) => fn(state));
sync(lightbox.getState());
const stop = lightbox.subscribe((state) => {
if (state.isOpen) sync(state);
else if (!state.isClosing) {
stop();
cleanups.forEach((fn) => fn());
}
});
}