Skip to content

ScrollView hosts one content View and scrolls vertically by default.

[[huxerui::composable]]
View Document() {
auto scroll = UseScrollController();
return ScrollView {
Column {
ForEach(sections, RenderSection),
}.With(
Padding(24.0F),
Spacing(16.0F)
),
}.Controller(scroll)
.With(ScrollBar());
}

Set .ScrollAxis(Axis::Horizontal) for horizontal content. .Controller(ScrollController) connects imperative coordination without moving the scroll offset into component state.

Use FillViewport() when a short page should cover a bounded viewport along the scroll axis. It gives the content a minimum size and still lets larger content scroll. For example, a short empty state can center within the viewport without changing the scroll behavior of a longer document.

ScrollController reports ScrollMetrics and supports ScrollTo, ScrollBy, and virtual-layout ScrollToItem. Create a scope-stable controller with UseScrollController(initial_offset). A controller can still move the content of a disabled ScrollView, even though user interaction is disabled.

ScrollBothAxes() measures the content for horizontal and vertical scrolling in one clipped viewport. Bound both viewport dimensions and bind a distinct controller for each axis.

#include <huxerui/huxerui.h>
using namespace huxerui;
[[huxerui::composable]]
View DiagramViewport() {
auto horizontal = UseScrollController();
auto vertical = UseScrollController();
return Column {
Button("Back to origin").OnClick([horizontal, vertical] {
horizontal.ScrollTo(0.0F);
vertical.ScrollTo(0.0F);
}),
ScrollView(Text("Diagram origin").With(Frame{.width = 880.0F, .height = 560.0F}))
.ScrollBothAxes()
.Controller(Axis::Horizontal, horizontal)
.Controller(Axis::Vertical, vertical)
.With(Frame{.width = 320.0F, .height = 240.0F}, ScrollBar()),
}.With(Spacing(8.0F));
}

ScrollBar() shows an indicator for each axis that has a scroll range. For a large two-dimensional data set, use a two-axis virtual layout so offscreen items need not mount.

The runtime owns pointer wheel or touch scrolling, momentum, bounds, clipping, scrollbar interaction, and nested scroll arbitration. Focused text input may request visibility, but user scrolling remains authoritative and can move away from the field.

ScrollView exposes scroll-view role, extent, current offset, and semantic scroll actions.

  • Constructor: explicit ScrollView(View content).
  • Fluent methods: .ScrollAxis(Axis) or .ScrollBothAxes(). Use .FillViewport() to extend short content. Use .Controller(ScrollController) for one axis and .Controller(Axis, ScrollController) for a specific axis in two-axis mode.
  • Common modifiers: ScrollBar, Frame, Grow, Background.

Use VirtualList or VirtualGrid for large collections.

ScrollView measures ordinary content; every child in that content exists even when offscreen. Give the viewport finite space on the scroll axis, for example a height Frame or Grow in a bounded Column. Otherwise the parent may expand to fit the content and no useful scrolling range remains. The default axis is Vertical; horizontal scrolling requires .ScrollAxis(Axis::Horizontal).

Keep one stable UseScrollController() per independently controlled axis. ScrollTo(offset) and ScrollBy(delta) return whether a current connection accepted the request. Offsets use logical units and clamp to the available range. ScrollToItem addresses virtual-layout items.

Metrics(), Offset(), MaxOffset(), ViewportExtent(), and ContentExtent() expose the current projection; IsConnected() lets a caller distinguish a mounted connection. Request scrolling from an event or committed effect, not while constructing the scrollable declaration before it has connected.

For a refreshable feed, wrap this ScrollView with RefreshBox. Wheel/trackpad input, touch drag, scrollbar drag, momentum, focus reveal, and semantic scrolling remain one shared scroll state rather than competing component-owned offsets.