Skip to content

use_scroll

See Scrolling for how scroll containers work, and an example that renders only the rows in view.

API

counterweight.hooks.use_scroll

use_scroll(handle: ElementHandle | None = None) -> Scroll
PARAMETER DESCRIPTION
handle

The element to report on, by the ElementHandle passed to its handle, as a React component reads another element's scrollTop through a ref, or None for the calling component's top-level element.

TYPE: ElementHandle | None DEFAULT: None

RETURNS DESCRIPTION
Scroll

How far the element is scrolled, and how far it can be, if it's a scroll container, as of the previous render cycle, as use_rects does. A component that calls use_scroll renders again as soon as that scroll changes, whether a control scrolled it or its content changed size. It's all zeros on the initial render, for an element that isn't a scroll container, and for a handle that named no visible element in the previous render cycle.

counterweight.hooks.Scroll dataclass

Scroll(
    *,
    offset: Position,
    max_offset: Position,
    view_width: int,
    view_height: int
)

How far a scroll container's content is scrolled, and how far it can be.

offset instance-attribute

offset: Position

How far the content is scrolled from where layout put it: x columns left and y rows up, so Position(0, 0) shows the start of the content, as the web's scrollLeft and scrollTop do.

max_offset instance-attribute

max_offset: Position

The largest offset on each axis, which shows the end of the content, as the web's scrollWidth minus clientWidth does. It's 0 on an axis the container doesn't scroll, and on one whose content fits.

view_width instance-attribute

view_width: int

The width of the container's padding box, the part of the content in view, as the web's clientWidth.

view_height instance-attribute

view_height: int

The height of the container's padding box, the part of the content in view, as the web's clientHeight.

clamp

clamp(offset: Position) -> Position

offset, moved onto the range the content scrolls through on each axis, from 0 to max_offset.