{"version":3,"file":"CodeBlock.mjs","sources":["../../../src/components/CodeBlock/CodeBlock.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport delay from 'lodash/delay.js';\nimport React from 'react';\n\nimport Markdown from 'react-markdown';\nimport { Prism, type SyntaxHighlighterProps } from 'react-syntax-highlighter';\n\nimport theme from './theme';\n\nimport Button from '../Button';\nimport { useSemanticIcon } from '../Icon';\n\nimport styles from './CodeBlock.module.css';\n\n// Workaround for temporary React 18 support\n// See: https://github.com/react-syntax-highlighter/react-syntax-highlighter/issues/539\nconst SyntaxHighlighter = Prism as any as React.FC<SyntaxHighlighterProps>;\n\nexport type CodeBlockProps = {\n  // Component API\n  children: string;\n  /**\n   * CSS class names that can be appended to the component.\n   */\n  className?: string;\n  /**\n   * Determines whether we render a copy button (on hover)\n   */\n  copyStyle?: 'text' | 'icon';\n  /**\n   * The programming language to use for syntax highlighting\n   */\n  language: SyntaxHighlighterProps['language'];\n  // Design API\n} & Omit<React.HTMLAttributes<HTMLDivElement>, 'children'>;\n\n/**\n * ## Usage\n *\n * Show a block of formatted code, with syntax highlighting, in a block container.\n *\n * | Type/Use | Description | Example |\n * |----------|-------------|---------|\n * | Read-only | The snippet is displayed on its own, with no copy affordance. | Illustrating syntax inline with surrounding prose. |\n * | Icon copy | `copyStyle=\"icon\"` adds an icon-only button that copies the snippet. | Dense layouts where a labeled button would crowd the block. |\n * | Text copy | `copyStyle=\"text\"` adds a labeled button that copies the snippet. | Snippets the reader is expected to run, like install commands. |\n *\n * ## Interaction\n *\n * `CodeBlock` displays syntax highlighting with a theme that follows the page color scheme:\n * `<html data-theme=\"light|dark\">` when set, otherwise the operating system preference. It also\n * includes the ability to show a copy button which, when clicked, will copy the code block to\n * the user's clipboard.\n *\n * ## Content & Accessibility\n *\n * ### Do's\n *\n * * Use `CodeBlock` when you want to format and display any multi-line code to the user.\n * * Use `CodeBlock` when a single-line code block applies to command line snippets, or other code which has complex, lengthy text.\n *\n * ### Don'ts\n *\n * * Avoid using `CodeBlock` for single-word code snippets. Instead use the `Text` component with one of the `code-*` presets.\n *\n * ## Resources\n *\n * * https://github.com/react-syntax-highlighter/react-syntax-highlighter\n */\nexport const CodeBlock = ({\n  children,\n  className,\n  copyStyle,\n  language,\n  ...other\n}: CodeBlockProps) => {\n  // The copy affordance is semantic: it is the same mark every copy button in the app\n  // carries, so it comes from `IconProvider` rather than from a prop on this block. The\n  // confirmation that follows a copy is a transient state, not a role, so it stays a\n  // checkmark.\n  const copyIcon = useSemanticIcon('copy');\n\n  const [hasCopied, setHasCopied] = React.useState<boolean>(false);\n  const [copyButtonText, setCopyButtonText] = React.useState<string>('Copy');\n\n  const componentClassName = clsx(styles['code-block'], className);\n  const composedCodeSnippet = `~~~${language}\n${children}\n~~~`;\n\n  return (\n    <div className={componentClassName} {...other}>\n      <Markdown\n        components={{\n          code(props) {\n            const { children, className, node, ...rest } = props;\n            const match = /language-(\\w+)/.exec(className || '');\n            return match ? (\n              <SyntaxHighlighter\n                {...rest}\n                children={String(children).replace(/\\n$/, '')}\n                codeTagProps={{\n                  className: styles['code-block__block'],\n                }}\n                language={match[1]}\n                PreTag=\"div\"\n                // override the type to match the declarations in RSH's built-in themes\n                style={\n                  theme as { [key: string]: React.CSSProperties } | undefined\n                }\n                wrapLongLines\n              />\n            ) : (\n              <code {...rest} className={className}>\n                {children}\n              </code>\n            );\n          },\n        }}\n      >\n        {composedCodeSnippet}\n      </Markdown>\n      {copyStyle && (\n        <div className={styles['code-block__copy']}>\n          <Button\n            aria-label=\"Copy this code block\"\n            data-testid=\"copy-button\"\n            icon={\n              copyStyle === 'icon'\n                ? hasCopied\n                  ? 'check'\n                  : copyIcon\n                : undefined\n            }\n            iconLayout={copyStyle === 'icon' ? 'icon-only' : undefined}\n            onClick={async () => {\n              try {\n                await navigator.clipboard.writeText(children);\n                copyStyle === 'icon'\n                  ? setHasCopied(true)\n                  : setCopyButtonText('Copied!');\n\n                delay(() => {\n                  copyStyle === 'icon'\n                    ? setHasCopied(false)\n                    : setCopyButtonText('Copy');\n                }, 3000);\n              } catch (error) {\n                console.error(error);\n              }\n            }}\n            rank=\"secondary\"\n            size=\"md\"\n          >\n            {copyStyle === 'text' ? copyButtonText : undefined}\n          </Button>\n        </div>\n      )}\n    </div>\n  );\n};\n\nCodeBlock.displayName = 'CodeBlock';\n"],"names":["React"],"mappings":";;;;;;;;;;;;AAcA;AACA;AACA,MAAM,iBAAiB,GAAG,KAAgD;AAoB1E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCG;AACI,MAAM,SAAS,GAAG,CAAC,EACxB,QAAQ,EACR,SAAS,EACT,SAAS,EACT,QAAQ,EACR,GAAG,KAAK,EACO,KAAI;;;;;AAKnB,IAAA,MAAM,QAAQ,GAAG,eAAe,CAAC,MAAM,CAAC;AAExC,IAAA,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,GAAGA,cAAK,CAAC,QAAQ,CAAU,KAAK,CAAC;AAChE,IAAA,MAAM,CAAC,cAAc,EAAE,iBAAiB,CAAC,GAAGA,cAAK,CAAC,QAAQ,CAAS,MAAM,CAAC;IAE1E,MAAM,kBAAkB,GAAG,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,SAAS,CAAC;IAChE,MAAM,mBAAmB,GAAG,CAAA,GAAA,EAAM,QAAQ;EAC1C,QAAQ;IACN;AAEF,IAAA,QACEA,cAAA,CAAA,aAAA,CAAA,KAAA,EAAA,EAAK,SAAS,EAAE,kBAAkB,KAAM,KAAK,EAAA;QAC3CA,cAAA,CAAA,aAAA,CAAC,QAAQ,EAAA,EACP,UAAU,EAAE;AACV,gBAAA,IAAI,CAAC,KAAK,EAAA;AACR,oBAAA,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,GAAG,KAAK;oBACpD,MAAM,KAAK,GAAG,gBAAgB,CAAC,IAAI,CAAC,SAAS,IAAI,EAAE,CAAC;oBACpD,OAAO,KAAK,IACVA,6BAAC,iBAA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