Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

fix(a11y): move focus into popover menus and back to their trigger (#17979)

Дамян Минковf885bc261e8c
diff --git a/react/features/base/popover/components/Popover.web.tsx b/react/features/base/popover/components/Popover.web.tsx
index b3ff2bd763..dd22b9cfee 100644
--- a/react/features/base/popover/components/Popover.web.tsx
+++ b/react/features/base/popover/components/Popover.web.tsx
@@ -7,7 +7,7 @@ import DialogPortal from '../../../toolbox/components/web/DialogPortal';
import Drawer from '../../../toolbox/components/web/Drawer';
import JitsiPortal from '../../../toolbox/components/web/JitsiPortal';
import { isElementInTheViewport } from '../../ui/functions.web';
-import { getContextMenuStyle } from '../functions.web';
+import { getContextMenuStyle, setPopoverTrigger } from '../functions.web';
/**
* The type of the React {@code Component} props of {@link Popover}.
@@ -123,7 +123,7 @@ interface IState {
* Whether the popover should be focus locked or not.
*
* This is enabled if we notice the popover is interactive
- * (trigger is click or focusable is true).
+ * (trigger is click, or focusable is true and the popover was opened with the keyboard).
*/
enableFocusLock: boolean;
}
@@ -152,7 +152,12 @@ class Popover extends Component<IProps, IState> {
*/
_containerRef: React.RefObject<HTMLDivElement>;
- _contextMenuRef: HTMLElement;
+ _contextMenuRef?: HTMLElement;
+
+ /**
+ * Whether the popover was opened using the keyboard.
+ */
+ _openedWithKeyboard = false;
/**
* Initializes a new {@code Popover} instance.
@@ -213,6 +218,8 @@ class Popover extends Component<IProps, IState> {
window.addEventListener('keydown', this._onEscKey);
} else {
window.removeEventListener('keydown', this._onEscKey);
+ this._openedWithKeyboard = false;
+ this.setState({ enableFocusLock: false });
}
}
}
@@ -349,13 +356,15 @@ class Popover extends Component<IProps, IState> {
/**
* Sets the context menu's ref.
*
- * @param {HTMLElement} elem -The html element of the context menu.
+ * @param {HTMLElement|null} elem -The html element of the context menu.
*
* @returns {void}
*/
- _setContextMenuRef(elem: HTMLElement) {
+ _setContextMenuRef(elem: HTMLElement | null) {
if (!elem || document.body.contains(elem)) {
- this._contextMenuRef = elem;
+ this._contextMenuRef && setPopoverTrigger(this._contextMenuRef);
+ this._contextMenuRef = elem ?? undefined;
+ elem && setPopoverTrigger(elem, this._containerRef.current);
}
}
@@ -463,6 +472,7 @@ class Popover extends Component<IProps, IState> {
if (this.props.visible) {
this._onHideDialog();
} else {
+ this._openedWithKeyboard = !this.props.disablePopover;
this._onShowDialog(e);
}
}
@@ -539,13 +549,14 @@ class Popover extends Component<IProps, IState> {
* Returns whether the popover is considered interactive or not.
*
* Interactive means the popover content is certainly composed of buttons, links…
- * Non-interactive popovers are mostly tooltips.
+ * Non-interactive popovers are mostly tooltips. Popovers opened on hover are considered interactive only when
+ * opened with the keyboard, so that hovering them with the mouse doesn't move the focus.
*
* @private
* @returns {boolean}
*/
_isInteractive() {
- return this.props.trigger === 'click' || Boolean(this.props.focusable);
+ return this.props.trigger === 'click' || (Boolean(this.props.focusable) && this._openedWithKeyboard);
}
/**
diff --git a/react/features/base/popover/functions.web.ts b/react/features/base/popover/functions.web.ts
index 111904a11c..29b348a2b2 100644
--- a/react/features/base/popover/functions.web.ts
+++ b/react/features/base/popover/functions.web.ts
@@ -151,3 +151,41 @@ export const getContextMenuStyle = (triggerBounds: DOMRect,
}
}
};
+
+/**
+ * Maps the portal element of an open popover to the element which triggers it.
+ */
+const popoverTriggers = new WeakMap<Element, HTMLElement>();
+
+/**
+ * Stores (or clears, if no trigger is given) the trigger of the popover rendered in the given portal element.
+ *
+ * @param {Element} portal - The element the popover content is rendered in.
+ * @param {HTMLElement} trigger - The element which triggers the popover.
+ * @returns {void}
+ */
+export function setPopoverTrigger(portal: Element, trigger?: HTMLElement | null) {
+ if (trigger) {
+ popoverTriggers.set(portal, trigger);
+ } else {
+ popoverTriggers.delete(portal);
+ }
+}
+
+/**
+ * Finds the focusable trigger of the popover which contains the given element.
+ *
+ * @param {Element} element - An element inside a popover.
+ * @returns {HTMLElement|undefined} - The element to focus in order to land back on the popover trigger.
+ */
+export function getPopoverTrigger(element?: Element | null): HTMLElement | undefined {
+ for (let current = element; current; current = current.parentElement) {
+ const trigger = popoverTriggers.get(current);
+
+ if (trigger) {
+ return trigger.matches('[tabindex]:not([tabindex="-1"]), button')
+ ? trigger
+ : trigger.querySelector<HTMLElement>('[tabindex]:not([tabindex="-1"]), button') ?? undefined;
+ }
+ }
+}
diff --git a/react/features/base/ui/components/web/BaseDialog.tsx b/react/features/base/ui/components/web/BaseDialog.tsx
index 8e52dec53c..96f2421133 100644
--- a/react/features/base/ui/components/web/BaseDialog.tsx
+++ b/react/features/base/ui/components/web/BaseDialog.tsx
@@ -1,9 +1,10 @@
-import React, { ReactNode, useCallback, useContext, useEffect } from 'react';
+import React, { ReactNode, useCallback, useContext, useEffect, useState } from 'react';
import { FocusOn } from 'react-focus-on';
import { useTranslation } from 'react-i18next';
import { keyframes } from 'tss-react';
import { makeStyles } from 'tss-react/mui';
+import { getPopoverTrigger } from '../../../popover/functions.web';
import { isElementInTheViewport } from '../../functions.web';
import { DialogTransitionContext } from './DialogTransition';
@@ -162,6 +163,31 @@ const BaseDialog = ({
const { isUnmounting } = useContext(DialogTransitionContext);
const { t } = useTranslation();
+ // Dialogs are often opened from a popover menu which closes right after the dialog opens, so by the time the
+ // dialog closes the element which opened it is gone. Remember the popover's trigger to return the focus to it.
+ const [ popoverTrigger ] = useState(() => getPopoverTrigger(document.activeElement));
+
+ const returnFocus = useCallback((opener: Element) => {
+ // The focus lock may get activated only after the popover has closed, in which case it saw the body as focused.
+ if (!popoverTrigger || (opener.isConnected && opener !== document.body)) {
+ // If we return the focus to an element outside the viewport the page will scroll to
+ // this element which in our case is undesirable and the element is outside of the
+ // viewport on purpose (to be hidden). For example if we return the focus to the toolbox
+ // when it is hidden the whole page will move up in order to show the toolbox. This is
+ // usually followed up with displaying the toolbox (because now it is on focus) but
+ // because of the animation the whole scenario looks like jumping large video.
+ return isElementInTheViewport(opener);
+ }
+
+ // This runs synchronously while the focus lock is being deactivated: the dialog is still attached and the
+ // lock still handles focus events, so it would pull the focus back. Defer it the same way react-focus-lock
+ // defers its own return focus.
+ Promise.resolve().then(
+ () => isElementInTheViewport(popoverTrigger) && popoverTrigger.focus({ preventScroll: true }));
+
+ return false;
+ }, [ popoverTrigger ]);
+
const onBackdropClick = useCallback(() => {
!disableBackdropClose && onClose?.();
}, [ disableBackdropClose, onClose ]);
@@ -189,16 +215,7 @@ const BaseDialog = ({
<FocusOn
className = { classes.focusLock }
onClickOutside = { onBackdropClick }
- returnFocus = {
-
- // If we return the focus to an element outside the viewport the page will scroll to
- // this element which in our case is undesirable and the element is outside of the
- // viewport on purpose (to be hidden). For example if we return the focus to the toolbox
- // when it is hidden the whole page will move up in order to show the toolbox. This is
- // usually followed up with displaying the toolbox (because now it is on focus) but
- // because of the animation the whole scenario looks like jumping large video.
- isElementInTheViewport
- }>
+ returnFocus = { returnFocus }>
<div
aria-description = { description }
aria-label = { title ?? t(titleKey ?? '') }
diff --git a/react/features/toolbox/components/web/DialogPortal.ts b/react/features/toolbox/components/web/DialogPortal.ts
index 1c9f57633e..33486fe13e 100644
--- a/react/features/toolbox/components/web/DialogPortal.ts
+++ b/react/features/toolbox/components/web/DialogPortal.ts
@@ -77,7 +77,6 @@ function DialogPortal({ children, className, style, getRef, setSize, targetSelec
useEffect(() => {
if (portalTarget && getRef) {
- getRef(portalTarget);
portalTarget.style.zIndex = `${ZINDEX_DIALOG_PORTAL}`;
}
}, [ portalTarget, getRef ]);
@@ -107,6 +106,9 @@ function DialogPortal({ children, className, style, getRef, setSize, targetSelec
if (document.body) {
document.body.appendChild(portalTarget);
observer.observe(target ?? portalTarget);
+
+ // Hand out the reference only once the target is attached to the document.
+ getRef?.(portalTarget);
}
return () => {
@@ -114,6 +116,7 @@ function DialogPortal({ children, className, style, getRef, setSize, targetSelec
if (document.body) {
document.body.removeChild(portalTarget);
}
+ getRef?.(null);
};
}, [ videoSpaceWidth ]);