{"version":3,"file":"CardWidgetAction.mjs","sources":["../../src/CardWidget/CardWidgetAction.tsx"],"sourcesContent":["import { isValidElement, type MouseEventHandler, type ReactNode } from 'react';\n\nimport Button from '../Button';\nimport type { ButtonPriority, ButtonProps } from '../Button/Button.types';\nimport IconButton, { type IconButtonProps } from '../IconButton';\n\n/** The action slot a `CardWidgetActionButton` was passed to, which is what determines its emphasis. */\nexport type CardWidgetActionSlot = 'action' | 'actionSecondary';\n\ntype CardWidgetActionButtonBase = Pick<ButtonProps, 'href' | 'target' | 'disabled'> & {\n  /**\n   * `HTMLElement` rather than a narrower target, because an action is a `button` or an `a`\n   * depending on whether `href` is set.\n   * @default undefined\n   */\n  onClick?: MouseEventHandler<HTMLElement>;\n\n  /**\n   * Rendered beside `label`, on the side set by `iconPosition`, or as the only content of an\n   * unlabelled action. Size it yourself — 16px is expected in both cases.\n   * @default undefined\n   */\n  icon?: ReactNode;\n  'data-testid'?: string;\n};\n\n/** An action with a visible label, rendered as a small button. */\ntype CardWidgetLabelledActionButton = CardWidgetActionButtonBase & {\n  /** Non-rendering values are excluded, so an unlabelled action has to meet the icon-only rules. */\n  label: Exclude<ButtonProps['children'], null | undefined | boolean>;\n  /**\n   * Side of the label the `icon` is rendered on.\n   * @default 'end'\n   */\n  iconPosition?: 'start' | 'end';\n  /** @default undefined */\n  'aria-label'?: string;\n};\n\n/**\n * An action with no visible label, rendered as a 32px icon button. The icon is\n * the only thing on screen and `aria-label` is the only accessible name, so\n * both are required.\n */\ntype CardWidgetIconActionButton = CardWidgetActionButtonBase & {\n  label?: never;\n  /** An unlabelled action has no label for the icon to sit beside. */\n  iconPosition?: never;\n  icon: ReactNode;\n  'aria-label': string;\n};\n\n/**\n * An action the `CardWidget` header renders as a button. With a `label` it is a small `Button`; without\n * one, a 32px `IconButton`, which requires both an `icon` and an `aria-label`. Emphasis is not\n * part of the object — it comes from the slot the action is passed to.\n */\nexport type CardWidgetActionButton = CardWidgetLabelledActionButton | CardWidgetIconActionButton;\n\nconst BUTTON_PRIORITY = {\n  action: 'secondary',\n  actionSecondary: 'secondary-neutral',\n} as const satisfies Record<CardWidgetActionSlot, ButtonPriority>;\n\nconst ICON_BUTTON_PRIORITY = {\n  action: 'secondary',\n  actionSecondary: 'tertiary',\n} as const satisfies Record<CardWidgetActionSlot, IconButtonProps['priority']>;\n\n/**\n * Tells a `CardWidgetActionButton` object apart from any other node passed to an action slot.\n * Every button action has a `label` or an `icon`, which no React element, portal or array does.\n */\nexport const isCardWidgetActionButton = (value: unknown): value is CardWidgetActionButton =>\n  typeof value === 'object' &&\n  value !== null &&\n  !isValidElement(value) &&\n  !Array.isArray(value) &&\n  ('label' in value || 'icon' in value);\n\n/** Renders a `CardWidgetActionButton` at the emphasis of the slot it was passed to. */\nconst CardWidgetAction = function CardWidgetAction({\n  action: {\n    label,\n    icon,\n    iconPosition = 'end',\n    'aria-label': ariaLabel,\n    'data-testid': dataTestId,\n    href,\n    target,\n    onClick,\n    disabled,\n  },\n  slot,\n}: {\n  action: CardWidgetActionButton;\n  slot: CardWidgetActionSlot;\n}) {\n  if (label === undefined) {\n    return (\n      <IconButton\n        size={32}\n        priority={ICON_BUTTON_PRIORITY[slot]}\n        aria-label={ariaLabel}\n        href={href}\n        target={target}\n        disabled={disabled}\n        data-testid={dataTestId}\n        onClick={onClick}\n      >\n        {icon}\n      </IconButton>\n    );\n  }\n\n  const addon = icon ? ({ type: 'icon', value: icon } as const) : undefined;\n\n  return (\n    <Button\n      v2\n      size=\"sm\"\n      priority={BUTTON_PRIORITY[slot]}\n      aria-label={ariaLabel}\n      href={href}\n      target={target}\n      disabled={disabled}\n      addonStart={iconPosition === 'start' ? addon : undefined}\n      addonEnd={iconPosition !== 'start' ? addon : undefined}\n      // The v2 resolver drops `Button`'s own `button` default, so an action would submit a form.\n      type=\"button\"\n      data-testid={dataTestId}\n      onClick={onClick}\n    >\n      {label}\n    </Button>\n  );\n};\n\nexport default CardWidgetAction;\n"],"names":["BUTTON_PRIORITY","action","actionSecondary","ICON_BUTTON_PRIORITY","isCardWidgetActionButton","value","isValidElement","Array","isArray","CardWidgetAction","label","icon","iconPosition","ariaLabel","dataTestId","href","target","onClick","disabled","slot","undefined","_jsx","IconButton","size","priority","children","addon","type","Button","v2","addonStart","addonEnd"],"mappings":";;;;;AA2DA,MAAMA,eAAe,GAAG;AACtBC,EAAAA,MAAM,EAAE,WAAW;AACnBC,EAAAA,eAAe,EAAE;CAC8C;AAEjE,MAAMC,oBAAoB,GAAG;AAC3BF,EAAAA,MAAM,EAAE,WAAW;AACnBC,EAAAA,eAAe,EAAE;CAC2D;AAE9E;;;AAGG;AACI,MAAME,wBAAwB,GAAIC,KAAc,IACrD,OAAOA,KAAK,KAAK,QAAQ,IACzBA,KAAK,KAAK,IAAI,IACd,eAACC,cAAc,CAACD,KAAK,CAAC,IACtB,CAACE,KAAK,CAACC,OAAO,CAACH,KAAK,CAAC,KACpB,OAAO,IAAIA,KAAK,IAAI,MAAM,IAAIA,KAAK;AAEtC;AACA,MAAMI,gBAAgB,GAAG,SAASA,gBAAgBA,CAAC;AACjDR,EAAAA,MAAM,EAAE;IACNS,KAAK;IACLC,IAAI;AACJC,IAAAA,YAAY,GAAG,KAAK;AACpB,IAAA,YAAY,EAAEC,SAAS;AACvB,IAAA,aAAa,EAAEC,UAAU;IACzBC,IAAI;IACJC,MAAM;IACNC,OAAO;AACPC,IAAAA;GACD;AACDC,EAAAA;AAAI,CAIL,EAAA;EACC,IAAIT,KAAK,KAAKU,SAAS,EAAE;IACvB,oBACEC,GAAA,CAACC,UAAU,EAAA;AACTC,MAAAA,IAAI,EAAE,EAAG;AACTC,MAAAA,QAAQ,EAAErB,oBAAoB,CAACgB,IAAI,CAAE;AACrC,MAAA,YAAA,EAAYN,SAAU;AACtBE,MAAAA,IAAI,EAAEA,IAAK;AACXC,MAAAA,MAAM,EAAEA,MAAO;AACfE,MAAAA,QAAQ,EAAEA,QAAS;AACnB,MAAA,aAAA,EAAaJ,UAAW;AACxBG,MAA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