Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed popup not being able to find the correct target because client side rendering hasn't finished yet #2466

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lucky-items-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@skeletonlabs/skeleton": patch
---

bugfix: fixed popup not being able to find the correct target because client side rendering hasn't finished yet
22 changes: 22 additions & 0 deletions packages/skeleton/src/lib/utilities/Popup/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ export function popup(triggerNode: HTMLElement, args: PopupSettings) {
elemPopup = document.querySelector(`[data-popup="${args.target}"]`) ?? document.createElement('div');
elemArrow = elemPopup.querySelector(`.arrow`) ?? document.createElement('div');
}

// MutationObserverCallback
function handleMutation(mutationList: MutationRecord[], observer: MutationObserver): void {
mutationList.forEach((mutation) => {
const mutationTarget = mutation.target as HTMLElement;

// If the mutation target has the data-popup attribute and the correct value is set, update the elements and disconnect the observer

if (mutationTarget.hasAttribute('data-popup') && mutationTarget.getAttribute('data-popup') == args.target) {
setDomElements();
observer.disconnect();
return;
}
});
}

// Instantiate the MutationObserver with the callback
const observer: MutationObserver = new MutationObserver(handleMutation);

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

setDomElements(); // init

// Render Floating UI Popup
Expand Down