feat: support closing modal via back button

This commit is contained in:
codytseng
2025-05-06 23:10:35 +08:00
parent 53c8483a3f
commit 60fca48a72
10 changed files with 264 additions and 44 deletions

View File

@@ -1,10 +1,49 @@
import * as React from 'react'
import * as DialogPrimitive from '@radix-ui/react-dialog'
import { X } from 'lucide-react'
import * as React from 'react'
import { randomString } from '@/lib/random'
import { cn } from '@/lib/utils'
import modalManager from '@/services/modal-manager.service'
const Dialog = DialogPrimitive.Root
const Dialog = ({ children, open, onOpenChange, ...props }: DialogPrimitive.DialogProps) => {
const [innerOpen, setInnerOpen] = React.useState(open ?? false)
const id = React.useMemo(() => `dialog-${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 (
<DialogPrimitive.Root
open={open ?? innerOpen}
onOpenChange={onOpenChange ?? setInnerOpen}
{...props}
>
{children}
</DialogPrimitive.Root>
)
}
const DialogTrigger = DialogPrimitive.Trigger
@@ -31,11 +70,10 @@ const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & {
withoutClose?: boolean
hideOverlay?: boolean
}
>(({ className, children, withoutClose, hideOverlay, ...props }, ref) => (
>(({ className, children, withoutClose, ...props }, ref) => (
<DialogPortal>
{!hideOverlay && <DialogOverlay />}
<DialogOverlay />
<DialogPrimitive.Content
ref={ref}
className={cn(
@@ -95,13 +133,13 @@ DialogDescription.displayName = DialogPrimitive.Description.displayName
export {
Dialog,
DialogPortal,
DialogOverlay,
DialogTrigger,
DialogClose,
DialogContent,
DialogHeader,
DialogDescription,
DialogFooter,
DialogHeader,
DialogOverlay,
DialogPortal,
DialogTitle,
DialogDescription
DialogTrigger
}

View File

@@ -1,14 +1,52 @@
import * as React from 'react'
import { Drawer as DrawerPrimitive } from 'vaul'
import { randomString } from '@/lib/random'
import { cn } from '@/lib/utils'
import modalManager from '@/services/modal-manager.service'
const Drawer = ({
shouldScaleBackground = true,
open,
onOpenChange,
...props
}: React.ComponentProps<typeof DrawerPrimitive.Root>) => (
<DrawerPrimitive.Root shouldScaleBackground={shouldScaleBackground} {...props} />
)
}: React.ComponentProps<typeof DrawerPrimitive.Root>) => {
const [innerOpen, setInnerOpen] = React.useState(open ?? false)
const id = React.useMemo(() => `drawer-${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 (
<DrawerPrimitive.Root
shouldScaleBackground={shouldScaleBackground}
open={open ?? innerOpen}
onOpenChange={onOpenChange ?? setInnerOpen}
{...props}
/>
)
}
Drawer.displayName = 'Drawer'
const DrawerTrigger = DrawerPrimitive.Trigger
@@ -90,13 +128,13 @@ DrawerDescription.displayName = DrawerPrimitive.Description.displayName
export {
Drawer,
DrawerPortal,
DrawerOverlay,
DrawerTrigger,
DrawerClose,
DrawerContent,
DrawerHeader,
DrawerDescription,
DrawerFooter,
DrawerHeader,
DrawerOverlay,
DrawerPortal,
DrawerTitle,
DrawerDescription
DrawerTrigger
}

View File

@@ -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