{"version":3,"file":"GraphLabel.mjs","names":[],"sources":["../../../src/graph-label/GraphLabel.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 { tokens } from '@neo4j-ndl/base';\nimport classNames from 'classnames';\nimport type React from 'react';\nimport { useMemo, useState } from 'react';\n\nimport {\n  type HtmlAttributes,\n  type PolymorphicCommonProps,\n} from '../_common/types';\nimport {\n  getDisabledTextColor,\n  getHoverColor,\n  getTextColorFromBackground,\n} from './color';\n\n/**\n *\n *\n * Types\n *\n *\n */\n\ntype GraphLabelProps = {\n  /**  The type of label */\n  type:\n    | 'node'\n    | 'relationship'\n    | 'propertyKey'\n    | 'relationshipLeft'\n    | 'relationshipRight';\n  /** Callback function that is called when the label is clicked */\n  onClick?: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;\n  /** Additional styles to apply to the label */\n  style?: React.CSSProperties;\n  /** Content displayed inside the label */\n  children?: React.ReactNode;\n  /** The background color of the label */\n  color?: string;\n  /** Whether the label is disabled */\n  isDisabled?: boolean;\n  /** Additional class names */\n  className?: string;\n  /** Whether the label is selected */\n  isSelected?: boolean;\n  /** Whether the label should expand to full available width */\n  isFluid?: boolean;\n  /** The size of the label */\n  size?: 'small' | 'large';\n};\n\ntype HexagonEndProps = {\n  /** The direction of the hexagon end */\n  direction: 'left' | 'right';\n  /** The color of the hexagon end */\n  color?: string;\n  /** The height of the hexagon end in pixels */\n  height?: number;\n  /** HTML attributes */\n  htmlAttributes?: HtmlAttributes<'div'>;\n};\n/**\n *\n *\n * Helpers\n *\n *\n */\n\nconst HexagonEnd = ({\n  direction = 'left',\n  color,\n  htmlAttributes,\n  height = 24,\n}: HexagonEndProps) => {\n  const classes = classNames('ndl-hexagon-end', {\n    'ndl-left': direction === 'left',\n    'ndl-right': direction === 'right',\n  });\n\n  return (\n    <div className={classes} {...htmlAttributes}>\n      <svg\n        aria-hidden={true}\n        className=\"ndl-hexagon-end-inner\"\n        fill=\"none\"\n        height={height}\n        preserveAspectRatio=\"none\"\n        viewBox=\"0 0 9 24\"\n        width=\"9\"\n        xmlns=\"http://www.w3.org/2000/svg\"\n      >\n        {/* triangle */}\n        <path\n          style={{ fill: color }}\n          fillRule=\"evenodd\"\n          clipRule=\"evenodd\"\n          d=\"M5.73024 1.03676C6.08165 0.397331 6.75338 0 7.48301 0H9V24H7.483C6.75338 24 6.08165 23.6027 5.73024 22.9632L0.315027 13.1094C-0.105009 12.4376 -0.105009 11.5624 0.315026 10.8906L5.73024 1.03676Z\"\n        />\n      </svg>\n    </div>\n  );\n};\n\ntype SquareEndProps = {\n  /** The direction of the square end (where it's rounded) */\n  direction: 'left' | 'right';\n  /** The color of the square end */\n  color: string;\n  /** HTML attributes */\n  htmlAttributes?: HtmlAttributes<'div'>;\n};\n\nconst SquareEnd = ({\n  direction = 'left',\n  color,\n  htmlAttributes,\n}: SquareEndProps) => {\n  const classes = classNames('ndl-square-end', {\n    'ndl-left': direction === 'left',\n    'ndl-right': direction === 'right',\n  });\n\n  return (\n    <div className={classes} {...htmlAttributes}>\n      <div\n        className=\"ndl-square-end-inner\"\n        style={{ backgroundColor: color }}\n      />\n    </div>\n  );\n};\n\n/**\n *\n *\n * GraphLabel Component\n *\n *\n */\n\nconst MAX_WIDTH = 200;\n\nexport const GraphLabel = <T extends React.ElementType = 'button'>({\n  type = 'node',\n  color,\n  isDisabled = false,\n  isSelected = false,\n  as,\n  onClick,\n  className,\n  style,\n  children,\n  htmlAttributes,\n  isFluid = false,\n  size = 'large',\n  ref,\n  ...restProps\n}: PolymorphicCommonProps<T, GraphLabelProps>) => {\n  const [isHover, setIsHover] = useState(false);\n\n  const handleMouseEnter = (\n    event: React.MouseEvent<HTMLButtonElement, MouseEvent>,\n  ) => {\n    setIsHover(true);\n\n    if (htmlAttributes && htmlAttributes.onMouseEnter !== undefined) {\n      htmlAttributes.onMouseEnter(event);\n    }\n  };\n\n  const handleMouseLeave = (\n    event: React.MouseEvent<HTMLButtonElement, MouseEvent>,\n  ) => {\n    setIsHover(false);\n\n    htmlAttributes?.onMouseLeave?.(event);\n  };\n\n  const Component: React.ElementType = as ?? 'button';\n  const isButton = Component === 'button';\n\n  const handleClick = (\n    event: React.MouseEvent<HTMLButtonElement, MouseEvent>,\n  ) => {\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    if (isDisabled) {\n      event.preventDefault();\n      event.stopPropagation();\n      return;\n    }\n\n    if (onClick) {\n      onClick(event);\n    }\n  };\n\n  let backgroundColor = useMemo(() => {\n    if (color === undefined) {\n      switch (type) {\n        case 'node':\n          return tokens.graph['1'];\n        case 'relationship':\n        case 'relationshipLeft':\n        case 'relationshipRight':\n          return tokens.theme.light.color.neutral.bg.strong;\n        default:\n          return tokens.theme.light.color.neutral.bg.strongest;\n      }\n    }\n    return color;\n  }, [color, type]);\n\n  const hoverColor = useMemo(\n    () =>\n      getHoverColor(\n        backgroundColor ? backgroundColor : tokens.palette.lemon[40],\n      ),\n    [backgroundColor],\n  );\n  const textColor = useMemo(\n    () =>\n      getTextColorFromBackground(\n        backgroundColor ? backgroundColor : tokens.palette.lemon[40],\n      ),\n    [backgroundColor],\n  );\n  const disabledTextColor = useMemo(\n    () =>\n      getDisabledTextColor(\n        backgroundColor ? backgroundColor : tokens.palette.lemon[40],\n      ),\n    [backgroundColor],\n  );\n\n  if (isHover && !isDisabled) {\n    backgroundColor = hoverColor;\n  }\n\n  const commonClasses = classNames('ndl-graph-label', className, {\n    'ndl-disabled': isDisabled,\n    'ndl-interactable': isButton,\n    'ndl-selected': isSelected,\n    'ndl-small': size === 'small',\n  });\n\n  if (type === 'node') {\n    const classes = classNames('ndl-node-label', commonClasses);\n\n    return (\n      <Component\n        className={classes}\n        ref={ref}\n        style={{\n          backgroundColor: backgroundColor,\n          color: isDisabled ? disabledTextColor : textColor,\n          maxWidth: isFluid ? '100%' : MAX_WIDTH,\n          ...style,\n        }}\n        {...(isButton && {\n          disabled: isDisabled,\n          onClick: handleClick,\n          onMouseEnter: handleMouseEnter,\n          onMouseLeave: handleMouseLeave,\n          type: 'button',\n        })}\n        {...htmlAttributes}\n      >\n        <div className=\"ndl-node-label-content\">{children}</div>\n      </Component>\n    );\n  } else if (\n    type === 'relationship' ||\n    type === 'relationshipLeft' ||\n    type === 'relationshipRight'\n  ) {\n    const classes = classNames('ndl-relationship-label', commonClasses);\n    const height = size === 'small' ? 20 : 24;\n    const focusRingClasses = classNames('ndl-relationship-focus-ring', {\n      'ndl-end-left-square': type === 'relationshipRight',\n      'ndl-end-right-square': type === 'relationshipLeft',\n    });\n\n    return (\n      <Component\n        style={{\n          maxWidth: isFluid ? '100%' : MAX_WIDTH,\n          ...style,\n          color: isDisabled ? disabledTextColor : textColor,\n        }}\n        className={classes}\n        {...(isButton && {\n          disabled: isDisabled,\n          onClick: handleClick,\n          onMouseEnter: handleMouseEnter,\n          onMouseLeave: handleMouseLeave,\n          type: 'button',\n        })}\n        ref={ref}\n        {...restProps}\n        {...htmlAttributes}\n      >\n        {type === 'relationshipLeft' || type === 'relationship' ? (\n          <HexagonEnd\n            direction=\"left\"\n            color={backgroundColor}\n            height={height}\n          />\n        ) : (\n          <SquareEnd direction=\"left\" color={backgroundColor} />\n        )}\n        <div\n          className=\"ndl-relationship-label-container\"\n          style={{\n            backgroundColor: backgroundColor,\n          }}\n        >\n          <div className=\"ndl-relationship-label-content\">{children}</div>\n        </div>\n        {type === 'relationshipRight' || type === 'relationship' ? (\n          <HexagonEnd\n            direction=\"right\"\n            color={backgroundColor}\n            height={height}\n          />\n        ) : (\n          <SquareEnd direction=\"right\" color={backgroundColor} />\n        )}\n        <span aria-hidden={true} className={focusRingClasses} />\n      </Component>\n    );\n  } else {\n    const classes = classNames('ndl-property-key-label', commonClasses);\n\n    return (\n      <Component\n        {...(isButton && {\n          type: 'button',\n        })}\n        style={{\n          backgroundColor: backgroundColor,\n          color: isDisabled ? disabledTextColor : textColor,\n          maxWidth: isFluid ? '100%' : MAX_WIDTH,\n          ...style,\n        }}\n        className={classes}\n        onClick={handleClick}\n        onMouseEnter={handleMouseEnter}\n        onMouseLeave={handleMouseLeave}\n        ref={ref}\n        {...htmlAttributes}\n      >\n        <div className=\"ndl-property-key-label-content\">{children}</div>\n      </Component>\n    );\n  }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoKE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;;;;;;;;AAvFF,MAAM,cAAc,EAClB,YAAY,QACZ,OACA,gBACA,SAAS,SACY;CACrB,MAAM,UAAU,WAAW,mBAAmB;EAC5C,YAAY,cAAc;EAC1B,aAAa,cAAc;CAC7B,CAAC;CAED,OACE,oBAAC,OAAA,eAAA,eAAA,EAAI,WAAW,QAAA,GAAa,cAAA,GAAA,CAAA,GAAA,EAAA,UAC3B,oBAAC,OAAD;EACE,eAAa;EACb,WAAU;EACV,MAAK;EACG;EACR,qBAAoB;EACpB,SAAQ;EACR,OAAM;EACN,OAAM;EAGN,UAAA,oBAAC,QAAD;GACE,OAAO,EAAE,MAAM,MAAM;GACrB,UAAS;GACT,UAAS;GACT,GAAE;EACH,CAAA;CACE,CAAA,EAAA,CACF,CAAA;AAET;AAWA,MAAM,aAAa,EACjB,YAAY,QACZ,OACA,qBACoB;CACpB,MAAM,UAAU,WAAW,kB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