Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 1 addition & 83 deletions src/components/ui/adaptive-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,20 @@ import {
DialogDescription,
DialogFooter,
} from "@/components/ui/dialog"
import {
Drawer,
DrawerContent,
DrawerHeader,
DrawerTitle,
DrawerDescription,
DrawerFooter,
} from "@/components/ui/drawer"

function useIsMobile() {
const [isMobile, setIsMobile] = React.useState(
() => typeof window !== "undefined" && window.matchMedia("(max-width: 767px)").matches
)
React.useEffect(() => {
const mq = window.matchMedia("(max-width: 767px)")
const handler = (e: MediaQueryListEvent) => setIsMobile(e.matches)
mq.addEventListener("change", handler)
return () => mq.removeEventListener("change", handler)
}, [])
return isMobile
}

interface AdaptiveDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
children: React.ReactNode
/** vaul snap points, mobile only */
/** unused — kept for API compatibility */
snapPoints?: (number | string)[]
}

export const AdaptiveDialog = ({
open,
onOpenChange,
children,
snapPoints,
}: AdaptiveDialogProps) => {
const isMobile = useIsMobile()
const [activeSnapPoint, setActiveSnapPoint] = React.useState<number | string | null>(
snapPoints?.[0] ?? null
)

React.useEffect(() => {
if (open) {
setActiveSnapPoint(snapPoints?.[0] ?? null)
}
}, [open]) // snapPoints are static per dialog instance

if (isMobile) {
return (
<Drawer
open={open}
onOpenChange={onOpenChange}
snapPoints={snapPoints}
activeSnapPoint={activeSnapPoint}
setActiveSnapPoint={setActiveSnapPoint}
>
{children}
</Drawer>
)
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
{children}
Expand All @@ -83,14 +37,6 @@ export const AdaptiveDialogContent = ({
children,
className,
}: AdaptiveDialogContentProps) => {
const isMobile = useIsMobile()
if (isMobile) {
return (
<DrawerContent className={className}>
{children}
</DrawerContent>
)
}
return (
<DialogContent className={className}>
{children}
Expand All @@ -105,25 +51,13 @@ export const AdaptiveDialogHeader = ({
children: React.ReactNode
className?: string
}) => {
const isMobile = useIsMobile()
if (isMobile) {
return <DrawerHeader className={className}>{children}</DrawerHeader>
}
return <DialogHeader className={className}>{children}</DialogHeader>
}

export const AdaptiveDialogTitle = React.forwardRef<
HTMLHeadingElement,
React.HTMLAttributes<HTMLHeadingElement>
>(({ children, className, ...props }, ref) => {
const isMobile = useIsMobile()
if (isMobile) {
return (
<DrawerTitle ref={ref} className={className} {...props}>
{children}
</DrawerTitle>
)
}
return (
<DialogTitle ref={ref} className={className} {...props}>
{children}
Expand All @@ -136,14 +70,6 @@ export const AdaptiveDialogDescription = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLParagraphElement>
>(({ children, className, ...props }, ref) => {
const isMobile = useIsMobile()
if (isMobile) {
return (
<DrawerDescription ref={ref} className={className} {...props}>
{children}
</DrawerDescription>
)
}
return (
<DialogDescription ref={ref} className={className} {...props}>
{children}
Expand All @@ -159,13 +85,5 @@ export const AdaptiveDialogFooter = ({
children: React.ReactNode
className?: string
}) => {
const isMobile = useIsMobile()
if (isMobile) {
return (
<DrawerFooter className={className}>
{children}
</DrawerFooter>
)
}
return <DialogFooter className={className}>{children}</DialogFooter>
}
Loading