Files
yaak-mountain-loop/packages/ui/src/components/Button.tsx
T
Gregory SchierandClaude Opus 5.5 c78732ecaf Polish Yaak Proxy's flow list, sidebar and header
Columns keep fixed widths, Path included, and the list scrolls sideways
with the header in step instead of dropping columns or capping Host.
Resize handles sit on every column boundary, a double-click resets one,
and a third click on a sorted header goes back to start time, with empty
values sorting lowest. The search box takes up to 44rem and wraps over
the list while focused, its completions as wide as it. The list/detail
handle fills the gutter. Native title tooltips become Yaak's tooltip,
shared buttons gain an opt-in for it, and folder and program rows in the
sidebar get Reveal in Finder and Copy path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 10:45:46 -07:00

147 lines
4.5 KiB
TypeScript

import type { Color } from "@yaakapp-internal/plugins";
import classNames from "classnames";
import type { HTMLAttributes, ReactNode } from "react";
import { forwardRef } from "react";
import { Icon } from "./Icon";
import { LoadingIcon } from "./LoadingIcon";
import { Tooltip } from "./Tooltip";
/** How long the pointer rests on a control before its tooltip shows, in milliseconds */
export const CONTROL_TOOLTIP_DELAY = 300;
type ButtonVariant = "border" | "solid" | "input";
type ButtonSize = "2xs" | "xs" | "sm" | "md" | "auto";
export type ButtonProps = Omit<HTMLAttributes<HTMLButtonElement>, "color" | "onChange"> & {
innerClassName?: string;
color?: Color | "custom" | "default";
tone?: Color | "default";
variant?: ButtonVariant;
isLoading?: boolean;
size?: ButtonSize;
justify?: "start" | "center";
type?: "button" | "submit";
/** Submit a form the button is not inside, by that form's id */
form?: string;
forDropdown?: boolean;
disabled?: boolean;
title?: string;
/** Shows `title` in Yaak's tooltip, quickly, rather than the browser's */
tooltip?: boolean;
leftSlot?: ReactNode;
rightSlot?: ReactNode;
};
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(function Button(
{
isLoading,
className,
innerClassName,
children,
color,
tone,
forDropdown,
type = "button",
justify = "center",
size = "md",
variant = "solid",
leftSlot,
rightSlot,
disabled,
title,
tooltip,
onClick,
...props
}: ButtonProps,
ref,
) {
const resolvedColor = color ?? tone ?? "default";
const isDisabled = disabled || isLoading;
const withTooltip = tooltip === true && title != null && title !== "";
const button = (
<button
ref={ref}
type={type}
className={classNames(
className,
"x-theme-button",
`x-theme-button--${variant}`,
`x-theme-button--${variant}--${resolvedColor}`,
"border",
"max-w-full min-w-0",
"hocus:opacity-100",
"whitespace-nowrap outline-hidden",
"shrink-0 flex items-center",
"outline-0",
isDisabled ? "pointer-events-none opacity-disabled" : "pointer-events-auto",
justify === "start" && "justify-start",
justify === "center" && "justify-center",
size === "md" && "h-md px-3 rounded-md",
size === "sm" && "h-sm px-2.5 rounded-md",
size === "xs" && "h-xs px-2 text-sm rounded-md",
size === "2xs" && "h-2xs px-2 text-xs rounded-sm",
size === "auto" && "px-3 py-2 rounded-md",
variant === "solid" && "border-transparent",
variant === "solid" &&
resolvedColor === "custom" &&
"focus-visible:outline-2 outline-border-focus",
variant === "solid" &&
resolvedColor !== "custom" &&
"text-text enabled:hocus:text-text enabled:hocus:bg-surface-highlight outline-border-subtle",
variant === "solid" &&
resolvedColor !== "custom" &&
resolvedColor !== "default" &&
"bg-surface",
variant === "border" && "border",
variant === "border" &&
resolvedColor !== "custom" &&
"border-border-subtle text-text-subtle enabled:hocus:border-border " +
"enabled:hocus:bg-surface-highlight enabled:hocus:text-text outline-border-subtler",
// Chrome of a form input rather than a button: no hover state, and colors resolve from the
// surrounding x-theme-input rather than a button theme.
variant === "input" && "border-border text-text",
)}
disabled={isDisabled}
onClick={onClick}
onDoubleClick={(e) => {
e.stopPropagation();
}}
title={withTooltip ? undefined : title}
aria-label={withTooltip ? title : undefined}
{...props}
>
{isLoading ? (
<LoadingIcon size={size === "auto" ? "md" : size} className="mr-1" />
) : leftSlot ? (
<div className="mr-2">{leftSlot}</div>
) : null}
<div
className={classNames(
"truncate w-full",
justify === "start" ? "text-left" : "text-center",
innerClassName,
)}
>
{children}
</div>
{rightSlot && <div className="ml-1">{rightSlot}</div>}
{forDropdown && (
<Icon
icon="chevron_down"
size={size === "auto" ? "md" : size}
className="ml-1 -mr-1 relative top-[0.1em]"
/>
)}
</button>
);
return withTooltip ? (
<Tooltip wrap content={title} delay={CONTROL_TOOLTIP_DELAY}>
{button}
</Tooltip>
) : (
button
);
});