{"version":3,"file":"UnstyledButton.cjs","names":[],"sources":["../../../src/components/Button/UnstyledButton.tsx"],"sourcesContent":["/*\nCopyright 2023 New Vector Ltd.\n\nSPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial\nPlease see LICENSE files in the repository root for full details.\n*/\n\nimport React, {\n  type PropsWithChildren,\n  forwardRef,\n  type ForwardedRef,\n  type Ref,\n} from \"react\";\n\ninterface ButtonComponent {\n  // With the explicit `as` prop\n  <C extends React.ElementType>(\n    props: { as: C } & UnstyledButtonPropsFor<C>,\n  ): React.ReactElement;\n  // Without the explicit `as` prop, defaulting to a <button>\n  (props: UnstyledButtonPropsFor<\"button\">): React.ReactElement;\n}\n\ntype UnstyledButtonProps = PropsWithChildren<{\n  /**\n   * Note that disabled attribute is not added to buttons, so that disabled buttons are discoverable by keyboard.\n   * `aria-disabled` attribute is used to indicate button is disabled.\n   * Event handlers are not passed to disabled buttons (onClick, onSubmit, etc.)\n   */\n  disabled?: boolean;\n}>;\n\nexport type UnstyledButtonPropsFor<C extends React.ElementType> =\n  UnstyledButtonProps &\n    Omit<\n      React.ComponentPropsWithoutRef<C>,\n      keyof UnstyledButtonProps | \"as\"\n    > & {\n      ref?: React.Ref<React.ComponentRef<C>>;\n    };\n\n/**\n * Unstyled button component. Can be disabled and optionally rendered as an anchor.\n * Intended to be wrapped with styles and exported as new button type - eg `IconButton` or `Button`.\n *\n * Not to be used externally.\n */\nexport const UnstyledButton = forwardRef(function UnstyledButton<\n  C extends React.ElementType = \"button\",\n>(\n  {\n    as,\n    children,\n    className,\n    disabled,\n    ...props\n  }: UnstyledButtonPropsFor<C> & { as?: C },\n  ref: ForwardedRef<C>,\n): React.ReactElement {\n  const Component = as || (\"button\" as const);\n\n  const {\n    onClick,\n    onSubmit,\n    onPointerDown,\n    onPointerUp,\n    onKeyDown,\n    onKeyUp,\n    onKeyPress,\n    ...restProps\n  } = props;\n  const eventHandlers = disabled\n    ? {}\n    : {\n        onClick,\n        onSubmit,\n        onPointerDown,\n        onPointerUp,\n        onKeyDown,\n        onKeyUp,\n        onKeyPress,\n      };\n\n  return (\n    <Component\n      ref={ref as Ref<C>}\n      className={className}\n      // The elements roles should be set to button default, or link in the case of anchors.\n      // This should be overridable by props, say for example if you want to use a button as an option within a listbox.\n      // Hence it taking precedence over restProps.\n      role={as === \"a\" ? \"link\" : \"button\"}\n      tabIndex={0}\n      aria-disabled={disabled}\n      {...restProps}\n      {...eventHandlers}\n    >\n      {children}\n    </Component>\n  );\n}) as ButtonComponent;\n"],"mappings":";;;;;;;;;;;AA+CA,IAAa,kBAAA,GAAA,MAAA,YAA4B,SAAS,eAGhD,EACE,IACA,UACA,WACA,UACA,GAAG,SAEL,KACoB;CACpB,MAAM,YAAY,MAAO;CAEzB,MAAM,EACJ,SACA,UACA,eACA,aACA,WACA,SACA,YACA,GAAG,cACD;CACJ,MAAM,gBAAgB,WAClB,EAAE,GACF;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACD;AAEL,QACE,iBAAA,GAAA,kBAAA,KAAC,WAAD;EACO;EACM;EAIX,MAAM,OAAO,MAAM,SAAS;EAC5B,UAAU;EACV,iBAAe;EACf,GAAI;EACJ,GAAI;EAEH;EACS,CAAA;EAEd"}