Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 36c07aa

Browse files
committedNov 21, 2024·
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.
1 parent 90e85e8 commit 36c07aa

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed
 

‎packages/docusaurus-theme-classic/src/theme/Navbar/Layout/index.tsx

+1-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import React, {type ComponentProps} from 'react';
8+
import React from 'react';
99
import clsx from 'clsx';
1010
import {useThemeConfig} from '@docusaurus/theme-common';
1111
import {
@@ -18,16 +18,6 @@ import type {Props} from '@theme/Navbar/Layout';
1818

1919
import styles from './styles.module.css';
2020

21-
function NavbarBackdrop(props: ComponentProps<'div'>) {
22-
return (
23-
<div
24-
role="presentation"
25-
{...props}
26-
className={clsx('navbar-sidebar__backdrop', props.className)}
27-
/>
28-
);
29-
}
30-
3121
export default function NavbarLayout({children}: Props): JSX.Element {
3222
const {
3323
navbar: {hideOnScroll, style},
@@ -56,7 +46,6 @@ export default function NavbarLayout({children}: Props): JSX.Element {
5646
},
5747
)}>
5848
{children}
59-
<NavbarBackdrop onClick={mobileSidebar.toggle} />
6049
<NavbarMobileSidebar />
6150
</nav>
6251
);

‎packages/docusaurus-theme-classic/src/theme/Navbar/Layout/styles.module.css

+10-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
.navbarHideable {
9-
transition: transform var(--ifm-transition-fast) ease;
8+
.navbar-sidebar {
9+
inset: 0;
10+
margin: 0;
11+
padding: 0;
12+
bottom: 0;
13+
border: 0;
14+
height: 100%;
15+
max-height: 100%;
1016
}
1117

12-
.navbarHidden {
13-
transform: translate3d(0, calc(-100% - 2px), 0);
18+
.navbar-sidebar::backdrop {
19+
background-color: #0009;
1420
}

‎packages/docusaurus-theme-classic/src/theme/Navbar/MobileSidebar/Layout/index.tsx

+39-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import React from 'react';
8+
import React, {useEffect, useRef} from 'react';
99
import clsx from 'clsx';
10-
import {useNavbarSecondaryMenu} from '@docusaurus/theme-common/internal';
10+
import {
11+
useNavbarMobileSidebar,
12+
useNavbarSecondaryMenu,
13+
} from '@docusaurus/theme-common/internal';
1114
import type {Props} from '@theme/Navbar/MobileSidebar/Layout';
1215

1316
export default function NavbarMobileSidebarLayout({
@@ -16,8 +19,40 @@ export default function NavbarMobileSidebarLayout({
1619
secondaryMenu,
1720
}: Props): JSX.Element {
1821
const {shown: secondaryMenuShown} = useNavbarSecondaryMenu();
22+
const navbarModalDialog = useRef<HTMLDialogElement | null>(null);
23+
const {shown, toggle} = useNavbarMobileSidebar();
24+
25+
useEffect(() => {
26+
const {current: dialogEl} = navbarModalDialog;
27+
28+
if (!dialogEl) return;
29+
if (shown) {
30+
dialogEl.showModal();
31+
} else {
32+
dialogEl.close();
33+
}
34+
});
35+
36+
useEffect(() => {
37+
const {current: dialogEl} = navbarModalDialog;
38+
39+
function toggleOnEscape(e: { key: string; }) {
40+
if (e.key === 'Escape') {
41+
if (shown) {
42+
toggle();
43+
}
44+
}
45+
}
46+
47+
dialogEl?.addEventListener('keydown', toggleOnEscape);
48+
49+
return () => {
50+
dialogEl?.removeEventListener('keydown', toggleOnEscape);
51+
};
52+
}, [shown, toggle]);
53+
1954
return (
20-
<div className="navbar-sidebar">
55+
<dialog className="navbar-sidebar" ref={navbarModalDialog}>
2156
{header}
2257
<div
2358
className={clsx('navbar-sidebar__items', {
@@ -26,6 +61,6 @@ export default function NavbarMobileSidebarLayout({
2661
<div className="navbar-sidebar__item menu">{primaryMenu}</div>
2762
<div className="navbar-sidebar__item menu">{secondaryMenu}</div>
2863
</div>
29-
</div>
64+
</dialog>
3065
);
3166
}

0 commit comments

Comments
 (0)
Please sign in to comment.