/* Copyright 2021, vite-plugin-book by Mirone. */ import { FC, useContext, useEffect } from 'react'; import { ShowDialogCtx } from '../provider/DialogProvider'; import cx from './Dialog.module.css'; const DialogButton: FC<{ onClick: () => void }> = ({ children, onClick }) => { return (
{children}
); }; export type DialogProps = { icon?: string; title: string; description: string | JSX.Element; onConfirm: () => void; onCancel: () => void; }; export const Dialog: FC = ({ title, description, onConfirm, onCancel, icon }) => { const isShow = useContext(ShowDialogCtx); useEffect(() => { const keyBoardEvent = (e: KeyboardEvent) => { if (e.key === 'Enter') { onConfirm(); } if (e.key === 'Escape') { onCancel(); } }; if (isShow) { window.addEventListener('keydown', keyBoardEvent); } return () => { window.removeEventListener('keydown', keyBoardEvent); }; }, [isShow, onCancel, onConfirm]); return (
{icon && (
{icon}
)}
{title}
{description}
closeCancel done Confirm
); };