Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

12.1 KB
Raw Blame
/* eslint-disable @repo/no-style-props, @repo/no-margin-on-root-elements, @repo/component-file-structure, @typescript-eslint/no-deprecated -- Legacy UI usage retained during migration to design-system components. */
"use client";

import * as React from "react";
import * as DialogPrimitive from "@radix-ui/react-dialog";
import { X } from "@langfuse/icons";
import { cva, type VariantProps } from "class-variance-authority";

import { cn } from "@/src/utils/tailwind";
import { useLayerContainer } from "@/src/context/LayerContext/LayerContext";
import { DialogController as DesignSystemDialogController } from "@/src/components/design-system/DialogController/DialogController";
import motionStyles from "./dialog-motion.module.css";

/**
 * @deprecated Use design-system/DialogController/DialogController with design-system/Dialog/Dialog.
 */
const Dialog = DialogPrimitive.Root;

/**
 * @deprecated Use openDialog from the children render prop of design-system/DialogController/DialogController.
 */
const DialogTrigger = DialogPrimitive.Trigger;

// Route the portal into the `modal` overlay layer (null until mounted →
// falls back to <body>, SSR-parity). Layer order, not z-index, stacks it.
/**
 * @deprecated Use design-system/Dialog/Dialog.
 */
const DialogPortal = ({
  ...props
}: React.ComponentPropsWithoutRef<typeof DialogPrimitive.Portal>) => {
  const container = useLayerContainer("modal");
  return <DialogPrimitive.Portal container={container} {...props} />;
};
DialogPortal.displayName = "DialogPortal";

/**
 * @deprecated Use closeDialog from design-system/DialogController/DialogController.
 */
const DialogClose = DialogPrimitive.Close;

type DialogOverlayMode = "subtle" | "invisible" | "blocking" | "soft";

const dialogOverlayClasses: Record<DialogOverlayMode, string> = {
  invisible: "bg-transparent",
  soft: "bg-black/10 dark:bg-black/65",
  subtle: "bg-black/25 dark:bg-black/45",
  blocking: "bg-black/50 dark:bg-black/65",
};

/**
 * @deprecated Use design-system/Dialog/Dialog.
 */
const DialogOverlay = React.forwardRef<
  React.ComponentRef<typeof DialogPrimitive.Overlay>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay> & {
    overlayMode?: DialogOverlayMode;
  }
>(({ className, overlayMode = "subtle", onClick, ...props }, ref) => (
  <DialogPrimitive.Overlay
    ref={ref}
    className={cn(
      motionStyles.overlay,
      "fixed inset-0",
      dialogOverlayClasses[overlayMode],
      className,
    )}
    // The dialog portals its DOM out of the app tree, but React synthetic
    // events still bubble through the REACT tree — into whatever rendered the
    // dialog (e.g. a clickable table row). Backdrop clicks must not leak there.
    onClick={(e) => {
      onClick?.(e);
      e.stopPropagation();
    }}
    {...props}
  />
));
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;

const dialogContentVariants = cva(
  "fixed left-[50%] top-[50%] overflow-hidden flex w-full translate-x-[-50%] translate-y-[-50%] flex-col bg-modal shadow-lg sm:rounded-lg",
  {
    variants: {
      size: {
        default: "max-w-lg max-h-[85vh]",
        lg: "max-w-4xl max-h-[85vh]",
        xl: "max-w-7xl h-[90vh]",
        xxl: "max-w-[95vw] h-[90vh]",
      },
    },
    defaultVariants: {
      size: "default",
    },
  },
);

/**
 * @deprecated Use Dialog from design-system/Dialog/Dialog inside design-system/DialogController/DialogController.
 */
const DialogContent = React.forwardRef<
  React.ComponentRef<typeof DialogPrimitive.Content>,
  Omit<
    React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & {
      closeOnInteractionOutside?: boolean;
      confirmCloseOnEscape?: string;
      overlayMode?: DialogOverlayMode;
      overlayClassName?: string;
      stopPropagationOnEnterSpace?: boolean;
      showCloseButton?: boolean;
    } & VariantProps<typeof dialogContentVariants>,
    "onPointerDownOutside" | "onInteractOutside"
  >
