{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "form-dialog",
  "title": "Form dialog",
  "description": "A create or edit form in a dialog: a centred card on desktop, a bottom sheet on phones, with keyboard submit.",
  "dependencies": [
    "@base-ui/react@^1.7.0",
    "@keyline-icons/react@^2.2.0"
  ],
  "registryDependencies": [
    "@khaeli/button",
    "@khaeli/dialog",
    "@khaeli/fancy-button",
    "@khaeli/kbd",
    "@khaeli/label",
    "@khaeli/switch",
    "@khaeli/utils"
  ],
  "files": [
    {
      "path": "registry/khaeli/ui/form-dialog.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { Dialog as DialogPrimitive } from \"@base-ui/react/dialog\"\nimport { cn } from \"@/lib/utils\"\nimport { CheckIcon, XIcon } from \"@keyline-icons/react\"\n\nimport { Button } from \"@/components/ui/button\"\nimport { FancyButton, FancyButtonIcon } from \"@/components/ui/fancy-button\"\nimport {\n  Dialog,\n  DialogClose,\n  DialogDescription,\n  DialogOverlay,\n  DialogPortal,\n  DialogTitle,\n} from \"@/components/ui/dialog\"\nimport { Kbd } from \"@/components/ui/kbd\"\nimport { Label } from \"@/components/ui/label\"\nimport { Switch } from \"@/components/ui/switch\"\n\n// --- FormDialog ---\n// Attio-style modal composition. 808/640/480px wide, radius 16,\n// shadow-modal, 49px header [icon] title [28px ✕], optional 28px-pill tabs row,\n// scrollable px-6 py-4 body, 44px border-t footer (default: Cancel + submit\n// with ⌘↵). Fields keep the foundation's 30px controls (no 36px dialog\n// override); textareas get 96px from foundation.css's\n// [data-slot=\"form-dialog\"] scope.\n// Phones (< md): large-detent sheet (Reminders \"New Reminder\",\n// Grok \"New Task\") — white popover surface, tray top radius, 76px header\n// [40px #F7F7F7 ✕ · 17px/600 title · 44px FancyButton confirm], 44px\n// #F7F7F7 fields, default footer folded into the header.\n\nconst formDialogWidths = {\n  sm: \"md:max-w-[480px]\",\n  md: \"md:max-w-[640px]\",\n  lg: \"md:max-w-[808px]\",\n} as const\n\nfunction FormDialog({\n  open,\n  onOpenChange,\n  onOpenChangeComplete,\n  icon,\n  title,\n  description,\n  showDescription = false,\n  tabs,\n  children,\n  footer,\n  createMore,\n  submitLabel = \"Save\",\n  submitKbd = false,\n  onSubmit,\n  formId,\n  pending = false,\n  submitDisabled = false,\n  submitDescribedBy,\n  submitVariant = \"default\",\n  size = \"md\",\n  className,\n}: {\n  open: boolean\n  onOpenChange: (open: boolean) => void\n  onOpenChangeComplete?: (open: boolean) => void\n  icon?: React.ReactNode\n  title: React.ReactNode\n  description?: React.ReactNode\n  showDescription?: boolean\n  tabs?: React.ReactNode\n  children: React.ReactNode\n  footer?: React.ReactNode\n  /**\n   * Renders \"Create more\" + a small Switch immediately left of Cancel in the\n   * default footer. While checked, finishing a submission (pending true →\n   * false) focuses `focusId`, unless focus already sits on a field the page\n   * marked aria-invalid.\n   */\n  createMore?: {\n    checked: boolean\n    onCheckedChange: (checked: boolean) => void\n    focusId: string\n  }\n  submitLabel?: string\n  submitKbd?: boolean\n  onSubmit?: () => void\n  formId?: string\n  pending?: boolean\n  submitDisabled?: boolean\n  /** Id of the text that explains the submit button (why it is disabled); set on both submit buttons. */\n  submitDescribedBy?: string\n  submitVariant?: \"default\" | \"destructive\"\n  size?: \"sm\" | \"md\" | \"lg\"\n  className?: string\n}) {\n  const submit = () => {\n    if (pending || submitDisabled) return\n    if (onSubmit) {\n      onSubmit()\n      return\n    }\n    if (formId) {\n      const form = document.getElementById(formId)\n      if (form instanceof HTMLFormElement) form.requestSubmit()\n    }\n  }\n\n  // Phone confirm: default-ish labels become Reminders' check circle (the\n  // label survives as aria-label); anything longer, or destructive, stays a\n  // readable Grok-style capsule.\n  const circleConfirm =\n    submitVariant !== \"destructive\" &&\n    [\"Save\", \"Create\", \"Done\", \"Add\"].includes(submitLabel)\n\n  const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {\n    if (event.key !== \"Enter\" || !(event.metaKey || event.ctrlKey)) return\n    if (!onSubmit && !formId) return\n    event.preventDefault()\n    event.stopPropagation()\n    submit()\n  }\n\n  // Pages disable their fieldset while pending, which blurs the focused field\n  // and makes focus() in mutation callbacks a no-op. Refocus once the render\n  // that re-enables the controls has committed.\n  const wasPending = React.useRef(pending)\n  const createMoreChecked = createMore?.checked ?? false\n  const createMoreFocusId = createMore?.focusId\n  React.useEffect(() => {\n    const finished = wasPending.current && !pending\n    wasPending.current = pending\n    if (!finished || !open || !createMoreChecked || !createMoreFocusId) return\n    const active = document.activeElement\n    if (active instanceof HTMLElement && active.getAttribute(\"aria-invalid\") === \"true\") return\n    document.getElementById(createMoreFocusId)?.focus()\n  }, [pending, open, createMoreChecked, createMoreFocusId])\n\n  return (\n    <Dialog open={open} onOpenChange={onOpenChange} onOpenChangeComplete={onOpenChangeComplete}>\n      <DialogPortal>\n        <DialogOverlay className=\"max-md:bg-[var(--scrim)] max-md:backdrop-filter-none!\" />\n        <DialogPrimitive.Popup\n          data-slot=\"form-dialog\"\n          onKeyDown={handleKeyDown}\n          className={cn(\n            \"fixed top-1/2 left-1/2 z-50 flex max-h-[85svh] w-full max-w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2 flex-col overflow-hidden bg-background text-base text-foreground duration-100 outline-none data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95\",\n            // Phones: large-detent sheet — full width, top at\n            // safe-area + 10px, tray top corners, white popover surface, modal\n            // shadow. Motion (380ms drawer slide) lives in foundation.css's phone\n            // sheet grammar.\n            \"max-md:top-[calc(env(safe-area-inset-top)+10px)] max-md:right-0 max-md:bottom-0 max-md:left-0 max-md:max-h-none max-md:max-w-none max-md:translate-none max-md:rounded-t-tray max-md:rounded-b-none max-md:bg-popover max-md:shadow-modal\",\n            // Phone fields (Reminders/Grok): 44px #F7F7F7 fields,\n            // rounded-xl, 17px text, 16px padding, no border/ring — focus and\n            // aria-invalid read as a 2px tinted halo. Desktop keeps the 30px\n            // foundation.css rule (min-width: 768px scope).\n            \"max-md:[&_:is([data-slot=input],[data-slot=native-select],[data-slot=select-trigger],[data-slot=combobox-input],[data-slot=input-group])]:h-11! max-md:[&_:is([data-slot=input],[data-slot=native-select],[data-slot=select-trigger],[data-slot=combobox-input],[data-slot=input-group])]:rounded-xl! max-md:[&_:is([data-slot=input],[data-slot=native-select],[data-slot=select-trigger],[data-slot=combobox-input],[data-slot=input-group])]:px-4!\",\n            \"max-md:[&_:is([data-slot=input],[data-slot=native-select],[data-slot=select-trigger],[data-slot=combobox-input],[data-slot=input-group],[data-slot=textarea])]:border-transparent! max-md:[&_:is([data-slot=input],[data-slot=native-select],[data-slot=select-trigger],[data-slot=combobox-input],[data-slot=input-group],[data-slot=textarea])]:bg-secondary! max-md:[&_:is([data-slot=input],[data-slot=native-select],[data-slot=select-trigger],[data-slot=combobox-input],[data-slot=input-group],[data-slot=textarea])]:text-base! max-md:[&_:is([data-slot=input],[data-slot=native-select],[data-slot=select-trigger],[data-slot=combobox-input],[data-slot=input-group],[data-slot=textarea])]:[box-shadow:none]! max-md:[&_:is([data-slot=input],[data-slot=combobox-input],[data-slot=textarea])]:placeholder:text-subtle-foreground!\",\n            \"max-md:[&_:is([data-slot=input],[data-slot=native-select],[data-slot=select-trigger],[data-slot=combobox-input],[data-slot=textarea]):focus-visible]:[box-shadow:0_0_0_2px_rgb(from_var(--ring)_r_g_b_/_0.35)]! max-md:[&_:is([data-slot=input],[data-slot=native-select],[data-slot=select-trigger],[data-slot=combobox-input],[data-slot=textarea])[aria-invalid=true]]:[box-shadow:0_0_0_2px_rgb(from_var(--destructive)_r_g_b_/_0.5)]!\",\n            \"max-md:[&_[data-slot=textarea]]:min-h-[120px]! max-md:[&_[data-slot=textarea]]:rounded-xl! max-md:[&_[data-slot=textarea]]:px-4! max-md:[&_[data-slot=textarea]]:py-3.5!\",\n            formDialogWidths[size],\n            className\n          )}\n        >\n          <div\n            data-slot=\"form-dialog-mobile-header\"\n            className=\"grid h-[76px] shrink-0 grid-cols-[1fr_auto_1fr] items-center gap-2 px-4 md:hidden\"\n          >\n            <button\n              type=\"button\"\n              aria-label=\"Cancel\"\n              disabled={pending}\n              onClick={() => onOpenChange(false)}\n              className=\"bg-secondary active:bg-border flex size-10 cursor-pointer items-center justify-center justify-self-start rounded-full outline-none disabled:opacity-50\"\n            >\n              <XIcon className=\"size-[22px]\" />\n            </button>\n            <span\n              aria-hidden=\"true\"\n              className=\"min-w-0 truncate px-1 text-center text-lg\"\n              title={typeof title === \"string\" ? title : undefined}\n            >\n              {title}\n            </span>\n            {/* Reminders confirms with a check circle; longer labels read as a\n              * capsule. Both are 44px FancyButtons so phone forms share the\n              * app's button shadows. */}\n            <FancyButton\n              variant={submitVariant === \"destructive\" ? \"destructive\" : \"primary\"}\n              iconOnly={circleConfirm}\n              type={formId ? \"submit\" : \"button\"}\n              form={formId}\n              disabled={submitDisabled}\n              aria-describedby={submitDescribedBy}\n              pending={pending}\n              onClick={formId ? undefined : submit}\n              aria-label={circleConfirm ? submitLabel : undefined}\n              className={cn(\n                \"h-11 justify-self-end rounded-full\",\n                circleConfirm ? \"size-11\" : \"px-[17px] text-base font-semibold\"\n              )}\n            >\n              {circleConfirm ? (\n                <FancyButtonIcon as={CheckIcon} className=\"size-6\" />\n              ) : (\n                submitLabel\n              )}\n            </FancyButton>\n          </div>\n          <div\n            data-slot=\"form-dialog-header\"\n            className=\"flex h-[49px] shrink-0 items-center gap-2 px-4 max-md:hidden\"\n          >\n            {icon ? (\n              <span className=\"flex size-4 shrink-0 items-center justify-center [&_svg]:size-4\">\n                {icon}\n              </span>\n            ) : null}\n            <DialogTitle className=\"truncate text-lg\" title={typeof title === \"string\" ? title : undefined}>\n              {title}\n            </DialogTitle>\n            <DialogClose\n              render={\n                <Button\n                  variant=\"ghost\"\n                  size=\"icon\"\n                  aria-label=\"Close\"\n                  className=\"ml-auto text-muted-foreground\"\n                />\n              }\n            >\n              <XIcon className=\"size-4\" />\n              <span className=\"sr-only\">Close</span>\n            </DialogClose>\n          </div>\n          {tabs ? (\n            <div\n              data-slot=\"form-dialog-tabs\"\n              className=\"flex h-11 shrink-0 items-center gap-1 border-b px-4 max-md:overflow-x-auto\"\n            >\n              {tabs}\n            </div>\n          ) : null}\n          <div\n            data-slot=\"form-dialog-body\"\n            className=\"flex max-h-[calc(85svh-93px)] min-h-0 flex-col gap-4 overflow-y-auto overscroll-contain px-6 py-4 max-md:max-h-none max-md:flex-1 max-md:gap-6 max-md:px-4 max-md:pt-2 max-md:pb-[max(24px,env(safe-area-inset-bottom))]\"\n          >\n            {description ? (\n              <DialogDescription\n                className={\n                  showDescription ? \"text-xs text-muted-foreground\" : \"sr-only\"\n                }\n              >\n                {description}\n              </DialogDescription>\n            ) : null}\n            {children}\n          </div>\n          {footer === null ? null : (\n            <div\n              data-slot=\"form-dialog-footer\"\n              className={cn(\n                \"flex h-11 shrink-0 items-center justify-end gap-2 border-t px-3\",\n                // Phones: header actions replace the default footer; a custom\n                // footer keeps its own actions, pinned above the home indicator.\n                footer === undefined\n                  ? \"max-md:hidden\"\n                  : \"max-md:h-auto max-md:min-h-11 max-md:flex-col max-md:items-stretch max-md:gap-2 max-md:border-t-0 max-md:bg-popover max-md:px-4 max-md:py-2 max-md:pb-[max(env(safe-area-inset-bottom),16px)] max-md:[&_[data-slot=fancy-button]]:h-[50px]! max-md:[&_[data-slot=fancy-button]]:w-full! max-md:[&_[data-slot=fancy-button]]:rounded-full! max-md:[&_[data-slot=fancy-button]]:text-base! max-md:[&_[data-slot=fancy-button]]:font-semibold\"\n              )}\n            >\n              {footer !== undefined ? (\n                footer\n              ) : (\n                <>\n                  {createMore ? (\n                    <label className=\"mr-2 flex cursor-pointer items-center gap-2 text-xs font-medium text-muted-foreground select-none\">\n                      Create more\n                      <Switch\n                        size=\"sm\"\n                        checked={createMore.checked}\n                        onCheckedChange={createMore.onCheckedChange}\n                      />\n                    </label>\n                  ) : null}\n                  <FancyButton\n                    disabled={pending}\n                    onClick={() => onOpenChange(false)}\n                  >\n                    Cancel\n                    <Kbd>Esc</Kbd>\n                  </FancyButton>\n                  <FancyButton\n                    variant={\n                      submitVariant === \"destructive\" ? \"destructive\" : \"primary\"\n                    }\n                    type={formId ? \"submit\" : \"button\"}\n                    form={formId}\n                    disabled={submitDisabled}\n                    aria-describedby={submitDescribedBy}\n                    pending={pending}\n                    onClick={formId ? undefined : submit}\n                  >\n                    {submitLabel}\n                    {submitKbd && !pending ? (\n                      <Kbd\n                        className={\n                          submitVariant === \"destructive\"\n                            ? \"bg-destructive/15 text-destructive\"\n                            : \"bg-primary-foreground/20 text-primary-foreground\"\n                        }\n                      >\n                        ⌘↵\n                      </Kbd>\n                    ) : null}\n                  </FancyButton>\n                </>\n              )}\n            </div>\n          )}\n        </DialogPrimitive.Popup>\n      </DialogPortal>\n    </Dialog>\n  )\n}\n\n// --- FormField ---\n// Desktop: 12px/500 subtle label (\" (optional)\" suffix), 6px gap, control,\n// optional 12px muted hint. Phones: iOS form section — 15px/600 muted header\n// indented 16px (x=32 on screen), 8px above the control, 13px hint below\n// (Grok \"Schedule\", Reminders \"Date & Time\").\n\nfunction FormField({\n  label,\n  optional = false,\n  hint,\n  htmlFor,\n  children,\n}: {\n  label: React.ReactNode\n  optional?: boolean\n  hint?: React.ReactNode\n  htmlFor?: string\n  children: React.ReactNode\n}) {\n  return (\n    <div data-slot=\"form-field\" className=\"flex min-w-0 flex-col gap-1.5 max-md:gap-2\">\n      <Label htmlFor={htmlFor} className=\"text-subtle-foreground max-md:px-4 max-md:text-sm max-md:font-semibold max-md:text-muted-foreground\">\n        <span className=\"truncate\" title={typeof label === \"string\" ? (label + (optional ? \" (optional)\" : \"\")) : undefined}>\n          {label}\n          {optional ? \" (optional)\" : null}\n        </span>\n      </Label>\n      {children}\n      {hint ? <p className=\"text-xs text-muted-foreground max-md:px-4\">{hint}</p> : null}\n    </div>\n  )\n}\n\n// --- FormRow ---\n\nfunction FormRow({ children }: { children: React.ReactNode }) {\n  return (\n    <div data-slot=\"form-row\" className=\"grid grid-cols-2 gap-4 max-md:grid-cols-1\">\n      {children}\n    </div>\n  )\n}\n\nexport { FormDialog, FormField, FormRow }\n",
      "type": "registry:ui"
    }
  ],
  "docs": "Install @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.",
  "type": "registry:ui"
}