nook
Browse the docs

Components

Image

Built on Base UI

An image in a frame that holds its shape, with loading, failure and a large view.

See the prompt
Dashboard preview
Press the image to view it larger
Team photo from the offsite
When a file cannot load, the frame says so

Installation

npx nook-ui@latest add image

Prompt

Paste this into your AI coding assistant. It adds this exact component, the same way the CLI does, and tells the assistant how to use it.

Short. For assistants that can run commands in your project.

Plain text
Add the Nook "Image" component to this project, exactly as Nook ships it.

An image in a frame that holds its shape, with loading, failure and a large view.

Steps
1. Check for `nook.json` in the project root. If it is missing, run `npx nook-ui@latest init` first (React or Next.js with Tailwind CSS v4). It adds the Nook tokens, the theme and `cn()`.
2. Run `npx nook-ui@latest add image`. It writes `components/ui/image.tsx` together with everything the component needs. Do not write the component from memory.
3. If you cannot run commands, fetch http://localhost:3000/r/image.json, write every entry of `files` to the path it names, install the packages in `dependencies`, and repeat for each name in `registryDependencies`.
4. Use it where I ask. Start from this example:

```tsx
import { Image } from "@/components/ui/image";

const art = (from: string, to: string) =>
  `data:image/svg+xml,${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 90"><defs><linearGradient id="g" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="${from}"/><stop offset="1" stop-color="${to}"/></linearGradient></defs><rect width="160" height="90" fill="url(#g)"/><g fill="#fff" fill-opacity=".85"><rect x="64" y="34" width="8" height="8"/><rect x="76" y="34" width="8" height="8"/><rect x="88" y="34" width="8" height="8"/><rect x="76" y="46" width="8" height="8"/></g></svg>`)}`;

export default function ImageDefault() {
  return (
    <div className="grid w-full max-w-xl gap-4 sm:grid-cols-2">
      <Image src={art("#2952e3", "#0f1d54")} alt="Dashboard preview" caption="Press the image to view it larger" zoomable />
      <Image src="/missing-picture.png" alt="Team photo from the offsite" ratio={16 / 9} caption="When a file cannot load, the frame says so" />
    </div>
  );
}
```

API
- `Image`: The frame, the picture and an optional caption.
  - `alt` (`string`): What the image shows.
  - `ratio` (`number`, default 16 / 9): Width divided by height.
  - `fit` (`"cover" | "contain"`, default "cover"): Fill the frame (and crop), or show the whole image.
  - `caption` (`ReactNode`): Text under the image.
  - `zoomable` (`boolean`): Press the image to see it large.
  - `fallback` (`ReactNode`): Shown if the file cannot load.
- `ImagePlaceholder`: A tile that stands in for a picture: a chart color with a flat drawing in the page colors.
  - `label` (`string`): What the picture will show, for assistive tech.
  - `src` (`string`): Optional picture over the drawing. The drawing stays visible when the picture is unavailable.
  - `ratio` (`number`, default 1): Width divided by height.
  - `tone` (`1 | 2 | 3 | 4 | 5`, default 1): Which chart color the tile uses.
  - `scene` (`"hills" | "city" | "stripes" | "blocks" | "waves"`): The drawing on the tile. Picked from the label when left out.

Rules
- Colors, radii, shadows and motion come from Nook tokens and classes such as `bg-primary`, `text-muted-foreground`, `border-border`, `rounded-lg` and `h-control`. Never hard-code them.
- Do not restyle or simplify the file. Change how it looks through `className`, its props and the theme.
- It must keep working with the keyboard, in light and dark mode, and in the sharp, soft and bold styles.
- Accessibility: `alt` is required: pass an empty string only for decoration. The frame reserves its space so the page does not jump. A failed image shows its alt text. The large view is a dialog: Escape or a click anywhere closes it.

Done when the component renders like the example in light and dark mode and the project type-checks.

Usage

import { Image } from "@/components/ui/image";

const art = (from: string, to: string) =>
  `data:image/svg+xml,${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 90"><defs><linearGradient id="g" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="${from}"/><stop offset="1" stop-color="${to}"/></linearGradient></defs><rect width="160" height="90" fill="url(#g)"/><g fill="#fff" fill-opacity=".85"><rect x="64" y="34" width="8" height="8"/><rect x="76" y="34" width="8" height="8"/><rect x="88" y="34" width="8" height="8"/><rect x="76" y="46" width="8" height="8"/></g></svg>`)}`;

export default function ImageDefault() {
  return (
    <div className="grid w-full max-w-xl gap-4 sm:grid-cols-2">
      <Image src={art("#2952e3", "#0f1d54")} alt="Dashboard preview" caption="Press the image to view it larger" zoomable />
      <Image src="/missing-picture.png" alt="Team photo from the offsite" ratio={16 / 9} caption="When a file cannot load, the frame says so" />
    </div>
  );
}

Examples

Aspect ratios

Placeholder scenes

Contain or cover

Harbor logo, fitted inside the frame
fit=contain
Harbor logo, cropped to fill the frame
fit=cover

Props

Image

The frame, the picture and an optional caption.

PropTypeDefaultDescription
altstring–What the image shows.
rationumber16 / 9Width divided by height.
fit"cover" | "contain""cover"Fill the frame (and crop), or show the whole image.
captionReactNode–Text under the image.
zoomableboolean–Press the image to see it large.
fallbackReactNode–Shown if the file cannot load.

ImagePlaceholder

A tile that stands in for a picture: a chart color with a flat drawing in the page colors.

PropTypeDefaultDescription
labelstring–What the picture will show, for assistive tech.
srcstring–Optional picture over the drawing. The drawing stays visible when the picture is unavailable.
rationumber1Width divided by height.
tone1 | 2 | 3 | 4 | 51Which chart color the tile uses.
scene"hills" | "city" | "stripes" | "blocks" | "waves"–The drawing on the tile. Picked from the label when left out.

Accessibility

`alt` is required: pass an empty string only for decoration. The frame reserves its space so the page does not jump. A failed image shows its alt text. The large view is a dialog: Escape or a click anywhere closes it.

Checked against WCAG 2.1 AA with automated axe tests and keyboard tests on every release.