refactor: restyle dialog with header, body and footer parts

This commit is contained in:
rajun 2026-10-27 16:28:47 +05:30
parent 7ecead13d8
commit 9e3f0c518b

View File

@ -1,119 +1,49 @@
import * as React from 'react'; import type { ComponentProps } from 'react';
import * as DialogPrimitive from '@radix-ui/react-dialog'; import { Root, Trigger, Portal, Close, Overlay, Content, Title, Description } from '@radix-ui/react-dialog';
import { X } from 'lucide-react'; import { X } from 'lucide-react';
import { cn } from '~/lib/utils'; import { cn } from '~/lib/utils';
const Dialog = DialogPrimitive.Root; export const Dialog = Root;
const DialogTrigger = DialogPrimitive.Trigger; export const DialogTrigger = Trigger;
const DialogPortal = DialogPrimitive.Portal; export const DialogClose = Close;
const DialogClose = DialogPrimitive.Close;
const DialogOverlay = React.forwardRef< export function DialogContent({ className, children, ...props }: ComponentProps<typeof Content>) {
React.ElementRef<typeof DialogPrimitive.Overlay>, return (
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay> <Portal>
>(({ className, ...props }, ref) => ( <Overlay className='fixed inset-0 z-50 bg-overlay backdrop-blur-[3px] data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:animate-in data-[state=open]:fade-in-0' />
<DialogPrimitive.Overlay <Content
ref={ref}
className={cn( className={cn(
'fixed inset-0 z-50 bg-black/85 backdrop-blur-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0', 'fixed top-1/2 left-1/2 z-50 flex max-h-[calc(100vh-3rem)] w-[calc(100%-2rem)] max-w-lg -translate-x-1/2 -translate-y-1/2 flex-col overflow-y-auto rounded-panel bg-surface-dialog shadow-[0_40px_80px] shadow-shadow/50 ring-1 ring-inset ring-border-dialog data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95',
className className,
)}
{...props}
/>
));
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;
const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
>(({ className, children, ...props }, ref) => (
<DialogPortal>
<DialogOverlay />
<DialogPrimitive.Content
ref={ref}
className={cn(
'fixed left-[50%] top-[50%] z-50 w-full translate-x-[-50%] translate-y-[-50%] border border-neutral-850 bg-[#121212] shadow-2xl duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-1/2 data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-1/2 rounded-2xl',
!className?.includes('flex') && !className?.includes('grid') && 'grid gap-4',
!className?.includes('max-w-') && 'max-w-lg',
!className?.includes('p-') && 'p-0 p-6', // default padding if not overridden
className
)} )}
{...props} {...props}
> >
{children} {children}
<DialogPrimitive.Close className="absolute right-4 top-4 rounded-full p-2 bg-black/60 border border-neutral-850 text-neutral-400 hover:text-white hover:bg-neutral-800 transition-all cursor-pointer opacity-70 hover:opacity-100 focus:outline-none disabled:pointer-events-none z-20"> <Close className='absolute top-5 right-5 flex h-[30px] w-[30px] cursor-pointer items-center justify-center rounded-item text-text-subtle transition-colors hover:bg-surface-hover hover:text-text-primary focus:outline-none'>
<X className="h-4 w-4" /> <X size={14} />
<span className="sr-only">Close</span> <span className='sr-only'>Close</span>
</DialogPrimitive.Close> </Close>
</DialogPrimitive.Content> </Content>
</DialogPortal> </Portal>
));
DialogContent.displayName = DialogPrimitive.Content.displayName;
const DialogHeader = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
'flex flex-col space-y-1.5 text-center sm:text-left',
className
)}
{...props}
/>
); );
DialogHeader.displayName = 'DialogHeader'; }
const DialogFooter = ({ export function DialogHeader({ className, ...props }: ComponentProps<'div'>) {
className, return <div className={cn('flex flex-col gap-1 border-b border-border-divider px-6 py-5 pr-16', className)} {...props} />;
...props }
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
'flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2',
className
)}
{...props}
/>
);
DialogFooter.displayName = 'DialogFooter';
const DialogTitle = React.forwardRef< export function DialogBody({ className, ...props }: ComponentProps<'div'>) {
React.ElementRef<typeof DialogPrimitive.Title>, return <div className={cn('flex flex-col gap-5 px-6 py-[22px]', className)} {...props} />;
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title> }
>(({ className, ...props }, ref) => (
<DialogPrimitive.Title
ref={ref}
className={cn(
'text-lg font-semibold leading-none tracking-tight text-white',
className
)}
{...props}
/>
));
DialogTitle.displayName = DialogPrimitive.Title.displayName;
const DialogDescription = React.forwardRef< export function DialogFooter({ className, ...props }: ComponentProps<'div'>) {
React.ElementRef<typeof DialogPrimitive.Description>, return <div className={cn('flex flex-col-reverse gap-2.5 border-t border-border-divider px-6 py-4 sm:flex-row sm:justify-end', className)} {...props} />;
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description> }
>(({ className, ...props }, ref) => (
<DialogPrimitive.Description
ref={ref}
className={cn('text-sm text-neutral-400', className)}
{...props}
/>
));
DialogDescription.displayName = DialogPrimitive.Description.displayName;
export { export function DialogTitle({ className, ...props }: ComponentProps<typeof Title>) {
Dialog, return <Title className={cn('text-lg font-semibold text-text-primary', className)} {...props} />;
DialogPortal, }
DialogOverlay,
DialogClose, export function DialogDescription({ className, ...props }: ComponentProps<typeof Description>) {
DialogTrigger, return <Description className={cn('text-sm text-text-subtle', className)} {...props} />;
DialogContent, }
DialogHeader,
DialogFooter,
DialogTitle,
DialogDescription,
};