# Image

An image that loads lazily without shifting the layout, and can open a full-screen preview to zoom, pan and rotate.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-image`
- Source: [libs/ui/src/lib/molecules/image/image.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/molecules/image/image.ts)

```typescript title="Import"
import { Image } from '@vigdx/ui/image';
```

## Basic and preview

`src` and `alt` are all it needs; `width` and `height` reserve the space before it loads, and it renders with `loading="lazy"` and `decoding="async"` (`priority` loads the page's main image eagerly with `fetchpriority="high"`). `preview` turns it into a button that opens the viewer; `previewSrc` shows a larger file there. If the file fails to load, a labelled error state (`labels.errorLabel`, `data-state="error"`) replaces it and no preview is offered.

```typescript title="image-basic.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Image } from '@vigdx/ui/image';

import { PHOTOS } from './photos';

@Component({
  selector: 'app-image-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Image],
  template: `
    <div class="grid max-w-xl grid-cols-2 gap-4">
      <vig-image [src]="lake.src" [alt]="lake.alt" [width]="lake.width" [height]="lake.height" />
      <vig-image
        [src]="cove.src"
        [alt]="cove.alt"
        [width]="cove.width"
        [height]="cove.height"
        preview
      />
    </div>
  `,
})
export class ImageBasicDemo {
  protected readonly lake = PHOTOS[5];
  protected readonly cove = PHOTOS[3];
}
```

## Your own image, e.g. NgOptimizedImage

Leave `src` unset and put your own `<img>` inside: it is shown untouched, so `ngSrc`, `priority` and `srcset` of `NgOptimizedImage` keep working. The preview follows its `src`, `srcset` and `alt` as they change and shows the largest `srcset` candidate; set `previewSrc` for a dedicated file (a load error of a projected image is yours to handle). The kit cannot apply `NgOptimizedImage` for you (it needs your image loader), but `srcset`, `sizes`, `width`, `height` and `loading` pass through when it renders the image.

```typescript title="image-projected.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Image } from '@vigdx/ui/image';

import { PHOTOS } from './photos';

@Component({
  selector: 'app-image-projected',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Image],
  template: `
    <!-- With NgOptimizedImage: <img ngSrc="/photos/bridge.jpg" width="1200" height="900" alt="…" /> -->
    <vig-image preview [previewSrc]="bridge.src" class="max-w-xs">
      <img
        [src]="bridge.src"
        [alt]="bridge.alt"
        [width]="bridge.width"
        [height]="bridge.height"
        class="block h-auto w-full rounded-media"
      />
    </vig-image>
  `,
})
export class ImageProjectedDemo {
  protected readonly bridge = PHOTOS[1];
}
```

## Styling hooks

Internal elements carry a stable `data-part` and the component's `data-scope`; both are public API, so removing one is a breaking change. Select a part by both, e.g. `[data-scope="image"][data-part="root"] { … }`: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through `aria-*` and, where listed, a `data-state`; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other `data-*` attributes listed below (such as `data-opening` while an overlay animates) are API too. See [Customizing](https://vigdx.com/docs/theming.md#customizing).

Scope: `[data-scope="image"]`

| Part | Element | data-state |
| --- | --- | --- |
| `root` | The host, an inline block. |   |
| `image` | The `<img>` it renders from `src` (not a projected one). |   |
| `error` | Shown instead of an image that failed to load (`labels.errorLabel`); the host gets `data-state="error"` too. | `error` |
| `trigger` | With `preview`: the button around the image. |   |
| `indicator` | The zoom glyph shown over the image on hover and focus. |   |
| `viewer` | The full-screen native modal `<dialog>`; its `::backdrop` is `--color-backdrop-strong`. | `open, closed` |
| `viewer-toolbar` | The row of viewer controls. |   |
| `viewer-zoom-level` | The visible zoom percentage (hidden from screen readers). |   |
| `viewer-status` | Visually hidden polite status with the zoom level, debounced for wheel and pinch. |   |
| `viewer-zoom-out` | Zoom out button (`aria-disabled` when fitted). |   |
| `viewer-zoom-in` | Zoom in button (`aria-disabled` at 400%). |   |
| `viewer-rotate` | Rotate a quarter turn clockwise (R; Shift+R back). |   |
| `viewer-reset` | Back to fitted and upright (0). |   |
| `viewer-close` | Close button; takes focus when the viewer opens. |   |
| `viewer-stage` | The area around the image: pinch, wheel and drag; a press on it closes. | `fitted, zoomed` |
| `viewer-image` | The image, transformed by zoom, rotation and panning. |   |
| `viewer-caption` | The caption under the image (hidden when empty). |   |

| Attribute | Meaning |
| --- | --- |
| `data-opening` | On the overlay element (panel or `<dialog>`) while its entrance transition plays; removed once it settles (2s at most). Style the entrance with `data-opening:` utilities. |
| `data-closing` | On the overlay element while its exit transition plays, before it closes; removed once it settles (2s at most). Style the closed look with `data-closing:` utilities. |

## Accessibility

- `alt` is required; dev mode warns when it is missing, and when an image with a preview has an empty one (its alt names the button: "Open preview: Crater lake").
- The viewer is the native modal `<dialog>`: the page behind is inert, focus starts on Close and returns to the image, Escape closes. A press beside the image closes too.
- Every control is a labelled button (zoom out, zoom in, rotate, reset, close), and has a key: + and − zoom, 0 resets, R rotates (Shift+R back), the arrow keys pan a zoomed image. The zoom level is announced politely: at once for a button or key, once a wheel or pinch comes to rest. Controls that cannot act are `aria-disabled` and stay focusable.
- Pointer extras: wheel and pinch zoom (Safari trackpad pinches too, instead of zooming the page), drag to pan, double click toggles 200%. The page behind never scrolls. Transitions run only without `prefers-reduced-motion`.

## API

### Image

Selector: `vig-image` · Kind: component

An image that can open a full-screen preview. It renders its own `<img>` (`src`, `alt`, `width`/`height` against layout shift, `srcset`/`sizes`, `loading="lazy"` and `decoding="async"` by default; `priority` for the page's main image) or shows an `<img>` you project, such as one with `NgOptimizedImage`'s `ngSrc`, untouched.

**Example :**

```html
<vig-image src="/photos/lake.jpg" alt="Lake at dawn" width="800" height="533" preview />

