{"version":3,"file":"IconButtonBase.mjs","names":[],"sources":["../../../src/icon-button-base/IconButtonBase.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 React, { useId } from 'react';\n\nimport { ButtonSpinner } from '../_common/ButtonSpinner';\nimport {\n  type PolymorphicCommonProps,\n  type TooltipObjectProps,\n} from '../_common/types';\nimport { needleWarningMessage } from '../_common/utils';\nimport { type ButtonSizes } from '../button';\nimport { Kbd } from '../kbd';\nimport { Tooltip } from '../tooltip';\n\n/**\n *\n *\n * Types\n *\n *\n */\n\nexport type IconButtonBaseProps = {\n  children?: React.ReactNode;\n\n  /** If the icon button is in disabled state */\n  isDisabled?: boolean;\n\n  /** If the button is doing something Async, it will display a loading spinner   */\n  isLoading?: boolean;\n\n  /** Size of button */\n  size?: ButtonSizes;\n\n  /** If the button is in floating state */\n  isFloating?: boolean;\n\n  /** Active - used for open context menus for example */\n  isActive?: boolean;\n\n  /** A string that will be shown as a tooltip when hovering over the button it also acts as an aria-label- {@link https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-label} */\n  description: string | null;\n\n  /** The Kbd components that will be displayed in the tooltip after the description text, this does not include the keyboard shortcut functionality this only displays the Kbd components. */\n  descriptionKbdProps?: React.ComponentProps<typeof Kbd>;\n\n  /** Variant of the button */\n  variant?: 'neutral' | 'danger';\n\n  /** Variant of the button */\n  iconButtonVariant?: 'default' | 'clean';\n\n  /**\n   * Props for the tooltip component.\n   */\n  tooltipProps?: TooltipObjectProps;\n\n  /** Click handler */\n  onClick?: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;\n\n  /** Accessible message for screen readers when the button is in a loading state */\n  loadingMessage?: string;\n};\n\nexport const IconButtonBase = <T extends React.ElementType = 'button'>({\n  children,\n  as,\n  iconButtonVariant = 'default',\n  isLoading = false,\n  isDisabled = false,\n  size = 'medium',\n  isFloating = false,\n  // Does not have a default value to avoid setting aria-pressed to true if not explicitly set\n  isActive = undefined,\n  description,\n  descriptionKbdProps,\n  tooltipProps,\n  className,\n  style,\n  variant = 'neutral',\n  htmlAttributes,\n  onClick,\n  ref,\n  loadingMessage = 'Loading',\n  ...restProps\n}: PolymorphicCommonProps<T, IconButtonBaseProps>) => {\n  const Component: React.ElementType = as ?? 'button';\n  const loadingId = useId();\n\n  const isInteractable = !isDisabled && !isLoading;\n\n  const isClean = iconButtonVariant === 'clean';\n  const isDanger = variant === 'danger';\n\n  const classes = classNames('ndl-icon-btn', className, {\n    'ndl-active': Boolean(isActive),\n    'ndl-clean': isClean,\n    'ndl-danger': isDanger,\n    'ndl-disabled': isDisabled,\n    'ndl-floating': isFloating,\n    'ndl-large': size === 'large',\n    'ndl-loading': isLoading,\n    'ndl-medium': size === 'medium',\n    'ndl-small': size === 'small',\n  });\n\n  if (isClean && isFloating) {\n    throw new Error(\n      'BaseIconButton: Cannot use isFloating and iconButtonVariant=\"clean\" at the same time.',\n    );\n  }\n\n  if (!description && !htmlAttributes?.['aria-label']) {\n    needleWarningMessage(\n      'Icon buttons do not have text, be sure to include a description or an aria-label for screen readers link: https://dequeuniversity.com/rules/axe/4.4/button-name?application=axeAPI',\n    );\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  const tooltipContent = React.useMemo(() => {\n    const descriptionContent = description ?? tooltipProps?.content?.children;\n    return (\n      <>\n        {descriptionContent}\n        {descriptionKbdProps && <Kbd {...descriptionKbdProps} />}\n      </>\n    );\n  }, [description, descriptionKbdProps, tooltipProps?.content?.children]);\n\n  return (\n    <Tooltip\n      hoverDelay={{\n        close: 0,\n        open: 500,\n      }}\n      isDisabled={description === null || isDisabled}\n      type=\"simple\"\n      {...tooltipProps?.root}\n    >\n      <Tooltip.Trigger {...tooltipProps?.trigger} hasButtonWrapper>\n        <Component\n          type=\"button\"\n          onClick={handleClick}\n          disabled={isDisabled}\n          aria-disabled={!isInteractable}\n          aria-label={description}\n          aria-pressed={isActive}\n          className={classes}\n          style={style}\n          ref={ref}\n          aria-describedby={isLoading ? loadingId : undefined}\n          {...restProps}\n          {...htmlAttributes}\n        >\n          <div className=\"ndl-icon-btn-inner\">\n            {isLoading ? (\n              <ButtonSpinner\n                loadingMessage={loadingMessage}\n                size=\"small\"\n                htmlAttributes={{ id: loadingId }}\n              />\n            ) : (\n              <div className=\"ndl-icon\">{children}</div>\n            )}\n          </div>\n        </Component>\n      </Tooltip.Trigger>\n      <Tooltip.Content {...tooltipProps?.content}>\n        {tooltipContent}\n      </Tooltip.Content>\n    </Tooltip>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoFE;CACA;CACA;CACA;CACA;CACA;CACA;CAEA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;AAnBF,MAAa,kBAA0D,SAqBjB;;CArBiB,IAAA,EACrE,UACA,IACA,oBAAoB,WACpB,YAAY,OACZ,aAAa,OACb,OAAO,UACP,aAAa,OAEb,WAAW,KAAA,GACX,aACA,qBACA,cACA,WACA,OACA,UAAU,WAC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