( elementRef: RefObject<HTMLElement>, attributeName: string, onChange: (value: string | null) => void )
| 31 | * Watches a DOM element's attribute for changes |
| 32 | */ |
| 33 | export function useAttributeObserver( |
| 34 | elementRef: RefObject<HTMLElement>, |
| 35 | attributeName: string, |
| 36 | onChange: (value: string | null) => void |
| 37 | ) { |
| 38 | const config = useMemo<MutationObserverInit>( |
| 39 | () => ({ |
| 40 | attributes: true, |
| 41 | attributeFilter: [attributeName], |
| 42 | }), |
| 43 | [attributeName] |
| 44 | ); |
| 45 | return useMutationObserver(elementRef, config, () => { |
| 46 | const newValue = elementRef.current!.getAttribute(attributeName); |
| 47 | onChange(newValue); |
| 48 | }); |
| 49 | } |
no test coverage detected