-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
280 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
"use client"; | ||
|
||
import { cn } from "cn"; | ||
import { useMediaQuery } from "hooks/use-media-query"; | ||
import type { ReactNode } from "react"; | ||
import { | ||
Dialog, | ||
DialogClose, | ||
DialogContent, | ||
DialogDescription, | ||
DialogFooter, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from "./dialog"; | ||
import { | ||
Drawer, | ||
DrawerClose, | ||
DrawerContent, | ||
DrawerDescription, | ||
DrawerFooter, | ||
DrawerHeader, | ||
DrawerTitle, | ||
DrawerTrigger, | ||
} from "./drawer"; | ||
|
||
interface BaseProps { | ||
children: ReactNode; | ||
} | ||
|
||
interface RootCredenzaProps extends BaseProps { | ||
open?: boolean; | ||
onOpenChange?: (open: boolean) => void; | ||
} | ||
|
||
interface CredenzaProps extends BaseProps { | ||
className?: string; | ||
asChild?: true; | ||
} | ||
|
||
const desktop = "(min-width: 768px)"; | ||
|
||
const Credenza = ({ children, ...props }: RootCredenzaProps) => { | ||
const isDesktop = useMediaQuery(desktop); | ||
const Credenza = isDesktop ? Dialog : Drawer; | ||
|
||
return <Credenza {...props}>{children}</Credenza>; | ||
}; | ||
|
||
const CredenzaTrigger = ({ className, children, ...props }: CredenzaProps) => { | ||
const isDesktop = useMediaQuery(desktop); | ||
const CredenzaTrigger = isDesktop ? DialogTrigger : DrawerTrigger; | ||
|
||
return ( | ||
<CredenzaTrigger className={className} {...props}> | ||
{children} | ||
</CredenzaTrigger> | ||
); | ||
}; | ||
|
||
const CredenzaClose = ({ className, children, ...props }: CredenzaProps) => { | ||
const isDesktop = useMediaQuery(desktop); | ||
const CredenzaClose = isDesktop ? DialogClose : DrawerClose; | ||
|
||
return ( | ||
<CredenzaClose className={className} {...props}> | ||
{children} | ||
</CredenzaClose> | ||
); | ||
}; | ||
|
||
const CredenzaContent = ({ className, children, ...props }: CredenzaProps) => { | ||
const isDesktop = useMediaQuery(desktop); | ||
const CredenzaContent = isDesktop ? DialogContent : DrawerContent; | ||
|
||
return ( | ||
<CredenzaContent className={className} {...props}> | ||
{children} | ||
</CredenzaContent> | ||
); | ||
}; | ||
|
||
const CredenzaDescription = ({ className, children, ...props }: CredenzaProps) => { | ||
const isDesktop = useMediaQuery(desktop); | ||
const CredenzaDescription = isDesktop ? DialogDescription : DrawerDescription; | ||
|
||
return ( | ||
<CredenzaDescription className={className} {...props}> | ||
{children} | ||
</CredenzaDescription> | ||
); | ||
}; | ||
|
||
const CredenzaHeader = ({ className, children, ...props }: CredenzaProps) => { | ||
const isDesktop = useMediaQuery(desktop); | ||
const CredenzaHeader = isDesktop ? DialogHeader : DrawerHeader; | ||
|
||
return ( | ||
<CredenzaHeader className={className} {...props}> | ||
{children} | ||
</CredenzaHeader> | ||
); | ||
}; | ||
|
||
const CredenzaTitle = ({ className, children, ...props }: CredenzaProps) => { | ||
const isDesktop = useMediaQuery(desktop); | ||
const CredenzaTitle = isDesktop ? DialogTitle : DrawerTitle; | ||
|
||
return ( | ||
<CredenzaTitle className={className} {...props}> | ||
{children} | ||
</CredenzaTitle> | ||
); | ||
}; | ||
|
||
const CredenzaBody = ({ className, children, ...props }: CredenzaProps) => { | ||
return ( | ||
<div className={cn("px-4 md:px-0", className)} {...props}> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
const CredenzaFooter = ({ className, children, ...props }: CredenzaProps) => { | ||
const isDesktop = useMediaQuery(desktop); | ||
const CredenzaFooter = isDesktop ? DialogFooter : DrawerFooter; | ||
|
||
return ( | ||
<CredenzaFooter className={className} {...props}> | ||
{children} | ||
</CredenzaFooter> | ||
); | ||
}; | ||
|
||
export { | ||
Credenza, | ||
CredenzaTrigger, | ||
CredenzaClose, | ||
CredenzaContent, | ||
CredenzaDescription, | ||
CredenzaHeader, | ||
CredenzaTitle, | ||
CredenzaBody, | ||
CredenzaFooter, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
"use client"; | ||
|
||
import { cn } from "cn"; | ||
import { | ||
type ComponentProps, | ||
type ComponentPropsWithoutRef, | ||
type ElementRef, | ||
type HTMLAttributes, | ||
forwardRef, | ||
} from "react"; | ||
import { Drawer as DrawerPrimitive } from "vaul"; | ||
|
||
const DrawerTrigger = DrawerPrimitive.Trigger; | ||
const DrawerPortal = DrawerPrimitive.Portal; | ||
const DrawerClose = DrawerPrimitive.Close; | ||
|
||
const Drawer = ({ shouldScaleBackground = true, ...props }: ComponentProps<typeof DrawerPrimitive.Root>) => ( | ||
<DrawerPrimitive.Root shouldScaleBackground={shouldScaleBackground} {...props} /> | ||
); | ||
|
||
const DrawerOverlay = forwardRef< | ||
ElementRef<typeof DrawerPrimitive.Overlay>, | ||
ComponentPropsWithoutRef<typeof DrawerPrimitive.Overlay> | ||
>(({ className, ...props }, ref) => ( | ||
<DrawerPrimitive.Overlay ref={ref} className={cn("fixed inset-0 z-50 bg-black/80", className)} {...props} /> | ||
)); | ||
|
||
const DrawerContent = forwardRef< | ||
ElementRef<typeof DrawerPrimitive.Content>, | ||
ComponentPropsWithoutRef<typeof DrawerPrimitive.Content> | ||
>(({ className, children, ...props }, ref) => ( | ||
<DrawerPortal> | ||
<DrawerOverlay /> | ||
<DrawerPrimitive.Content | ||
ref={ref} | ||
className={cn( | ||
"fixed inset-x-0 bottom-0 z-50 mt-24 flex h-auto flex-col rounded-t-[10px] border bg-background", | ||
className, | ||
)} | ||
{...props} | ||
> | ||
<div className="mx-auto mt-4 h-2 w-[100px] rounded-full bg-muted" /> | ||
{children} | ||
</DrawerPrimitive.Content> | ||
</DrawerPortal> | ||
)); | ||
|
||
const DrawerHeader = ({ className, ...props }: HTMLAttributes<HTMLDivElement>) => ( | ||
<div className={cn("grid gap-1.5 p-4 text-center sm:text-left", className)} {...props} /> | ||
); | ||
|
||
const DrawerFooter = ({ className, ...props }: HTMLAttributes<HTMLDivElement>) => ( | ||
<div className={cn("mt-auto flex flex-col gap-2 p-4", className)} {...props} /> | ||
); | ||
|
||
const DrawerTitle = forwardRef< | ||
ElementRef<typeof DrawerPrimitive.Title>, | ||
ComponentPropsWithoutRef<typeof DrawerPrimitive.Title> | ||
>(({ className, ...props }, ref) => ( | ||
<DrawerPrimitive.Title | ||
ref={ref} | ||
className={cn("font-semibold text-lg leading-none tracking-tight", className)} | ||
{...props} | ||
/> | ||
)); | ||
|
||
const DrawerDescription = forwardRef< | ||
ElementRef<typeof DrawerPrimitive.Description>, | ||
ComponentPropsWithoutRef<typeof DrawerPrimitive.Description> | ||
>(({ className, ...props }, ref) => ( | ||
<DrawerPrimitive.Description ref={ref} className={cn("text-muted-foreground text-sm", className)} {...props} /> | ||
)); | ||
|
||
DrawerHeader.displayName = "DrawerHeader"; | ||
DrawerFooter.displayName = "DrawerFooter"; | ||
DrawerTitle.displayName = DrawerPrimitive.Title.displayName; | ||
DrawerOverlay.displayName = DrawerPrimitive.Overlay.displayName; | ||
DrawerDescription.displayName = DrawerPrimitive.Description.displayName; | ||
Drawer.displayName = "Drawer"; | ||
DrawerContent.displayName = "DrawerContent"; | ||
|
||
export { | ||
Drawer, | ||
DrawerPortal, | ||
DrawerOverlay, | ||
DrawerTrigger, | ||
DrawerClose, | ||
DrawerContent, | ||
DrawerHeader, | ||
DrawerFooter, | ||
DrawerTitle, | ||
DrawerDescription, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
export function useMediaQuery(query: string) { | ||
const [value, setValue] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
function onChange(event: MediaQueryListEvent) { | ||
setValue(event.matches); | ||
} | ||
|
||
const result = matchMedia(query); | ||
result.addEventListener("change", onChange); | ||
setValue(result.matches); | ||
|
||
return () => result.removeEventListener("change", onChange); | ||
}, [query]); | ||
|
||
return value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters