-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #205 from Enterwell/stage
Stage
- Loading branch information
Showing
14 changed files
with
278 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useState } from 'react'; | ||
import { useMutationObserver } from '@enterwell/react-hooks'; | ||
|
||
export function ExampleUseMutationObserver() { | ||
const [mutations, setMutations] = useState<MutationRecord[]>([]); | ||
|
||
// @highlight-start | ||
useMutationObserver( | ||
document.querySelector('html'), | ||
(mutations) => { | ||
setMutations((curr) => [...curr, ...mutations]); | ||
}, { | ||
attributes: true, | ||
characterData: true, | ||
childList: true, | ||
}); | ||
// @highlight-end | ||
|
||
return ( | ||
<div> | ||
<p>Try changing theme to trigger DOM changes.</p> | ||
<br /> | ||
<div>Mutations:</div> | ||
<ul> | ||
{mutations.map((mutation, index) => ( | ||
<li key={index}> | ||
<pre>{JSON.stringify(mutation, null, 2)}</pre> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
apps/docs/pages/react-hooks/hooks/use-mutation-observer.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
title: useMutationObserver | ||
--- | ||
|
||
import { useResizeObserver } from '@enterwell/react-hooks'; | ||
import { ComponentWithSource } from '../../../components/docs/ComponentWithSource.tsx'; | ||
import { ComponentDescription, ComponentParameters, ComponentSource } from '../../../components/docs/ComponentDocs'; | ||
import { ExampleUseMutationObserver } from '../../../components/ExampleUseMutationObserver'; | ||
|
||
# useMutationObserver | ||
|
||
## Description | ||
|
||
<ComponentDescription name="useMutationObserver" /> | ||
|
||
### Parameters | ||
|
||
<ComponentParameters name="useMutationObserver" /> | ||
|
||
## Example | ||
|
||
<ComponentWithSource component={ ExampleUseMutationObserver } centered /> | ||
|
||
## Inspect | ||
|
||
<details> | ||
<summary>Source code</summary> | ||
<ComponentSource | ||
package="@enterwell/react-hooks" | ||
directory="hooks" | ||
name="useMutationObserver" | ||
/> | ||
</details> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { MutableRefObject, useCallback, useEffect, useRef } from 'react' | ||
|
||
/** | ||
* A type that represents either a React ref or a DOM node. | ||
* @public | ||
*/ | ||
export type MaybeRef<T> = T | MutableRefObject<T> | ||
|
||
function isRef(obj: unknown): boolean { | ||
return obj !== null && | ||
typeof obj === 'object' && | ||
Object.prototype.hasOwnProperty.call(obj, 'current'); | ||
} | ||
|
||
function unRef<T = HTMLElement>(target: MaybeRef<T>): T { | ||
return isRef(target) | ||
? (target as MutableRefObject<T>).current | ||
: (target as T); | ||
} | ||
|
||
/** | ||
* Watch for changes being made to the DOM tree. | ||
* | ||
* @param target - React ref or DOM node | ||
* @param callback - callback to execute when mutations are observed | ||
* @param options - Options passed to mutation observer | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver MutationObserver MDN | ||
* @see https://react-hooks-library.vercel.app/core/useMutationObserver | ||
* | ||
* @public | ||
*/ | ||
export function useMutationObserver( | ||
target: MaybeRef<Element | null | undefined>, | ||
callback: MutationCallback, | ||
options: MutationObserverInit = {} | ||
) { | ||
const observer = useRef<MutationObserver | null>(null) | ||
|
||
const stop = useCallback(() => { | ||
if (!observer.current) return | ||
|
||
observer.current.disconnect() | ||
observer.current = null | ||
}, []) | ||
|
||
// On unmount, cleanup | ||
useEffect(() => stop, [stop]); | ||
|
||
useEffect(() => { | ||
const el = unRef(target); | ||
|
||
if (!(el && window)) return | ||
|
||
observer.current = new window.MutationObserver(callback) | ||
observer.current?.observe(el, options) | ||
|
||
return stop | ||
}, [callback, stop, options, target]) | ||
|
||
return { | ||
stop | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.