{"version":3,"file":"ClipboardButton.mjs","names":["CheckOutlineIcon","CopyOutlineIcon"],"sources":["../../../src/clipboard-button/ClipboardButton.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 React from 'react';\n\nimport { type ButtonSizes } from '../button';\nimport { CleanIconButton } from '../clean-icon-button';\nimport { useCopyToClipboard } from '../hooks';\nimport { IconButton } from '../icon-button';\nimport CheckOutlineIcon from '../icons/generated/custom/CheckOutline';\nimport CopyOutlineIcon from '../icons/generated/custom/CopyOutline';\nimport { Kbd } from '../kbd';\nimport { OutlinedButton } from '../outlined-button';\nimport { Tooltip } from '../tooltip';\n\ntype ConfirmationButtonProps = {\n  description: string;\n  descriptionKbdProps?: React.ComponentProps<typeof Kbd>;\n  actionFeedbackText: string;\n  tooltipProps?: Omit<React.ComponentProps<typeof Tooltip>, 'children'>;\n  onClick?: React.ComponentPropsWithoutRef<'button'>['onClick'];\n  children: React.ReactNode;\n  className?: string;\n  icon: React.ReactNode;\n  htmlAttributes?: React.ComponentPropsWithoutRef<'button'>;\n} & (\n  | {\n      type: 'clean-icon-button';\n      cleanIconButtonProps?: Omit<\n        React.ComponentProps<typeof CleanIconButton>,\n        'children' | 'onClick' | 'tooltip'\n      >;\n    }\n  | {\n      type: 'icon-button';\n      iconButtonProps?: Omit<\n        React.ComponentProps<typeof IconButton>,\n        'children' | 'onClick' | 'tooltip'\n      >;\n    }\n  | {\n      type: 'outlined-button';\n      outlinedButtonProps?: Omit<\n        React.ComponentProps<typeof OutlinedButton>,\n        'children' | 'onClick' | 'tooltip'\n      >;\n    }\n);\n\nconst ConfirmationButton = ({\n  descriptionKbdProps,\n  description,\n  actionFeedbackText,\n  icon,\n  children,\n  onClick,\n  htmlAttributes,\n  tooltipProps,\n  type = 'clean-icon-button',\n  ...restProps\n}: ConfirmationButtonProps) => {\n  const [timeoutId, setTimeoutId] = React.useState<number | null>(null);\n  const [isOpen, setIsOpen] = React.useState(false);\n\n  const handleClick = () => {\n    if (timeoutId !== null) {\n      clearTimeout(timeoutId);\n    }\n\n    const newTimeoutId = window.setTimeout(() => {\n      setTimeoutId(null);\n    }, 2000);\n\n    setTimeoutId(newTimeoutId);\n  };\n\n  const handleCloseTooltip = () => {\n    setIsOpen(false);\n  };\n\n  const handleOpenTooltip = () => {\n    setIsOpen(true);\n  };\n\n  const content = timeoutId === null ? description : actionFeedbackText;\n  const currentIcon =\n    timeoutId === null ? icon : <CheckOutlineIcon className=\"ndl-icon-svg\" />;\n  /* Controlling the tip to make it disappear in a timely manner */\n\n  if (type === 'clean-icon-button') {\n    return (\n      <CleanIconButton\n        {...(\n          restProps as {\n            cleanIconButtonProps: React.ComponentProps<typeof CleanIconButton>;\n          }\n        ).cleanIconButtonProps}\n        description={content}\n        descriptionKbdProps={descriptionKbdProps}\n        tooltipProps={{\n          root: {\n            ...tooltipProps,\n            isOpen: isOpen || timeoutId !== null,\n          },\n          trigger: {\n            htmlAttributes: {\n              onBlur: handleCloseTooltip,\n              onFocus: handleOpenTooltip,\n              onMouseEnter: handleOpenTooltip,\n              onMouseLeave: handleCloseTooltip,\n            },\n          },\n        }}\n        onClick={(event) => {\n          if (onClick) {\n            onClick(event);\n          }\n          handleClick();\n        }}\n        className={restProps.className}\n        htmlAttributes={htmlAttributes}\n      >\n        {currentIcon}\n      </CleanIconButton>\n    );\n  } else if (type === 'icon-button') {\n    return (\n      <IconButton\n        {...(\n          restProps as {\n            iconButtonProps: React.ComponentProps<typeof IconButton>;\n          }\n        ).iconButtonProps}\n        description={content}\n        tooltipProps={{\n          root: {\n            ...tooltipProps,\n            isOpen: isOpen || timeoutId !== null,\n          },\n          trigger: {\n            htmlAttributes: {\n              onBlur: handleCloseTooltip,\n              onFocus: handleOpenTooltip,\n              onMouseEnter: handleOpenTooltip,\n              onMouseLeave: handleCloseTooltip,\n            },\n          },\n        }}\n        onClick={(event) => {\n          if (onClick) {\n            onClick(event);\n          }\n          handleClick();\n        }}\n        className={restProps.className}\n        htmlAttributes={htmlAttributes}\n      >\n        {currentIcon}\n      </IconButton>\n    );\n  } else if (type === 'outlined-button') {\n    return (\n      <Tooltip\n        type=\"simple\"\n        isOpen={isOpen || timeoutId !== null}\n        {...tooltipProps}\n        onOpenChange={(open) => {\n          if (open) {\n            handleOpenTooltip();\n          } else {\n            handleCloseTooltip();\n          }\n          tooltipProps?.onOpenChange?.(open);\n        }}\n      >\n        <Tooltip.Trigger\n          hasButtonWrapper\n          htmlAttributes={{\n            'aria-label': content,\n            onBlur: handleCloseTooltip,\n            onFocus: handleOpenTooltip,\n            onMouseEnter: handleOpenTooltip,\n            onMouseLeave: handleCloseTooltip,\n          }}\n        >\n          <OutlinedButton\n            variant=\"neutral\"\n            {...(\n              restProps as {\n                outlinedButtonProps: React.ComponentProps<\n                  typeof OutlinedButton\n                >;\n              }\n            ).outlinedButtonProps}\n            onClick={(event) => {\n              if (onClick) {\n                onClick(event);\n              }\n              handleClick();\n            }}\n            leadingVisual={currentIcon}\n            className={restProps.className}\n            htmlAttributes={htmlAttributes}\n          >\n            {children}\n          </OutlinedButton>\n        </Tooltip.Trigger>\n        <Tooltip.Content>\n          {content}\n          {descriptionKbdProps && <Kbd {...descriptionKbdProps} />}\n        </Tooltip.Content>\n      </Tooltip>\n    );\n  }\n};\n\n/**\n * @remarks\n * For more information of the `TooltipProps` type, see the Tooltip component documentation.\n */\ninterface ClipboardButtonProps {\n  /** The text that should be set to users clipboard */\n  textToCopy: string;\n  descriptionKbdProps?: React.ComponentProps<typeof Kbd>;\n  tooltipProps?: Omit<React.ComponentProps<typeof Tooltip>, 'children'>;\n  isDisabled?: boolean;\n  size?: ButtonSizes;\n  htmlAttributes?: React.ComponentPropsWithoutRef<'button'>;\n  type?: 'outlined-button' | 'clean-icon-button' | 'icon-button';\n}\n\nexport const ClipboardButton = ({\n  textToCopy,\n  descriptionKbdProps,\n  isDisabled,\n  size,\n  tooltipProps,\n  htmlAttributes,\n  type,\n}: ClipboardButtonProps) => {\n  const [, copyFunction] = useCopyToClipboard();\n\n  const isOutlinedButton = type === 'outlined-button';\n  const isIconButton = type === 'icon-button';\n\n  const props = isOutlinedButton\n    ? {\n        outlinedButtonProps: {\n          isDisabled,\n          size,\n        },\n        type: 'outlined-button' as const,\n      }\n    : isIconButton\n      ? {\n          iconButtonProps: {\n            description: 'Copy to clipboard',\n            isDisabled,\n            size,\n          },\n          type: 'icon-button' as const,\n        }\n      : {\n          cleanIconButtonProps: {\n            description: 'Copy to clipboard',\n            isDisabled,\n            size,\n          },\n          type: 'clean-icon-button' as const,\n        };\n\n  return (\n    <ConfirmationButton\n      onClick={() => copyFunction(textToCopy)}\n      description=\"Copy to clipboard\"\n      actionFeedbackText=\"Copied\"\n      descriptionKbdProps={descriptionKbdProps}\n      {...props}\n      tooltipProps={tooltipProps}\n      icon={<CopyOutlineIcon className=\"ndl-icon-svg\" />}\n      htmlAttributes={{\n        'aria-live': 'polite',\n        ...htmlAttributes,\n      }}\n    >\n      {type === 'outlined-button' && 'Copy'}\n    </ConfirmationButton>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmEE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;AATF,MAAM,sBAAsB,SAWG;CAXH,IAAA,EAC1B,qBACA,aACA,oBACA,MACA,UACA,SACA,gBACA,cACA,OAAO,wBAAA,MACJ,YAAA,yBAAA,MAAA,SAAA;CAEH,MAAM,C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