From 0f2ad1abaffe7e2b8b9c66866234ee87d953c86b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Malinowski?= Date: Mon, 1 Nov 2021 22:48:51 +0100 Subject: [PATCH] fix(ToolTip): better toggling logic --- src/Tooltip/Tooltip.svelte | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Tooltip/Tooltip.svelte b/src/Tooltip/Tooltip.svelte index c1583968..d8860b9c 100644 --- a/src/Tooltip/Tooltip.svelte +++ b/src/Tooltip/Tooltip.svelte @@ -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(); @@ -174,7 +183,7 @@ } } - open = false; + if (!ref.contains(target)) open = false; } }}" /> @@ -190,8 +199,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}" > @@ -205,8 +214,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}" >