Overview
onParent(targetRef, events) is an element directive placed on a child (or descendant). It attaches listeners to the element held by targetRef (typically a parent/ancestor set with setRef).
Listeners bind when the ref’s element becomes available, re-sync when the event map updates, and unbind when the host element disconnects.
onEvent(map) binds listeners on the host itself. Keys are DOM event names plus enterViewport, exitViewport, and clickOutside. Import it as onEvent — the name on is the reactive hook. removeOn(el) tears those listeners down early.
onEleEvent(name, handler) listens on a parent for a bubbling CustomEvent. useEleEvent() is called in a component’s outer function and returns dispatch(eventName, detail?), which fires from that component’s host.
Which event API
|
API
|
Listener target
|
Notes
|
|---|---|---|
@click=${fn}
| Same element | Native lit-html listener on the host node. |
onParent(ref, map)
| Ref’d ancestor | Child hosts the directive; events fire on the parent/ancestor element. |
Hook on(fn)
| Reactive subscription | Outer-function hook. Not an element listener. |
onEvent(map)
| Host element | DOM events plus enterViewport, exitViewport, and clickOutside. |
onEleEvent / useEleEvent
| Bubbling CustomEvent | Parent listens. dispatch() fires from the child component host. |
Child listening to parent scroll
The scroll container owns a ref. Each row applies onParent(scrollRef, { scroll, click }) so listeners attach to the parent and clean up with the row.
Parent click counter
Listen for click on an ancestor ref from a nested child.
onEvent and onEleEvent
onEvent binds host listeners, including clickOutside. useEleEvent().dispatch fires a bubbling CustomEvent from the child component host. The parent hears it with onEleEvent.