{"version":3,"file":"LegacyButton.mjs","sources":["../../src/button/LegacyButton.tsx"],"sourcesContent":["import { clsx } from 'clsx';\nimport { ElementType, forwardRef, MouseEvent } from 'react';\nimport { useIntl } from 'react-intl';\n\nimport {\n  Size,\n  ControlType,\n  Priority,\n  ControlTypeAccent,\n  ControlTypeNegative,\n  ControlTypePositive,\n  PriorityPrimary,\n  PrioritySecondary,\n  PriorityTertiary,\n  SizeExtraSmall,\n  SizeSmall,\n  SizeMedium,\n  SizeLarge,\n} from '../common';\nimport ProcessIndicator from '../processIndicator';\n\nimport messages from '../i18n/commonMessages/Button.messages';\nimport { typeClassMap, priorityClassMap } from './classMap';\nimport { establishNewPriority, establishNewType, logDeprecationNotices } from './legacyUtils';\nimport { ButtonReferenceType } from './Button.types';\n\n/** @deprecated */\ntype DeprecatedTypes = 'primary' | 'pay' | 'secondary' | 'danger' | 'link';\n\n/** @deprecated */\ntype DeprecatedSizes = SizeExtraSmall;\n\n/**\n * @deprecated Use new button instead of these APIs.\n * @example\n * ```\n * import { Button, ButtonProps } from '@transferwise/components';\n *\n * <Button v2>...</Button>\n * ```\n */\nexport type CommonProps = {\n  v2?: false;\n\n  /** Makes the button take up the full width of its container */\n  block?: boolean;\n\n  /**\n   * The `target` attribute for HTML anchor.\n   * If set to `_blank`, `rel=\"noopener noreferrer\"` is automatically added to the rendered node.\n   */\n  target?: string;\n  /**\n   * Toggles the disabled state\n   * @default false\n   */\n\n  disabled?: boolean;\n  /**\n   * Toggles the loading state\n   * @default false\n   */\n  loading?: boolean;\n  type?: ControlTypeAccent | ControlTypeNegative | ControlTypePositive | DeprecatedTypes | null;\n  priority?: PriorityPrimary | PrioritySecondary | PriorityTertiary | null;\n  size?: SizeSmall | SizeMedium | SizeLarge | DeprecatedSizes;\n  htmlType?: 'submit' | 'reset' | 'button';\n};\n\n/**\n * @deprecated Use new button instead of APIs.\n * @example\n * ```\n * import { Button, ButtonProps } from '@transferwise/components';\n *\n * <Button v2>...</Button>\n * ```\n */\nexport type ButtonProps = CommonProps &\n  Omit<React.ComponentPropsWithRef<'button'>, 'type'> & {\n    as?: 'button';\n  };\n\ntype AnchorProps = CommonProps &\n  Omit<React.ComponentPropsWithRef<'a'>, 'type'> & {\n    as?: 'a';\n  };\n\n/**\n * @deprecated Use new button instead of APIs.\n * @example\n * ```\n * import { Button, ButtonProps } from '@transferwise/components';\n *\n * <Button v2>...</Button>\n * ```\n */\nexport type LegacyButtonProps = ButtonProps | AnchorProps;\n\n/**\n * @deprecated Use new Button component via `<Button v2 .. />` and new props.\n */\nconst LegacyButton = forwardRef<ButtonReferenceType, LegacyButtonProps>(\n  (\n    {\n      as: component = 'button',\n      block = false,\n      children,\n      className,\n      disabled,\n      loading = false,\n      priority = Priority.PRIMARY,\n      size = Size.MEDIUM,\n      type = ControlType.ACCENT,\n      onClick,\n      ...rest\n    }: LegacyButtonProps,\n    ref,\n  ) => {\n    const intl = useIntl();\n\n    logDeprecationNotices({ size, type });\n\n    const newType = establishNewType(type);\n    const newPriority = establishNewPriority(priority, type);\n\n    const classes = clsx(\n      `btn btn-${size}`,\n      `np-btn np-btn-${size}`,\n      {\n        'btn-loading': loading,\n        'btn-block np-btn-block': block,\n        disabled,\n      },\n      // @ts-expect-error fix when refactor `typeClassMap` to TypeScript\n      typeClassMap[newType],\n      // @ts-expect-error fix when refactor `typeClassMap` to TypeScript\n      priorityClassMap[newPriority],\n      className,\n    );\n\n    function processIndicatorSize() {\n      return ['sm', 'xs'].includes(size) ? 'xxs' : 'xs';\n    }\n\n    const Element = (component as ElementType) ?? 'button';\n    let props;\n\n    if (Element === 'button') {\n      const { htmlType = 'button', ...restProps } = rest as ButtonProps;\n      props = {\n        ...restProps,\n        disabled,\n        'aria-disabled': loading,\n        type: htmlType,\n      };\n    } else {\n      props = {\n        ...rest,\n        'aria-disabled': loading,\n      } as AnchorProps;\n    }\n\n    /**\n     * Ensures that the button cannot be activated in loading or disabled mode,\n     * when `aria-disabled` might be used over the `disabled` HTML attribute\n     */\n    const handleClick =\n      (handler: LegacyButtonProps['onClick']) =>\n      (event: MouseEvent<HTMLButtonElement> & MouseEvent<HTMLAnchorElement>) => {\n        if (disabled || loading) {\n          event.preventDefault();\n        } else if (typeof handler === 'function') {\n          handler(event);\n        }\n      };\n\n    return (\n      <Element\n        ref={ref as React.Ref<ButtonReferenceType>}\n        className={classes}\n        onClick={handleClick(onClick)}\n        {...props}\n        aria-live={loading ? 'polite' : 'off'}\n        aria-busy={loading}\n        aria-label={loading ? intl.formatMessage(messages.loadingAriaLabel) : rest['aria-label']}\n      >\n        {children}\n        {loading && (\n          <ProcessIndicator\n            size={processIndicatorSize()}\n            className=\"btn-loader\"\n            data-testid=\"ButtonProgressIndicator\"\n          />\n        )}\n      </Element>\n    );\n  },\n);\n\nexport default LegacyButton;\n"],"names":["LegacyButton","forwardRef","as","component","block","children","className","disabled","loading","priority","Priority","PRIMARY","size","Size","MEDIUM","type","ControlType","ACCENT","onClick","rest","ref","intl","useIntl","logDeprecationNotices","newType","establishNewType","newPriority","establishNewPriority","classes","clsx","typeClassMap","priorityClassMap","processIndicatorSize","includes","Element","props","htmlType","restProps","handleClick","handler","event","preventDefault","_jsxs","formatMessage","messages","loadingAriaLabel","_jsx","ProcessIndicator"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsGA,MAAMA,YAAY,gBAAGC,UAAU,CAC7B,CACE;EACEC,EAAE,EAAEC,SAAS,GAAG,QAAQ;AACxBC,EAAAA,KAAK,GAAG,KAAK;EACbC,QAAQ;EACRC,SAAS;EACTC,QAAQ;AACRC,EAAAA,OAAO,GAAG,KAAK;EACfC,QAAQ,GAAGC,QAAQ,CAACC,OAAO;EAC3BC,IAAI,GAAGC,IAAI,CAACC,MAAM;EAClBC,IAAI,GAAGC,WAAW,CAACC,MAAM;EACzBC,OAAO;EACP,GAAGC;AAAI,CACW,EACpBC,GAAG,KACD;AACF,EAAA,MAAMC,IAAI,GAAGC,OAAO,EAAE;AAEtBC,EAAAA,qBA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