From a12b13f7d1150b93f502ca0adfc9531eef73173e Mon Sep 17 00:00:00 2001 From: Krishna Ayyalasomayajula Date: Mon, 1 Jun 2026 23:31:20 -0500 Subject: [PATCH] fix: use client-side ref to handle onclick string from transformerCopyButton --- components/blog/button-client.tsx | 19 +++++++++++++++++++ mdx-components.tsx | 10 +++++----- 2 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 components/blog/button-client.tsx diff --git a/components/blog/button-client.tsx b/components/blog/button-client.tsx new file mode 100644 index 0000000..22c3c09 --- /dev/null +++ b/components/blog/button-client.tsx @@ -0,0 +1,19 @@ +'use client' + +import { useEffect, useRef } from 'react' + +export function ClientButton({ children, onClick, ...props }: { children?: React.ReactNode; onClick?: string } & React.ButtonHTMLAttributes) { + const btnRef = useRef(null) + + useEffect(() => { + if (typeof onClick === 'string' && btnRef.current) { + btnRef.current.onclick = onClick as any + } + }, [onClick]) + + return ( + + ) +} diff --git a/mdx-components.tsx b/mdx-components.tsx index 750c996..9991dca 100644 --- a/mdx-components.tsx +++ b/mdx-components.tsx @@ -1,4 +1,5 @@ import type { MDXComponents } from 'mdx/types' +import { ClientButton } from '@/components/blog/button-client' export function getMDXComponents(components: MDXComponents): MDXComponents { return { @@ -106,10 +107,9 @@ export function getMDXComponents(components: MDXComponents): MDXComponents { details: (props) =>
, summary: (props) => , - button: ({ children, onClick, ...props }) => ( - typeof onClick === 'string' - ? - ), + button: ({ children, onClick, ...props }) => { + const onClickString = typeof onClick === 'string' ? onClick : undefined + return {children} + }, } }