Skip to content

use_scroll

use_scroll tells a component how far a scroll container is scrolled, how far it can be, and how much of its content is in view, as a Scroll, which holds what the web's scrollTop, scrollHeight and clientHeight say. Counterweight draws no scrollbars, so this is how a component shows the reader where they are. Here the title counts the lines in view, and the bottom border counts the lines below them:

@component
def document(lines: tuple[str, ...] = LINES) -> Div:
    body, _ = use_state(ElementHandle)
    scroll = use_scroll(body)

    first = scroll.offset.y + 1
    last = min(scroll.offset.y + scroll.view_height, len(lines))
    percent = 100 * scroll.offset.y // scroll.max_offset.y if scroll.max_offset.y else 100

    return Div(
        style=col | border | border_lightrounded | size(30, 9),
        children=(
            Text(
                content=f" Lines {first}-{last} of {len(lines)} ({percent}%) ",
                style=position_absolute | inset_top(-1) | inset_left(1) | clip_to_border | text_color("amber", 400),
            ),
            Div(
                style=fill(1) | col | pad_x(1) | overflow_y_scroll,
                handle=body,
                on_input=scroll_on_wheel(),
                children=tuple(Text(content=line) for line in lines),
            ),
            Text(
                content=f" ↓ {len(lines) - last} more " if last < len(lines) else "",
                style=position_absolute | inset_bottom(-1) | inset_right(1) | clip_to_border | text_color("slate", 400),
            ),
        ),
    )

Lines 7 to 13 of 30 in view, 26% of the way down, with 17 more below

╭─ Lines 7-13 of 30 (26%) ───╮
│  7 GET /items/259 404      │
│  8 GET /items/296 200      │
│  9 GET /items/333 200      │
│ 10 GET /items/370 200      │
│ 11 GET /items/407 200      │
│ 12 GET /items/444 200      │
│ 13 GET /items/481 200      │
╰──────────────── ↓ 17 more ─╯

As the wheel scrolls the log down and back up, the counts follow:

The labels sit on the box around the log, not in the log itself, since a scroll container moves everything inside it by its offset. So document gives the log an ElementHandle and reads its scroll with use_scroll(body). Called without a handle, use_scroll reports on the calling component's own top-level element.

max_offset is how far the content reaches past the view, so it's 0 when everything fits, which is why percent checks it before dividing.

use_scroll reports the scroll as of the last layout, and a component that calls it renders again as soon as that scroll changes, before the frame is painted, as use_rects does, so the counts change in the frame the wheel scrolls. See Scrolling for an example that renders only the rows in view, and Jumping to the end for a button that appears once the reader scrolls back.

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 last layout, 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, before the frame that scrolled it is painted. It's all zeros in a component's first render, for an element that isn't a scroll container, and for a handle that named no visible element in the last layout.

counterweight.geometry.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.