Skip to content

onElementRemoval ​

Category
Export Size
Last Changed
1 hour ago

Fires when the element or any element containing it is removed.

Demo ​

demo1: recreate new element

removed times: 0

demo2: reuse same element

target element
removed times: 0

Usage ​

vue
<script setup lang="ts">
import { onElementRemoval } from '@vueuse/core'
import { ref } from 'vue'

const btnRef = ref<HTMLElement | null>(null)
const btnState = ref(true)
const removedCount = ref(0)

function btnOnClick() {
  btnState.value = !btnState.value
}

onElementRemoval(btnRef, () => ++removedCount.value)
</script>

<template>
  <button
    v-if="btnState"
    @click="btnOnClick"
  >
    recreate me
  </button>
  <button
    v-else
    ref="btnRef"
    @click="btnOnClick"
  >
    remove me
  </button>
  <b>removed times: {{ removedCount }}</b>
</template>

Type Declarations ​

typescript
export interface OnElementRemovalOptions
  extends ConfigurableWindow,
    ConfigurableDocumentOrShadowRoot,
    WatchOptionsBase {}
/**
 * Fires when the element or any element containing it is removed.
 *
 * @param target
 * @param callback
 * @param options
 */
export declare function onElementRemoval(
  target: MaybeElementRef,
  callback: (mutationRecords: MutationRecord[]) => void,
  options?: OnElementRemovalOptions,
): Fn

Source ​

Source • Demo • Docs

Contributors ​

Ben Lau

Changelog ​

v12.3.0 on 1/2/2025
08cf5 - feat: new function, refactor useActiveElement useElementHover (#4410)

Released under the MIT License.