Documentation / Reference

view

Create a basic box for styling, semantics, or interaction.

Construction

Use view() -> Container. Import constructors and common modifiers from refract::prelude::*.

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

let card = view()
    .child(text("A basic view"))
    .label("Summary")
    .padding(20.0)
    .background(Color::rgb(22, 40, 64))
    .foreground(Color::WHITE)
    .radius(12.0);

A containing element

view() is Refract’s generic box primitive, analogous to an HTML div. It creates an ElementKind::Box with the default Layout::Box; the web adapter renders it as a div with block layout.

It starts as an empty host box. Add content with child or children, or style the empty box directly. Use it to establish a boundary for styling, semantics, or activation. For an explicit row, column, or overlay arrangement, choose the corresponding stack.

The view() constructor and the #[view] attribute macro share a name in separate Rust namespaces. The constructor creates a box; the macro declares a reusable reactive view. Both are available through refract::prelude::*.

View-specific reference

view() returns an empty Container. Each child call appends a view in declaration order. Dimensions and paint preserve Container, so child and style methods can be interleaved.

MethodPurpose
childAppends one child. Successive calls can accept different concrete view types; declaration order determines child order.
childrenAppends an iterator in iteration order. Items must share one concrete view type. All children are constructed eagerly.
maybe_childAppends the supplied view for Some. None inserts an Empty description to preserve the position of later unkeyed siblings; it does not create a host element.
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.

child

Appends one child. Successive calls can accept different concrete view types; declaration order determines child order.

Signature
fn child(self, child: impl View) -> Self

children

Appends an iterator in iteration order. Items must share one concrete view type. All children are constructed eagerly.

Signature
fn children<V: View>(self, children: impl IntoIterator<Item = V>) -> Self

maybe_child

Appends the supplied view for Some. None inserts an Empty description to preserve the position of later unkeyed siblings; it does not create a host element.

Signature
fn maybe_child<V: View>(self, child: Option<V>) -> 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/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.