Skip to content

Isometric line figures that answer the pointer. For React and for anything with a DOM.

Copy: a second copy restarts the time Copied shows, instead of ending with what was left of the first

Lucas Marques3424b7d44c2e
diff --git a/apps/site/components/copy.tsx b/apps/site/components/copy.tsx
index 9d5b34d..aa97f28 100644
--- a/apps/site/components/copy.tsx
+++ b/apps/site/components/copy.tsx
@@ -9,18 +9,19 @@ import { useCallback, useEffect, useState, type RefObject } from "react";
* the text reached the clipboard.
*/
export function useCopy(): [copied: boolean, copy: (text: string, fallback?: Element | null) => Promise<boolean>] {
- const [copied, setCopied] = useState(false);
+ // counted, not flagged, so a copy made while the last one shows restarts its time
+ const [copies, setCopies] = useState(0);
useEffect(() => {
- if (!copied) return;
- const timer = setTimeout(() => setCopied(false), 1600);
+ if (!copies) return;
+ const timer = setTimeout(() => setCopies(0), 1600);
return () => clearTimeout(timer);
- }, [copied]);
+ }, [copies]);
const copy = useCallback(async (text: string, fallback?: Element | null) => {
try {
await navigator.clipboard.writeText(text);
- setCopied(true);
+ setCopies((n) => n + 1);
return true;
} catch {
if (!fallback) return false;
@@ -33,7 +34,7 @@ export function useCopy(): [copied: boolean, copy: (text: string, fallback?: Ele
}
}, []);
- return [copied, copy];
+ return [copies > 0, copy];
}
/**
diff --git a/apps/site/test/site.spec.ts b/apps/site/test/site.spec.ts
index b5e665f..8215d1b 100644
--- a/apps/site/test/site.spec.ts
+++ b/apps/site/test/site.spec.ts
@@ -244,6 +244,25 @@ test("the install pill copies every manager's command", async ({ page, context }
await expect(pill).toHaveAttribute("data-install", "npm");
});
+test("a second copy keeps Copied up for its own full time, not what was left of the first", async ({ page, context }) => {
+ await context.grantPermissions(["clipboard-read", "clipboard-write"]);
+ await page.clock.install();
+ await page.goto("/");
+ const pill = page.locator("[data-install]").first();
+ const copy = pill.getByRole("button", { name: "Copy install command" });
+ await copy.click();
+ await expect(copy).toHaveAttribute("data-copied", "true");
+ await page.clock.runFor(1000);
+ await pill.getByRole("button", { name: /\(npm\): switch to/ }).click();
+ await copy.click();
+ // the second copy has landed once the clipboard holds its command
+ await expect.poll(() => page.evaluate(() => navigator.clipboard.readText())).toBe(MANAGERS[1][1]);
+ await page.clock.runFor(1000);
+ await expect(pill.getByText("Copied")).toBeAttached();
+ await page.clock.runFor(700);
+ await expect(copy).not.toHaveAttribute("data-copied");
+});
+
test("without a clipboard, copy selects the text instead and throws nothing", async ({ page }) => {
await page.addInitScript(() => {
Object.defineProperty(navigator, "clipboard", { value: { writeText: () => Promise.reject(new DOMException("denied", "NotAllowedError")) } });