Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

4.6 KB
Raw Blame
"use client";

import { classNames } from "@cap/utils";
import * as DialogPrimitive from "@radix-ui/react-dialog";
import { X } from "lucide-react";
import * as React from "react";

const Dialog = DialogPrimitive.Root;
const DialogTrigger = DialogPrimitive.Trigger;
const DialogPortal = DialogPrimitive.Portal;

const DialogOverlay = React.forwardRef<
	React.ElementRef<typeof DialogPrimitive.Overlay>,
	React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
	<DialogPrimitive.Overlay
		ref={ref}
		className={classNames("fixed inset-0 z-[500] bg-black/60", className)}
		{...props}
	/>
));
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;

type DialogContentProps = React.ComponentPropsWithoutRef<
	typeof DialogPrimitive.Content
> & {
	/**
	 * Suppress the built-in corner close button. For dialogs whose header packs
	 * its own controls onto that row and needs the close to sit with them rather
	 * than floating above.
	 */
	hideCloseButton?: boolean;
};

const DialogContent = React.forwardRef<
	React.ElementRef<typeof DialogPrimitive.Content>,
	DialogContentProps
>(({ className, children, hideCloseButton = false, ...props }, ref) => (
	<DialogPortal>
		<DialogOverlay className="data-[state=open]:animate-fadeIn data-[state=closed]:animate-fadeOut motion-reduce:animate-none" />
		<div className="flex fixed inset-0 z-[501] justify-center items-center">
			<DialogPrimitive.Content
				ref={ref}
				// A plain fade, on the same keyframes as the overlay, so the two
				// read as one surface appearing rather than a panel travelling
				// into place. Keyed off data-state so closing animates too:
				// Radix keeps the node mounted while a CSS animation runs.
				className={classNames(
					"relative p-0 w-full max-w-md rounded-xl border shadow-lg bg-gray-2 border-gray-4 data-[state=open]:animate-fadeIn data-[state=closed]:animate-fadeOut motion-reduce:animate-none",
					className,
				)}
				style={{
					position: "fixed",
					top: "50%",
					left: "50%",
					transform: "translate(-50%, -50%)",
				}}
				{...props}
			>
				{children}
				{!hideCloseButton && (
					<DialogPrimitive.Close className="absolute right-4 top-4 rounded-xs opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-hidden focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
						<X className="w-5 h-5 text-gray-9" />
						<span className="sr-only">Close</span>
					</DialogPrimitive.Close>
				)}
			</DialogPrimitive.Content>
		</div>
	</DialogPortal>
));
DialogContent.displayName = DialogPrimitive.Content.displayName;

type DialogHeaderProps = React.HTMLAttributes<HTMLDivElement> & {
	icon?: React.JSX.Element;
	description?: string;
};

const DialogHeader = ({
	className,
	icon,
	description,
	children,
	...props
}: DialogHeaderProps) => (
	<div
		className={classNames(
			"flex flex-col gap-3 items-start p-5 border-b md:items-center md:flex-row border-gray-4",
			className,
		)}
		{...props}
	>
		{icon && (
			<div className="flex justify-center items-center rounded-full border md:mb-0 text-gray-12 border-gray-5 bg-gray-3 min-w-10 min-h-10 size-10">
				{icon}
			</div>
		)}
		<div className="flex flex-col">
			{children}
			{description && <p className="text-sm text-gray-10">{description}</p>}
		</div>
	</div>
);
DialogHeader.displayName = "DialogHeader";

const DialogFooter = ({
	className,
	...props
}: React.HTMLAttributes<HTMLDivElement>) => (
	<div
		className={classNames(
			"flex flex-col-reverse gap-1 p-5 border-t border-gray-4 sm:flex-row sm:justify-end sm:space-x-2",
			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={classNames("text-lg font-medium text-gray-12", 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={classNames("p-5 text-sm text-gray-11", className)}
		{...props}
	/>
));
DialogDescription.displayName = DialogPrimitive.Description.displayName;

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

export type { DialogHeaderProps };