Files
fnx_web/svelte/src/layout/CopyButton.svelte
T

74 lines
1.1 KiB
Svelte
Raw Normal View History

<script lang="ts">
2025-10-13 16:05:50 +02:00
import { copy_text } from "util/Util";
2025-10-13 16:05:50 +02:00
let {
text = "",
style = "",
large_icon = false,
small_icon = false,
children
}: {
text?: string;
style?: string;
large_icon?: boolean;
small_icon?: boolean;
children?: import('svelte').Snippet;
} = $props();
let failed = $state(false)
let success = $state(false)
// Exported so it can be called manually
export const copy = () => {
try {
copy_text(text)
success = true
} catch (err) {
console.error("Failed to copy text", err)
failed = true
}
setTimeout(() => {
success = false
failed = false
}, 10000)
}
</script>
<button
2025-10-13 16:05:50 +02:00
onclick={copy}
style={style}
class="button"
class:button_highlight={success}
class:button_red={failed}
class:large_icon
2025-03-21 01:11:03 +01:00
class:small_icon
title="Copy text to clipboard"
disabled={text === ""}
>
<i class="icon">content_copy</i>
<span>
{#if success}
Copied!
{:else if failed}
Copy failed
{:else}
2025-10-13 16:05:50 +02:00
{@render children?.()}
{/if}
</span>
</button>
<style>
.large_icon {
flex-direction: column;
min-width: 5em;
}
.large_icon>.icon {
font-size: 40px;
display: block;
}
2025-03-21 01:11:03 +01:00
.small_icon>.icon {
font-size: 1.2em;
display: block;
}
</style>