项目作者: simbathesailor

项目描述 :
A minimal react toast hook
高级语言: TypeScript
项目地址: git://github.com/simbathesailor/minimal-react-toast.git
创建时间: 2020-05-05T18:25:21Z
项目社区:https://github.com/simbathesailor/minimal-react-toast

开源协议:MIT License

下载


A minimal React Toast Library

History

I have worked on many frontend projects which makes use of toasts. But everytime there is a need of toast, we end up using some big library out of which we are using minimal amount of feature. And also these libraries will need so many props. Damn !!. In most of the use cases, the developer needs the flexibility of design toast, which should be as straightforward as writing a React component.
Passing lots of props and trying to adjust them, looks like over kill always.

Install

  1. yarn add @simbathesailor/minimal-react-toast

or

  1. npm i @simbathesailor/minimal-react-toast

Usage

  1. import { useToast } from '@simbathesailor/minimal-react-toast';
  2. function SuccessToastComponent() {
  3. return <div>Button Clicked successfully !!</div>;
  4. }
  5. function App() {
  6. const [triggerToast, toastRender] = useToast({
  7. styleToastContainer: {
  8. border: '1px solid red',
  9. position: 'fixed',
  10. top: '80px',
  11. left: ' 50%',
  12. transform: 'translateX(-50%)',
  13. zIndex: '12',
  14. },
  15. });
  16. const finalToast = toastRender();
  17. return (
  18. <>
  19. <button
  20. onClick={() => {
  21. triggerToast();
  22. }}
  23. >
  24. Click me
  25. </button>
  26. {finalToast(<SuccessToastComponent ></SuccessToastComponent>)}
  27. </>
  28. );
  29. }

Options

  1. const defaultOptions = {
  2. timeout: 3000,
  3. idToastContainer: 'minimal-react-toast',
  4. styleToastContainer: undefined,
  5. };
  6. interface IStyleToastContainer {
  7. [key: string]: any;
  8. }
  9. interface IUseToast {
  10. timeout?: number;
  11. idToastContainer?: string;
  12. styleToastContainer?: IStyleToastContainer;
  13. }

Demo

Demo

Built with TSDX