mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
fixing dialogs
This commit is contained in:
parent
80fc6523b7
commit
9220f09fe5
6 changed files with 77 additions and 10 deletions
34
src/utils/hooks.ts
Normal file
34
src/utils/hooks.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
export const useCloseOnEscape = (onRequestClose: () => void, ignore_inputs = false) => {
|
||||
const onEscape = useCallback(
|
||||
event => {
|
||||
if (event.key !== "Escape") return;
|
||||
if (
|
||||
ignore_inputs &&
|
||||
(event.target.tagName === "INPUT" || event.target.tagName === "TEXTAREA")
|
||||
)
|
||||
return;
|
||||
|
||||
onRequestClose();
|
||||
},
|
||||
[onRequestClose]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
window.addEventListener("keyup", onEscape);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener("keyup", onEscape);
|
||||
};
|
||||
}, [onEscape]);
|
||||
};
|
||||
|
||||
export const useDelayedReady = (setReady: (val: boolean) => void, delay: number = 500) => {
|
||||
useEffect(() => {
|
||||
const timer = setTimeout(() => setReady(true), delay);
|
||||
|
||||
return () => {
|
||||
if (timer) clearTimeout(timer);
|
||||
};
|
||||
}, []);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue