import type { ButtonHTMLAttributes, CSSProperties, ReactNode } from "react"; import { useEffect, useState } from "react"; import { cx } from "../../utils/cx.utils"; import { Icon } from "../icons/icon"; import type { IconName } from "../icons/icons"; import "./button.component.css"; /** * Icon slot props shared by the text button variants ({@link Button}, * {@link Button.Outlined}, {@link Button.Naked}). * * Only one of `iconLeft` or `iconRight` may be supplied at a time; the type * enforces this so passing both is a compile-time error. */ export type ButtonIconSlotProps = | { iconLeft?: IconName; iconRight?: never } | { iconLeft?: never; iconRight?: IconName }; /** Sizes accepted by every button variant. Defaults to `"md"`. */ export type ButtonSize = "sm" | "md" | "xl"; /** Props shared by every button variant, text or icon-only. */ type ButtonSizeProps = { /** Size of the button. Defaults to `"md"`. */ size?: ButtonSize; }; /** Props shared by every button variant, for the optional disabled `mono` rendering. */ type ButtonMonoProps = { /** * When true and the button is `disabled`, renders it in black and white * with a diagonal black stripe overlay instead of the default grey * disabled look. Has no effect when the button is not disabled. */ mono?: boolean; }; /** Props shared by every text button variant. */ type ButtonVariantProps = Omit, "children"> & ButtonIconSlotProps & ButtonSizeProps & ButtonMonoProps & { children: ReactNode; }; /** Props accepted by {@link Button} and {@link Button.Outlined}. */ export type ButtonProps = ButtonVariantProps & { /** When true, the button stretches to fill the width of its container. */ fullWidth?: boolean; /** * When true, shows an hourglass icon in front of the label that flips * 180 degrees every `flipIntervalMs`, replacing any `iconLeft`/`iconRight`. * The button is disabled by default while loading, unless `disabled` is * explicitly set. */ loading?: boolean; /** Milliseconds between each 180 degree icon flip while `loading`. Defaults to 2000. */ flipIntervalMs?: number; }; /** Props shared by every icon-only button variant. */ export interface IconButtonProps extends Omit, "children">, ButtonSizeProps, ButtonMonoProps { /** Name of the icon to render, from the {@link IconName} registry. */ icon: IconName; /** Accessible label; required since icon-only buttons have no text content. */ "aria-label": string; } /** Icon pixel size per {@link ButtonSize}. */ const ICON_SIZES: Record = { sm: 14, md: 16, xl: 24, }; const DEFAULT_LOADING_FLIP_INTERVAL_MS = 2000; function renderContent({ iconLeft, iconRight, iconSize, iconClassName, iconStyle, children, }: { iconLeft?: IconName; iconRight?: IconName; iconSize: number; iconClassName?: string; iconStyle?: CSSProperties; children: ReactNode; }) { const iconClasses = cx("eink-button__icon", [iconClassName ?? "", !!iconClassName]); return ( <> {iconLeft ? (