Skip to content

Commit

Permalink
feat: added MutationObserver (#20)
Browse files Browse the repository at this point in the history
* feat: added MutationObserver

* mend
  • Loading branch information
chrisbakr authored Oct 11, 2024
1 parent c2f4de9 commit 3f3bf0c
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions assets/js/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
var is_open = true;
active_item = $(this);

// Remove any existing popups to prevent duplicates
jQuery(".acf-svg-icon-picker__popup-holder").remove();

if (acfSvgIconPicker.svgs.length == 0) {
var list = '<p>' + acfSvgIconPicker.no_icons_msg + '</p>';
} else {
Expand Down Expand Up @@ -212,4 +215,23 @@
});
});
}

// Use MutationObserver to detect changes in the DOM
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes.length) {
$(mutation.addedNodes)
.find(".acf-svg-icon-picker")
.each(function () {
initialize_field($(this));
});
}
});
});

// Start observing the document body for changes
observer.observe(document.body, {
childList: true,
subtree: true,
});
})(jQuery);

0 comments on commit 3f3bf0c

Please sign in to comment.