diff --git a/gui/app/src/components/commons/Tooltip.tsx b/gui/app/src/components/commons/Tooltip.tsx index e6083249c..a562c9210 100644 --- a/gui/app/src/components/commons/Tooltip.tsx +++ b/gui/app/src/components/commons/Tooltip.tsx @@ -14,6 +14,7 @@ import { import { createPortal } from 'react-dom'; import { Typography } from './Typography'; import { CloseIcon } from './icon/CloseIcon'; +import { focusVisible } from '@/utils/focus-nav'; export type Direction = 'top' | 'left' | 'right' | 'bottom'; interface TooltipProps { @@ -124,17 +125,23 @@ export function FloatingTooltip({ const open = () => setIsOpen(true); const close = () => setIsOpen(false); + // Focus only shows the tooltip when the focus is one a user should see + // marked. A click focuses the control too, and so does a closing modal + // handing focus back to the control that opened it. + const focused = () => { + if (focusVisible(elem)) setIsOpen(true); + }; elem.addEventListener('mouseenter', open); elem.addEventListener('mouseleave', close); - elem.addEventListener('focus', open); + elem.addEventListener('focus', focused); elem.addEventListener('blur', close); return () => { elem.style.removeProperty('anchor-name'); elem.removeEventListener('mouseenter', open); elem.removeEventListener('mouseleave', close); - elem.removeEventListener('focus', open); + elem.removeEventListener('focus', focused); elem.removeEventListener('blur', close); }; } diff --git a/gui/app/src/hooks/controller-nav.ts b/gui/app/src/hooks/controller-nav.ts index 896db5e02..170f14fd0 100644 --- a/gui/app/src/hooks/controller-nav.ts +++ b/gui/app/src/hooks/controller-nav.ts @@ -2,6 +2,7 @@ import { useEffect, useRef } from 'react'; import { useLocation } from 'react-router-dom'; import { ARROW_KEY, + CONTROLLER_ACTIVE_CLASS, firstFocusable, pickBest, scrollableToward, @@ -18,7 +19,7 @@ import { } from '@/utils/gamepad'; import { useConfig } from './config'; -const ACTIVE_CLASS = 'controller-active'; +const ACTIVE_CLASS = CONTROLLER_ACTIVE_CLASS; const EDIT_CLASS = 'nav-editing'; const ARROW_OWNER = '[aria-expanded="true"],[data-nav-arrows]'; diff --git a/gui/app/src/utils/focus-nav.ts b/gui/app/src/utils/focus-nav.ts index e412a8af5..7186b9d8a 100644 --- a/gui/app/src/utils/focus-nav.ts +++ b/gui/app/src/utils/focus-nav.ts @@ -26,6 +26,13 @@ export const ARROW_KEY: Record = { right: 'ArrowRight', }; +export const CONTROLLER_ACTIVE_CLASS = 'controller-active'; + +export function focusVisible(el: HTMLElement): boolean { + if (document.documentElement.classList.contains(CONTROLLER_ACTIVE_CLASS)) return true; + return el.matches(':focus-visible'); +} + export function isFocusable(el: HTMLElement): boolean { // Natively disabled elements cannot take focus at all, so there is nothing to // aim at. `aria-disabled` ones can, and should: a control that is off for a