R
fn

onElementRemoval

v0.0.15testeddemo

Fire a callback when the target element — or any ancestor containing it — is removed from the DOM. Backed by a single `childList`/`subtree` `MutationObserver` on the element's owning document, so it also catches removal of a parent further up the tree.

Examples

ts
const el = useTemplateRef<HTMLElement>('el');
onElementRemoval(el, () => console.log('gone'));
ts
const stop = onElementRemoval(el, (records) => report(records), { flush: 'post' });

Demo

Loading demo…

Signature

ts
export function onElementRemoval(
  target: MaybeComputedElementRef,
  callback: OnElementRemovalCallback,
  options: OnElementRemovalOptions ={ ... }

Parameters

ParameterTypeDescription
targetMaybeComputedElementRefElement (or ref/getter) to watch for removal
callbackOnElementRemovalCallbackInvoked with the mutation records that removed the element
options?OnElementRemovalOptions`window`, `document`, and watcher `flush` timing

Returns

VoidFunctionStop handle that tears down the watcher and observer