CodePreview.jsx
This file
Path
examples/demo/src/components/CodePreview.jsx
40 lines · 1.4 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than f85898f point at it.
chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 1–40why, one step back1import { create } from 'zustand'2import { Highlight } from 'prism-react-renderer'3import CopyButton from './CopyButton'4import SnippetLang from './SnippetLang'5import javascriptCode from '../resources/javascript-code'6import typescriptCode from '../resources/typescript-code'78const useStore = create((set, get) => ({9 lang: 'javascript',10 setLang: (lang) => set(() => ({ lang })),11 getCode: () =>12 get().lang === 'javascript' ? javascriptCode : typescriptCode,13}))1415export default function CodePreview() {16 const { lang, setLang, getCode } = useStore()17 const code = getCode()1819 return (20 <Highlight code={code} language="tsx" theme={undefined}>21 {({ className, style, tokens, getLineProps, getTokenProps }) => (22 // define how each line is to be rendered in the code block,23 // position is set to relative so the copy button can align to bottom right24 <pre className={className} style={{ ...style, position: 'relative' }}>25 {tokens.map((line, i) => (26 <div {...getLineProps({ line })} key={i}>27 {line.map((token, key) => (28 <span {...getTokenProps({ token })} key={key} />29 ))}30 </div>31 ))}32 <div className="snippet-container">33 <SnippetLang lang={lang} setLang={setLang} />34 <CopyButton code={code} />35 </div>36 </pre>37 )}38 </Highlight>39 )40}