<vig-image preview previewSrc="/photos/lake-full.jpg">
  <img ngSrc="/photos/lake.jpg" alt="Lake at dawn" width="800" height="533" />
</vig-image>
```

With `preview`, the image is a button that opens the kit's image viewer, a native modal `<dialog>`: zoom, rotation, panning, keyboard (+ − 0 R, arrows, Escape) and focus returning to the image. While the viewer plays its entrance or exit transition, its `<dialog>` carries `data-opening` or `data-closing` (stable hooks for styling and for waiting in tests; see the theming guide). data-opening data-closing

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `alt` | `string \| undefined` | `undefined` | Text alternative; required. `""` marks a decorative image, which cannot have a `preview` (its button would have no name). Dev mode warns when it is missing. |
| `height` | `number \| undefined` | `undefined` | — |
| `labels` | `Partial<ImageLabels>` | — | Texts (i18n) over the locale defaults. |
| `loading` | `ImageLoading` | `'lazy'` | `lazy` (default) or `eager`. |
| `open` (model) | `boolean` | `false` | Two-way: whether the preview is open. |
| `preview` | `boolean` | `false` | Makes the image a button that opens the full-screen viewer. |
| `previewSrc` | `string \| undefined` | `undefined` | Larger image for the viewer; defaults to `src` (or the projected image). |
| `priority` | `boolean` | `false` | The page's main (LCP) image: loads eagerly with `fetchpriority="high"`. |
| `sizes` | `string \| undefined` | `undefined` | `sizes` for `srcset` width descriptors. |
| `src` | `string \| undefined` | `undefined` | Image URL. Leave unset to project your own `<img>` (e.g. with `ngSrc`). |
| `srcset` | `string \| undefined` | `undefined` | Candidate sources for `srcset`, e.g. `"lake-480.jpg 480w, lake-960.jpg 960w"`. |
| `width` | `number \| undefined` | `undefined` | Intrinsic width in pixels; with `height` it reserves the space before loading. |

#### Outputs

| Name | Payload | Description |
| --- | --- | --- |
| `openChange` | `boolean` | Two-way: whether the preview is open. |
