import clsx from "clsx"; import { Dialog as DialogPrimitive } from "radix-ui"; import * as React from "react"; import { useEvent, useGetKey, useGetSet } from "../../hooks"; import { ButtonContext } from "../Link"; import * as styles from "./styles.module.css"; const InDialogContext = React.createContext(false); export const useInDialog = () => React.use(InDialogContext); const DialogContext = React.createContext< React.ComponentPropsWithRef >({}); export function Dialog({ children, preview, onOpenChange, ...rest }: DialogPrimitive.DialogProps & { className?: string; children: React.ReactNode; preview?: true; "data-id": string; }) { const key = useGetKey(rest); const [{ isOpen }, setState] = useGetSet(key, { isOpen: false }, true); const open = React.useCallback(() => { setState({ isOpen: true }, process.env.PREVIEW ? `onChange` : undefined); }, [setState]); const close = React.useCallback(() => { setState({ isOpen: false }, process.env.PREVIEW ? `onChange` : undefined); }, [setState]); const id = rest["data-id"]; useEvent(id, "open", open); useEvent(id, "close", close); return ( { setState({ isOpen }, process.env.PREVIEW ? `onChange` : undefined); onOpenChange?.(isOpen); }} > {children} ); } Dialog.DialogTrigger = ({ children, ...props }: React.ComponentPropsWithRef) => { const child = props.asChild === false ? children : React.Children.toArray(children)?.[0]; return ( {child} ); }; const DialogOverlay = ({ className, ...props }: React.ComponentPropsWithRef) => ( ); Dialog.DialogOverlay = DialogOverlay; const DialogContent = ({ size = "md", className, children, autoFocus = true, ...props }: React.ComponentPropsWithRef & { autoFocus: boolean; size: "sm" | "md" | "lg" | "xl" | "full"; }) => { const additionalProps: Partial< React.ComponentPropsWithoutRef > = {}; if (!autoFocus) { additionalProps.onOpenAutoFocus = block; additionalProps.onCloseAutoFocus = block; additionalProps.onPointerDownOutside = block; additionalProps.onInteractOutside = block; } const overlayProps = React.useContext(DialogContext); return ( {children} ); }; Dialog.DialogContent = DialogContent; DialogContent.DialogClose = ({ className, children, ...props }: DialogPrimitive.DialogCloseProps) => { const child = props.asChild === false ? children : React.Children.toArray(children)?.[0]; return ( {child} ); }; DialogContent.DialogHeader = ({ className, ...props }: React.HTMLAttributes) => (
); DialogContent.DialogFooter = ({ className, ...props }: React.HTMLAttributes) => (
); DialogContent.DialogTitle = ({ className, ...props }: React.ComponentPropsWithRef) => ( ); DialogContent.DialogDescription = ({ className, ...props }: React.ComponentPropsWithRef) => ( ); function block(e: Event) { e?.preventDefault(); }