>(
  (
    {
      className,
      children,
      closeOnInteractionOutside = false,
      confirmCloseOnEscape,
      overlayMode = "subtle",
      overlayClassName,
      stopPropagationOnEnterSpace = true,
      showCloseButton = true,
      onEscapeKeyDown,
      onClick,
      size,
      ...props
    },
    ref,
  ) => {
    const handleKeyDown = (e: React.KeyboardEvent) => {
      // Prevent Enter/Space key events from propagating to parent elements
      // This prevents triggering actions like row clicks when submitting forms in dialogs
      if (stopPropagationOnEnterSpace && (e.key === "Enter" || e.key === " ")) {
        e.stopPropagation();
      }
    };
    const handleEscapeKeyDown: React.ComponentPropsWithoutRef<
      typeof DialogPrimitive.Content
    >["onEscapeKeyDown"] = (e) => {
      onEscapeKeyDown?.(e);

      if (e.defaultPrevented || !confirmCloseOnEscape) {
        return;
      }

      if (!window.confirm(confirmCloseOnEscape)) {
        e.preventDefault();
      }
    };

    return (
      <DialogPortal>
        <DialogOverlay overlayMode={overlayMode} className={overlayClassName} />
        <DialogPrimitive.Content
          ref={ref}
          className={cn(
            motionStyles.content,
            dialogContentVariants({ size, className }),
          )}
          aria-describedby={undefined}
          onKeyDown={handleKeyDown}
          onEscapeKeyDown={handleEscapeKeyDown}
          // See DialogOverlay: clicks inside the dialog must not bubble
          // through the React tree into the component that rendered it.
          onClick={(e) => {
            onClick?.(e);
            e.stopPropagation();
          }}
          onPointerDownOutside={(e) => {
            if (!closeOnInteractionOutside) {
              e.preventDefault();
            }
          }}
          onInteractOutside={(e) => {
            if (!closeOnInteractionOutside) {
              e.preventDefault();
            }
          }}
          {...props}
        >
          {children}
          {showCloseButton && (
            <div className="[&:has(.dialog-header)]:hidden [&:not(:has(.dialog-header))]:absolute [&:not(:has(.dialog-header))]:top-3 [&:not(:has(.dialog-header))]:right-3 [&:not(:has(.dialog-header))]:z-20">
              <DialogPrimitive.Close className="ring-offset-background focus:ring-ring data-[state=open]:bg-accent data-[state=open]:text-muted-foreground rounded-sm opacity-70 transition-opacity hover:opacity-100 focus:ring-2 focus:ring-offset-2 focus:outline-hidden disabled:pointer-events-none">
                <X size="base" />
                <span className="sr-only">Close</span>
              </DialogPrimitive.Close>
            </div>
          )}
        </DialogPrimitive.Content>
      </DialogPortal>
    );
  },
);
DialogContent.displayName = DialogPrimitive.Content.displayName;

/**
 * Owns dialog open state while callers retain trigger and content presentation.
 */
type DialogControllerProps<State = void> = {
  // Evaluated only when the controller mounts; later callback or dependency changes do not update the dialog.
  initialState?: () => State | undefined;
  children: (control: {
    isOpen: boolean;
    openDialog: (...args: [State] extends [void] ? [] : [state: State]) => void;
  }) => React.ReactNode;
  closeOnInteractionOutside: boolean;
  onBeforeClose?: () => boolean;
  onDismiss?: () => void;
  renderContent: (control: {
    state: State;
    closeDialog: () => void;
  }) => React.ReactNode;
  size: React.ComponentProps<typeof DialogContent>["size"];
};

/**
 * @deprecated Use design-system/DialogController/DialogController with its renderDialog prop and design-system/Dialog/Dialog.
 */
const DialogController = <State = void,>({
  initialState,
  children,
  closeOnInteractionOutside,
  onBeforeClose,
  onDismiss,
  renderContent,
  size,
}: DialogControllerProps<State>) => (
  <DesignSystemDialogController
    initialState={initialState}
    onBeforeClose={onBeforeClose}
    onDismiss={onDismiss}
    renderDialog={({ state, closeDialog }) => (
      <DialogContent
        size={size}
        closeOnInteractionOutside={closeOnInteractionOutside}
      >
        {renderContent({ state, closeDialog })}
      </DialogContent>
    )}
  >
    {children}
  </DesignSystemDialogController>
);

