feat: rx.element_observer reports an element's size, position and visibility to event triggers
Anything that depends on the layout of the page (a header that shrinks once the page scrolls, a table of contents that marks the section in view, a panel that animates to the height of its content) needs a custom JavaScript component today, because nothing reports an element's size, position or visibility. rx.element_observer does, to event triggers that can set client state:
height = rx.client_state(0.0)
rx.element_observer(
rx.el.header(...),
on_resize=lambda width, h: height.set(h),
)
on_resize(width, height):ResizeObserver(border box).on_move(x, y): the position in the viewport, after scrolls (of the page or any scroll container), window resizes and resizes of the element.on_intersect(is_intersecting, ratio):IntersectionObserver, withthresholdandroot_margin.on_transition_end(property_name): transitions of the element itself, not of its descendants.layout=Truestarts observing in a layout effect, so the first size and position come in the commit that mounts or enables the observer, before its effects.disabledpauses observing.
Like rx.clipboard it renders its only child unchanged and observes it, or the element whose id is given as target. The hook (utils/helpers/element_observer.js) keeps the handlers in a ref, so a re-rendered handler does not restart the observers.
Tests: unit tests for the compiled hook, vitest tests for the hook's timing, deduplication and restarts, and a Playwright test for resize, scroll, visibility and transitions in an app without state. The docs get a page under Library > Other.