pushmodal
Make life easier handling dialogs, sheets and drawers for shadcn.
this package is not stable yet 🫣 Breaking change from 0.0.5 -> 0.0.6
Installation
pnpm add pushmodal
We take for granted that you already have @radix-ui/react-dialog installed. If not ➡️ pnpm add @radix-ui/react-dialog
Usage
1. Create a modal
When creating a dialog/sheet/drawer you need to wrap your component with the <(Dialog|Sheet|Drawer)Content> component. But skip the Root since we do that for you.
import { DialogContent } from '@/ui/dialog'
export default function ModalExample({ foo }: { foo: string }) {
return (
<DialogContent>
Your modal
</DialogContent>
)
}
2. Initialize your modals
import ModalExample from './modal-example'
import SheetExample from './sheet-example'
import DrawerExample from './drawer-examle'
import { createPushModal } from 'pushmodal'
import { Drawer } from '@/ui/drawer'
export const {
pushModal,
popModal,
popAllModals,
replaceWithModal,
ModalProvider
} = createPushModal({
modals: {
ModalExample,
SheetExample,
DrawerExample: {
Wrapper: Drawer,
Component: DrawerExample
}
},
})
How we usually structure things
src
├── ...
├── modals
│ ├── modal-example.tsx
│ ├── sheet-example.tsx
│ ├── drawer-examle.tsx
│ ├── ... more modals here ...
│ └── index.tsx
├── ...
└── ...
3. Add the <ModalProvider /> to your root file.
import { ModalProvider } from '@/modals'
export default function App({ children }: { children: React.ReactNode }) {
return (
<>
{/* Notice! You should not wrap your children */}
<ModalProvider />
{children}
</>
)
}
4. Use pushModal
pushModal can have 1-2 arguments
name - name of your modal
props (might be optional) - props for your modal, types are infered from your component!
import { pushModal } from '@/modals'
export default function RandomComponent() {
return (
<div>
<button onClick={() => pushModal('ModalExample', { foo: 'string' })}>
Open modal
</button>
<button onClick={() => pushModal('SheetExample')}>
Open Sheet
</button>
<button onClick={() => pushModal('DrawerExample')}>
Open Drawer
</button>
</div>
)
}
4. Closing modals
You can close a modal in three different ways:
popModal() - will pop the last added modal
popModal('Modal1') - will pop the last added modal with name Modal1
popAllModals() - will close all your modals
5. Replacing current modal
Replace the last pushed modal. Same interface as pushModal.
replaceWithModal('SheetExample', { })
Contributors