MCPcopy Create free account
hub / github.com/ampproject/amphtml / useAttributeObserver

Function useAttributeObserver

src/preact/hooks/useMutationObserver.tsx:33–49  ·  view source on GitHub ↗
(
  elementRef: RefObject<HTMLElement>,
  attributeName: string,
  onChange: (value: string | null) => void
)

Source from the content-addressed store, hash-verified

31 * Watches a DOM element's attribute for changes
32 */
33export 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}

Callers 1

SlottedDomWrapperFunction · 0.90

Calls 2

useMutationObserverFunction · 0.85
getAttributeMethod · 0.80

Tested by

no test coverage detected