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

Popup closeQuery prop does not work as expected #2218

Closed
endigo9740 opened this issue Nov 6, 2023 · 2 comments · Fixed by #2237
Closed

Popup closeQuery prop does not work as expected #2218

endigo9740 opened this issue Nov 6, 2023 · 2 comments · Fixed by #2237
Assignees
Labels
bug Something isn't working

Comments

@endigo9740
Copy link
Contributor

          Currently the `closeQuery` prop doesn't work as outlined in the documentation. It states: `You may provide a custom query or set '' to disable this feature.`

However, setting closeQuery to '' throws an error in the console.

image

We should probably adjust the onWindowClick event to account for this:

function onWindowClick(event: any): void {
	// Return if the popup is not yet open
	if (popupState.open === false) return;
	// Return if click is the trigger element
	if (triggerNode.contains(event.target)) return;
	// If click it outside the popup
	if (elemPopup && elemPopup.contains(event.target) === false) {
		close();
		return;
	}
	// Handle Close Query State
	const closeQueryString: string = args.closeQuery === undefined ? 'a[href], button' : args.closeQuery;
	// Respect not querying for things if the string is empty
	if (closeQueryString === '') {
		return;
	}
	const closableMenuElements = elemPopup?.querySelectorAll(closeQueryString);
	closableMenuElements?.forEach((elem) => {
		if (elem.contains(event.target)) close();
	});
}

Also I would propose that we could potentially solve some issues with data-popup conflicts by being able to pass in a bind:this reference instead of only a data-popup value.

Example:

<script lang="ts">
// Let the user maintain their own reference to the target
let popupEl: HTMLElement | null = null;

const popupFeatured: PopupSettings = {
	event: 'focus-click',
	// Matches the real element from bind:this
	target: popupEl,
	placement: 'bottom',
};
</script>

<button class="btn variant-filled" use:popup={popupFeatured}>Show Popup</button>

<div class="card p-4 w-72 shadow-xl" bind:this={popupEl}>
	<div><p>Demo Content</p></div>
</div>

Accomplishing this would likely be fairly straightforward as well by modifying the setDomElements method.

function setDomElements(): void {
	// If a real element was passed, use it as the elemPopup
	if (args.target instanceof Element) {
		elemPopup = args.target;
	} else {
		elemPopup = document.querySelector(`[data-popup="${args.target}"]`) ?? document.createElement('div');
	}
	elemArrow = elemPopup.querySelector(`.arrow`) ?? document.createElement('div');
}

Originally posted by @DerrikMilligan in #1916 (comment)

@endigo9740 endigo9740 added the bug Something isn't working label Nov 6, 2023
@DerrikMilligan
Copy link
Contributor

I would be happy to make a pull request for this if we feel that my solution was acceptable. I was trying to read the contribution guidelines and someone needs to be assigned to work on an issue before they make a pull request right?

@Sarenor
Copy link
Contributor

Sarenor commented Nov 11, 2023

Consider that done 👍 (the assigning I mean)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants