mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-27 05:49:08 +00:00
Pull out shared hook
This commit is contained in:
parent
43e2f2f091
commit
f3c0b54fe9
4 changed files with 53 additions and 99 deletions
|
@ -0,0 +1,42 @@
|
|||
import { useEffect, useState } from "react";
|
||||
import type { TypedEmitter } from "tiny-typed-emitter";
|
||||
|
||||
type Events<Name extends string, T> = {
|
||||
[K in Name]: (value: T) => void;
|
||||
};
|
||||
|
||||
interface UseFocusOutlineEventProps<Name extends string, T> {
|
||||
value: T;
|
||||
ee: TypedEmitter<Events<Name, T>>;
|
||||
event: Name;
|
||||
defaultIsOutlined?: boolean;
|
||||
}
|
||||
|
||||
export function useFocusOutlineEvent<Name extends string, T>({
|
||||
value,
|
||||
ee,
|
||||
event,
|
||||
defaultIsOutlined = false,
|
||||
}: UseFocusOutlineEventProps<Name, T>) {
|
||||
const [isOutlined, setIsOutlined] = useState(defaultIsOutlined);
|
||||
|
||||
useEffect(() => {
|
||||
ee.on(event, (focusValue: T) => {
|
||||
if (focusValue !== value) return;
|
||||
setIsOutlined(true);
|
||||
});
|
||||
}, [ee, event, value]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isOutlined) return;
|
||||
const timer = setTimeout(() => {
|
||||
setIsOutlined(false);
|
||||
}, 500);
|
||||
|
||||
return () => {
|
||||
clearTimeout(timer);
|
||||
};
|
||||
}, [isOutlined]);
|
||||
|
||||
return isOutlined;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue