{"version":3,"file":"ButtonBase.mjs","names":[],"sources":["../../../src/button/ButtonBase.tsx"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\nimport classNames from 'classnames';\nimport type React from 'react';\n\nimport { ButtonSpinner } from '../_common/ButtonSpinner';\nimport {\n  type HtmlAttributes,\n  type PolymorphicCommonProps,\n} from '../_common/types';\n\n/**\n *\n * Types\n *\n */\n\ntype ButtonTypes = 'submit' | 'button' | 'reset';\nexport type ButtonSizes = 'small' | 'medium' | 'large';\nexport type ButtonVariants = 'primary' | 'danger' | 'neutral';\n\nexport type CommonButtonProps = {\n  children?: React.ReactNode;\n  /** Icon to display before the button content */\n  leadingVisual?: React.ReactNode;\n  /** Icon to display after the button content */\n  trailingVisual?: React.ReactNode;\n  /** If the button is doing something Async, it will display a loading spinner   */\n  isLoading?: boolean;\n  /** If the button is in disabled state */\n  isDisabled?: boolean;\n  /** If the button is fluid, it will take the full width of its container */\n  isFluid?: boolean;\n  /** Type of button */\n  type?: ButtonTypes;\n  /** Size of button */\n  size?: ButtonSizes;\n  /** onClick callback */\n  onClick?: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;\n  /** onKeyDown callback */\n  onKeyDown?: (event: React.KeyboardEvent<HTMLButtonElement>) => void;\n  /** Accessible message for screen readers when the button is in a loading state */\n  loadingMessage?: string;\n} /**\n * If `as=\"a\"`, `href` is required and `target` is available.\n * For all other elements, these props are not present.\n */ & (\n  | {\n      as: 'a';\n      /** The URL to link to. Only applies when `as=\"a\"` */\n      href?: HtmlAttributes<'a'>['href'];\n      /**\n       * The target window for the link. Only applies when `as=\"a\"`.\n       * Not present or required for other element types.\n       */\n      target?: HtmlAttributes<'a'>['target'];\n    }\n  | {\n      as?: React.ElementType;\n    }\n);\n\ntype ButtonBaseProps = CommonButtonProps & {\n  /** If the button is in floating state */\n  isFloating?: boolean;\n  /** variant of the button */\n  variant?: ButtonVariants;\n  /** fill of the button */\n  buttonFill?: 'filled' | 'outlined' | 'text';\n};\n\nexport const ButtonBase = <T extends React.ElementType = 'button'>({\n  as,\n  buttonFill = 'filled',\n  children,\n  className,\n  variant = 'primary',\n  htmlAttributes,\n  isDisabled = false,\n  isFloating = false,\n  isFluid = false,\n  isLoading = false,\n  leadingVisual,\n  trailingVisual,\n  onClick,\n  ref,\n  size = 'medium',\n  style,\n  type = 'button',\n  loadingMessage = 'Loading',\n  ...restProps\n}: PolymorphicCommonProps<T, ButtonBaseProps>) => {\n  const Component: React.ElementType = as ?? 'button';\n  const isInteractable = !isDisabled && !isLoading;\n\n  const classes = classNames(className, 'ndl-btn', {\n    'ndl-disabled': isDisabled,\n    'ndl-floating': isFloating,\n    'ndl-fluid': isFluid,\n    'ndl-loading': isLoading,\n    [`ndl-${size}`]: size,\n    [`ndl-${buttonFill}-button`]: buttonFill,\n    [`ndl-${variant}`]: variant,\n  });\n\n  const handleClick = (\n    event: React.MouseEvent<HTMLButtonElement, MouseEvent>,\n  ) => {\n    if (!isInteractable) {\n      // By default, a <button /> element with disabled set to true will not get its\n      // on click handler called. To support the same behavior on <a /> tags we\n      // swallow the event here when disabled, since the <a /> tag with disabled set to\n      // true will have its onClick handler called\n      event.preventDefault();\n      event.stopPropagation();\n      return;\n    }\n\n    if (onClick) {\n      onClick(event);\n    }\n  };\n\n  return (\n    <Component\n      type={type}\n      onClick={handleClick}\n      disabled={isDisabled}\n      aria-disabled={!isInteractable}\n      className={classes}\n      style={style}\n      ref={ref}\n      {...restProps}\n      {...htmlAttributes}\n    >\n      <div className=\"ndl-btn-inner\">\n        {Boolean(leadingVisual) && (\n          <div className=\"ndl-btn-leading-element\">{leadingVisual}</div>\n        )}\n        {Boolean(children) && (\n          <span className=\"ndl-btn-content\">{children}</span>\n        )}\n        {isLoading && (\n          <ButtonSpinner loadingMessage={loadingMessage} size={size} />\n        )}\n        {Boolean(trailingVisual) && (\n          <div className=\"ndl-btn-trailing-element\">{trailingVisual}</div>\n        )}\n      </div>\n    </Component>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;CA0FE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;AAlBF,MAAa,cAAsD,SAoBjB;CApBiB,IAAA,EACjE,IACA,aAAa,UACb,UACA,WACA,UAAU,WACV,gBACA,aAAa,OACb,aAAa,OACb,UAAU,OACV,YAAY,OACZ,eACA,gBACA,SACA,KACA,OAAO,UACP,OACA,OAAO,UACP,iBAAiB,cAAA,MACd,YAAA,yBAAA,MAAA,SAAA;CAEH,MAAM,YAA+B,OAAA,QAAA,OAAA,KAAA,IAAA,KAAM;CAC3C,MAAM,iBAAiB,CAAC,cAAc,CAAC;CAEvC,MAAM,UAAU,WAAW,WAAW,WAAW;EAC/C,gBAAgB;EAChB,gBAAgB;EAChB,aAAa;EACb,eAAe;GACd,OAAO,SAAS;GAChB,OAAO,WAAW,WAAW;GAC7B,OAAO,YAAY;CACtB,CAAC;CAED,MAAM,eACJ,UACG;EACH,IAAI,CAAC,gBAAgB;GAKnB,MAAM,eAAe;GACrB,MAAM,gBAAgB;GACtB;EACF;EAEA,IAAI,SACF,QAAQ,KAAK;CAEjB;CAEA,OACE,oBAAC,WAAA,eAAA,eAAA,eAAA;EACO;EACN,SAAS;EACT,UAAU;EACV,iBAAe,CAAC;EAChB,WAAW;EACJ;EACF;CACD,GAAA,SAAA,GACA,cAAA,GAAA,CAAA,GAAA,EAAA,UAEJ,qBAAC,OAAD;EAAK,WAAU;EAAf,UAAA;GACG,QAAQ,aAAa,KACpB,oBAAC,OAAD;IAAK,WAAU;IAA2B,UAAA;GAAmB,CAAA;GAE9D,QAAQ,QAAQ,KACf,oBAAC,QAAD;IAAM,WAAU;IAAmB;GAAe,CAAA;GAEnD,aACC,oBAAC,eAAD;IAA+B;IAAsB;GAAO,CAAA;GAE7D,QAAQ,cAAc,KACrB,oBAAC,OAAD;IAAK,WAAU;IAA4B,UAAA;GAAoB,CAAA;EAE9D;CACI,CAAA,EAAA,CAAA,CAAA;AAEf"}