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),
),
),
)
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
TYPE:
|
| 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 |
counterweight.geometry.Scroll
dataclass
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.