дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 01:01:17 +02:00
120 рядки
3.7 KiB
TypeScript
120 рядки
3.7 KiB
TypeScript
import { forwardRef, type ButtonHTMLAttributes, type ReactNode } from "react";
|
|
import clsx from "clsx";
|
|
import { getAction, executeAction, useRunningActions } from "@/platform/actions";
|
|
import { shortcutLabel } from "@/platform/keymap";
|
|
import { Icon, type IconName } from "./icons";
|
|
import { Spinner } from "./Spinner";
|
|
import { tooltipAttrs } from "./Tooltip";
|
|
import styles from "./Button.module.css";
|
|
import { useGivenTx } from "@/platform/i18n";
|
|
|
|
export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
|
|
variant?: "primary" | "secondary" | "ghost";
|
|
size?: "normal" | "small";
|
|
icon?: IconName | (string & {});
|
|
children?: ReactNode;
|
|
}
|
|
|
|
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(function Button(
|
|
{ variant = "secondary", size = "normal", icon, className, children, type = "button", title, ...rest },
|
|
ref,
|
|
) {
|
|
const tx = useGivenTx();
|
|
return (
|
|
<button
|
|
ref={ref}
|
|
type={type}
|
|
className={clsx(styles.button, styles[variant], size === "small" && styles.small, className)}
|
|
title={tx(title)}
|
|
{...rest}
|
|
>
|
|
{icon && <Icon name={icon} size={14} />}
|
|
{typeof children === "string" ? tx(children) : children}
|
|
</button>
|
|
);
|
|
});
|
|
|
|
export interface IconButtonProps extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, "title"> {
|
|
icon: IconName | (string & {});
|
|
/** Accessible label and tooltip text. Defaults to the action's title. */
|
|
title?: string;
|
|
/** Run this action on click; supplies title + shortcut. */
|
|
action?: string;
|
|
actionArg?: unknown;
|
|
/** Explicit shortcut label shown in the tooltip (defaults to the action's binding). */
|
|
shortcut?: string;
|
|
/** Toggle state (renders `aria-pressed`). */
|
|
pressed?: boolean;
|
|
/** Button dimensions; icons keep their 16px size unless iconSize is set. */
|
|
size?: "small" | "normal" | "large";
|
|
iconSize?: number;
|
|
tone?: "default" | "danger" | "accent";
|
|
}
|
|
|
|
export const IconButton = forwardRef<HTMLButtonElement, IconButtonProps>(function IconButton(
|
|
{
|
|
icon,
|
|
title,
|
|
action,
|
|
actionArg,
|
|
shortcut,
|
|
pressed,
|
|
size = "normal",
|
|
iconSize,
|
|
tone = "default",
|
|
className,
|
|
onClick,
|
|
disabled,
|
|
type = "button",
|
|
"aria-label": ariaLabel,
|
|
...rest
|
|
},
|
|
ref,
|
|
) {
|
|
const tx = useGivenTx();
|
|
const a = action ? getAction(action) : undefined;
|
|
const label = tx(title) ?? a?.title ?? action ?? icon;
|
|
const keys = shortcut ?? (action ? shortcutLabel(action) : undefined);
|
|
const busy = useRunningActions((s) => (action ? s.running.has(action) : false));
|
|
const glyphSize = iconSize ?? 16;
|
|
const close = icon === "close";
|
|
return (
|
|
<button
|
|
ref={ref}
|
|
type={type}
|
|
aria-label={tx(ariaLabel) ?? label}
|
|
aria-pressed={pressed}
|
|
aria-keyshortcuts={keys}
|
|
aria-busy={busy || undefined}
|
|
// Not `disabled`: a disabled button drops keyboard focus while the action runs.
|
|
aria-disabled={busy || undefined}
|
|
disabled={disabled}
|
|
className={clsx(
|
|
styles.iconButton,
|
|
styles[`is-${size}`],
|
|
close && styles.closeButton,
|
|
tone !== "default" && styles[`tone-${tone}`],
|
|
className,
|
|
)}
|
|
onClick={(e) => {
|
|
if (busy) return;
|
|
onClick?.(e);
|
|
if (!e.defaultPrevented && action) void executeAction(action, actionArg);
|
|
}}
|
|
{...tooltipAttrs(label, keys, true)}
|
|
{...rest}
|
|
>
|
|
{busy ? (
|
|
<Spinner size={glyphSize} />
|
|
) : close ? (
|
|
<>
|
|
<Icon name="project-tab-close" size={16} className={styles.closeIdle} />
|
|
<Icon name="project-tab-close-hovered" size={16} className={styles.closeHovered} />
|
|
</>
|
|
) : (
|
|
<Icon name={icon} size={glyphSize} />
|
|
)}
|
|
</button>
|
|
);
|
|
});
|