Skip to content

The agent that grows with you

13.7 KB
Raw Blame
import { Dialog as DialogPrimitive } from 'radix-ui'
import * as React from 'react'

import { Button } from '@/components/ui/button'
import { DialogPortalContainerContext } from '@/components/ui/dialog-portal-context'
import { useI18n } from '@/i18n'
import { X } from '@/lib/icons'
import { cn } from '@/lib/utils'

function Dialog({ ...props }: React.ComponentProps<typeof DialogPrimitive.Root>) {
  return <DialogPrimitive.Root data-slot="dialog" {...props} />
}

function DialogTrigger({ ...props }: React.ComponentProps<typeof DialogPrimitive.Trigger>) {
  return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />
}

function DialogPortal({ ...props }: React.ComponentProps<typeof DialogPrimitive.Portal>) {
  return <DialogPrimitive.Portal data-slot="dialog-portal" {...props} />
}

function DialogClose({ ...props }: React.ComponentProps<typeof DialogPrimitive.Close>) {
  return <DialogPrimitive.Close data-slot="dialog-close" {...props} />
}

function DialogOverlay({
  className,
  blur = true,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Overlay> & {
  blur?: boolean
}) {
  return (
    <DialogPrimitive.Overlay
      className={cn(
        'fixed inset-0 z-(--z-modal-backdrop) pointer-events-auto bg-black/22 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:animate-in data-[state=open]:fade-in-0',
        blur && 'backdrop-blur-[0.125rem]',
        className
      )}
      data-slot="dialog-overlay"
      {...props}
    />
  )
}

type DialogBannerTone = 'error' | 'warn' | 'info'

// Tinted, edge-to-edge bottom banner per tone. Error/warn keep their semantic
// destructive/primary tokens; info derives from the dialog's own bubble
// background so it reads as part of the themed dialog — lifted 30% toward white
// in light mode, deepened 20% toward black in dark mode.
const DIALOG_BANNER_TONES: Record<DialogBannerTone, string> = {
  error: 'bg-destructive/12 text-destructive',
  warn: 'bg-primary/12 text-primary',
  info: 'bg-[color-mix(in_srgb,var(--ui-chat-bubble-background),white_30%)] text-[color-mix(in_srgb,var(--ui-chat-bubble-background),black_60%)] dark:bg-[color-mix(in_srgb,var(--ui-chat-bubble-background),black_20%)] dark:text-[color-mix(in_srgb,var(--ui-chat-bubble-background),white_60%)]'
}

// Radix focuses the first focusable element inside Dialog.Content on open. In
// most dialogs that's a real input and the default autofocus is exactly what
// we want, so it's opt-in rather than a shared default here. In dialogs with
// no input (e.g. the updates overlay's idle/error views), the first focusable
// element ends up being the close button, and since Tip shows on focus as well
// as hover, that autofocus makes the "Close" tip appear immediately with no
// pointer ever near the button. Dialogs like that should pass this in
// explicitly as `onOpenAutoFocus={preventCloseButtonAutoFocus}`. Note it leaves
// focus wherever it was — outside the dialog — so a dialog that answers keys
// (Enter to confirm) must focus something of its own instead.
export function preventCloseButtonAutoFocus(event: Event) {
  event.preventDefault()
}

// The dialog's top-right X. Radix Close routes through the modal's
// onOpenChange, same path as Escape. Exported for bespoke Radix shells (the
// boot-failure overlay) that can't use DialogContent but want the same X.
function DialogCloseButton() {
  const { t } = useI18n()

  return (
    <DialogPrimitive.Close asChild data-slot="dialog-close-button">
      <Button
        aria-label={t.common.close}
        className="absolute right-2.5 top-2.5 z-20 text-(--ui-text-tertiary) hover:bg-(--chrome-action-hover) hover:text-foreground"
        size="icon-xs"
        variant="ghost"
      >
        <X className="size-4" />
        <span className="sr-only">{t.common.close}</span>
      </Button>
    </DialogPrimitive.Close>
  )
}

function DialogContent({
  className,
  bodyClassName,
  children,
  showCloseButton = true,
  fitContent = false,
  blurBackdrop = true,
  banner,
  bannerTone = 'error',
  chrome,
  overlayClassName,
  onOpenAutoFocus,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Content> & {
  showCloseButton?: boolean
  // Backdrop skin, e.g. a heavier scrim for media viewers.
  overlayClassName?: string
  // Controls pinned to the shell rather than the scrolling body (e.g. prev/next
  // pagers). The shell doesn't clip, so these may sit past its edges.
  chrome?: React.ReactNode
  // Keep the underlying task readable for context-sensitive prompts.
  blurBackdrop?: boolean
  // Size the dialog to its content (capped at the viewport) instead of the
  // default fixed `max-w-lg`. For content that has no intrinsic width (grids,
  // full-width inputs) pair it with a `min-w-*` in `className`.
  fitContent?: boolean
  // Layout and scroll classes for the inner body box: padding, gap, display,
  // overflow. `className` styles the OUTER shell: position, size, border, and
  // background. The note on the shell below explains this split.
  bodyClassName?: string
  // A dialog-level notice rendered as a banner flush to the bottom edge (tinted,
  // inherited bottom radius) so it reads as part of the dialog, not a floating
  // alert. Falsy → no banner. Tone picks the colour.
  banner?: React.ReactNode
  bannerTone?: DialogBannerTone
}) {
  const widthClass = fitContent ? 'w-auto max-w-[92vw]' : 'w-full max-w-lg'

  // Publish the dialog's content node so popovers (Select / Popover /
  // DropdownMenu) opened inside it portal INTO the dialog instead of
  // document.body. That keeps them as DOM descendants — focus never leaves the
  // dialog, so dismissing a dropdown (or clicking another field) no longer
  // trips the Dialog's outside-interaction/focus-out close. See
  // dialog-portal-context.ts. State (not just a ref) so consumers re-render once
  // the node mounts.
  const [contentNode, setContentNode] = React.useState<HTMLElement | null>(null)

  // Opened from inside another dialog (e.g. an image lightbox over a detail
  // modal): both layers step above the parent so its scrim dims the parent too.
  // Shared z tokens alone would slot this backdrop under the parent's content.
  const nested = React.useContext(DialogPortalContainerContext) !== null

  const overlay = (
    <DialogOverlay blur={blurBackdrop} className={cn(nested && 'z-[calc(var(--z-modal)+1)]', overlayClassName)} />
  )

  const layerClass = nested && 'z-[calc(var(--z-modal)+2)]'

  // No default here — Radix's normal autofocus (first focusable element, often
  // an input) is what most dialogs want. Dialogs with no input should pass
  // `onOpenAutoFocus={preventCloseButtonAutoFocus}` explicitly instead.

  // No tip on the X — the glyph is the label (DialogCloseButton keeps aria-label / sr-only).
  const closeButton = showCloseButton ? <DialogCloseButton /> : null

  // With a banner, the border can't live on the scroll/clip box (it would draw a
  // line around the banner too). The white body keeps its own bottom radius and
  // sits over the tinted footer; the outer shell only clips the banner to the
  // dialog's rounded bottom edge.
  if (banner) {
    return (
      <DialogPortal>
        {overlay}
        <DialogPrimitive.Content
          className={cn(
            // The same split as the plain variant. The shell must not clip,
            // because it crops the popovers that portal into it. The banner
            // below has its own `overflow-hidden`, which rounds its corners.
            'fixed left-1/2 top-1/2 z-(--z-modal) pointer-events-auto flex max-h-[85vh] -translate-x-1/2 -translate-y-1/2 flex-col rounded-xl bg-(--ui-chat-bubble-background) text-[length:var(--conversation-text-font-size)] text-foreground shadow-nous duration-200 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',
            widthClass,
            layerClass,
            className,
            // Callers often pass `gap-*` for the no-banner grid layout — suppress
            // it here so the banner can tuck under the body's rounded bottom edge.
            'gap-0'
          )}
          data-slot="dialog-content"
          onOpenAutoFocus={onOpenAutoFocus}
          ref={setContentNode}
          {...props}
        >
          <DialogPortalContainerContext.Provider value={contentNode}>
            {/* Scroll lives on an inner box so this shell keeps a painted bottom radius. */}
            <div className="relative z-10 overflow-hidden rounded-xl border border-b-0 border-(--stroke-nous) bg-(--ui-chat-bubble-background)">
              <div
                className={cn(
                  'grid max-h-[calc(85vh-5rem)] min-h-0 grid-cols-[minmax(0,1fr)] gap-3 overflow-y-auto p-4',
                  bodyClassName
                )}
              >
                {children}
              </div>
            </div>
            <div
              className={cn(
                // Overlap by one corner radius so the white bottom lobes read clearly
                // over the tint instead of meeting it on a straight seam.
                'relative z-0 -mt-[var(--radius-xl)] overflow-hidden rounded-b-xl px-4 pb-2.5 pt-[calc(var(--radius-xl)+0.625rem)] text-center text-[length:var(--conversation-tool-font-size)] leading-relaxed shadow-[inset_0_7px_7px_-4px_rgb(0_0_0/0.28)]',
                DIALOG_BANNER_TONES[bannerTone]
              )}
              data-slot="dialog-banner"
              role={bannerTone === 'error' ? 'alert' : 'status'}
            >
              {banner}
            </div>
            {chrome}
            {closeButton}
          </DialogPortalContainerContext.Provider>
        </DialogPrimitive.Content>
      </DialogPortal>
    )
  }

  return (
    <DialogPortal>
      {overlay}
      <DialogPrimitive.Content
        className={cn(
          // The SHELL: position, size, and skin. It has no overflow of its own,
          // and that is deliberate. It is the portal container for the popovers
          // that open inside the dialog (see DialogPortalContainerContext), and
          // a clipping ancestor crops them. The body box below owns the scroll,
          // so a tall dialog scrolls and a Select or Popover can still paint
          // past the edge of that box.
          'fixed left-1/2 top-1/2 z-(--z-modal) pointer-events-auto flex max-h-[85vh] -translate-x-1/2 -translate-y-1/2 flex-col rounded-xl border border-(--stroke-nous) bg-(--ui-chat-bubble-background) text-[length:var(--conversation-text-font-size)] text-foreground shadow-nous duration-200 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',
          widthClass,
          layerClass,
          className
        )}
        data-slot="dialog-content"
        onOpenAutoFocus={onOpenAutoFocus}
        ref={setContentNode}
        {...props}
      >
        <DialogPortalContainerContext.Provider value={contentNode}>
          {/* The BODY: layout and scroll. `min-h-0` lets this box shrink inside
              the max-height of the shell. The overflow then scrolls here
              instead of pushing the shell past the viewport. The explicit
              `minmax(0,1fr)` column keeps the implicit grid track from sizing
              to unbreakable content (a long URL in a nowrap <code>), which
              otherwise widens the track past the dialog and grows a horizontal
              scrollbar that clips the content instead of truncating it. */}
          <div
            className={cn(
              'grid min-h-0 grid-cols-[minmax(0,1fr)] gap-3 overflow-y-auto rounded-[inherit] p-4',
              bodyClassName
            )}
          >
            {children}
          </div>
          {chrome}
          {closeButton}
        </DialogPortalContainerContext.Provider>
      </DialogPrimitive.Content>
    </DialogPortal>
  )
}

function DialogHeader({ className, ...props }: React.ComponentProps<'div'>) {
  return (
    <div
      className={cn('flex flex-col gap-1 text-center sm:text-left', className)}
      data-slot="dialog-header"
      {...props}
    />
  )
}

function DialogFooter({ className, ...props }: React.ComponentProps<'div'>) {
  return (
    <div
      className={cn('flex flex-col-reverse gap-2 sm:flex-row sm:justify-end', className)}
      data-slot="dialog-footer"
      {...props}
    />
  )
}

function DialogTitle({
  className,
  icon: Icon,
  children,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Title> & {
  // Pass an icon (from `@/lib/icons`) to get the canonical dialog-header glyph: a plain
  // primary-tinted icon inline with the title (no bg chip / ring). This is the
  // single source of truth for dialog header icons — don't hand-roll wrappers.
  icon?: React.ComponentType<{ className?: string }>
}) {
  return (
    <DialogPrimitive.Title
      className={cn(
        'text-[0.9375rem] font-semibold tracking-tight text-foreground',
        Icon && 'flex items-center gap-2',
        className
      )}
      data-slot="dialog-title"
      {...props}
    >
      {Icon ? <Icon className="size-4 shrink-0 text-primary" /> : null}
      {children}
    </DialogPrimitive.Title>
  )
}

function DialogDescription({ className, ...props }: React.ComponentProps<typeof DialogPrimitive.Description>) {
  return (
    <DialogPrimitive.Description
      className={cn(
        'text-[length:var(--conversation-caption-font-size)] leading-(--conversation-caption-line-height) text-(--ui-text-tertiary)',
        className
      )}
      data-slot="dialog-description"
      {...props}
    />
  )
}

export {
  Dialog,
  DialogClose,
  DialogCloseButton,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogOverlay,
  DialogPortal,
  DialogTitle,
  DialogTrigger
}