📄 onElementRemoval¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 3 |
| 📦 Imports | 11 |
| 📐 Interfaces | 1 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/core/onElementRemoval/index.ts
📦 Imports¶
| Name | Source |
|---|---|
Fn |
@vueuse/shared |
WatchOptionsBase |
vue |
ConfigurableDocumentOrShadowRoot |
../_configurable |
ConfigurableWindow |
../_configurable |
MaybeElementRef |
../unrefElement |
noop |
@vueuse/shared |
tryOnScopeDispose |
@vueuse/shared |
watchEffect |
vue |
defaultWindow |
../_configurable |
unrefElement |
../unrefElement |
useMutationObserver |
../useMutationObserver |
Functions¶
onElementRemoval(target: MaybeElementRef, callback: (mutationRecords: MutationRecord[]) => …, options: OnElementRemovalOptions): Fn¶
Fires when the element or any element containing it is removed.
Parameters:
targetany: No descriptioncallbackany: No descriptionoptionsany: No description
Raw JSDoc
Calls:
stopFnwatchEffect (from vue)unrefElement (from ../unrefElement)useMutationObserver (from ../useMutationObserver)mutationsList .map(mutation => [...mutation.removedNodes]) .flat() .somenode.containscallbackcleanupAndUpdatestopWatchtryOnScopeDispose (from @vueuse/shared)
Internal Comments:
Code
export function onElementRemoval(
target: MaybeElementRef,
callback: (mutationRecords: MutationRecord[]) => void,
options: OnElementRemovalOptions = {},
): Fn {
const {
window = defaultWindow,
document = window?.document,
flush = 'sync',
} = options
// SSR
if (!window || !document)
return noop
let stopFn: Fn | undefined
const cleanupAndUpdate = (fn?: Fn) => {
stopFn?.()
stopFn = fn
}
const stopWatch = watchEffect(() => {
const el = unrefElement(target)
if (el) {
const { stop } = useMutationObserver(
document as any,
(mutationsList) => {
const targetRemoved = mutationsList
.map(mutation => [...mutation.removedNodes])
.flat()
.some(node => node === el || node.contains(el))
if (targetRemoved) {
callback(mutationsList)
}
},
{
window,
childList: true,
subtree: true,
},
)
cleanupAndUpdate(stop)
}
}, { flush })
const stopHandle = () => {
stopWatch()
cleanupAndUpdate()
}
tryOnScopeDispose(stopHandle)
return stopHandle
}
Internal helpers¶
Declared inside another function in this file.
cleanupAndUpdate(fn: Fn): void¶
Parameters:
fnFn
Returns: void
Calls:
stopFn
stopHandle(): void¶
Returns: void
Calls:
stopWatchcleanupAndUpdate
Interfaces¶
OnElementRemovalOptions¶
Interface Code
Generated by Syntax Scribe