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B,SAAe;IAE/B;;OAEG;IAEH,IAAI,oBAA4B;IAEhC,gBAAgB;IAOhB,MAAM;IA4EN,MAAM,CAAC,MAAM,MAAU;CACxB;AAED,eAAe,aAAa,CAAC","file":"code-snippet.d.ts","sourcesContent":["/**\n * @license\n *\n * Copyright IBM Corp. 2019, 2021\n *\n * This source code is licensed under the Apache-2.0 license found in the\n * LICENSE file in the root directory of this source tree.\n */\n\nimport { classMap } from 'lit-html/directives/class-map';\nimport { TemplateResult } from 'lit-html';\nimport { html, property, query, customElement, LitElement } from 'lit-element';\nimport ChevronDown16 from '@carbon/icons/lib/chevron--down/16';\nimport settings from 'carbon-components/es/globals/js/settings';\nimport FocusMixin from '../../globals/mixins/focus';\nimport {\n  _createHandleFeedbackTooltip as createHandleCopyButtonFeedbackTooltip,\n  _renderButton as renderCopyButton,\n} from '../copy-button/copy-button';\nimport { CODE_SNIPPET_COLOR_SCHEME, CODE_SNIPPET_TYPE } from './defs';\nimport styles from './code-snippet.scss';\n\nexport { CODE_SNIPPET_COLOR_SCHEME, CODE_SNIPPET_TYPE };\n\nconst { prefix } = settings;\n\n/**\n * @param values The values to render.\n * @param values.children The child nodes.\n * @param values.handleClick The handler for the `click` event on the button.\n * @returns The template result for the expando.\n */\nconst renderExpando = ({ children, handleClick }: { children: string | TemplateResult; handleClick: EventListener }) => html`\n  <button type=\"button\" class=\"${prefix}--snippet-btn--expand\" @click=\"${handleClick}\">\n    <span id=\"button-text\" class=\"${prefix}--snippet-btn--text\"> ${children} </span>\n    ${ChevronDown16({\n      'aria-labeledby': 'button-text',\n      class: `${prefix}--icon-chevron--down ${prefix}--snippet__icon`,\n      role: 'img',\n    })}\n  </button>\n`;\n\n/**\n * @param values The values to render.\n * @param values.assistiveText The assistive text to announce that the node is for code snippet.\n * @param [values.expanded] `true` to show the expanded state (for multi-line variant).\n * @param values.children The child nodes.\n * @returns The template result for the code snippet.\n */\nconst renderCode = ({\n  assistiveText,\n  expanded,\n  children,\n}: {\n  assistiveText: string;\n  expanded?: boolean;\n  children: string | TemplateResult;\n}) => {\n  const classes = classMap({\n    [`${prefix}--snippet-container`]: true,\n    [`${prefix}-ce--snippet-container--expanded`]: Boolean(expanded),\n  });\n  // Ensures no extra whitespace text node\n  // prettier-ignore\n  return html`\n    <div role=\"textbox\" tabindex=\"0\" class=\"${classes}\" aria-label=\"${assistiveText}\"><code><pre>${children}</pre></code></div>\n  `;\n};\n\n/**\n * Basic code snippet.\n * @element bx-code-snippet\n */\n@customElement(`${prefix}-code-snippet`)\nclass BXCodeSnippet extends FocusMixin(LitElement) {\n  /**\n   * `true` to expand multi-line variant of code snippet.\n   */\n  private _expanded = false;\n\n  /**\n   * `true` to show the feedback tooltip.\n   */\n  private _showCopyButtonFeedback = false;\n\n  /**\n   * `true` to show the expando.\n   */\n  private _showExpando = false;\n\n  /**\n   * Handles `click` event on the copy button.\n   */\n  private _handleClickCopyButton() {\n    const { ownerDocument: doc } = this;\n    const selection = doc!.defaultView!.getSelection();\n    selection!.removeAllRanges();\n    const code = doc!.createElement('code');\n    code.className = `${prefix}--visually-hidden`;\n    const pre = doc!.createElement('pre');\n    pre.textContent = this.textContent;\n    code.appendChild(pre);\n    // Using `<code>` in shadow DOM seems to lose the LFs in some browsers\n    doc!.body.appendChild(code);\n    const range = doc!.createRange();\n    range.selectNodeContents(code);\n    selection!.addRange(range);\n    doc!.execCommand('copy');\n    this._handleCopyButtonFeedbackTooltip(this.copyButtonFeedbackTimeout);\n    doc!.body.removeChild(code);\n    selection!.removeAllRanges();\n  }\n\n  /**\n   * Handles showing/hiding the feedback tooltip.\n   */\n  private _handleCopyButtonFeedbackTooltip = createHandleCopyButtonFeedbackTooltip(\n    ({ showFeedback = false }: { showFeedback?: boolean }) => {\n      this._showCopyButtonFeedback = showFeedback;\n      this.requestUpdate();\n    }\n  );\n\n  /**\n   * Handles `click` event on the expando.\n   */\n  private _handleClickExpando() {\n    this._expanded = !this._expanded;\n    this.requestUpdate();\n  }\n\n  /**\n   * Handles change in slot content to determine if the content\n   */\n  private _handleSlotChange() {\n    const { type, _preNode: preNode } = this;\n    if (type === CODE_SNIPPET_TYPE.MULTI) {\n      if (preNode.getBoundingClientRect().height > 255) {\n        this._showExpando = true;\n        this.requestUpdate();\n      }\n    }\n  }\n\n  /**\n   * The `<pre>` element in the shadow DOM.\n   */\n  @query('pre')\n  private _preNode!: HTMLPreElement;\n\n  /**\n   * An assistive text for screen reader to advice a DOM node is for code snippet.\n   */\n  @property({ attribute: 'code-assistive-text' })\n  codeAssistiveText = 'code-snippet';\n\n  /**\n   * The context content for the collapse button.\n   */\n  @property({ attribute: 'collapse-button-text' })\n  collapseButtonText = 'Show less';\n\n  /**\n   * The color scheme.\n   */\n  @property({ attribute: 'color-scheme', reflect: true })\n  colorScheme = CODE_SNIPPET_COLOR_SCHEME.REGULAR;\n\n  /**\n   * An assistive text for screen reader to announce, telling that the button copies the content to the clipboard.\n   */\n  @property({ attribute: 'copy-button-assistive-text' })\n  copyButtonAssistiveText = 'Copy to clipboard';\n\n  /**\n   * The feedback text for the copy button.\n   */\n  @property({ attribute: 'copy-button-feedback-text' })\n  copyButtonFeedbackText = 'Copied!';\n\n  /**\n   * The number in milliseconds to determine how long the tooltip for the copy button should remain.\n   */\n  @property({ type: Number, attribute: 'copy-button-feedback-timeout' })\n  copyButtonFeedbackTimeout = 2000;\n\n  /**\n   * The context content for the expand button.\n   */\n  @property({ attribute: 'expand-button-text' })\n  expandButtonText = 'Show more';\n\n  /**\n   * The type of code snippet.\n   */\n  @property({ reflect: true })\n  type = CODE_SNIPPET_TYPE.SINGLE;\n\n  createRenderRoot() {\n    return this.attachShadow({\n      mode: 'open',\n      delegatesFocus: Number((/Safari\\/(\\d+)/.exec(navigator.userAgent) ?? ['', 0])[1]) <= 537,\n    });\n  }\n\n  render() {\n    const {\n      codeAssistiveText,\n      collapseButtonText,\n      copyButtonAssistiveText,\n      copyButtonFeedbackText,\n      expandButtonText,\n      type,\n      _expanded: expanded,\n      _showCopyButtonFeedback: showCopyButtonFeedback,\n      _showExpando: showExpando,\n      _handleClickCopyButton: handleClickCopyButton,\n      _handleClickExpando: handleClickExpando,\n      _handleSlotChange: handleSlotChange,\n    } = this;\n\n    if (type === CODE_SNIPPET_TYPE.SINGLE) {\n      // Ensures no extra whitespace text node\n      // prettier-ignore\n      return html`\n        ${renderCode({\n          assistiveText: codeAssistiveText,\n          expanded,\n          children: html`<slot @slotchange=\"${handleSlotChange}\"></slot>`,\n        })}\n        ${renderCopyButton({\n          assistiveText: copyButtonAssistiveText,\n          feedbackText: copyButtonFeedbackText,\n          showFeedback: showCopyButtonFeedback,\n          handleClickButton: handleClickCopyButton,\n          className: `${prefix}--snippet-button`,\n        })}\n      `;\n    }\n\n    if (type === CODE_SNIPPET_TYPE.MULTI) {\n      // Ensures no extra whitespace text node\n      // prettier-ignore\n      return html`\n        ${renderCode({\n          assistiveText: codeAssistiveText,\n          expanded,\n          children: html`<slot @slotchange=\"${handleSlotChange}\"></slot>`,\n        })}\n        ${renderCopyButton({\n          assistiveText: copyButtonAssistiveText,\n          feedbackText: copyButtonFeedbackText,\n          showFeedback: showCopyButtonFeedback,\n          handleClickButton: handleClickCopyButton,\n          className: `${prefix}--snippet-button`,\n        })}\n        ${!showExpando\n          ? undefined\n          : renderExpando({\n              children: expanded\n                ? html`<slot name=\"collapse-button-text\">${collapseButtonText}</slot>`\n                : html`<slot name=\"expand-button-text\">${expandButtonText}</slot>`,\n              handleClick: handleClickExpando,\n            })}\n      `;\n    }\n\n    // Ensures no extra whitespace text node\n    // prettier-ignore\n    return html`\n      ${renderCopyButton({\n        assistiveText: copyButtonAssistiveText,\n        feedbackText: copyButtonFeedbackText,\n        showFeedback: showCopyButtonFeedback,\n        handleClickButton: handleClickCopyButton,\n        className: `${prefix}--snippet ${prefix}--snippet--inline`,\n        children: html`<code aria-label=\"${codeAssistiveText}\"><slot></slot></code>`,\n      })}\n    `;\n  }\n\n  static styles = styles;\n}\n\nexport default BXCodeSnippet;\n"]}