{"version":3,"file":"Button.mjs","sources":["../../../../../../../../../web/src/ui/components/atoms/Button.tsx"],"sourcesContent":["import classNames from 'classnames';\nimport React, { KeyboardEventHandler, ReactNode } from 'react';\n\nimport styles from './Button.module.css';\nimport { CircularProgress } from './CircularProgress';\n\nexport type ButtonProps = {\n  variant: 'primary' | 'secondary' | 'outline' | 'ghost' | 'destructive';\n  children: ReactNode;\n\n  /**\n   * Button is full width and content is centered\n   * @default true\n   */\n  block?: boolean;\n\n  icon?: ReactNode;\n\n  /** If true, display a spinner and disables the button */\n  loading?: boolean;\n  disabled?: boolean;\n\n  /**\n   * Unlike HTML <button> we deliberately set the default to button to\n   * avoid bugs where buttons inadvertently submit forms\n   * @default 'button'\n   **/\n  type?: 'button' | 'submit' | 'reset';\n  onClick?: () => void;\n  onKeyDown?: KeyboardEventHandler<HTMLElement>;\n  id?: string;\n};\n\nexport function buttonClassNames({ variant, block = true }: Pick<ButtonProps, 'variant' | 'block'>) {\n  return classNames(styles.button, styles[variant], {\n    [styles.block]: block,\n  });\n}\n\n// Base renderer for both <a> and <button> based <Button>s\nconst baseButton = (props: ButtonProps & { as?: string }) => {\n  const { as, children, icon, onClick, loading, disabled = loading, block, variant, ...otherProps } = props;\n  const Element = as as 'button';\n\n  let buttonContent = (\n    <>\n      {icon}\n      <span>{children}</span>\n    </>\n  );\n\n  if (loading) {\n    buttonContent = <CircularProgress size={20} />;\n  }\n\n  return (\n    <Element className={buttonClassNames(props)} onClick={onClick} disabled={disabled} {...otherProps}>\n      {buttonContent}\n    </Element>\n  );\n};\n\nconst Button = (props: ButtonProps) =>\n  baseButton({\n    type: 'button',\n    as: 'button',\n    ...props,\n  });\n\nexport default Button;\n\n// Deriving this from ButtonProps is not very neat, but ButtonAnchor is not frequently used\nexport type ButtonAnchorProps = Omit<ButtonProps, 'type' | 'as'> & {\n  href: string;\n  download?: string;\n  target?: string;\n  rel?: string;\n};\n\nexport const ButtonAnchor = (props: ButtonAnchorProps) =>\n  baseButton({\n    ...props,\n    // Links don't fire click event on space while buttons do, so we implement that ourselves for these\n    // https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/button_role#accessibility_concerns\n    onKeyDown: (evt) => {\n      props.onKeyDown?.(evt);\n      if (evt.defaultPrevented) return;\n      if (evt.key === ' ') evt.currentTarget.click();\n    },\n    as: 'a',\n  });\n"],"names":["buttonClassNames","variant","block","classNames","styles","button","baseButton","props","as","children","icon","onClick","loading","disabled","otherProps","Element","buttonContent","React","span","CircularProgress","size","className","Button","type","ButtonAnchor","onKeyDown","evt","defaultPrevented","key","currentTarget","click"],"mappings":";;;;;AAiCO,SAASA,gBAAAA,CAAiB,EAAEC,OAAO,EAAEC,KAAAA,GAAQ,IAAI,EAA0C,EAAA;AAChG,IAAA,OAAOC,WAAWC,gBAAAA,CAAOC,MAAM,EAAED,gBAAM,CAACH,QAAQ,EAAE;QAChD,CAACG,gBAAAA,CAAOF,KAAK,GAAGA;AAClB,KAAA,CAAA;AACF;AAEA;AACA,MAAMI,aAAa,CAACC,KAAAA,GAAAA;IAClB,MAAM,EAAEC,EAAE,EAAEC,QAAQ,EAAEC,IAAI,EAAEC,OAAO,EAAEC,OAAO,EAAEC,QAAAA,GAAWD,OAAO,EAAEV,KAAK,EAAED,OAAO,EAAE,GAAGa,YAAY,GAAGP,KAAAA;AACpG,IAAA,MAAMQ,OAAAA,GAAUP,EAAAA;AAEhB,IAAA,IAAIQ,aAAAA,iBACFC,EAAA,CAAA,aAAA,CAAAA,EAAA,CAAA,QAAA,EAAA,IAAA,EACGP,IAAAA,gBACDO,EAAA,CAAA,aAAA,CAACC,MAAAA,EAAAA,IAAAA,EAAMT,QAAAA,CAAAA,CAAAA;AAIX,IAAA,IAAIG,OAAAA,EAAS;AACXI,QAAAA,aAAAA,iBAAgBC,EAAA,CAAA,aAAA,CAACE,gBAAAA,EAAAA;YAAiBC,IAAAA,EAAM;;AAC1C,IAAA;AAEA,IAAA,qBACEH,EAAA,CAAA,aAAA,CAACF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