~james/core

core/web/src/lib/components/ui/CopyButton.svelte -rw-r--r-- 5.5 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<script module lang="ts">
	export type CopyStatus = "idle" | "copied" | "error";
</script>

<script lang="ts">
	import type { ResolvedPathname } from "$app/types";
	import type { Component } from "svelte";
	import type { SvelteHTMLElements } from "svelte/elements";
	import Copy from "$icon/copy";
	import X from "$icon/x";
	import {
		CROSSFADE_DURATION,
		CROSSFADE_EASING,
		DRAW_DURATION,
		DRAW_EASING
	} from "#lib/motion/spring.ts";
	import { prefersReducedMotion } from "svelte/motion";
	import type { Snippet } from "svelte";
	import Button, { buttonIcon, type ButtonVariants } from "#lib/components/ui/Button.svelte";

	interface Props {
		// a function gets called on click, not when the button renders
		value: string | (() => string);
		href?: ResolvedPathname;
		icon?: Component<SvelteHTMLElements["svg"]>;
		label?: string;
		copiedLabel?: string;
		errorLabel?: string;
		showLabel?: boolean;
		timeout?: number;
		variant?: ButtonVariants["variant"];
		size?: ButtonVariants["size"];
		disabled?: boolean;
		onCopy?: (value: string) => void;
		onError?: (reason: unknown) => void;
		title?: string;
		"aria-label"?: string;
		class?: string;
		children?: Snippet;
		feedback?: "label" | "icon";
	}

	let {
		value,
		href,
		icon: Icon = Copy,
		label = "Copy",
		copiedLabel = "Copied",
		errorLabel = "Failed",
		showLabel = false,
		timeout = 2000,
		variant = "ghost",
		size = "small",
		disabled = false,
		onCopy,
		onError,
		class: className,
		title,
		"aria-label": ariaLabel,
		children,
		feedback = "label"
	}: Props = $props();

	let status = $state<CopyStatus>("idle");
	let labelWidths = $state<Partial<Record<CopyStatus, number>>>({});
	let timer: ReturnType<typeof setTimeout> | undefined;

	$effect(() => () => clearTimeout(timer));

	const copy = async () => {
		try {
			const text = typeof value === "function" ? value() : value;
			await navigator.clipboard.writeText(text);
			status = "copied";
			onCopy?.(text);
		} catch (reason) {
			status = "error";
			onError?.(reason);
		}
		clearTimeout(timer);
		timer = setTimeout(() => (status = "idle"), timeout);
	};

	const iconSize = $derived(buttonIcon({ size }));
	const draw = $derived(prefersReducedMotion.current ? 0 : DRAW_DURATION);
	const layer = (key: CopyStatus) =>
		`swap-layer ${iconSize}${key === status ? "" : " swap-icon-out"}`;
	const labels = $derived<[CopyStatus, string][]>([
		["idle", label],
		["copied", copiedLabel],
		["error", errorLabel]
	]);
</script>

{#snippet icon()}
	<span class="cell {iconSize} grid shrink-0" data-status={status} aria-hidden="true">
		<Icon class={layer("idle")} />
		{@render check()}
		<X class={layer("error")} />
	</span>
{/snippet}

{#snippet check()}
	<!-- lucide's check reversed, so the stroke draws from the short leg up to the
	     tip the way a hand would write it. pathLength normalises the dash to 1. -->
	<svg
		class={layer("copied")}
		viewBox="0 0 24 24"
		fill="none"
		stroke="currentColor"
		stroke-width="2"
		stroke-linecap="round"
		stroke-linejoin="round"
	>
		<path class="check" pathLength="1" d="M4 12l5 5L20 6" />
	</svg>
{/snippet}

<Button
	{href}
	{variant}
	{size}
	{disabled}
	class={className}
	{title}
	aria-label={ariaLabel ?? label}
	onclick={(event) => {
		event.preventDefault();
		void copy();
	}}
	style="--swap-duration: {CROSSFADE_DURATION}ms; --swap-easing: {CROSSFADE_EASING}; --copy-draw: {draw}ms; --copy-draw-ease: {DRAW_EASING}"
>
	{#if children && feedback === "icon"}
		<span class="flex items-center">
			<span
				class={[
					"grid transition-[grid-template-columns] duration-(--swap-duration) ease-(--swap-easing)",
					status === "idle" ? "grid-cols-[0fr]" : "grid-cols-[1fr]"
				]}
				aria-hidden="true"
			>
				<span class="flex overflow-hidden">
					<span class="cell {iconSize} mr-2xs grid shrink-0" data-status={status}>
						{@render check()}
						<X class={layer("error")} />
					</span>
				</span>
			</span>
			<span class="whitespace-nowrap">{@render children()}</span>
		</span>
	{:else if children}
		<span class="grid justify-items-center">
			<span class={["whitespace-nowrap swap-layer", status !== "idle" && "swap-label-out"]}>
				{@render children()}
			</span>
			<span
				class={[
					"flex items-center gap-2xs swap-layer",
					status === "idle" && "swap-label-out"
				]}
				aria-hidden="true"
			>
				{@render icon()}
				<span class="grid font-sans">
					{#each labels.filter(([key]) => key !== "idle") as [key, text] (key)}
						<span
							class={[
								"whitespace-nowrap swap-layer",
								key !== status && "swap-label-out"
							]}>{text}</span
						>
					{/each}
				</span>
			</span>
		</span>
	{:else}
		{@render icon()}
		{#if showLabel}
			<span
				class="relative grid justify-items-center transition-[width] duration-(--swap-duration) ease-(--swap-easing)"
				style:width={labelWidths[status] === undefined
					? undefined
					: `${labelWidths[status]}px`}
				aria-hidden="true"
			>
				{#each labels as [key, text] (key)}
					<span
						class={[
							"whitespace-nowrap swap-layer",
							key !== status && "absolute inset-x-0 mx-auto w-max swap-label-out"
						]}
						bind:offsetWidth={labelWidths[key]}>{text}</span
					>
				{/each}
			</span>
		{/if}
	{/if}
	<span role="status" aria-live="polite" class="sr-only">
		{status === "copied" ? copiedLabel : status === "error" ? errorLabel : ""}
	</span>
</Button>

<style>
	.check {
		stroke-dasharray: 1;
		stroke-dashoffset: 1;
		transition: stroke-dashoffset var(--copy-draw) var(--copy-draw-ease);
	}

	.cell[data-status="copied"] .check {
		stroke-dashoffset: 0;
	}
</style>