hstemplewski / react-dialog-hook Goto Github PK
View Code? Open in Web Editor NEWReact hook for manage dialogs state in promise based way.
Home Page: https://www.npmjs.com/package/react-dialog-hook
License: MIT License
React hook for manage dialogs state in promise based way.
Home Page: https://www.npmjs.com/package/react-dialog-hook
License: MIT License
Add proper README with all necessary info about contributing, bug reporting etc.
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 };
`
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.