Skip to content

Commit

Permalink
fix: 771 - scroll bar shifting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Quiddlee committed Feb 23, 2025
1 parent a538fdd commit 87cc7c0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ export const COURSE_LINKS = {
AWS_CLOUD_DEVELOPER: 'https://rs.school/courses/aws-cloud-developer',
AWS_DEVOPS: 'https://rs.school/courses/aws-devops',
};

export const KEY_CODES = { ESCAPE: 'Escape' } as const;
12 changes: 11 additions & 1 deletion src/shared/ui/modal/modal.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { ReactNode, useCallback, useEffect, useRef } from 'react';
import React, { KeyboardEvent, ReactNode, useCallback, useEffect, useRef } from 'react';
import classNames from 'classnames/bind';
import Image from 'next/image';
import { createPortal } from 'react-dom';

import closeIcon from '@/shared/assets/svg/close.svg';
import { KEY_CODES } from '@/shared/constants';

import styles from './modal.module.scss';

Expand Down Expand Up @@ -56,6 +57,14 @@ export const Modal = ({ isOpen, onClose, children, title, className }: ModalProp
[handleClose],
);

const handleCloseOnEscPress = (e: KeyboardEvent<HTMLDialogElement>) => {
e.preventDefault();

if (e.key === KEY_CODES.ESCAPE) {
handleClose();
}
};

useEffect(() => {
document.addEventListener('mousedown', handleMouseDown);

Expand Down Expand Up @@ -87,6 +96,7 @@ export const Modal = ({ isOpen, onClose, children, title, className }: ModalProp
className={cx('modal', className)}
ref={dialogRef}
onClose={handleClose}
onKeyDown={handleCloseOnEscPress}
data-testid="modal"
>
<div className={cx('modal-header', { 'no-title': !title })} data-testid="modal-header">
Expand Down

0 comments on commit 87cc7c0

Please sign in to comment.