# z_stack

Layer children in shared bounds.

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

## Construction

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

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

let card = z_stack()
    .alignment(Alignment::End, Alignment::Start)
    .child(view().width(240.0).height(160.0).background(Color::BLUE))
    .child(text("New").padding(8.0).background(Color::WHITE));
```

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

## Layering and bounds

Children share the overlay's bounds instead of advancing along a row or column. By default, later children are painted above earlier children. Use [`z_index(i32)`](/docs/view-modifiers#paint) to change sibling stacking order. Give the overlay explicit dimensions when it should occupy a fixed area.

Use `alignment(horizontal, vertical)` to position each child within the shared grid cell on both web and native. `End, Start` places smaller children at the top right, as in the badge example above. Both axes default to `Stretch`; explicit child sizes are preserved.

Alignment changes placement without changing child identity, declaration order, paint order, or keyboard focus order. Configure alignment before wrapper modifiers such as `padding` or `frame`.

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

## View-specific reference

`z_stack` returns `Overlay`, with child composition, two-axis `alignment`, and shared element modifiers. Dimensions and paint preserve `Overlay`, so they can be interleaved with child and alignment methods. It has no row/column direction, gap, or 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. |
| [`alignment`](#alignment) | Aligns each child horizontally and vertically within the shared cell. Each axis accepts `Start`, `Center`, `End`, or `Stretch` and defaults to `Stretch`. Explicit child sizes are preserved. `SpaceBetween` behaves like `Start` because children overlap rather than being distributed. Preserves `Overlay` and does not change paint or keyboard focus order. |

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

### `alignment`

Aligns each child horizontally and vertically within the shared cell. Each axis accepts `Start`, `Center`, `End`, or `Stretch` and defaults to `Stretch`. Explicit child sizes are preserved. `SpaceBetween` behaves like `Start` because children overlap rather than being distributed. Preserves `Overlay` and does not change paint or keyboard focus order.

```rust
fn alignment(self, horizontal: Alignment, vertical: 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`.
