# scroll_view

Place content in a viewport with initial positioning, following, and scroll observation.

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

## Construction

Use `scroll_view(content: impl View) -> ScrollView`. Import constructors and common modifiers from `refract::prelude::*`.

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

let messages = v_stack()
    .gap(8.0)
    .children((0..30).map(|n| text(format!("Message {n}"))));

let viewport = scroll_view(messages)
    .initial_position(ScrollPosition::End)
    .follow(ScrollFollow::EndWhenPinned)
    .height(240.0);
```

<a id="viewport"></a>

## Content and viewport

Scrolling defaults to `Axis::Vertical`. Set `.axis(Axis::Horizontal)` or `.axis(Axis::Both)` to change the direction. Constrain the viewport along the scrolling axis so content can exceed its bounds. `content.scrollable()` is the `ViewExt` shorthand for `scroll_view(content)`.

Children are constructed eagerly. Scrolling clips the viewport but does not virtualize the content.

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

## View-specific reference

`ScrollView` methods configure behavior and return `Self`. Dimensions, paint, labels, disabled state, and activation also preserve `ScrollView`. Configure the viewport before adding `padding` or `frame`. `ScrollEvent` contains metrics for offset, viewport size, and content size, plus net actual movement in `delta`.

| Method | Purpose |
| --- | --- |
| [`axis`](#axis) | Sets the scrolling direction. Defaults to `Axis::Vertical`; choose `Horizontal` or `Both` for other directions. Preserves viewport styling, content, and scroll behavior. |
| [`initial_position`](#initial-position) | Chooses `Start` (default) or `End` when the mounted viewport first has nonzero size. This is an initial position, not an ongoing follow policy. |
| [`follow`](#follow) | `None` is the default. `EndWhenPinned` follows geometry changes while within two logical pixels of the old end; scrolling away suspends following. Enabled axes follow independently. |
| [`velocity`](#velocity) | Drives continuous scrolling in logical pixels per second on the enabled axes. Positive x/y moves right/down. Pass `ScrollOffset::default()` to stop; hosts clamp at content bounds and stop scheduling frames there. Useful for drag autoscrolling; reset velocity when the drag ends or is cancelled. |
| [`on_scroll`](#on-scroll) | Observes actual movement and geometry changes, at most once per adapter frame, without bubbling. The first event has zero delta. Later geometry-only changes may also have zero delta. Reading metrics does not invalidate views; writing graph state may do so. |

<a id="axis"></a>

### `axis`

Sets the scrolling direction. Defaults to `Axis::Vertical`; choose `Horizontal` or `Both` for other directions. Preserves viewport styling, content, and scroll behavior.

```rust
fn axis(self, axis: Axis) -> Self
```

<a id="initial-position"></a>

### `initial_position`

Chooses `Start` (default) or `End` when the mounted viewport first has nonzero size. This is an initial position, not an ongoing follow policy.

```rust
fn initial_position(self, position: ScrollPosition) -> Self
```

<a id="follow"></a>

### `follow`

`None` is the default. `EndWhenPinned` follows geometry changes while within two logical pixels of the old end; scrolling away suspends following. Enabled axes follow independently.

```rust
fn follow(self, policy: ScrollFollow) -> Self
```

<a id="velocity"></a>

### `velocity`

Drives continuous scrolling in logical pixels per second on the enabled axes. Positive x/y moves right/down. Pass `ScrollOffset::default()` to stop; hosts clamp at content bounds and stop scheduling frames there. Useful for drag autoscrolling; reset velocity when the drag ends or is cancelled.

```rust
fn velocity(self, velocity: ScrollOffset) -> Self
```

<a id="on-scroll"></a>

### `on_scroll`

Observes actual movement and geometry changes, at most once per adapter frame, without bubbling. The first event has zero delta. Later geometry-only changes may also have zero delta. Reading metrics does not invalidate views; writing graph state may do so.

```rust
fn on_scroll(self, action: impl Fn(&mut ActionContext<'_>, &ScrollEvent) + 'static) -> Self
```

<a id="wheel-input"></a>

## Wheel input and scroll observation

Use the shared `on_wheel` handler to inspect input before host scrolling. Use `on_scroll` to observe the resulting movement, including movement driven by `velocity`. Wheel deltas describe intent; content bounds can prevent actual movement.

During dispatch, `cx.geometry().and_then(|geometry| geometry.scroll)` exposes available scroll metrics for the current handler's field or scroll view. See [wheel input](/docs/input-focus#wheel) for an overflow-aware handler and the difference between stopping propagation and preventing scrolling.

<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/scroll.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`.
