A Vue composable which finds the first element matched by a selector, using useMutationObserver
(i.e. requires the @vueuse/core
package).
While using template refs is the canonical way to access elements in Vue, there may be situations (e.g. when you wrap non-Vue code you have no control over) where using raw DOM access may be needed.
import { watchEffect } from 'vue'
import { useQuerySelector } from './use-query-selector.js'
const { element } = useQuerySelector('.my-selector')
watchEffect(() => {
console.log('First element matching .my-selector: %o', element.value)
})
- Combinators are not supported in selectors (as they greatly reduce the performance of the MutationObserver).