Files
Thuc Pham 06e085e58e feat: code editor & document editor (#79)
* feat: code editor

* fix lock

* fix format

* enhance style

* use github light

* increase font-size

* install document editor packages

* enhance styling of code editor

* add document editor

* add menu add setup

* fix document editor

* missing plugin

* transform markdown

* update editor style

* remove unused packages

* handle save content

* fix context

* refactor: updateArtifact

* reparse markdown when changing versions

* add toolbars

* update icon

* enhance toolbar actions

* fix format

* Create thin-shrimps-agree.md

* fix: lint

* update text

* use light theme for code in example pages

* fix overlapping header in codeblock
2025-05-05 11:38:55 +07:00

45 lines
1.1 KiB
TypeScript

import hljs from 'highlight.js'
import { useEffect, useRef } from 'react'
import { useCopyToClipboard } from '@/app/use-copy-to-clipboard'
export function Code({
content,
language,
}: {
content: string
language: string
}) {
const codeRef = useRef<HTMLElement>(null)
const { copyToClipboard, isCopied } = useCopyToClipboard({
timeout: 2000,
})
useEffect(() => {
if (codeRef.current && codeRef.current.dataset.highlighted !== 'yes') {
hljs.highlightElement(codeRef.current)
}
}, [language, content])
return (
<div className="relative">
<button
type="button"
onClick={() => {
copyToClipboard(content)
}}
className="absolute right-2 top-2 rounded-md bg-zinc-700 px-3 py-1 text-sm text-white hover:bg-zinc-600"
>
{isCopied ? 'Copied!' : 'Copy'}
</button>
<pre className="overflow-hidden rounded-lg border border-zinc-700 shadow-lg">
<code
className={`language-${language} block p-4 font-mono`}
ref={codeRef}
>
{content}
</code>
</pre>
</div>
)
}