Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add closing of the icon details modal with the ESC key #900

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
7 changes: 7 additions & 0 deletions packages/preview-astro/src/components/icondetailmodal.tsx
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect, useRef, useState } from "react";
import { FaRegClipboard } from "react-icons/fa6";
import copy from "copy-to-clipboard";
import toast from "cogo-toast";
import { useKeyDown } from '../utils/usekeydown'

interface colorVariant {
bg: string;
Expand Down Expand Up @@ -202,6 +203,12 @@ function Modal({
isOpen,
});

useKeyDown((keyCode) => {
if(keyCode === 'Escape') {
handleCloseModal()
}
})

return (
<div className={`modal-root ${isOpen ? "" : "hidden"}`}>
<div
Expand Down
22 changes: 22 additions & 0 deletions packages/preview-astro/src/utils/usekeydown.ts
@@ -0,0 +1,22 @@
import { useEffect, useRef } from 'react';

type callbackType = (key: string) => void;

export const useKeyDown = (callback: callbackType): void => {
const refCallback = useRef(callback);

useEffect(() => {
refCallback.current = callback;
}, [callback]);

useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
refCallback.current(event.key);
};

window.addEventListener('keydown', handleKeyDown);
return () => {
window.removeEventListener('keydown', handleKeyDown);
};
}, []);
};