Skip to content

fix: hide non functioning copy button on slow connections #9281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/components/codeBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import {useRef, useState} from 'react';
import {useEffect, useRef, useState} from 'react';
import {Clipboard} from 'react-feather';

import {makeKeywordsClickable} from './codeKeywords';
Expand All @@ -16,6 +16,13 @@ export function CodeBlock({filename, language, children}: CodeBlockProps) {
const [showCopied, setShowCopied] = useState(false);
const codeRef = useRef<HTMLDivElement>(null);

// Show the copy button after js has loaded
// otherwise the copy button will not work
const [showCopyButton, setShowCopyButton] = useState(false);
useEffect(() => {
setShowCopyButton(true);
}, []);

async function copyCode() {
if (codeRef.current === null) {
return;
Expand All @@ -39,9 +46,11 @@ export function CodeBlock({filename, language, children}: CodeBlockProps) {
<div className="code-block">
<div className="code-actions">
<code className="filename">{filename}</code>
<button className="copy" onClick={() => copyCode()}>
<Clipboard size={16} />
</button>
{showCopyButton && (
<button className="copy" onClick={() => copyCode()}>
<Clipboard size={16} />
</button>
)}
</div>
<div className="copied" style={{opacity: showCopied ? 1 : 0}}>
Copied
Expand Down