Giter Club home page Giter Club logo

react-dialog-hook's People

Contributors

hstemplewski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-dialog-hook's Issues

An awesome feature / suggestion from your useDialog codes written in Javascript:

To use define the DialogProvider and useDialog within the DialogProvider .
const dialog = useDialog();
dialog.open(params, Component); Any component. The component is given props like opended, params, close: close.current, dispose: dispose.current to operate the dialog. Dispose was added to allow dialog closing animations to finish before mounting the component. Thank you: Review the code below:

`

  const useDialogProvider = () => {

    const [opended, setOpened] = useState(false);
    const [result, setResult] = useState(null);
    const [params, setParams] = useState(null);
    const [view, setView] = useState({ Component: null });

    const close = useRef(() => {
        throw new Error("Trying to close dialog without opening it.");
    });

    const dispose = useRef(() => {
        setView({ Component: null });
        setParams(null);
    });

    const prepare = useCallback((params, Component) => {
        setOpened(true);
        setResult(null);
        setParams(params);

        setView({ Component: Component });

        return new Promise((resolve) => {
            close.current = (result) => {
                setOpened(false);
                setResult(result);
                resolve(result);
            };
        });
    }, []);

    const Component = ((view.Component && <view.Component {...{ opended, params, close: close.current, dispose: dispose.current }} />) || (<></>));

    return {
        Component,
        opended,
        params,
        result,
        open: (params, Component) => prepare(params, Component),
        confirm: (params) => prepare(params, ConfirmationViewComponent),
        close: close.current,
    };
};

const DialogContext = createContext({});

const DialogProvider = ({ children }) => {

    const value = useDialogProvider();

    return (
        <DialogContext.Provider value={value}>
            {children}
            {value.Component}
        </DialogContext.Provider>
    )
};

const DialogConsumer = ({ children }) => {
    return (
        <DialogContext.Consumer>
            {context => {
                if (context === undefined) {
                    throw new Error('DialogConsumer must be used within a DialogProvider.')
                }
                return children(context)
            }}
        </DialogContext.Consumer>
    )
};

const useDialog = () => {
    return useContext(DialogContext);
};

export { DialogProvider, DialogConsumer, useDialog };

`

Add proper README.md

Add proper README with all necessary info about contributing, bug reporting etc.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.