Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: Polish API key copy button and harden async clipboard fallback (#5006)

Lee Jacksonbfa17330bdf1
diff --git a/studio/frontend/src/features/auth/api-keys-page.tsx b/studio/frontend/src/features/auth/api-keys-page.tsx
index 5f76a917593..2065384a7dd 100644
--- a/studio/frontend/src/features/auth/api-keys-page.tsx
+++ b/studio/frontend/src/features/auth/api-keys-page.tsx
@@ -14,7 +14,7 @@ import {
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { cn } from "@/lib/utils";
-import { copyToClipboard } from "@/lib/copy-to-clipboard";
+import { copyToClipboardAsync } from "@/lib/copy-to-clipboard";
import {
AlertCircleIcon,
Copy01Icon,
@@ -104,20 +104,29 @@ function CopyButton({ text }: { text: string }) {
};
}, []);
- const handleCopy = () => {
- if (!copyToClipboard(text)) return;
+ const handleCopy = async () => {
+ if (!(await copyToClipboardAsync(text))) return;
setCopied(true);
if (timerRef.current) clearTimeout(timerRef.current);
timerRef.current = setTimeout(() => setCopied(false), 2000);
};
return (
- <Button variant="outline" size="sm" onClick={handleCopy} className="shrink-0">
+ <Button
+ variant="ghost"
+ size="icon-sm"
+ onClick={handleCopy}
+ className={cn(
+ "shrink-0 rounded-md text-muted-foreground hover:text-foreground",
+ copied && "text-emerald-600 hover:text-emerald-600",
+ )}
+ aria-label={copied ? "Copied API key" : "Copy API key"}
+ title={copied ? "Copied" : "Copy"}
+ >
<HugeiconsIcon
icon={copied ? Tick02Icon : Copy01Icon}
- className={cn("size-3.5 mr-1.5", copied && "text-emerald-600")}
+ className="size-4"
/>
- {copied ? "Copied" : "Copy"}
</Button>
);
}
diff --git a/studio/frontend/src/lib/copy-to-clipboard.ts b/studio/frontend/src/lib/copy-to-clipboard.ts
index df4eeec0a8f..7c97904d737 100644
--- a/studio/frontend/src/lib/copy-to-clipboard.ts
+++ b/studio/frontend/src/lib/copy-to-clipboard.ts
@@ -6,42 +6,76 @@
* Uses a synchronous textarea + execCommand fallback so the copy runs in the
* same user gesture as the click (required by Safari's clipboard security).
*/
+function copyWithExecCommand(text: string): boolean {
+ if (typeof document === "undefined" || !document.body) return false;
+
+ const textarea = document.createElement("textarea");
+ textarea.value = text;
+ textarea.readOnly = true;
+ textarea.style.position = "fixed";
+ textarea.style.top = "0";
+ textarea.style.left = "0";
+ textarea.style.fontSize = "12pt";
+ textarea.style.opacity = "0";
+ textarea.setAttribute("aria-hidden", "true");
+ document.body.appendChild(textarea);
+ textarea.focus({ preventScroll: true });
+ textarea.select();
+
+ try {
+ const ok = document.execCommand("copy");
+ document.body.removeChild(textarea);
+ return ok;
+ } catch {
+ document.body.removeChild(textarea);
+ return false;
+ }
+}
+
export function copyToClipboard(text: string): boolean {
if (typeof text !== "string" || text.length === 0) {
return false;
}
- // Synchronous fallback: works in Safari/Mac when clipboard API fails
- // because it runs entirely within the user gesture (click) stack.
- if (document.queryCommandSupported?.("copy") !== false) {
- const textarea = document.createElement("textarea");
- textarea.value = text;
- textarea.style.position = "fixed";
- textarea.style.top = "0";
- textarea.style.left = "0";
- textarea.style.opacity = "0";
- textarea.setAttribute("aria-hidden", "true");
- document.body.appendChild(textarea);
- textarea.focus({ preventScroll: true });
- textarea.select();
- try {
- const ok = document.execCommand("copy");
- document.body.removeChild(textarea);
- return ok;
- } catch {
- document.body.removeChild(textarea);
- return false;
- }
+ if (typeof document !== "undefined" && document.queryCommandSupported?.("copy") !== false) {
+ if (copyWithExecCommand(text)) return true;
}
- // Modern API only when fallback not available (e.g. non-browser)
+ // Async fallback for environments where execCommand is entirely unsupported
+ // but the Clipboard API is available (rare; kept for original contract parity).
if (typeof navigator?.clipboard?.writeText === "function") {
navigator.clipboard.writeText(text).then(
() => {},
- () => {}
+ () => {},
);
return true;
}
return false;
}
+
+export async function copyToClipboardAsync(text: string): Promise<boolean> {
+ if (typeof text !== "string" || text.length === 0) {
+ return false;
+ }
+
+ // Prefer the async Clipboard API: avoids focus disruption in Radix
+ // focus-trapped dialogs where execCommand always fails.
+ if (typeof navigator?.clipboard?.writeText === "function") {
+ try {
+ await navigator.clipboard.writeText(text);
+ return true;
+ } catch {
+ // Clipboard API rejected (e.g. NotAllowedError, permission policy).
+ // User activation is still valid through promise chains per spec, so
+ // execCommand can succeed for callers outside focus-trapped dialogs.
+ // Inside a Radix modal the focus trap will block textarea.focus() and
+ // execCommand returns false harmlessly.
+ return copyWithExecCommand(text);
+ }
+ }
+
+ // No Clipboard API (older browser / non-secure context): still in the
+ // original user-gesture frame, so execCommand can work.
+ return copyWithExecCommand(text);
+}