|
@@ -33,6 +33,7 @@ View the documentation of previous major releases: [3.0.0](https://github.com/Sv
|
|
|
- [interceptEvent()](#interceptevent) - conditionally intercepts events registered by `addEventListener()` on any given EventTarget object
|
|
|
- [interceptWindowEvent()](#interceptwindowevent) - conditionally intercepts events registered by `addEventListener()` on the window object
|
|
|
- [isScrollable()](#isscrollable) - check if an element has a horizontal or vertical scroll bar
|
|
|
+ - [observeElementProperty()](#observeelementproperty) - observe changes to an element's property that can't be observed with MutationObserver
|
|
|
- [**Math:**](#math)
|
|
|
- [clamp()](#clamp) - constrain a number between a min and max value
|
|
|
- [mapRange()](#maprange) - map a number from one range to the same spot in another range
|
|
@@ -675,6 +676,59 @@ console.log("Element has a vertical scroll bar:", vertical);
|
|
|
|
|
|
</details>
|
|
|
|
|
|
+<br>
|
|
|
+
|
|
|
+### observeElementProperty()
|
|
|
+Usage:
|
|
|
+```ts
|
|
|
+observeElementProperty(
|
|
|
+ element: Element,
|
|
|
+ property: string,
|
|
|
+ callback: (oldValue: any, newValue: any) => void
|
|
|
+): void
|
|
|
+```
|
|
|
+
|
|
|
+Observes changes to an element's property.
|
|
|
+While regular attributes can be observed using a MutationObserver, this is not always possible for properties that are changed through setter functions and assignment.
|
|
|
+This function shims the setter of the provided property and calls the callback function whenever it is changed through any means.
|
|
|
+
|
|
|
+When using TypeScript, the types for `element`, `property` and the arguments for `callback` will be automatically inferred.
|
|
|
+
|
|
|
+<details><summary><b>Example - click to view</b></summary>
|
|
|
+
|
|
|
+```ts
|
|
|
+import { observeElementProperty } from "@sv443-network/userutils";
|
|
|
+
|
|
|
+const myInput = document.querySelector("input#my-input");
|
|
|
+
|
|
|
+let value = 0;
|
|
|
+
|
|
|
+setInterval(() => {
|
|
|
+ value += 1;
|
|
|
+ myInput.value = String(value);
|
|
|
+}, 1000);
|
|
|
+
|
|
|
+
|
|
|
+const observer = new MutationObserver((mutations) => {
|
|
|
+ // will never be called:
|
|
|
+ console.log("MutationObserver mutation:", mutations);
|
|
|
+});
|
|
|
+
|
|
|
+// one would think this should work, but "value" is a JS object *property*, not a DOM *attribute*
|
|
|
+observer.observe(myInput, {
|
|
|
+ attributes: true,
|
|
|
+ attributeFilter: ["value"],
|
|
|
+});
|
|
|
+
|
|
|
+
|
|
|
+observeElementProperty(myInput, "value", (oldValue, newValue) => {
|
|
|
+ // will be called every time the value changes:
|
|
|
+ console.log("Value changed from", oldValue, "to", newValue);
|
|
|
+});
|
|
|
+```
|
|
|
+
|
|
|
+</details>
|
|
|
+
|
|
|
<br><br>
|
|
|
|
|
|
<!-- #SECTION Math -->
|