Skip to content

Commit

Permalink
fix(message): fix message disappearing
Browse files Browse the repository at this point in the history
  • Loading branch information
wallpants committed May 21, 2024
1 parent 868f31f commit 9eb0f38
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
createContext,
useContext,
useEffect,
useMemo,
useState,
type Dispatch,
type ReactNode,
Expand All @@ -23,9 +22,8 @@ const confirmContext = createContext<ConfirmContext | null>(null);
export function useConfirm() {
const context = useContext(confirmContext);
if (!context) throw Error("'useConfirm' use outside of Provider detected");
const { message, setMessage, resolve, setResolve, setIsAsking } = context;

const isAsking = useMemo(() => message !== undefined, [message]);
const { message, setMessage, resolve, setResolve, setIsAsking, isAsking } =
context;

const ask = async (msg: ReactNode): Promise<boolean> => {
return new Promise((resolve) => {
Expand All @@ -45,7 +43,10 @@ export function useConfirm() {
};

useEffect(() => {
if (!isAsking) setMessage(undefined);
if (!isAsking) {
// timeout to prevent the message from disappearing whilst modal is still up
setTimeout(() => setMessage(undefined), 200);
}
}, [isAsking]);

return { message, isAsking, ask, confirm, deny };
Expand Down

0 comments on commit 9eb0f38

Please sign in to comment.