aboutsummaryrefslogtreecommitdiff
path: root/client/src/components/Modal/Modal.jsx
diff options
context:
space:
mode:
authorBlaster4385 <blaster4385@tablaster.dev>2024-05-15 22:55:13 +0530
committerBlaster4385 <venkatesh@tablaster.dev>2024-06-09 14:10:27 +0530
commit731453588b93e02a113332d68f81e48bc11f7da7 (patch)
tree6b16d80ba200862ea71842b04de49298316551a2 /client/src/components/Modal/Modal.jsx
parent189ef9ef816d60fab71c0a12cdaaad245c988f37 (diff)
feat: added keyboard accessibility
Diffstat (limited to 'client/src/components/Modal/Modal.jsx')
-rw-r--r--client/src/components/Modal/Modal.jsx70
1 files changed, 55 insertions, 15 deletions
diff --git a/client/src/components/Modal/Modal.jsx b/client/src/components/Modal/Modal.jsx
index bf0540b..4c68f53 100644
--- a/client/src/components/Modal/Modal.jsx
+++ b/client/src/components/Modal/Modal.jsx
@@ -1,39 +1,79 @@
import React, { useRef, useEffect } from "react";
import styles from "./Modal.module.css";
-const Modal = ({ openModal, setOpenModal, onSuccessClick, onCancelClick }) => {
+const Modal = ({
+ openModal,
+ setOpenModal,
+ onSuccessClick,
+ onCancelClick,
+ textAreaRef,
+}) => {
const modalRef = useRef(null);
useEffect(() => {
const handleClickOutside = (event) => {
if (modalRef.current && !modalRef.current.contains(event.target)) {
setOpenModal(false);
+ if (textAreaRef.current) {
+ setTimeout(() => {
+ textAreaRef.current.focus();
+ }, 0);
+ }
+ }
+ };
+
+ const handleKeyDown = (event) => {
+ if (event.key === "Escape") {
+ setOpenModal(false);
+ if (textAreaRef.current) {
+ setTimeout(() => {
+ textAreaRef.current.focus();
+ }, 0);
+ }
+ } else if (
+ (event.key.toLowerCase() === "y" ||
+ event.key.toLowerCase() === "enter") &&
+ openModal
+ ) {
+ onSuccessClick();
+ event.preventDefault();
+ setOpenModal(false);
+ } else if (event.key.toLowerCase() === "n" && openModal) {
+ onCancelClick();
+ setOpenModal(false);
}
};
document.addEventListener("mousedown", handleClickOutside);
+ document.addEventListener("keydown", handleKeyDown);
return () => {
document.removeEventListener("mousedown", handleClickOutside);
+ document.removeEventListener("keydown", handleKeyDown);
};
- }, [setOpenModal]);
+ }, [openModal]);
return (
- <div className={`${styles.background} ${openModal && styles.active}`}>
- <div ref={modalRef} className={styles.container}>
- <button
- className={styles.container__close}
- onClick={() => setOpenModal(false)}
- >
- <span>&#10007;</span>
- </button>
- <p className={styles.container__title}>Encrypt content?</p>
- <div className={styles.container__actions}>
- <button onClick={onSuccessClick}>Yes</button>
- <button onClick={onCancelClick}>No</button>
+ openModal && (
+ <div className={`${styles.background} ${openModal && styles.active}`}>
+ <div ref={modalRef} className={styles.container}>
+ <button
+ className={styles.container__close}
+ onClick={() => setOpenModal(false)}
+ >
+ <span>&#10007;</span>
+ </button>
+ <p className={styles.container__title}>
+ Encrypt content?{" "}
+ <span className={styles.container__title__span}>[Y/n]</span>
+ </p>
+ <div className={styles.container__actions}>
+ <button onClick={onSuccessClick}>Yes</button>
+ <button onClick={onCancelClick}>No</button>
+ </div>
</div>
</div>
- </div>
+ )
);
};