Untitled

mail@pastecode.io avatar
unknown
plain_text
15 days ago
800 B
7
Indexable
Never
import { createContext, useState, ReactNode, useContext } from "react";

interface IModalContext {
    isOpen: boolean;
    setIsOpen: (value: boolean) => void;
}

export const ModalContext = createContext<IModalContext | undefined>(undefined);

interface IModalContextProps {
    children: ReactNode;
}

export const ModalProvider: React.FC<IModalContextProps> = ({ children }) => {
    const [isOpen, setIsOpen] = useState<boolean>(false);

    return (
        <ModalContext.Provider value={{ isOpen, setIsOpen }}>
            {children}
        </ModalContext.Provider>
    );
};

export function useModal() {
    const context = useContext(ModalContext);
    if (context === undefined) {
        throw new Error("useModal deve ser usado dentro de um ModalProvider");
    }
    return context;
}
Leave a Comment