Documentation / Reference

image

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

Construction

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

image.rs
use refract::prelude::*;

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

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.

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.

MethodPurpose
fitSets 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.
labelSets the semantic label, including image alt text. Also available through ViewExt after styling; it labels the current outer element.
on_pressAdds 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.
disabledControls whether the element accepts interaction. The default is false; disabling a control does not remove it from layout.

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.

Signature
fn fit(self, fit: ImageFit) -> Self

label

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

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

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.

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

disabled

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

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

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 for shared methods and modifier ordering. Focus bindings and callbacks preserve concrete builders; see focus modifiers and Input & Focus for requests, observation, and indicators.

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.