# @solid-primitives/mutation-observer
[](https://bundlephobia.com/package/@solid-primitives/mutation-observer)
[](https://www.npmjs.com/package/@solid-primitives/mutation-observer)
[](https://github.com/solidjs-community/solid-primitives#contribution-process)
[](https://vitest.dev)
Primitive providing the ability to watch for changes made to the DOM tree. A wrapper for Browser's [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) API.
## Installation
```
npm install @solid-primitives/mutation-observer
# or
yarn add @solid-primitives/mutation-observer
# or
pnpm add @solid-primitives/mutation-observer
```
## How to use it
### createMutationObserver
```tsx repl
import { render } from "@solidjs/web";
import { createSignal, For } from "solid-js";
import { createMutationObserver } from "@solid-primitives/mutation-observer";
function Demo() {
const [items, setItems] = createSignal([1, 2, 3]);
const [log, setLog] = createSignal([]);
const [add] = createMutationObserver([], { childList: true }, records => {
for (const r of records) {
const msg = `+${r.addedNodes.length} / -${r.removedNodes.length} nodes`;
setLog(l => [msg, ...l].slice(0, 5));
}
});
return (
<>
{id =>
Item {id}
}
{log().join("\n") || "Click a button to mutate the list"}
>
);
}
render(() => , document.getElementById("app")!);
```
```ts
import { createMutationObserver } from "@solid-primitives/mutation-observer";
// Use the returned `add` as a ref — options are set at creation time:
const [add] = createMutationObserver([], { childList: true }, records => console.log(records));
// Observe multiple elements:
const [add, { start, stop }] = createMutationObserver(
() => [el1, el2, el3],
{ attributes: true, subtree: true },
records => console.log(records)
);
// Per-element options:
createMutationObserver(
[[el, { attributes: true }], [el1, { childList: true }]],
records => console.log(records)
);
```
Automatically starts observing after the component settles (via `onSettled`) and disconnects on cleanup. You can also control observation manually with `start()` and `stop()`.
### Standalone Ref
`mutationObserver` is a convenience for observing a single element without calling `createMutationObserver` separately:
```tsx
import { mutationObserver } from "@solid-primitives/mutation-observer";
console.log(records))} />;
```
The observer is created when `mutationObserver(...)` is evaluated and disconnects with the surrounding owner, so a single value can be used as the `ref` of several elements.
### Types
```ts
function createMutationObserver(
initial: MaybeAccessor,
options: MutationObserverInit,
callback: MutationCallback,
): MutationObserverReturn;
function createMutationObserver(
initial: MaybeAccessor<[Node, MutationObserverInit][]>,
callback: MutationCallback,
): MutationObserverReturn;
type MutationObserverReturn = [
add: MutationObserverAdd,
rest: {
start: Fn;
stop: Fn;
instance: MutationObserver;
isSupported: boolean;
},
];
type MutationObserverAdd = (target: Node, options?: MaybeAccessor) => void;
const mutationObserver: (
options: MutationObserverInit,
callback: MutationCallback,
) => (target: Element) => void;
```
## Changelog
See [CHANGELOG.md](./CHANGELOG.md)