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

fix(ToolTip): better toggling logic #885

Merged
merged 3 commits into from
Nov 7, 2021
Merged
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
28 changes: 21 additions & 7 deletions src/Tooltip/Tooltip.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,19 @@
}
}

function openMenu() {
function onFocus() {
open = true;
}

function onMousedown() {
// determine the desired state before the focus event triggers.
const shouldClose = open;
// ensure changes are scheduled at the end, i.e. after the possible focus event.
setTimeout(() => {
open = shouldClose ? false : true;
});
}

afterUpdate(() => {
if (open) {
const button = ref.getBoundingClientRect();
Expand Down Expand Up @@ -173,8 +182,13 @@
ref.focus();
}
}

open = false;
}
}}"
on:click|capture="{({ target }) => {
if (open && !ref.contains(target) && !refTooltip.contains(target)) {
setTimeout(() => {
open = false;
});
}
}}"
/>
Expand All @@ -190,8 +204,8 @@
bind:this="{refIcon}"
{...buttonProps}
aria-describedby="{tooltipId}"
on:click|preventDefault|stopPropagation="{openMenu}"
on:focus="{openMenu}"
on:mousedown="{onMousedown}"
on:focus="{onFocus}"
on:blur="{onBlur}"
on:keydown="{onKeydown}"
>
Expand All @@ -205,8 +219,8 @@
bind:this="{ref}"
{...buttonProps}
aria-describedby="{tooltipId}"
on:click|preventDefault|stopPropagation="{openMenu}"
on:focus="{openMenu}"
on:mousedown="{onMousedown}"
on:focus="{onFocus}"
on:blur="{onBlur}"
on:keydown="{onKeydown}"
>
Expand Down