fix: use native modal dialog and use its backdrop

This uses the `<dialog>` element with `showModal()` method,
so that we can leverage the browser's built-in ways to make
this dialog modal, including keyboard handling (Esc), backdrop
and focus trap.

Fixes the issue that it was possible to tab outside the dialog
while it was open.

This removes the backdrop component, as the `<dialog>` element
comes with a backdrop built-in, that is styled via CSS instead.
This commit is contained in:
Hidde de Vries 2024-11-21 12:02:35 +01:00
parent 90e85e83a5
commit dbee3b0411
3 changed files with 52 additions and 20 deletions

View file

@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import React, {type ComponentProps} from 'react'; import React from 'react';
import clsx from 'clsx'; import clsx from 'clsx';
import {useThemeConfig} from '@docusaurus/theme-common'; import {useThemeConfig} from '@docusaurus/theme-common';
import { import {
@ -18,16 +18,6 @@ import type {Props} from '@theme/Navbar/Layout';
import styles from './styles.module.css'; import styles from './styles.module.css';
function NavbarBackdrop(props: ComponentProps<'div'>) {
return (
<div
role="presentation"
{...props}
className={clsx('navbar-sidebar__backdrop', props.className)}
/>
);
}
export default function NavbarLayout({children}: Props): JSX.Element { export default function NavbarLayout({children}: Props): JSX.Element {
const { const {
navbar: {hideOnScroll, style}, navbar: {hideOnScroll, style},
@ -56,7 +46,6 @@ export default function NavbarLayout({children}: Props): JSX.Element {
}, },
)}> )}>
{children} {children}
<NavbarBackdrop onClick={mobileSidebar.toggle} />
<NavbarMobileSidebar /> <NavbarMobileSidebar />
</nav> </nav>
); );

View file

@ -5,10 +5,16 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
.navbarHideable { .navbar-sidebar {
transition: transform var(--ifm-transition-fast) ease; inset: 0;
margin: 0;
padding: 0;
bottom: 0;
border: 0;
height: 100%;
max-height: 100%;
} }
.navbarHidden { .navbar-sidebar::backdrop {
transform: translate3d(0, calc(-100% - 2px), 0); background-color: #0009;
} }

View file

@ -5,9 +5,12 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import React from 'react'; import React, {useEffect, useRef} from 'react';
import clsx from 'clsx'; import clsx from 'clsx';
import {useNavbarSecondaryMenu} from '@docusaurus/theme-common/internal'; import {
useNavbarMobileSidebar,
useNavbarSecondaryMenu,
} from '@docusaurus/theme-common/internal';
import type {Props} from '@theme/Navbar/MobileSidebar/Layout'; import type {Props} from '@theme/Navbar/MobileSidebar/Layout';
export default function NavbarMobileSidebarLayout({ export default function NavbarMobileSidebarLayout({
@ -16,8 +19,42 @@ export default function NavbarMobileSidebarLayout({
secondaryMenu, secondaryMenu,
}: Props): JSX.Element { }: Props): JSX.Element {
const {shown: secondaryMenuShown} = useNavbarSecondaryMenu(); const {shown: secondaryMenuShown} = useNavbarSecondaryMenu();
const navbarModalDialog = useRef<HTMLDialogElement | null>(null);
const {shown, toggle} = useNavbarMobileSidebar();
useEffect(() => {
const {current: dialogEl} = navbarModalDialog;
if (!dialogEl) {
return;
}
if (shown) {
dialogEl.showModal();
} else {
dialogEl.close();
}
});
useEffect(() => {
const {current: dialogEl} = navbarModalDialog;
function toggleOnEscape(e: {key: string}) {
if (e.key === 'Escape') {
if (shown) {
toggle();
}
}
}
dialogEl?.addEventListener('keydown', toggleOnEscape);
return () => {
dialogEl?.removeEventListener('keydown', toggleOnEscape);
};
}, [shown, toggle]);
return ( return (
<div className="navbar-sidebar"> <dialog className="navbar-sidebar" ref={navbarModalDialog}>
{header} {header}
<div <div
className={clsx('navbar-sidebar__items', { className={clsx('navbar-sidebar__items', {
@ -26,6 +63,6 @@ export default function NavbarMobileSidebarLayout({
<div className="navbar-sidebar__item menu">{primaryMenu}</div> <div className="navbar-sidebar__item menu">{primaryMenu}</div>
<div className="navbar-sidebar__item menu">{secondaryMenu}</div> <div className="navbar-sidebar__item menu">{secondaryMenu}</div>
</div> </div>
</div> </dialog>
); );
} }