fix: use client-side ref to handle onclick string from transformerCopyButton
This commit is contained in:
19
components/blog/button-client.tsx
Normal file
19
components/blog/button-client.tsx
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
'use client'
|
||||||
|
|
||||||
|
import { useEffect, useRef } from 'react'
|
||||||
|
|
||||||
|
export function ClientButton({ children, onClick, ...props }: { children?: React.ReactNode; onClick?: string } & React.ButtonHTMLAttributes<HTMLButtonElement>) {
|
||||||
|
const btnRef = useRef<HTMLButtonElement>(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof onClick === 'string' && btnRef.current) {
|
||||||
|
btnRef.current.onclick = onClick as any
|
||||||
|
}
|
||||||
|
}, [onClick])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button ref={btnRef} {...props}>
|
||||||
|
{children}
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { MDXComponents } from 'mdx/types'
|
import type { MDXComponents } from 'mdx/types'
|
||||||
|
import { ClientButton } from '@/components/blog/button-client'
|
||||||
|
|
||||||
export function getMDXComponents(components: MDXComponents): MDXComponents {
|
export function getMDXComponents(components: MDXComponents): MDXComponents {
|
||||||
return {
|
return {
|
||||||
@@ -106,10 +107,9 @@ export function getMDXComponents(components: MDXComponents): MDXComponents {
|
|||||||
details: (props) => <details className="my-4 rounded-lg border border-border p-4" {...props} />,
|
details: (props) => <details className="my-4 rounded-lg border border-border p-4" {...props} />,
|
||||||
summary: (props) => <summary className="cursor-pointer font-semibold" {...props} />,
|
summary: (props) => <summary className="cursor-pointer font-semibold" {...props} />,
|
||||||
|
|
||||||
button: ({ children, onClick, ...props }) => (
|
button: ({ children, onClick, ...props }) => {
|
||||||
typeof onClick === 'string'
|
const onClickString = typeof onClick === 'string' ? onClick : undefined
|
||||||
? <button {...props} dangerouslySetInnerHTML={{ __html: children as string }} onclick={onClick} />
|
return <ClientButton onClick={onClickString} {...props}>{children}</ClientButton>
|
||||||
: <button {...props} onClick={onClick}>{children}</button>
|
},
|
||||||
),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user