( elementRef: RefObject<HTMLElement>, config: MutationObserverInit, onMutation: (record: MutationRecord) => void )
| 7 | * Subscribes to an element's DOM mutations |
| 8 | */ |
| 9 | export function useMutationObserver( |
| 10 | elementRef: RefObject<HTMLElement>, |
| 11 | config: MutationObserverInit, |
| 12 | onMutation: (record: MutationRecord) => void |
| 13 | ) { |
| 14 | const configValues = useDeepValue(config); |
| 15 | |
| 16 | const onMutationHandler = useEvent(onMutation); |
| 17 | useEffect(() => { |
| 18 | if (elementRef.current) { |
| 19 | const mo = new MutationObserver((records) => { |
| 20 | records.forEach((record) => { |
| 21 | onMutationHandler(record); |
| 22 | }); |
| 23 | }); |
| 24 | mo.observe(elementRef.current, configValues); |
| 25 | return () => mo.disconnect(); |
| 26 | } |
| 27 | }, [configValues, /** These are stable: */ elementRef, onMutationHandler]); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Watches a DOM element's attribute for changes |
no test coverage detected