Documentation / Reference
view
Create a basic box for styling, semantics, or interaction.
Construction
Use view() -> Container. Import constructors and common modifiers from refract::prelude::*.
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.
| Method | Purpose |
|---|---|
child | Appends one child. Successive calls can accept different concrete view types; declaration order determines child order. |
children | Appends an iterator in iteration order. Items must share one concrete view type. All children are constructed eagerly. |
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. |
label | Sets the semantic label, including image alt text. Also available through ViewExt after styling; it labels the current outer element. |
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 | Controls 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.
fn child(self, child: impl View) -> Selfchildren
Appends an iterator in iteration order. Items must share one concrete view type. All children are constructed eagerly.
fn children<V: View>(self, children: impl IntoIterator<Item = V>) -> Selfmaybe_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.
fn maybe_child<V: View>(self, child: Option<V>) -> Selflabel
Sets the semantic label, including image alt text. Also available through ViewExt after styling; it labels the current outer element.
fn label(self, label: impl Into<String>) -> Selfon_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.
fn on_press(self, action: impl Fn(&mut ActionContext<'_>) + 'static) -> Selfdisabled
Controls whether the element accepts interaction. The default is false; disabling a control does not remove it from layout.
fn disabled(self, value: bool) -> SelfCommon 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.