# image

Display an image from a URL, file path, or embedded asset.

<a id="construction"></a>

## Construction

Use `image(source: impl Into<ImageSource>) -> Image`. Import constructors and common modifiers from `refract::prelude::*`.

```rust
use refract::prelude::*;

let cover = image("assets/cover.png")
    .label("Book cover")
    .fit(ImageFit::Cover)
    .width(240.0)
    .height(160.0)
    .radius(8.0);
```

<a id="sources"></a>

## Image sources

`String` sources become `ImageSource::Url`. The browser resolves URLs; the native adapter accepts HTTP(S) URLs and paths relative to its working directory. Loading, decoding, and resource ownership belong to the adapter.

Use `ImageSource::embedded(format, bytes)` with `ImageFormat::Png`, `Jpeg`, or `WebP` for encoded assets. Keep embedded sources in a model or a static `LazyLock` so repeated view evaluation can share the bytes.

Alternative text is optional: `image(source).label(description)` supplies the image's semantic label. Omitted or empty alternative text emits `alt=""` in the browser. Use a description for meaningful content.

<a id="view-specific-reference"></a>

## View-specific reference

`image` returns `Image`, which exposes `label` and `fit` but no child, gap, or stack alignment methods. `fit` preserves `Image`; apply it to the image before wrapper modifiers such as `padding` or `frame`. Size the image node with `width` and `height` to define its content box.

| Method | Purpose |
| --- | --- |
| [`fit`](#fit) | Sets how pixels fit the image's content box. `Contain` is the default and preserves the whole image; `Cover` preserves aspect ratio while cropping; `Stretch` fills the box without preserving aspect ratio. Images are centered. |
| [`label`](#label) | Sets the semantic label, including image alt text. Also available through `ViewExt` after styling; it labels the current outer element. |
| [`on_press`](#on-press) | Adds pointer/keyboard activation to a box, text, or image without changing its primitive kind. Callbacks compose in registration order and preserve other event handlers. Preserves the concrete primitive or scroll builder; the `ViewExt` fallback returns `Modified` for composed views. Structural modifier order defines the hit area. Targets default to button semantics unless a role is already set. On the web, their text is non-selectable so repeated clicks do not highlight labels. |
| [`disabled`](#disabled) | Controls whether the element accepts interaction. The default is `false`; disabling a control does not remove it from layout. |

<a id="fit"></a>

### `fit`

Sets how pixels fit the image's content box. `Contain` is the default and preserves the whole image; `Cover` preserves aspect ratio while cropping; `Stretch` fills the box without preserving aspect ratio. Images are centered.

```rust
fn fit(self, fit: ImageFit) -> Self
```

<a id="label"></a>

### `label`

Sets the semantic label, including image alt text. Also available through `ViewExt` after styling; it labels the current outer element.

```rust
fn label(self, label: impl Into<String>) -> Self
```

<a id="on-press"></a>

### `on_press`

Adds pointer/keyboard activation to a box, text, or image without changing its primitive kind. Callbacks compose in registration order and preserve other event handlers. Preserves the concrete primitive or scroll builder; the `ViewExt` fallback returns `Modified` for composed views. Structural modifier order defines the hit area. Targets default to button semantics unless a role is already set. On the web, their text is non-selectable so repeated clicks do not highlight labels.

```rust
fn on_press(self, action: impl Fn(&mut ActionContext<'_>) + 'static) -> Self
```

<a id="disabled"></a>

### `disabled`

Controls whether the element accepts interaction. The default is `false`; disabling a control does not remove it from layout.

```rust
fn disabled(self, value: bool) -> Self
```

<a id="common-modifiers"></a>

## Common view modifiers

Dimensions, paint, labels, and disabled state preserve each concrete builder type, so their builder methods can be interleaved. `padding` and `frame` return wrappers; configure the inner element before adding those. `ViewExt` supplies the shared API for composed views.

See [View Modifiers](/docs/view-modifiers) for shared methods and modifier ordering. Focus bindings and callbacks preserve concrete builders; see [focus modifiers](/docs/view-modifiers#focus) and [Input & Focus](/docs/input-focus) for requests, observation, and indicators.

<a id="source"></a>

## Source reference

The implementation is in `crates/core/src/image.rs and crates/core/src/view/primitives.rs`. Primitive and layout builders are in `crates/core/src/view/primitives.rs`. Shared modifiers are in `crates/core/src/view/modifiers.rs`. Style values and validation are in `crates/core/src/style.rs`.
