-
-
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.
feat(useMutationObserver): init (#13)
- Loading branch information
Showing
9 changed files
with
157 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './useEventListener' | ||
export * from './useMediaQuery' | ||
export * from './useMutationObserver' | ||
export * from './usePreferredDark' | ||
export * from './useStorage' | ||
export * from '@svelte-use/shared' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<script lang="ts"> | ||
import { onMount } from 'svelte' | ||
import { useMutationObserver } from '.' | ||
let el: HTMLDivElement | ||
let messages: string[] = [] | ||
let className = '' | ||
let style = '' | ||
onMount(() => { | ||
useMutationObserver(el, mutations => { | ||
const mutation = mutations[0] | ||
if (!mutation) { | ||
return | ||
} | ||
messages.push(mutation.attributeName!) | ||
messages = messages | ||
}, { attributes: true }) | ||
}) | ||
setTimeout(() => { | ||
className = 'test test2' | ||
}, 1000) | ||
setTimeout(() => { | ||
style = 'color: red' | ||
}, 1550) | ||
</script> | ||
|
||
<div bind:this={el} class={className} style={style}> | ||
{#each messages as text} | ||
<div>Mutation Attribute: { text }</div> | ||
{/each} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
category: Sensors | ||
--- | ||
|
||
# useMutationObserver | ||
|
||
Watch for changes being made to the DOM tree. [MutationObserver MDN](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) | ||
|
||
## Usage | ||
|
||
```html | ||
<script> | ||
import { useMutationObserver } from '@svelte-use/core' | ||
let el | ||
let messages = [] | ||
onMount(() => { | ||
useMutationObserver( | ||
el, | ||
(mutations) => { | ||
if (!mutations[0]) { | ||
messages.push(mutations[0].attributeName) | ||
messages = messages | ||
} | ||
}, | ||
{ attributes: true }, | ||
) | ||
}) | ||
</script> | ||
``` |
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,56 @@ | ||
import { MaybeReadable, toReadable, tryOnDestroy } from '@svelte-use/shared' | ||
import { ConfigurableWindow, defaultWindow } from '../_configurable' | ||
|
||
export interface MutationObserverOptions | ||
extends MutationObserverInit, | ||
ConfigurableWindow {} | ||
|
||
/** | ||
* Watch for changes being made to the DOM tree. | ||
* | ||
* @see https://svelte-use.vercel.app/core/useMutationObserver | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver MutationObserver MDN | ||
* @param target | ||
* @param callback | ||
* @param options | ||
*/ | ||
export function useMutationObserver( | ||
target: MaybeReadable<Element | undefined | null>, | ||
callback: MutationCallback, | ||
options: MutationObserverOptions = {}, | ||
) { | ||
const { window = defaultWindow, ...mutationOptions } = options | ||
let observer: MutationObserver | undefined | ||
const isSupported = window && 'IntersectionObserver' in window | ||
|
||
const cleanup = () => { | ||
if (observer) { | ||
observer.disconnect() | ||
observer = undefined | ||
} | ||
} | ||
|
||
const unsubscribe = toReadable(target).subscribe((el) => { | ||
cleanup() | ||
|
||
if (isSupported && window && el) { | ||
// @ts-expect-error missing type | ||
observer = new window.MutationObserver(callback) | ||
observer!.observe(el, mutationOptions) | ||
} | ||
}) | ||
|
||
const stop = () => { | ||
cleanup() | ||
unsubscribe() | ||
} | ||
|
||
tryOnDestroy(stop) | ||
|
||
return { | ||
isSupported, | ||
stop, | ||
} | ||
} | ||
|
||
export type UseMutationObserverReturn = ReturnType<typeof useMutationObserver> |
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