139 lines
4.3 KiB
TypeScript
139 lines
4.3 KiB
TypeScript
'use client';
|
|
|
|
import * as React from 'react';
|
|
import * as DialogPrimitive from '@radix-ui/react-dialog';
|
|
import { X } from 'lucide-react';
|
|
import { cn } from '@/lib/utils';
|
|
|
|
/**
|
|
* Modale Xpeditis, sur @radix-ui/react-dialog.
|
|
*
|
|
* Apporte ce que l'implementation manuelle precedente n'avait pas :
|
|
* portal, focus trap, fermeture par Escape, verrouillage du scroll,
|
|
* role/aria-modal, et animations d'entree/sortie.
|
|
*/
|
|
const Dialog = DialogPrimitive.Root;
|
|
const DialogTrigger = DialogPrimitive.Trigger;
|
|
const DialogPortal = DialogPrimitive.Portal;
|
|
const DialogClose = DialogPrimitive.Close;
|
|
|
|
const DialogOverlay = React.forwardRef<
|
|
React.ElementRef<typeof DialogPrimitive.Overlay>,
|
|
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
|
|
>(({ className, ...props }, ref) => (
|
|
<DialogPrimitive.Overlay
|
|
ref={ref}
|
|
className={cn(
|
|
'fixed inset-0 z-50 bg-brand-navy/40',
|
|
'data-[state=open]:animate-in data-[state=open]:fade-in-0',
|
|
'data-[state=closed]:animate-out data-[state=closed]:fade-out-0',
|
|
className
|
|
)}
|
|
{...props}
|
|
/>
|
|
));
|
|
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;
|
|
|
|
const sizeClasses = {
|
|
sm: 'max-w-sm',
|
|
md: 'max-w-lg',
|
|
lg: 'max-w-2xl',
|
|
xl: 'max-w-4xl',
|
|
} as const;
|
|
|
|
export interface DialogContentProps
|
|
extends React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> {
|
|
size?: keyof typeof sizeClasses;
|
|
/** Masque le bouton de fermeture en coin (rare : reserve aux flux bloquants). */
|
|
hideClose?: boolean;
|
|
}
|
|
|
|
const DialogContent = React.forwardRef<
|
|
React.ElementRef<typeof DialogPrimitive.Content>,
|
|
DialogContentProps
|
|
>(({ className, children, size = 'md', hideClose = false, ...props }, ref) => (
|
|
<DialogPortal>
|
|
<DialogOverlay />
|
|
<DialogPrimitive.Content
|
|
ref={ref}
|
|
className={cn(
|
|
'fixed left-1/2 top-1/2 z-50 w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2',
|
|
'max-h-[calc(100dvh-4rem)] overflow-y-auto',
|
|
'rounded-lg border border-border bg-white p-5 shadow-lg',
|
|
'duration-200',
|
|
'data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95',
|
|
'data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95',
|
|
sizeClasses[size],
|
|
className
|
|
)}
|
|
{...props}
|
|
>
|
|
{children}
|
|
{!hideClose && (
|
|
<DialogPrimitive.Close
|
|
className={cn(
|
|
'absolute right-4 top-4 rounded-md p-1 text-neutral-500',
|
|
'transition-colors hover:bg-neutral-100 hover:text-brand-navy',
|
|
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2',
|
|
'disabled:pointer-events-none'
|
|
)}
|
|
>
|
|
<X className="size-4" />
|
|
<span className="sr-only">Fermer</span>
|
|
</DialogPrimitive.Close>
|
|
)}
|
|
</DialogPrimitive.Content>
|
|
</DialogPortal>
|
|
));
|
|
DialogContent.displayName = DialogPrimitive.Content.displayName;
|
|
|
|
const DialogHeader = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
|
|
<div className={cn('mb-4 flex flex-col gap-1 pr-8', className)} {...props} />
|
|
);
|
|
DialogHeader.displayName = 'DialogHeader';
|
|
|
|
const DialogFooter = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
|
|
<div
|
|
className={cn('mt-6 flex flex-col-reverse gap-2 sm:flex-row sm:justify-end', className)}
|
|
{...props}
|
|
/>
|
|
);
|
|
DialogFooter.displayName = 'DialogFooter';
|
|
|
|
const DialogTitle = React.forwardRef<
|
|
React.ElementRef<typeof DialogPrimitive.Title>,
|
|
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title>
|
|
>(({ className, ...props }, ref) => (
|
|
<DialogPrimitive.Title
|
|
ref={ref}
|
|
className={cn('font-heading text-h5 text-brand-navy', className)}
|
|
{...props}
|
|
/>
|
|
));
|
|
DialogTitle.displayName = DialogPrimitive.Title.displayName;
|
|
|
|
const DialogDescription = React.forwardRef<
|
|
React.ElementRef<typeof DialogPrimitive.Description>,
|
|
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description>
|
|
>(({ className, ...props }, ref) => (
|
|
<DialogPrimitive.Description
|
|
ref={ref}
|
|
className={cn('text-body-sm text-neutral-500', className)}
|
|
{...props}
|
|
/>
|
|
));
|
|
DialogDescription.displayName = DialogPrimitive.Description.displayName;
|
|
|
|
export {
|
|
Dialog,
|
|
DialogPortal,
|
|
DialogOverlay,
|
|
DialogTrigger,
|
|
DialogClose,
|
|
DialogContent,
|
|
DialogHeader,
|
|
DialogFooter,
|
|
DialogTitle,
|
|
DialogDescription,
|
|
};
|