fix(ToolTip): better toggling logic (#885)

* fix(ToolTip): better toggling logic

* fix(ToolTip): remove redundant click handler

* 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.
This commit is contained in:
Paweł Malinowski 2021-11-07 21:03:32 +01:00 committed by GitHub
commit f32f8fb9dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -89,10 +89,19 @@
} }
} }
function openMenu() { function onFocus() {
open = true; 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(() => { afterUpdate(() => {
if (open) { if (open) {
const button = ref.getBoundingClientRect(); const button = ref.getBoundingClientRect();
@ -173,8 +182,13 @@
ref.focus(); ref.focus();
} }
} }
}
open = false; }}"
on:click|capture="{({ target }) => {
if (open && !ref.contains(target) && !refTooltip.contains(target)) {
setTimeout(() => {
open = false;
});
} }
}}" }}"
/> />
@ -190,8 +204,8 @@
bind:this="{refIcon}" bind:this="{refIcon}"
{...buttonProps} {...buttonProps}
aria-describedby="{tooltipId}" aria-describedby="{tooltipId}"
on:click|preventDefault|stopPropagation="{openMenu}" on:mousedown="{onMousedown}"
on:focus="{openMenu}" on:focus="{onFocus}"
on:blur="{onBlur}" on:blur="{onBlur}"
on:keydown="{onKeydown}" on:keydown="{onKeydown}"
> >
@ -205,8 +219,8 @@
bind:this="{ref}" bind:this="{ref}"
{...buttonProps} {...buttonProps}
aria-describedby="{tooltipId}" aria-describedby="{tooltipId}"
on:click|preventDefault|stopPropagation="{openMenu}" on:mousedown="{onMousedown}"
on:focus="{openMenu}" on:focus="{onFocus}"
on:blur="{onBlur}" on:blur="{onBlur}"
on:keydown="{onKeydown}" on:keydown="{onKeydown}"
> >