feat: support closing modal via back button
This commit is contained in:
parent
53c8483a3f
commit
60fca48a72
10 changed files with 264 additions and 44 deletions
|
|
@ -3,9 +3,48 @@ import * as SheetPrimitive from '@radix-ui/react-dialog'
|
|||
import { cva, type VariantProps } from 'class-variance-authority'
|
||||
import { X } from 'lucide-react'
|
||||
|
||||
import { randomString } from '@/lib/random'
|
||||
import { cn } from '@/lib/utils'
|
||||
import modalManager from '@/services/modal-manager.service'
|
||||
|
||||
const Sheet = SheetPrimitive.Root
|
||||
const Sheet = ({ children, open, onOpenChange, ...props }: SheetPrimitive.DialogProps) => {
|
||||
const [innerOpen, setInnerOpen] = React.useState(open ?? false)
|
||||
const id = React.useMemo(() => `sheet-${randomString()}`, [])
|
||||
|
||||
React.useEffect(() => {
|
||||
if (open) {
|
||||
modalManager.register(id, () => {
|
||||
onOpenChange?.(false)
|
||||
})
|
||||
} else {
|
||||
modalManager.unregister(id)
|
||||
}
|
||||
}, [open])
|
||||
|
||||
React.useEffect(() => {
|
||||
if (open !== undefined) {
|
||||
return
|
||||
}
|
||||
|
||||
if (innerOpen) {
|
||||
modalManager.register(id, () => {
|
||||
setInnerOpen(false)
|
||||
})
|
||||
} else {
|
||||
modalManager.unregister(id)
|
||||
}
|
||||
}, [innerOpen])
|
||||
|
||||
return (
|
||||
<SheetPrimitive.Root
|
||||
open={open ?? innerOpen}
|
||||
onOpenChange={onOpenChange ?? setInnerOpen}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</SheetPrimitive.Root>
|
||||
)
|
||||
}
|
||||
|
||||
const SheetTrigger = SheetPrimitive.Trigger
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue