Skip to content

Commit ef00919

Browse files
authored
Merge pull request #113 from navikt/modal-focus
Fiks fokus ved tastaturnavigasjon i modal
2 parents a824455 + 9425ea8 commit ef00919

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

xp-archive/client/versionSelector/SlidePanel/SlidePanel.tsx

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect } from 'react';
1+
import { useEffect, useRef } from 'react';
22
import { createPortal } from 'react-dom';
33
import style from './SlidePanel.module.css';
44

@@ -9,21 +9,27 @@ type SlidePanelProps = {
99
};
1010

1111
export const SlidePanel = ({ isOpen, onClose, children }: SlidePanelProps) => {
12+
const dialogRef = useRef<HTMLDialogElement>(null);
13+
1214
useEffect(() => {
13-
const handleEscape = (e: KeyboardEvent) => {
14-
if (e.key === 'Escape') onClose();
15-
};
16-
document.addEventListener('keydown', handleEscape);
17-
return () => document.removeEventListener('keydown', handleEscape);
18-
}, [onClose]);
15+
const dialog = dialogRef.current;
16+
if (!dialog) return;
17+
18+
if (isOpen) {
19+
dialog.showModal();
20+
} else {
21+
dialog.close();
22+
}
23+
}, [isOpen]);
1924

2025
if (!isOpen) return null;
2126

2227
return createPortal(
2328
<dialog
29+
ref={dialogRef}
2430
className={style.overlay}
25-
open={isOpen}
2631
onClose={onClose}
32+
aria-modal="true"
2733
aria-label="Version selector"
2834
>
2935
<button

0 commit comments

Comments
 (0)