-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add copy to clipboard button to code snippet
- Loading branch information
1 parent
6a8a18d
commit f3abf89
Showing
3 changed files
with
62 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { createSignal, Show } from "solid-js"; | ||
import { ClipboardIcon } from "./icons/clipboard-icon"; | ||
|
||
interface CopyToClipboardProps { | ||
class?: string; | ||
manager: string; | ||
command: string; | ||
} | ||
|
||
export function CopyToClipboard(props: CopyToClipboardProps) { | ||
const copyText = () => `${props.manager} ${props.command}`; | ||
|
||
const [isCopied, setIsCopied] = createSignal(false); | ||
|
||
const copyToClipboard = async () => { | ||
try { | ||
await navigator.clipboard.writeText(copyText()); | ||
setIsCopied(true); | ||
setTimeout(() => setIsCopied(false), 2000); // Reset after 2 seconds | ||
} catch (err) { | ||
console.error("Failed to copy text: ", err); | ||
} | ||
}; | ||
|
||
return ( | ||
<button | ||
class={props.class} | ||
onClick={copyToClipboard} | ||
aria-label={isCopied ? "Copied!" : "Copy to clipboard"} | ||
> | ||
<Show when={isCopied()} fallback={<ClipboardIcon class="h-4 w-4" />}> | ||
✔︎ | ||
</Show> | ||
</button> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export function ClipboardIcon(props: { class?: string }) { | ||
return ( | ||
<svg | ||
class={props.class || "w-4 h-4"} | ||
xmlns="http://www.w3.org/2000/svg" | ||
viewBox="0 0 24 24" | ||
fill="none" | ||
stroke="currentColor" | ||
stroke-width="2" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
> | ||
<rect width="14" height="14" x="8" y="8" rx="2" ry="2" /> | ||
<path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" /> | ||
</svg> | ||
); | ||
} |