Skip to main content
Home

Built and signed on GitHub Actions

Works with
This package works with Browsers
This package works with Browsers
JSR Score100%
Downloads1/wk
Published2 months ago (1.0.41)

Persistent DOM mutation observations based on CSS query selectors

mutative

mutative utility on Storybook mutative version on JSR JSR score

Persistent DOM mutation observations based on CSS query selectors

  • Version: 1.0.41
  • License: LGPL-3

Using this package

Browser

<script src="https://esm.sh/jsr/@web-components/mutative" type="module"></script>

Deno

deno add jsr:@web-components/mutative

NPM

npx jsr add @web-components/mutative

Documentation

Persistent DOM mutation observations based on CSS query selectors. It's essentially a wrapper for a global MutationObserver that filters records to specific callbacks. The API is similar to MutationObserver, but not the same.

The advantage is that observers can be set up independently/ahead of matching DOM elements, which is useful when working with SPAs or other reactive content.

// pass a single selector and callback
Mutative.observe("p", (record) => console.log(record));

//pass multiple selectors with the same callback
Mutative.observe(["p", ".text", "*[data-text]"], (record) => console.log("text mutated", record));

//pass multiple selectors at once with different callbacks
Mutative.observe({
    "*[data-text]": (rec) => console.log(rec),
    p: (rec) => alert("paragraph edited"),
    output: (rec) => console.log("calculation updated", rec),
});

//Remove observations for the 'p' selector only
Mutative.disconnect("p");

// Pause all observations
Mutative.disconnect();

//Resume existing observations
Mutative.observe();

observe()

The parameters are different from the MutationObserver implementation. Instead of a target and options there is selectors and callback.

  • selectors - Several types are allowed:
    • null - If no arguments are passed, observation of existing selectors will resume.
    • string - a single CSS query selector.
    • string[] - multiple CSS query selectors that use the same callback.
    • object - CSS query selectors strings as keys, callbacks as values.
  • callback - Only required when selectors is a string or array of strings. A function that accepts a MutationRecord as it's only parameter.

disconnect()

Mutations that have been detected but not yet reported to observers are not discarded. Observer callbacks are triggered before disconnection. This method will either pause or remove observations and callbacks.

  • When called with no arguments: acts the same as disconnect(), ignoring all future mutations. Note: this does not clear the internal selector list, so calling observe() again will continue with existing selectors.
  • When passed with an argument: The arguments follow the same formats as observe()'s selectors parameter. Only observers with the passed selectors are removed. If no selectors remain, observation is paused (as if no arguments were passed).

takeRecords()

Takes all records from the Mutative object, use carefully. See: takeRecords().

How it works

A single MutationObserver is created on the document body with the first call of mutative.observe(). MutationRecords are only passed to callbacks that have a matching selector for at least one of target, addedNodes, or removedNodes.


Made by jackcarey.

Examples

Example 1

Mutative.observe("p", (record) => console.log(record));
Built and signed on
GitHub Actions

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.