An open source design system that's fully customizable and agent ready
TypeScript
fix(codeblock): restart the copied-indicator timer on rapid re-copy (#3769)
Shahrier Islam Arham3b04b19831a1
diff --git a/.changeset/codeblock-copy-reset-timer.md b/.changeset/codeblock-copy-reset-timer.mdnew file mode 100644index 0000000000..13a7aec20e--- /dev/null+++ b/.changeset/codeblock-copy-reset-timer.md@@ -0,0 +1,8 @@+---+'@astryxdesign/core': patch+---++[fix] CodeBlock: restart the copied-indicator timer on rapid re-copy++Each copy click armed an independent 2s reset timer, so copying again within the window let the first click's timer revert the "Copied" indicator early. The timer now restarts on every copy and is cleared on unmount.+@arham766diff --git a/packages/core/src/CodeBlock/CodeBlock.test.tsx b/packages/core/src/CodeBlock/CodeBlock.test.tsxindex 7c797e0836..b689fa9460 100644--- a/packages/core/src/CodeBlock/CodeBlock.test.tsx+++ b/packages/core/src/CodeBlock/CodeBlock.test.tsx@@ -10,7 +10,7 @@*/import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest';-import {render, screen, fireEvent, waitFor} from '@testing-library/react';+import {act, render, screen, fireEvent, waitFor} from '@testing-library/react';import {CodeBlock} from './CodeBlock';import {__resetLiveRegionsForTest} from '../hooks/useAnnounce';import {dracula} from '../theme/syntax';@@ -72,6 +72,41 @@ describe('CodeBlock', () => {});});+ it('keeps the copied indicator a full 2s after a rapid re-copy', async () => {+ vi.useFakeTimers();+ try {+ render(<CodeBlock code="const x = 1;" language="javascript" />);+ fireEvent.click(screen.getByRole('button', {name: 'Copy code'}));+ // Flush the async clipboard write.+ await act(async () => {});+ expect(screen.getByRole('button', {name: 'Copied'})).toBeInTheDocument();++ // 1.5s later the user copies again.+ act(() => {+ vi.advanceTimersByTime(1500);+ });+ fireEvent.click(screen.getByRole('button', {name: 'Copied'}));+ await act(async () => {});++ // 600ms after the second copy (2.1s after the first): the first+ // click's timer must not have reverted the indicator early.+ act(() => {+ vi.advanceTimersByTime(600);+ });+ expect(screen.getByRole('button', {name: 'Copied'})).toBeInTheDocument();++ // It resets 2s after the most recent copy.+ act(() => {+ vi.advanceTimersByTime(1400);+ });+ expect(+ screen.getByRole('button', {name: 'Copy code'}),+ ).toBeInTheDocument();+ } finally {+ vi.useRealTimers();+ }+ });+it('does NOT collapse the block when the copy button is clicked', () => {render(<CodeBlockdiff --git a/packages/core/src/CodeBlock/CodeBlock.tsx b/packages/core/src/CodeBlock/CodeBlock.tsxindex 20548f9160..bb06e54663 100644--- a/packages/core/src/CodeBlock/CodeBlock.tsx+++ b/packages/core/src/CodeBlock/CodeBlock.tsx@@ -663,8 +663,18 @@ export function CodeBlock({...props}: CodeBlockProps) {const [copied, setCopied] = useState(false);+ const copyResetTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);const announce = useAnnounce();+ // Clear a pending "copied" reset when the block unmounts.+ useEffect(() => {+ return () => {+ if (copyResetTimerRef.current != null) {+ clearTimeout(copyResetTimerRef.current);+ }+ };+ }, []);+const useSpans =highlightMode === 'spans' ||(highlightMode === 'auto' && !hasHighlightAPI()) ||@@ -693,7 +703,15 @@ export function CodeBlock({// screen readers, so confirm the copy via a polite live region.announce('Copied');onCopy?.();- setTimeout(() => setCopied(false), 2000);+ // Restart the reset timer on every copy — otherwise a rapid re-copy+ // is reverted early by the previous click's timer.+ if (copyResetTimerRef.current != null) {+ clearTimeout(copyResetTimerRef.current);+ }+ copyResetTimerRef.current = setTimeout(() => {+ copyResetTimerRef.current = null;+ setCopied(false);+ }, 2000);} catch {// Clipboard failures leave the copied state unchanged.}