feat: make the code snippets copyable
This commit is contained in:
parent
9d9cbd8e0e
commit
3cd28d2136
@ -1,12 +1,45 @@
|
|||||||
import clsx from 'clsx'
|
import { useState, HTMLProps } from 'react'
|
||||||
import { type HTMLProps } from 'react'
|
import { CopyIcon, CheckIcon } from '@primer/octicons-react'
|
||||||
|
|
||||||
type Properties = HTMLProps<HTMLSpanElement>
|
import { cn } from '~/utils/cn'
|
||||||
|
import { toast } from '~/components/Toaster'
|
||||||
|
|
||||||
|
interface Props extends HTMLProps<HTMLSpanElement> {
|
||||||
|
isCopyable?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function Code(props: Props) {
|
||||||
|
const [isCopied, setIsCopied] = useState(false)
|
||||||
|
|
||||||
export default function Code(properties: Properties) {
|
|
||||||
return (
|
return (
|
||||||
<code className={clsx('bg-gray-100 dark:bg-zinc-700 p-0.5 rounded-md', properties.className)}>
|
<>
|
||||||
{properties.children}
|
<code className={cn(
|
||||||
</code>
|
'bg-ui-100 dark:bg-ui-800 p-0.5 rounded-md',
|
||||||
|
props.className
|
||||||
|
)}>
|
||||||
|
{props.children}
|
||||||
|
</code>
|
||||||
|
{props.isCopyable && (
|
||||||
|
<button
|
||||||
|
className={cn(
|
||||||
|
'ml-1 p-1 rounded-md',
|
||||||
|
'bg-ui-100 dark:bg-ui-800',
|
||||||
|
'text-ui-500 dark:text-ui-400',
|
||||||
|
'inline-flex items-center justify-center'
|
||||||
|
)}
|
||||||
|
onClick={() => {
|
||||||
|
navigator.clipboard.writeText(props.children as string)
|
||||||
|
toast('Copied to clipboard')
|
||||||
|
setIsCopied(true)
|
||||||
|
setTimeout(() => setIsCopied(false), 1000)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{isCopied ?
|
||||||
|
<CheckIcon className="h-3 w-3" /> :
|
||||||
|
<CopyIcon className="h-3 w-3" />
|
||||||
|
}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -50,10 +50,8 @@ export default function New(data: NewProps) {
|
|||||||
<Dialog.Text className='mb-4'>
|
<Dialog.Text className='mb-4'>
|
||||||
The machine key is given when you run
|
The machine key is given when you run
|
||||||
{' '}
|
{' '}
|
||||||
<Code>
|
<Code isCopyable>
|
||||||
tailscale up --login-server=
|
tailscale up --login-server=
|
||||||
</Code>
|
|
||||||
<Code>
|
|
||||||
{data.server}
|
{data.server}
|
||||||
</Code>
|
</Code>
|
||||||
{' '}
|
{' '}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user