Which events a component can listen to

A component can listen to onpointerdown, onpointermove, onpointerup, onpointercancel, ongotpointercapture, onlostpointercapture, onclick and ondblclick, besides the key, focus and input events of the previous lesson. A listener for any other event name is refused when the document updates, with a message that names the event and lists the ones that fire. That includes pointerover, pointerenter, pointerout, pointerleave, wheel and scroll. To react to the pointer being over an element, use the :hover rule in the stylesheet.

The demos for this lesson are toasts and scroll. Each has its source beside it.

Read where the pointer is

A pointer listener that takes &mut Event can read event.client(), the position from the viewport’s top-left, and event.offset(), the position from the target’s padding box’s top-left. Both are in CSS pixels and not rounded. event.target() is the element the event was sent to, and it stays the same while the event bubbles.

pointer.wisp
<script>let position: Signal<String> = Signal::new(String::from("none yet"));let clicks: Signal<u32> = Signal::new(0);let on_move = move |event: &mut Event| {    let at = event.offset();    position.set(format!("{:.0}, {:.0}", at[0], at[1]));};let on_click = move || clicks.update(|count| *count += 1);</script><style>.pad { width: 240px; height: 120px; background-color: #dde1e5; }.pad:hover { background-color: #c7ccd1; }.hint { pointer-events: none; }</style><div class="pad" onpointermove={on_move} onclick={on_click}>  <p class="hint">Move and click here</p></div><p>Pointer: {position}</p><p>Clicks: {clicks}</p>
vapors component source

Save this example over crates/demos/demos/hello.wisp and run cargo run -p vapors-app --example demo -- hello. Move over the grey box and the first paragraph shows the position inside it. The :hover rule darkens the box. The hint paragraph has pointer-events: none, so a pointer over the text is over the box, and event.offset() is measured from the box.

Hit testing and event handling

Events target the visible element under the pointer, using the last painted frame. pointer-events: none excludes an element from hit testing. Events bubble through ancestors unless a handler calls stop_propagation().

:hover follows the element under the pointer, and :active applies while it is pressed. A pointer-down handler can capture a pressed pointer with event.set_pointer_capture() to keep receiving its movement outside the original element.

Give scrolling content its own container

Use overflow: auto or overflow: scroll on a container with a constrained height. At this revision, page-level scrolling is not supported; use an inner container, as the scroll demo does.

A wheel scrolls the innermost container under the pointer that can move in that direction. Trackpads and wheels can report different distances. A touch drag stays with the container where the gesture began and stops at its content bounds. The drag must move beyond the framework’s touch threshold before scrolling starts.

Scroll offsets change the visible frame without requiring new style or layout results. The GPU still draws the visible content; Performance explains the retained drawing data.

Scrollbar styling

The current overlay scrollbar indicates position but cannot be dragged. scrollbar-width accepts auto, thin or none. scrollbar-color takes the thumb color followed by transparent; a colored track is not supported.

The host can read or set offsets with Document::scroll_offset and Document::scroll_to. Component wheel and scroll event listeners are not supported at this revision.

Try independent scrolling

Open the scroll demo. Scroll its left list and check that the right list stays still. Move over the right list and scroll it separately. Try a touch drag on a phone, then drag from the fixed bar: the gesture should only scroll when it starts in a scroll container.