mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-05-01 23:56:41 +07:00
made simple tiny slider
This commit is contained in:
parent
5da9a0547d
commit
3808f2f516
7 changed files with 95 additions and 36 deletions
36
src/utils/hooks/index.ts
Normal file
36
src/utils/hooks/index.ts
Normal file
|
@ -0,0 +1,36 @@
|
|||
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();
|
||||
},
|
||||
[ignore_inputs, 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);
|
||||
};
|
||||
}, [delay, setReady]);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue