For me, being able to offer my library as a web component is kind of neat. Sure I can ask users of my library to import it then query select math inputs, then apply my library on these, and remember to apply it also on DOM change.
<script type="module">
import lib from "/path/to/my/lib.js";
for (const target of document.querySelectorAll(".target")) {
lib(target);
}
// TODO: Watch for DOM change and apply lib.
<script>
<span class="target">Some Input</span>
Or I can simply ask them to import my web component module and use the custom element:
It may just be me, but all of these seem a lot inferior API design then a simple web component. Particularly if I’m asking my users to setup a mutation observer.
Aside: Don’t you need to pass the library function into the callback while constructing the mutation observer?
import lib, { mutationObserverCallback } from "https://my-lib.example";
lib({
target: ".target",
observer: new MutationObserver(mutationObserverCallback),
});
Honestly, this feels like so much magic compared to a simple:
<my-lib>Some Input</my-lib>
where I handle update in my library by listening to the slotchange event.