# view

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

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

## Construction

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

```rust
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 id="layout"></a>

## 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::*`.

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

## 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`](#child) | Appends one child. Successive calls can accept different concrete view types; declaration order determines child order. |
| [`children`](#children) | Appends an iterator in iteration order. Items must share one concrete view type. All children are constructed eagerly. |
| [`maybe_child`](#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`](#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="child"></a>

### `child`

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

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

<a id="children"></a>

### `children`

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

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

<a id="maybe-child"></a>

### `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.

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