# Image viewer

A picture over the whole window: it grows from where it was, zooms at the pointer, pans, and goes to the next or previous.

Live variants: https://design.arcadia.is/components/image-viewer

```tsx
import { ImageViewer } from "@arcadia/ui";
```

```tsx
import { useState, useEffect, useRef } from "react";
import { ImageAlbum, ImageViewer, ReactionPicker, Avatar } from "@arcadia/ui";

function Conversation({ initialIndex }: { initialIndex?: number } = {}) {
  const [open, setOpen] = useState<{ index: number; from: HTMLElement | null } | null>(null);
  useEffect(() => {
    if (initialIndex !== undefined) setOpen({ index: initialIndex, from: null });
  }, [initialIndex]);
  const page = useRef<HTMLDivElement>(null);
  const groups = [...new Set(SHOTS.map((s) => s.group))];
  const actions = (_: Shot): readonly ViewerAction[] => [
    { label: "Reply", icon: "reply", run: () => {}, closes: true, buttonProps: { "data-shot-action": "reply" } },
    {
      label: "React", icon: "react", buttonProps: { "data-shot-action": "react" },
      panel: (close) => <ReactionPicker choices={REACTION_SET} onChoose={close} />,
    },
    { label: "Copy image", icon: "copy", run: () => {} },
    { label: "Download", icon: "download", run: () => {} },
  ];
  return (
    <div ref={page} {...sx("cmp-viewer-column", ImageViewerStyles.column)}>
      {groups.map((g) => {
        const images = SHOTS.filter((s) => s.group === g);
        return (
          <ImageAlbum key={g} images={images} side={images[0]!.by === "Ada" ? "end" : "start"}
            onOpen={(index, from) => setOpen({ index: SHOTS.indexOf(images[index]!), from })} />
        );
      })}
      {open && (
        <ImageViewer
          images={SHOTS} index={open.index} from={open.from} onClose={() => setOpen(null)}
          placeOf={(image) => page.current?.querySelector<HTMLElement>(`[data-image="${image.id}"]`) ?? null}
          who={(image) => (
            <>
              <Avatar name={image.by} size={28} aria-hidden />
              <span {...sx("cmp-viewer-who", ImageViewerStyles.who)}>
                <span {...sx("cmp-viewer-name", ImageViewerStyles.name)}>{image.by}</span>
                <span {...sx("cmp-viewer-when", ImageViewerStyles.when)}>{image.at}</span>
              </span>
            </>
          )}
          caption={(image) => image.words}
          actions={actions}
        />
      )}
    </div>
  );
}
```

## Accessibility

A modal dialog named by the picture's `label`, the page behind inert; the picture shown carries that name as its alt text. The arrows at its sides are buttons named Previous and Next, there where a pointer can hover; on touch a swipe moves between pictures and a tap shows or hides the bar. Zoom in and Zoom out keep the middle of the picture in place. The words at rest are one Tab stop that opens them. The browser's Back closes it too. Closing gives the focus to the picture's own place, else to what had it. It does so once the overlay is gone, so a product that then moves the focus elsewhere (to its message field, after a reply) does it one frame later.

- Left, Right: The previous and the next picture
- Escape: Closes the words opened, then the viewer
- 0: The whole picture
- +, -: Closer and further, at the middle
- Enter, Space on the words: Opens them whole

## API reference

### ImageViewer

- `images` (`readonly T[]`, required)
- `index` (`number`, required)
- `from` (`HTMLElement | null`, default `null`)
- `around` (`ViewerAround<T>`)
- `label` (`(image: T) => string`, default `(i) => i.name ?? "Image"`)
- `who` (`(image: T) => ReactNode`)
- `caption` (`(image: T) => ReactNode`)
- `actions` (`(image: T) => readonly ViewerAction[]`)
- `placeOf` (`(image: T) => HTMLElement | null`)
- `onClose` (`() => void`, required)
- `rootStyle` (`RootStyle<ImageViewerKnobs>`)
