mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-28 21:06:31 -08:00
Fix tooltips focus when opening modals
This commit is contained in:
1 parent
110b44cd9b
commit
be2e1be7f8
3 files changed
+18
-3
No files matched your search
@@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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]';
|
||||
|
||||
@@ -26,6 +26,13 @@ export const ARROW_KEY: Record<Direction, string> = {
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user