{"version":3,"file":"CodeBlock.mjs","names":["SyntaxHighlighter"],"sources":["../../../src/code-block/CodeBlock.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';\nimport { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter';\n\nimport { type CommonProps } from '../_common/types';\nimport { CleanIconButton } from '../clean-icon-button';\nimport { type IconButton } from '../icon-button';\nimport { ChevronDownIconOutline } from '../icons/generated/heroIcons/ChevronDownIconOutline';\nimport { ChevronUpIconOutline } from '../icons/generated/heroIcons/ChevronUpIconOutline';\nimport { type LanguageProp } from './code-languages';\nimport { type CodeTheme } from './code-themes';\nimport { useCodeLanguage } from './hooks/use-code-language';\nimport { useCodeTheme } from './hooks/use-code-theme';\nimport { useExpandable } from './hooks/use-expandable';\n\ninterface CodeBlockProps {\n  code: string;\n  isDisabled?: boolean;\n  maxHeight?: number;\n  language: LanguageProp | 'text';\n  shouldShowLineNumbers?: boolean;\n  theme?: CodeTheme;\n  heading?: React.ReactNode;\n  actions?: React.ComponentProps<typeof IconButton<'button'>>[];\n}\n\nexport const CodeBlock = ({\n  maxHeight,\n  code,\n  language,\n  shouldShowLineNumbers,\n  theme,\n  heading,\n  actions,\n  isDisabled,\n  className,\n  style,\n  htmlAttributes,\n  ref,\n  ...restProps\n}: CommonProps<'div', CodeBlockProps>) => {\n  const {\n    containerHeight,\n    isExpanded,\n    toggleExpand,\n    hasOverflowingContent,\n    ref: syntaxHighlighterWrapperRef,\n  } = useExpandable(maxHeight, code);\n  const codeTheme = useCodeTheme(theme);\n  const codeLanguage = useCodeLanguage(language);\n\n  const hasMaxHeight = maxHeight !== undefined;\n  const hasHeading = Boolean(heading);\n  const numberOfActions = actions?.length ?? 0;\n\n  const shouldShowExpandButton = hasMaxHeight && hasOverflowingContent;\n  const rightPadding = hasHeading\n    ? 0\n    : Math.max(numberOfActions * 36, (shouldShowExpandButton ? 1 : 0) * 36) + 8;\n\n  return (\n    <div\n      ref={ref}\n      className={classNames('ndl-code-block-container', className)}\n      style={{ height: containerHeight, ...style }}\n      {...restProps}\n      {...htmlAttributes}\n    >\n      <div className=\"ndl-code-block-layout\">\n        {hasHeading && (\n          <div\n            className={classNames('ndl-code-block-title', {\n              'ndl-disabled': isDisabled,\n            })}\n            style={{\n              maxWidth: `calc(100% - ${(actions?.length ?? 0) * 36 + 20}px)`,\n            }}\n            data-testid=\"ndl-code-block-title\"\n          >\n            {heading}\n          </div>\n        )}\n        <div\n          className={classNames('ndl-code-content-container', {\n            'ndl-disabled': isDisabled,\n          })}\n          style={{\n            height: hasHeading ? 'calc(100% - 24px - 12px)' : '100%',\n          }}\n        >\n          <div\n            ref={syntaxHighlighterWrapperRef}\n            className=\"ndl-highlight-wrapper\"\n            tabIndex={0}\n            role=\"region\"\n            aria-label={`Code content ${isDisabled ? '(disabled)' : ''}`}\n          >\n            <SyntaxHighlighter\n              language={codeLanguage}\n              style={{\n                ...codeTheme,\n                'pre[class*=\"language-\"]': {\n                  border: 0,\n                  color: 'var(--theme-color-neutral-text-default)',\n                  lineHeight: '1',\n                  padding: `0 ${rightPadding}px 0.75em 0.75em`,\n                  width: 'fit-content',\n                },\n              }}\n              codeTagProps={{ className: 'ndl-typography-code' }}\n              // Turn on 'shouldShowLineNumbers' & 'wrapLongLines' at the same time, the display is wrong\n              // https://github.com/react-syntax-highlighter/react-syntax-highlighter/issues/402\n              // wrapLongLines\n              showLineNumbers={shouldShowLineNumbers}\n            >\n              {code}\n            </SyntaxHighlighter>\n          </div>\n        </div>\n      </div>\n      <div\n        className={classNames('ndl-code-block-actions', {\n          'ndl-disabled': isDisabled,\n        })}\n      >\n        {actions?.map((iconButtonProps, index) => (\n          <CleanIconButton\n            key={index}\n            isDisabled={isDisabled}\n            {...iconButtonProps}\n            description={`${iconButtonProps.description ?? 'CodeBlock Action'}`}\n            htmlAttributes={{\n              'data-testid': `ndl-action-button-${index}`,\n              ...iconButtonProps.htmlAttributes,\n            }}\n          />\n        ))}\n      </div>\n      {shouldShowExpandButton && (\n        <div className=\"ndl-code-block-expand-button\">\n          <CleanIconButton\n            description={isExpanded ? 'Collapse' : 'Expand'}\n            tooltipProps={{\n              root: {\n                shouldCloseOnReferenceClick: true,\n              },\n            }}\n            onClick={toggleExpand}\n          >\n            {isExpanded ? <ChevronUpIconOutline /> : <ChevronDownIconOutline />}\n          </CleanIconButton>\n        </div>\n      )}\n    </div>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+CE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;AAZF,MAAa,aAAa,SAcgB;;CAdhB,IAAA,EACxB,WACA,MACA,UACA,uBACA,OACA,SACA,SACA,YACA,WACA,OACA,gBACA,QAAA,MACG,YAAA,yBAAA,MAAA,SAAA;CAEH,MAAM,EACJ,iBACA,YACA,cACA,uBACA,KAAK,gCACH,cAAc,WAAW,IAAI;CACjC,MAAM,YAAY,aAAa,KAAK;CACpC,MAAM,eAAe,gBAAgB,QAAQ;CAE7C,MAAM,eAAe,cAAc,KAAA;CACnC,MAAM,aAAa,QAAQ,OAAO;CAClC,MAAM,mBAAA,kBAAA,YAAA,QAAA,YAAA,KAAA,IAAA,KAAA,IAAkB,QAAS,YAAA,QAAA,oBAAA,KAAA,IAAA,kBAAU;CAE3C,MAAM,yBAAyB,gBAAgB;CAC/C,MAAM,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