Skip to content

Commit

Permalink
copy button refactor and changeset (#1063)
Browse files Browse the repository at this point in the history
refactor and add changeset
  • Loading branch information
dimaMachina committed Dec 11, 2022
1 parent 29968c1 commit 99ec64e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .changeset/brown-rice-attend.md
@@ -0,0 +1,7 @@
---
'nextra': patch
'nextra-theme-blog': patch
'nextra-theme-docs': patch
---

fix indentation for copy code button
25 changes: 15 additions & 10 deletions packages/nextra/src/components/pre.tsx
@@ -1,4 +1,11 @@
import React, { ComponentProps, ReactElement, LegacyRef, useCallback, useRef, useState, useEffect } from 'react'
import React, {
ComponentProps,
ReactElement,
useCallback,
useRef,
useState,
useEffect
} from 'react'
import { CopyToClipboard } from './copy-to-clipboard'
import { Button } from './button'
import { WordWrapIcon } from '../icons'
Expand All @@ -13,8 +20,8 @@ export const Pre = ({
filename?: string
value?: string
}): ReactElement => {
const ref = useRef<HTMLPreElement | undefined>();
const [codeString, setCodeString] = useState<string | undefined>(value);
const preRef = useRef<HTMLPreElement | null>(null)
const [codeString, setCodeString] = useState(value)

const toggleWordWrap = useCallback(() => {
const htmlDataset = document.documentElement.dataset
Expand All @@ -27,14 +34,12 @@ export const Pre = ({
}, [])

useEffect(() => {
if (typeof ref.current !== "undefined") {
let code = ref.current.querySelector("code");
const codeEl = preRef.current?.querySelector('code')

if (typeof code?.textContent === "string") {
setCodeString(code.textContent);
}
if (codeEl?.textContent) {
setCodeString(codeEl.textContent)
}
}, [ref]);
}, [preRef])

return (
<>
Expand All @@ -50,7 +55,7 @@ export const Pre = ({
filename ? 'nx-pt-12 nx-pb-4' : 'nx-py-4',
className
].join(' ')}
ref={ref as LegacyRef<HTMLPreElement> | undefined}
ref={preRef}
{...props}
>
{children}
Expand Down

1 comment on commit 99ec64e

@vercel
Copy link

@vercel vercel bot commented on 99ec64e Dec 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.