Skip to content

Commit

Permalink
fix(modal): set focus to modal
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinWijnant committed May 24, 2020
1 parent 0a30be8 commit 43874ff
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/components/Modal/ModalDialog/ModalDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ type Props = {
children?: ReactNode;
};

const enableBodyScroll = (isEnabled: boolean) => {
if (isEnabled) {
document.body.style.overflow = 'inherit';
} else {
const disableBodyScroll = (isDisabled: boolean) => {
if (isDisabled) {
document.body.style.overflow = 'hidden';
} else {
document.body.style.overflow = 'inherit';
}
};

Expand All @@ -31,7 +31,7 @@ const ModalDialog: React.FC = ({
const modalRef = React.useRef<HTMLDivElement>(null);

useEffect(() => {
enableBodyScroll(!isOpen);
disableBodyScroll(isOpen);
// eslint-disable-next-line no-unused-expressions
if (isOpen) modalRef.current?.focus();
}, [isOpen, modalRef]);
Expand All @@ -51,6 +51,8 @@ const ModalDialog: React.FC = ({
ref={modalRef}
className={styles.modal}
onKeyDown={handleKeyDown}
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
tabIndex={0}
role="presentation"
>
{title && <h1 className={styles.title}>{title}</h1>}
Expand Down

0 comments on commit 43874ff

Please sign in to comment.