Documentation / Reference

scroll_view

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

Construction

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

scroll-view.rs
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);

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.

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.

MethodPurpose
axisSets the scrolling direction. Defaults to Axis::Vertical; choose Horizontal or Both for other directions. Preserves viewport styling, content, and scroll behavior.
initial_positionChooses Start (default) or End when the mounted viewport first has nonzero size. This is an initial position, not an ongoing follow policy.
followNone is the default. EndWhenPinned follows geometry changes while within two logical pixels of the old end; scrolling away suspends following. Enabled axes follow independently.
velocityDrives 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_scrollObserves 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.

axis

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

Signature
fn axis(self, axis: Axis) -> Self

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.

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

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.

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

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.

Signature
fn velocity(self, velocity: ScrollOffset) -> Self

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.

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

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 for an overflow-aware handler and the difference between stopping propagation and preventing scrolling.

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