import { MaybeRef , unRef } from '@react-hooks-library/shared' ; import { useCallback , useEffect , useRef } from 'react' ; import { _window } from '../_ssr.config' ; import { useIsSupported } from '../useIsSupported' ; import { useUnMount } from '../useUnMount' ; param target param callback param options see https see https export function useMutationObserver ( target , callback , options = { } ) { const observer = useRef < MutationObserver | null > ( null ) ; const isSupported = useIsSupported ( ( ) => ! ! _window ?. IntersectionObserver ) ; const stop = useCallback ( ( ) => { if ( ! observer . current ) return ; observer . current . disconnect ( ) ; observer . current = null ; } , [ ] ) ; useUnMount ( stop ) ; useEffect ( ( ) => { const el = unRef ( target ) ; if ( ! ( isSupported && el && _window ) ) return ; observer . current = new _window . MutationObserver ( callback ) ; observer . current ?. observe ( el , options ) ; return stop ; } , [ callback , stop , options , target , isSupported ] ) ; return { isSupported , stop } ; }