File size: 1,257 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// @ts-nocheck
import { ButtonCopy } from '../Buttons/ButtonCopy'
import { LivePreview, LivePreviewProps } from './LivePreview'
import { pre } from './Pre.css'
import clsx from 'clsx'
import { preCopy } from './Code.css'

interface CodeProps
  extends Pick<
    LivePreviewProps,
    'code' | 'defaultOpen' | 'showCode' | 'template'
  > {
  id?: string
  isLive?: boolean
  showLineNumbers?: boolean
  ['data-showing-lines']?: boolean
  children?: any
  className?: string
  copy?: string
}

export const Code = ({
  isLive,
  code,
  showCode,
  className,
  copy,
  defaultOpen,
  template,
  showLineNumbers,
  id,
  'data-showing-lines': dataShowingLines,
  children,
  ...restProps
}: CodeProps) => {
  if (isLive) {
    return (
      <LivePreview
        code={code}
        showCode={showCode}
        className={className}
        defaultOpen={defaultOpen}
        preProps={{
          showLineNumbers,
          id,
          ['data-showing-lines']: dataShowingLines,
          children,
        }}
        template={template}
      />
    )
  } else {
    return (
      <pre className={clsx(pre, className)} {...restProps}>
        {copy ? <ButtonCopy className={preCopy}>{copy}</ButtonCopy> : null}
        {children}
      </pre>
    )
  }
}