Skip to content

Instantly share code, notes, and snippets.

@ayamomiji
Created October 16, 2020 02:42
Show Gist options
  • Select an option

  • Save ayamomiji/7809dd51915a80fe93aeb452d7f16662 to your computer and use it in GitHub Desktop.

Select an option

Save ayamomiji/7809dd51915a80fe93aeb452d7f16662 to your computer and use it in GitHub Desktop.

Revisions

  1. ayamomiji created this gist Oct 16, 2020.
    1 change: 1 addition & 0 deletions example.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    <div x-data @remove='console.log($el, "removed")'></div>
    18 changes: 18 additions & 0 deletions remove.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    const observer = new MutationObserver(mutations => {
    for (const mutation of mutations) {
    if (mutation.type !== 'childList') continue

    mutation.removedNodes.forEach(node => {
    if (node.nodeType !== Node.ELEMENT_NODE) return

    [node, ...node.querySelectorAll('[\\@remove]')].forEach(node => {
    node.dispatchEvent(new CustomEvent('remove'))
    })
    })
    }
    })

    observer.observe(document, {
    childList: true,
    subtree: true
    })