import { Button } from "@senvenli/react/action";
import { ImageViewer } from "@senvenli/react/display";
import { useState } from "react";
// Flat placeholder photos drawn in the file, so the example needs no network.
const photo = (label: string, hue: number) =>
`data:image/svg+xml,${encodeURIComponent(
`<svg xmlns="http://www.w3.org/2000/svg" width="1600" height="900"><rect width="100%" height="100%" fill="hsl(${hue} 30% 62%)"/><text x="50%" y="50%" fill="hsl(${hue} 30% 20%)" font-family="system-ui" font-size="72" text-anchor="middle" dominant-baseline="middle">${label}</text></svg>`,
)}`;
const PHOTOS = [
{ src: photo("Front view", 210), alt: "The standing desk from the front" },
{ src: photo("Side view", 150), alt: "The desk from the side, raised" },
{ src: photo("Detail", 30), alt: "The height controls up close" },
];
export default function Example() {
const [isOpen, setOpen] = useState(false);
return (
<>
<Button iconBefore="images" onPress={() => setOpen(true)}>
View photos
</Button>
<ImageViewer items={PHOTOS} title="Standing desk" isOpen={isOpen} onOpenChange={setOpen} />
</>
);
}import { ImageViewer } from "@senvenli/react/display";Examples
Open on a chosen photo
Each trigger opens the viewer on its own photo.import { Button } from "@senvenli/react/action";
import { ImageViewer } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { useState } from "react";
// Flat placeholder photos drawn in the file, so the example needs no network.
const photo = (label: string, hue: number) =>
`data:image/svg+xml,${encodeURIComponent(
`<svg xmlns="http://www.w3.org/2000/svg" width="1600" height="900"><rect width="100%" height="100%" fill="hsl(${hue} 30% 62%)"/><text x="50%" y="50%" fill="hsl(${hue} 30% 20%)" font-family="system-ui" font-size="72" text-anchor="middle" dominant-baseline="middle">${label}</text></svg>`,
)}`;
const PHOTOS = [
{ label: "Front view", src: photo("Front view", 210) },
{ label: "Side view", src: photo("Side view", 150) },
{ label: "Detail", src: photo("Detail", 30) },
];
export default function Example() {
const [openAt, setOpenAt] = useState<number | null>(null);
return (
<>
<Stack direction="horizontal" space="space-100">
{PHOTOS.map((item, index) => (
<Button key={item.label} onPress={() => setOpenAt(index)}>
{item.label}
</Button>
))}
</Stack>
{openAt !== null && (
<ImageViewer
items={PHOTOS.map((item) => ({ src: item.src, alt: item.label }))}
defaultIndex={openAt}
title="Standing desk"
onOpenChange={(open) => !open && setOpenAt(null)}
/>
)}
</>
);
}Guidelines
When to use it
- Looking at photos full size.
Accessibility
Keyboard
| Keys | What happens |
|---|---|
| Arrow left / right | Previous or next photo, following the reading direction. |
| Esc | Closes the viewer. |
Built in
- Focus stays inside while open and returns to what opened it.
You provide
- A title and alt text.
API reference
ImageViewer
| Prop | Type | Default | Description |
|---|---|---|---|
image | ImageViewerImage | PlainImage | The image renderer (e.g. next/image); a plain |
itemsRequired | CarouselItem[] | – | The photos in display order, each with its own alt text: the same shape as Carousel's |
defaultIndex | number | 0 | The photo to open on; wrapped into range. |
isOpen | boolean | true | Open by default, so rendering it opens it; or control it with |
onOpenChange | (isOpen: boolean) => void | – | Called with |
titleRequired | string | – | Caption shown at the bottom and used as the accessible dialog title. |
labels | { close?: string; previous?: string; next?: string; position?: (index: number, total: number) => string; } | – | The viewer's words; the DS strings by default ("Close", "Previous image", "Next image", "Image 2 of 5"). |
imageImageViewerImageDefaultPlainImageThe image renderer (e.g. next/image); a plain
<img>by default.itemsRequiredCarouselItem[]The photos in display order, each with its own alt text: the same shape as Carousel's
items.defaultIndexnumberDefault0The photo to open on; wrapped into range.
isOpenbooleanDefaulttrueOpen by default, so rendering it opens it; or control it with
onOpenChange.onOpenChange(isOpen: boolean) => voidCalled with
falseon Esc, the close button or a press on the scrim.titleRequiredstringCaption shown at the bottom and used as the accessible dialog title.
labels{ close?: string; previous?: string; next?: string; position?: (index: number, total: number) => string; }The viewer's words; the DS strings by default ("Close", "Previous image", "Next image", "Image 2 of 5").