const dialogHeaderVariants = cva(
  "bg-modal sticky top-0 z-30 flex shrink-0 flex-col space-y-1.5 rounded-t-lg p-4",
  {
    variants: {
      variant: {
        default: "border-b",
        // Borderless confirm dialogs drop the divider, so trim the bottom
        // padding to keep title and body from drifting apart.
        action: "pb-2",
      },
    },
    defaultVariants: {
      variant: "default",
    },
  },
);

/**
 * @deprecated Use the title prop of design-system/Dialog/Dialog.
 */
const DialogHeader = ({
  className,
  children,
  variant,
  ...props
}: React.HTMLAttributes<HTMLDivElement> &
  VariantProps<typeof dialogHeaderVariants>) => (
  <div
    className={cn(
      "dialog-header",
      dialogHeaderVariants({ variant, className }),
    )}
    {...props}
  >
    <div className="flex w-full items-center justify-between gap-4 text-center sm:text-left">
      <div className="min-w-0 flex-1">{children}</div>
      {/* Untabbable on purpose: as the first tabbable descendant of the
          content it would take Radix's initial focus away from the dialog's
          first field or primary action. Escape and the mouse still close. */}
      <DialogPrimitive.Close
        className="ring-offset-background focus:ring-ring data-[state=open]:bg-accent data-[state=open]:text-muted-foreground hover:bg-accent z-20 -my-2 -mr-2 ml-2 inline-flex size-9 shrink-0 items-center justify-center rounded-md opacity-70 transition-opacity hover:opacity-100 focus:ring-2 focus:ring-offset-2 focus:outline-hidden disabled:pointer-events-none"
        tabIndex={-1}
      >
        <X size="base" />
        <span className="sr-only">Close</span>
      </DialogPrimitive.Close>
    </div>
  </div>
);
DialogHeader.displayName = "DialogHeader";

/**
 * @deprecated Use Dialog.Body from design-system/Dialog/Dialog.
 */
const DialogBody = React.forwardRef<
  HTMLDivElement,
  React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
  <div
    ref={ref}
    className={cn("flex flex-1 flex-col gap-4 overflow-y-auto p-4", className)}
    {...props}
  />
));
DialogBody.displayName = "DialogBody";

const dialogFooterVariants = cva(
  "bg-modal sticky bottom-0 z-10 flex shrink-0 flex-col-reverse rounded-b-lg p-6 px-6 sm:flex-row sm:justify-end sm:space-x-2",
  {
    variants: {
      variant: {
        default: "border-t",
        // Borderless confirm dialogs drop the divider, so trim the top padding
        // to pull the buttons closer to the content.
        action: "pt-2",
      },
    },
    defaultVariants: {
      variant: "default",
    },
  },
);

/**
 * @deprecated Use the actions prop of design-system/Dialog/Dialog.
 */
const DialogFooter = ({
  className,
  variant,
  ...props
}: React.HTMLAttributes<HTMLDivElement> &
  VariantProps<typeof dialogFooterVariants>) => (
  <div
    className={cn(
      "dialog-footer",
      dialogFooterVariants({ variant, className }),
    )}
    {...props}
  />
);
DialogFooter.displayName = "DialogFooter";

/**
 * @deprecated Use the title prop of design-system/Dialog/Dialog.
 */
const DialogTitle = React.forwardRef<
  React.ComponentRef<typeof DialogPrimitive.Title>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title>
>(({ className, ...props }, ref) => (
  <DialogPrimitive.Title
    ref={ref}
    className={cn("text-xl leading-none font-bold tracking-tight", className)}
    {...props}
  />
));
DialogTitle.displayName = DialogPrimitive.Title.displayName;

/**
 * @deprecated Use the text prop or Dialog.Body from design-system/Dialog/Dialog.
 */
const DialogDescription = React.forwardRef<
  React.ComponentRef<typeof DialogPrimitive.Description>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description>
>(({ className, ...props }, ref) => (
  <DialogPrimitive.Description
    ref={ref}
    className={cn("text-muted-foreground mt-1 text-sm", className)}
    {...props}
  />
));
DialogDescription.displayName = DialogPrimitive.Description.displayName;

export {
  Dialog,
  DialogController,
  DialogClose,
  DialogTrigger,
  DialogContent,
  DialogHeader,
  DialogBody,
  DialogFooter,
  DialogTitle,
  DialogDescription,
};