# stack

Arrange children with a configurable horizontal or vertical direction.

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

## Construction

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

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

let content = stack()
    .direction(Direction::Horizontal)
    .alignment(Alignment::Start)
    .gap(12.0)
    .child(text("Library"))
    .child(text("Saved items"))
    .padding(16.0);
```

<a id="layout"></a>

## Layout and available space

`stack()` defaults to `Direction::Vertical`. Use `.direction(Direction::Horizontal)` to arrange a row, or change direction from state while keeping the same `Stack` type. `h_stack()` and `v_stack()` are shorthand constructors; both also support `.direction(...)`.

By default, children follow declaration order along the vertical axis. The stack starts with zero `gap` and `Stretch` cross-axis alignment. Set `alignment` explicitly when children should keep their natural cross-axis size.

`justify` distributes space along the current stack direction. Extra space requires a bounded size from the parent or a size modifier. A direct [`spacer`](/docs/spacer) child can consume remaining space.

Use stable keys for reorderable children. Child composition is eager; a long list does not become virtualized by placing it in a stack.

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

## View-specific reference

`stack`, `h_stack`, and `v_stack` return `Stack`. Dimensions and paint preserve `Stack`, so they can be interleaved with child, direction, gap, alignment, and justification methods.

| 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. |
| [`direction`](#direction) | Chooses `Horizontal` or `Vertical` without replacing the stack or its children. `stack()` defaults to vertical; `h_stack()` starts horizontal and `v_stack()` starts vertical. Gap, alignment, and justification apply to the current direction. |
| [`gap`](#gap) | Sets the distance between adjacent children in logical pixels. The default is zero. Values must be finite and nonnegative; use `padding` for space around the container. |
| [`alignment`](#alignment) | Sets alignment perpendicular to the current direction (horizontal for a vertical stack, vertical for a horizontal stack). Use `Start`, `Center`, `End`, or `Stretch`. The stack constructor defaults to `Stretch`. |
| [`justify`](#justify) | Sets placement along the current direction. The default is `Start`. `SpaceBetween` distributes available space between children; `Center` and `End` move the sequence within its bounds. |

<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="direction"></a>

### `direction`

Chooses `Horizontal` or `Vertical` without replacing the stack or its children. `stack()` defaults to vertical; `h_stack()` starts horizontal and `v_stack()` starts vertical. Gap, alignment, and justification apply to the current direction.

```rust
fn direction(self, direction: Direction) -> Self
```

<a id="gap"></a>

### `gap`

Sets the distance between adjacent children in logical pixels. The default is zero. Values must be finite and nonnegative; use `padding` for space around the container.

```rust
fn gap(self, gap: f32) -> Self
```

<a id="alignment"></a>

### `alignment`

Sets alignment perpendicular to the current direction (horizontal for a vertical stack, vertical for a horizontal stack). Use `Start`, `Center`, `End`, or `Stretch`. The stack constructor defaults to `Stretch`.

```rust
fn alignment(self, align: Alignment) -> Self
```

<a id="justify"></a>

### `justify`

Sets placement along the current direction. The default is `Start`. `SpaceBetween` distributes available space between children; `Center` and `End` move the sequence within its bounds.

```rust
fn justify(self, align: Alignment) -> 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`.
