import { isValidElement, type MouseEventHandler, type ReactNode } from 'react'; import Button from '../Button'; import type { ButtonPriority, ButtonProps } from '../Button/Button.types'; import IconButton, { type IconButtonProps } from '../IconButton'; /** The action slot a `CardWidgetActionButton` was passed to, which is what determines its emphasis. */ export type CardWidgetActionSlot = 'action' | 'actionSecondary'; type CardWidgetActionButtonBase = Pick & { /** * `HTMLElement` rather than a narrower target, because an action is a `button` or an `a` * depending on whether `href` is set. * @default undefined */ onClick?: MouseEventHandler; /** * Rendered beside `label`, on the side set by `iconPosition`, or as the only content of an * unlabelled action. Size it yourself — 16px is expected in both cases. * @default undefined */ icon?: ReactNode; 'data-testid'?: string; }; /** An action with a visible label, rendered as a small button. */ type CardWidgetLabelledActionButton = CardWidgetActionButtonBase & { /** Non-rendering values are excluded, so an unlabelled action has to meet the icon-only rules. */ label: Exclude; /** * Side of the label the `icon` is rendered on. * @default 'end' */ iconPosition?: 'start' | 'end'; /** @default undefined */ 'aria-label'?: string; }; /** * An action with no visible label, rendered as a 32px icon button. The icon is * the only thing on screen and `aria-label` is the only accessible name, so * both are required. */ type CardWidgetIconActionButton = CardWidgetActionButtonBase & { label?: never; /** An unlabelled action has no label for the icon to sit beside. */ iconPosition?: never; icon: ReactNode; 'aria-label': string; }; /** * An action the `CardWidget` header renders as a button. With a `label` it is a small `Button`; without * one, a 32px `IconButton`, which requires both an `icon` and an `aria-label`. Emphasis is not * part of the object — it comes from the slot the action is passed to. */ export type CardWidgetActionButton = CardWidgetLabelledActionButton | CardWidgetIconActionButton; const BUTTON_PRIORITY = { action: 'secondary', actionSecondary: 'secondary-neutral', } as const satisfies Record; const ICON_BUTTON_PRIORITY = { action: 'secondary', actionSecondary: 'tertiary', } as const satisfies Record; /** * Tells a `CardWidgetActionButton` object apart from any other node passed to an action slot. * Every button action has a `label` or an `icon`, which no React element, portal or array does. */ export const isCardWidgetActionButton = (value: unknown): value is CardWidgetActionButton => typeof value === 'object' && value !== null && !isValidElement(value) && !Array.isArray(value) && ('label' in value || 'icon' in value); /** Renders a `CardWidgetActionButton` at the emphasis of the slot it was passed to. */ const CardWidgetAction = function CardWidgetAction({ action: { label, icon, iconPosition = 'end', 'aria-label': ariaLabel, 'data-testid': dataTestId, href, target, onClick, disabled, }, slot, }: { action: CardWidgetActionButton; slot: CardWidgetActionSlot; }) { if (label === undefined) { return ( {icon} ); } const addon = icon ? ({ type: 'icon', value: icon } as const) : undefined; return ( ); }; export default CardWidgetAction;