A custom element that offers a declarative interface to the MutationObserver API.
$ npm install --save @georapbox/mutation-observer-elementimport { MutationObserverElement } from './node_modules/@georapbox/mutation-observer-element/dist/mutation-observer.js';
// Manually define the element.
MutationObserverElement.defineCustomElement();Alternatively, you can import the automatically defined custom element.
import './node_modules/@georapbox/mutation-observer-element/dist/mutation-observer-defined.js';<mutation-observer
attr="*"
attr-old-value
char-data
char-data-old-value
child-list
>
<button class="btn-primary">Click to mutate me</button>
</mutation-observer>| Name | Reflects | Type | Default | Description |
|---|---|---|---|---|
attr |
✓ | String | "" |
A space-separated string of attribute names to monitor (e.g., "title class href"). Use "*" to monitor all attributes. |
attrOldValueattr-old-value |
✓ | Boolean | false |
Set to true to record the previous value of any attribute that changes when monitoring the node or nodes for attribute changes. |
childListchild-list |
✓ | Boolean | false |
Set to true to monitor the target node for the addition of new child nodes or removal of existing child nodes. |
charDatachar-data |
✓ | Boolean | false |
Set to true to monitor the specified target node for changes to the character data contained within the node or nodes. |
charDataOldValuechar-data-old-value |
✓ | Boolean | false |
Set to true to record the previous value of a node's text whenever the text changes on nodes being monitored. |
disabled |
✓ | Boolean | false |
Set to true to stop monitoring for mutations. |
NOTE: From the properties above, at least one of
attr,child-listorchar-datamust be present, otherwise, no changes are monitored and no mutation events are emitted.
| Name | Description |
|---|---|
| (default) | The default slot where the target node or nodes to be monitored are placed. |
| Name | Type | Description | Arguments |
|---|---|---|---|
defineCustomElement |
Static | Defines/registers the custom element with the name provided. If no name is provided, the default name is used. The method checks if the element is already defined, hence will skip trying to redefine it. | elementName='mutation-observer' |
| Name | Description | Event Detail |
|---|---|---|
mutation-observer:mutate |
Emitted when the element is mutated. | { mutationList: MutationRecord[] |
For API updates and breaking changes, check the CHANGELOG.
The project requires Node.js and npm to be installed on your environment. Preferrably, use nvm Node Version Manager and use the version of Node.js specified in the .nvmrc file by running nvm use.
Install the project dependencies by running the following command.
npm installWatch for changes and start a development server by running the following command.
npm startLint the code by running the following command.
npm run lintRun the tests by running any of the following commands.
npm test
npm run test:watch # watch modeCreate a production build by running the following command.
npm run build