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 1/3] 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 c1583968a8..d8860b9cd1 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}" > From 23c10351868bda74122339a46b23db0cbb9be777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Malinowski?=
Date: Tue, 2 Nov 2021 18:01:15 +0100 Subject: [PATCH 2/3] fix(ToolTip): remove redundant click handler --- src/Tooltip/Tooltip.svelte | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Tooltip/Tooltip.svelte b/src/Tooltip/Tooltip.svelte index d8860b9cd1..08a4aa4f2d 100644 --- a/src/Tooltip/Tooltip.svelte +++ b/src/Tooltip/Tooltip.svelte @@ -182,8 +182,6 @@ ref.focus(); } } - - if (!ref.contains(target)) open = false; } }}" /> From 028ebdf39103e71351593f1380625102f63f6de6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Malinowski?=
Date: Sat, 6 Nov 2021 11:48:06 +0100 Subject: [PATCH 3/3] fix(ToolTip): ensure at most one ToolTip is open at the same time - a click event closes the tooltip if the target is not the tooltip or the tooltip icon. - the event is registered in the capture phase and a `setTimeout` is used to deal with edge cases in which a button is used to toggle the tooltip on or off. --- src/Tooltip/Tooltip.svelte | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Tooltip/Tooltip.svelte b/src/Tooltip/Tooltip.svelte index 08a4aa4f2d..b206c869d8 100644 --- a/src/Tooltip/Tooltip.svelte +++ b/src/Tooltip/Tooltip.svelte @@ -184,6 +184,13 @@ } } }}" + on:click|capture="{({ target }) => { + if (open && !ref.contains(target) && !refTooltip.contains(target)) { + setTimeout(() => { + open = false; + }); + } + }}" />