Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

4.0 KB
Raw Blame
/**
 * Form-style picker shell: a full-width trigger that looks exactly like an Input/Select
 * (controlBase + the shared size tier, sm by default — leading icon, truncating label, trailing
 * chevron) with a portaled dropdown hanging under its left edge. It is the single source of the
 * "form-variant" look shared by the model picker, the workspace picker and the schedule's
 * session picker, so the three read identically and none re-hand-rolls the trigger. The model
 * and workspace pickers open a dialog instead of a menu, so they take the trigger alone
 * (FormPickerTrigger).
 *
 * It owns only the trigger + Dropdown wiring; the menu body is the caller's `children`
 * (a searchable list, …), and open/close state stays with the caller (the pickers drive it
 * and close on pick).
 */
import type { ReactNode } from "react";
import { ChevronDown } from "../../icons/marks/marks";
import { Dropdown } from "../../overlays/dropdown/dropdown";
import { controlBase } from "../field/field";
import { sizeClass } from "../input/input";
import type { ControlSize } from "../input/input";

/** The trigger's own props: everything FormPicker draws, minus the menu it hangs. */
export interface FormPickerTriggerProps {
  /** Leading visual (provider logo / folder icon), sized by the caller; omitted when there's none. */
  leading?: ReactNode;
  /** The selected value's display, or a placeholder (pair with `muted`). */
  label: ReactNode;
  /** Extra classes on the label span (e.g. `font-mono` for a path value). */
  labelClassName?: string;
  /** Grays the label as a placeholder (nothing selected yet). */
  muted?: boolean;
  /**
   * The tooltip, only when it says more than the visible value (the field name with the value in
   * it, e.g. "Model: GPT-6"). Omit it when it would repeat the field label above the control.
   */
  title?: string;
  /** `title` holds what the label leaves out (a full path, an id): it shows over the label in full. */
  titleExtra?: boolean;
  ariaLabel: string;
  ariaHaspopup?: "listbox" | "dialog";
  /** Whether the thing it opens is open (announced as `aria-expanded`). */
  expanded: boolean;
  disabled?: boolean;
  /** Same size tier as Input/Select; sm is the form rung every picker sits at today. */
  size?: ControlSize;
  onClick: () => void;
}

/**
 * The form-variant trigger on its own, for a picker that opens a dialog rather than a hanging
 * menu (the model picker, the workspace finder): the same button FormPicker draws, so both
 * kinds of picker read identically in a form.
 */
export function FormPickerTrigger({
  leading,
  label,
  labelClassName = "",
  muted = false,
  title,
  titleExtra = false,
  ariaLabel,
  ariaHaspopup = "listbox",
  expanded,
  disabled = false,
  size = "sm",
  onClick,
}: FormPickerTriggerProps) {
  return (
    <button
      type="button"
      data-tooltip={title}
      data-tooltip-extra={titleExtra || undefined}
      aria-label={ariaLabel}
      aria-haspopup={ariaHaspopup}
      aria-expanded={expanded}
      disabled={disabled}
      onClick={onClick}
      className={`flex w-full items-center gap-2 text-left ${controlBase} ${sizeClass[size]} disabled:cursor-not-allowed disabled:opacity-60`}
    >
      {leading}
      <span
        className={`min-w-0 flex-1 truncate ${muted ? "text-fg-subtle" : ""} ${labelClassName}`}
      >
        {label}
      </span>
      <ChevronDown className="text-fg-subtle" />
    </button>
  );
}

export function FormPicker({
  open,
  setOpen,
  menuClass,
  children,
  ...trigger
}: Omit<FormPickerTriggerProps, "expanded" | "onClick"> & {
  open: boolean;
  setOpen: (open: boolean) => void;
  /** Width / origin classes for the dropdown panel (placement itself is measured from the trigger). */
  menuClass: string;
  /** The dropdown menu body. */
  children: ReactNode;
}) {
  return (
    <Dropdown
      open={open}
      setOpen={setOpen}
      menuClass={menuClass}
      portal={{ direction: "down", align: "left" }}
      button={<FormPickerTrigger {...trigger} expanded={open} onClick={() => setOpen(!open)} />}
    >
      {children}
    </Dropdown>
  );
}