📄 index.ts
¶
📊 Analysis Summary¶
Metric | Count |
---|---|
🔧 Functions | 3 |
📦 Imports | 11 |
📊 Variables & Constants | 1 |
📐 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 |
Variables & Constants¶
Name | Type | Kind | Value | Exported |
---|---|---|---|---|
stopFn |
Fn | undefined |
let/var | *not shown* |
✗ |
Functions¶
onElementRemoval(target: MaybeElementRef, callback: (mutationRecords: MutationRecord[]) => void, options: OnElementRemovalOptions): Fn
¶
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
}
-
JSDoc:
-
Parameters:
target: MaybeElementRef
callback: (mutationRecords: MutationRecord[]) => void
options: OnElementRemovalOptions
- Return Type:
Fn
- Calls:
stopFn
watchEffect (from vue)
unrefElement (from ../unrefElement)
useMutationObserver (from ../useMutationObserver)
mutationsList .map(mutation => [...mutation.removedNodes]) .flat() .some
node.contains
callback
cleanupAndUpdate
stopWatch
tryOnScopeDispose (from @vueuse/shared)
- Internal Comments:
cleanupAndUpdate(fn: Fn): void
¶
- Parameters:
fn: Fn
- Return Type:
void
- Calls:
stopFn
stopHandle(): void
¶
- Return Type:
void
- Calls:
stopWatch
cleanupAndUpdate