mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-17 11:20:37 +09:00
Improve mobile comment copy button feedback
This commit is contained in:
parent
038dd2bb82
commit
1e4ccb2b1f
2 changed files with 127 additions and 8 deletions
|
|
@ -61,12 +61,26 @@ vi.mock("@/plugins/slots", () => ({
|
||||||
|
|
||||||
describe("CommentThread", () => {
|
describe("CommentThread", () => {
|
||||||
let container: HTMLDivElement;
|
let container: HTMLDivElement;
|
||||||
|
let writeTextMock: ReturnType<typeof vi.fn>;
|
||||||
|
let execCommandMock: ReturnType<typeof vi.fn>;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
container = document.createElement("div");
|
container = document.createElement("div");
|
||||||
document.body.appendChild(container);
|
document.body.appendChild(container);
|
||||||
vi.useFakeTimers();
|
vi.useFakeTimers();
|
||||||
vi.setSystemTime(new Date("2026-03-11T12:00:00.000Z"));
|
vi.setSystemTime(new Date("2026-03-11T12:00:00.000Z"));
|
||||||
|
writeTextMock = vi.fn(async () => {});
|
||||||
|
execCommandMock = vi.fn(() => true);
|
||||||
|
Object.assign(navigator, {
|
||||||
|
clipboard: {
|
||||||
|
writeText: writeTextMock,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
Object.defineProperty(window, "isSecureContext", {
|
||||||
|
value: true,
|
||||||
|
configurable: true,
|
||||||
|
});
|
||||||
|
document.execCommand = execCommandMock;
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
|
@ -234,4 +248,59 @@ describe("CommentThread", () => {
|
||||||
root.unmount();
|
root.unmount();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("uses a larger copy control with feedback and a clipboard fallback", async () => {
|
||||||
|
const root = createRoot(container);
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
root.render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<CommentThread
|
||||||
|
comments={[{
|
||||||
|
id: "comment-1",
|
||||||
|
companyId: "company-1",
|
||||||
|
issueId: "issue-1",
|
||||||
|
authorAgentId: null,
|
||||||
|
authorUserId: "user-1",
|
||||||
|
body: "Hello from the comment body",
|
||||||
|
createdAt: new Date("2026-03-11T11:00:00.000Z"),
|
||||||
|
updatedAt: new Date("2026-03-11T11:00:00.000Z"),
|
||||||
|
}]}
|
||||||
|
onAdd={async () => {}}
|
||||||
|
/>
|
||||||
|
</MemoryRouter>,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
const copyButton = Array.from(container.querySelectorAll("button")).find(
|
||||||
|
(element) => element.getAttribute("aria-label") === "Copy comment as markdown",
|
||||||
|
) as HTMLButtonElement | undefined;
|
||||||
|
|
||||||
|
expect(copyButton).toBeDefined();
|
||||||
|
expect(copyButton?.className).toContain("min-h-8");
|
||||||
|
expect(copyButton?.textContent).toContain("Copy");
|
||||||
|
|
||||||
|
Object.defineProperty(window, "isSecureContext", {
|
||||||
|
value: false,
|
||||||
|
configurable: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
copyButton?.click();
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(writeTextMock).not.toHaveBeenCalled();
|
||||||
|
expect(execCommandMock).toHaveBeenCalledWith("copy");
|
||||||
|
expect(copyButton?.textContent).toContain("Copied");
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
vi.advanceTimersByTime(1500);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(copyButton?.textContent).toContain("Copy");
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
root.unmount();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -210,21 +210,71 @@ function runStatusClass(status: string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function copyTextWithFallback(text: string) {
|
||||||
|
if (navigator.clipboard && window.isSecureContext) {
|
||||||
|
await navigator.clipboard.writeText(text);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const textarea = document.createElement("textarea");
|
||||||
|
textarea.value = text;
|
||||||
|
textarea.style.position = "fixed";
|
||||||
|
textarea.style.left = "-9999px";
|
||||||
|
document.body.appendChild(textarea);
|
||||||
|
|
||||||
|
try {
|
||||||
|
textarea.select();
|
||||||
|
const success = document.execCommand("copy");
|
||||||
|
if (!success) throw new Error("execCommand copy failed");
|
||||||
|
} finally {
|
||||||
|
document.body.removeChild(textarea);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function CopyMarkdownButton({ text }: { text: string }) {
|
function CopyMarkdownButton({ text }: { text: string }) {
|
||||||
const [copied, setCopied] = useState(false);
|
const [status, setStatus] = useState<"idle" | "copied" | "failed">("idle");
|
||||||
|
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => () => {
|
||||||
|
if (timeoutRef.current) {
|
||||||
|
clearTimeout(timeoutRef.current);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const label = status === "copied" ? "Copied" : status === "failed" ? "Copy failed" : "Copy";
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="text-muted-foreground hover:text-foreground transition-colors"
|
className={cn(
|
||||||
title="Copy as markdown"
|
"inline-flex min-h-8 items-center gap-1.5 rounded-md px-2.5 text-xs font-medium transition-colors",
|
||||||
|
status === "copied"
|
||||||
|
? "bg-green-100 text-green-700 dark:bg-green-500/15 dark:text-green-300"
|
||||||
|
: status === "failed"
|
||||||
|
? "bg-destructive/10 text-destructive"
|
||||||
|
: "text-muted-foreground hover:bg-accent/60 hover:text-foreground",
|
||||||
|
)}
|
||||||
|
title={label}
|
||||||
|
aria-label="Copy comment as markdown"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
navigator.clipboard.writeText(text).then(() => {
|
void copyTextWithFallback(text)
|
||||||
setCopied(true);
|
.then(() => setStatus("copied"))
|
||||||
setTimeout(() => setCopied(false), 2000);
|
.catch(() => setStatus("failed"));
|
||||||
});
|
|
||||||
|
if (timeoutRef.current) {
|
||||||
|
clearTimeout(timeoutRef.current);
|
||||||
|
}
|
||||||
|
timeoutRef.current = setTimeout(() => {
|
||||||
|
setStatus("idle");
|
||||||
|
timeoutRef.current = null;
|
||||||
|
}, 1500);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{copied ? <Check className="h-3 w-3" /> : <Copy className="h-3 w-3" />}
|
{status === "copied" ? <Check className="h-3.5 w-3.5" /> : <Copy className="h-3.5 w-3.5" />}
|
||||||
|
<span className="sm:hidden">{label}</span>
|
||||||
|
<span className="sr-only" aria-live="polite">
|
||||||
|
{label}
|
||||||
|
</span>
|
||||||
</button>
|
</button>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue