Skip to content

theodorusclarence/dialog-manager

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Dialog Manager

ts-nextjs-tailwind-starter

Code to observe:

Key Points

Initialize the store and add the Alert

import * as React from 'react';

import useDialogStore from '@/lib/zustand/useDialogStore';

import Alert from '@/components/Alert';

export default function Layout({ children }: { children: React.ReactNode }) {
  const { open, state, handleClose, handleSubmit } = useDialogStore();

  return (
    <>
      {children}
      <Alert
        onClose={handleClose}
        onSubmit={handleSubmit}
        open={open}
        options={state}
      />
    </>
  );
}

Call the dialog

import useDialog from '@/hooks/useDialog';

const dialog = useDialog();

dialog({
  title: 'Warning!',
  description: (
    <>
      This is a <strong className='text-yellow-600'>warning dialog</strong>.
    </>
  ),
  catchOnCancel: true,
  submitText: 'Okay',
  variant: 'warning',
})
  .then(() => setStatus('Warning Dialog: Submitted'))
  .catch(() => setStatus('Warning Dialog: Rejected'));

If you don't have to catch reject, you can omit catchOnCancel

Attribution

Code is inspired from https://dev.to/dmtrkovalenko/the-neatest-way-to-handle-alert-dialogs-in-react-1aoe

Check it out if you prefer to use Context API

About

πŸ’¬ Dialog Manager using TypeScript, Zustand with Immer, and Tailwind CSS

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published