~james/core

core/web/src/lib/components/ui/Button.svelte -rw-r--r-- 16.2 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<script module lang="ts">
	import { getContext, setContext } from "svelte";
	import { tv, type VariantProps } from "tailwind-variants";

	const emphasis = [
		"[--btn-from:color-mix(in_oklch,var(--btn-brand),white_15%)] [--btn-highlight:color-mix(in_oklch,var(--btn-brand),white_30%)] [--btn-ring:color-mix(in_oklch,var(--btn-brand),black_10%)] [--btn-ring-from:color-mix(in_oklch,var(--btn-from),black_10%)]",
		"bg-(--btn-highlight) bg-linear-to-b from-(--btn-from) to-(--btn-brand) text-white text-shadow-2xs/25 [&_svg:not(.animate-spin)]:drop-shadow-[0_1px_0_rgb(0_0_0/0.25)] [&_[data-spinner]]:drop-shadow-[0_1px_0_rgb(0_0_0/0.25)] shadow-[inset_0_2px_0_0_var(--btn-highlight)] edge-ring-inset inset-ring-transparent",
		"after:pointer-events-none after:absolute after:inset-0 after:rounded-[inherit] after:p-px after:bg-linear-to-b after:from-(--btn-ring-from) after:to-(--btn-ring) after:[mask:linear-gradient(#000_0_0)_content-box_exclude,linear-gradient(#000_0_0)] after:transition-[--tw-gradient-from] after:duration-150 after:ease-out-strong",
		"hover:from-(--btn-highlight) hover:[--btn-ring-from:color-mix(in_oklch,var(--btn-highlight),black_10%)] aria-expanded:from-(--btn-highlight) aria-expanded:[--btn-ring-from:color-mix(in_oklch,var(--btn-highlight),black_10%)]",
		"focus-visible:outline-(--btn-brand)",
		"disabled:opacity-50 aria-disabled:opacity-50"
	];

	export const button = tv({
		base: [
			"relative inline-flex cursor-pointer items-center justify-center overflow-hidden rounded-l-(--btn-radius-l) rounded-r-(--btn-radius-r) text-center no-underline select-none [--btn-radius-l:var(--btn-radius)] [--btn-radius-r:var(--btn-radius)] [--btn-radius:var(--radius-md)]",
			"hover:no-underline",
			"transition-[scale,background-color,color,--tw-gradient-from] duration-150 ease-out-strong",
			"active:scale-(--btn-press,0.97) motion-reduce:active:scale-100",
			"focus-visible:outline-2 focus-visible:outline-offset-2",
			"disabled:pointer-events-none aria-disabled:pointer-events-none"
		],
		variants: {
			variant: {
				default: [
					"bg-background-default text-foreground-default edge-ring-inset",
					"hover:bg-background-inset aria-expanded:bg-background-subtle",
					"focus-visible:outline-border-focus",
					"disabled:bg-background-subtle disabled:text-foreground-subtle disabled:inset-ring-transparent",
					"aria-disabled:bg-background-subtle aria-disabled:text-foreground-subtle aria-disabled:inset-ring-transparent"
				],
				primary: ["[--btn-brand:var(--color-background-info-emphasis)]", ...emphasis],
				destructive: ["[--btn-brand:var(--color-red-700)]", ...emphasis],
				ghost: [
					"bg-transparent",
					"hover:bg-background-inset aria-expanded:bg-background-inset",
					"focus-visible:outline-border-focus",
					"disabled:text-foreground-disabled aria-disabled:text-foreground-disabled"
				],
				danger: [
					"bg-background-default text-foreground-danger edge-ring-inset",
					"hover:bg-background-subtle aria-expanded:bg-background-subtle",
					"focus-visible:outline-foreground-danger",
					"disabled:text-foreground-danger-disabled disabled:inset-ring-border-subtle aria-disabled:text-foreground-danger-disabled"
				],
				success: [
					"bg-background-default text-foreground-success edge-ring-inset",
					"hover:bg-background-subtle aria-expanded:bg-background-subtle",
					"focus-visible:outline-foreground-success",
					"disabled:text-foreground-success-disabled disabled:inset-ring-border-subtle aria-disabled:text-foreground-success-disabled"
				],
				warning: [
					"bg-background-default text-foreground-warning edge-ring-inset",
					"hover:bg-background-subtle aria-expanded:bg-background-subtle",
					"focus-visible:outline-foreground-warning",
					"disabled:text-foreground-warning-disabled disabled:inset-ring-border-subtle aria-disabled:text-foreground-warning-disabled"
				]
			},
			size: {
				mini: "min-h-[calc(1.5rem-var(--seg-inset,0px))] min-w-xl gap-2xs px-2xs py-3xs typography-paragraph-mini [--btn-radius:0.25rem]",
				small: "min-h-[calc(1.75rem-var(--seg-inset,0px))] gap-2xs px-2 py-3xs typography-paragraph-small [--btn-radius:var(--radius-sm)]",
				regular:
					"min-h-[calc(2rem-var(--seg-inset,0px))] gap-1.5 px-sm py-3xs typography-paragraph-regular",
				large: "min-h-[calc(2.25rem-var(--seg-inset,0px))] gap-xs px-sm py-3xs typography-paragraph-large"
			},
			iconSide: {
				left: "",
				right: "",
				none: ""
			},
			square: {
				true: "min-w-0 px-0",
				false: ""
			},
			hideLabelOnMobile: {
				true: "mobile:min-w-0 mobile:px-0",
				false: ""
			}
		},
		compoundVariants: [
			{ square: true, size: "mini", class: "w-[calc(1.5rem-var(--seg-inset,0px))]" },
			{ square: true, size: "small", class: "w-[calc(1.75rem-var(--seg-inset,0px))]" },
			{ square: true, size: "regular", class: "w-[calc(2rem-var(--seg-inset,0px))]" },
			{ square: true, size: "large", class: "w-[calc(2.25rem-var(--seg-inset,0px))]" },
			{
				hideLabelOnMobile: true,
				size: "mini",
				class: "mobile:w-[calc(1.5rem-var(--seg-inset,0px))]"
			},
			{
				hideLabelOnMobile: true,
				size: "small",
				class: "mobile:w-[calc(1.75rem-var(--seg-inset,0px))]"
			},
			{
				hideLabelOnMobile: true,
				size: "regular",
				class: "mobile:w-[calc(2rem-var(--seg-inset,0px))]"
			},
			{
				hideLabelOnMobile: true,
				size: "large",
				class: "mobile:w-[calc(2.25rem-var(--seg-inset,0px))]"
			},
			{ size: "mini", iconSide: "left", class: "pl-1" },
			{ size: "mini", iconSide: "right", class: "pr-1" },
			{ size: "small", iconSide: "left", class: "pl-1.75" },
			{ size: "small", iconSide: "right", class: "pr-1.75" },
			{ size: "regular", iconSide: "left", class: "pl-2.25" },
			{ size: "regular", iconSide: "right", class: "pr-2.25" },
			{ size: "large", iconSide: "left", class: "pl-2.75" },
			{ size: "large", iconSide: "right", class: "pr-2.75" }
		],
		defaultVariants: {
			variant: "default",
			size: "regular",
			iconSide: "none",
			square: false,
			hideLabelOnMobile: false
		}
	});

	export const buttonIcon = tv({
		base: "shrink-0",
		variants: {
			size: {
				mini: "size-3",
				small: "size-3.5",
				regular: "size-3.5",
				large: "size-4"
			}
		},
		defaultVariants: {
			size: "regular"
		}
	});

	export type ButtonVariants = VariantProps<typeof button>;
	export type ButtonIconVariants = VariantProps<typeof buttonIcon>;

	const BUTTON_ICON_SIZE = Symbol("button-icon-size");

	export const setButtonIconSize = (size: () => ButtonIconVariants["size"]) =>
		setContext(BUTTON_ICON_SIZE, size);

	export const getButtonIconSize = () =>
		getContext<(() => ButtonIconVariants["size"]) | undefined>(BUTTON_ICON_SIZE);
</script>

<script lang="ts">
	import type { ResolvedPathname } from "$app/types";
	import type { Component, Snippet } from "svelte";
	import type {
		HTMLAnchorAttributes,
		HTMLButtonAttributes,
		SvelteHTMLElements
	} from "svelte/elements";
	import Spinner from "#lib/components/ui/Spinner.svelte";
	import Check from "$icon/check";
	import { on } from "svelte/events";

	interface Props extends Omit<
		HTMLAnchorAttributes & HTMLButtonAttributes,
		"class" | "children"
	> {
		variant?: ButtonVariants["variant"];
		size?: ButtonVariants["size"];
		square?: boolean;
		href?: ResolvedPathname;
		type?: "button" | "submit" | "reset";
		disabled?: boolean;
		icon?: Component<SvelteHTMLElements["svg"]>;
		iconSide?: "left" | "right";
		hideLabelOnMobile?: boolean;
		class?: string;
		loading?: boolean;
		holdToConfirm?: boolean;
		onconfirm?: () => void;
		spinnerClass?: string;
		ref?: HTMLAnchorElement | HTMLButtonElement;
		children?: Snippet;
	}

	let {
		variant = "default",
		size = "regular",
		square,
		href,
		type = "button",
		disabled = false,
		icon,
		iconSide = "left",
		hideLabelOnMobile = false,
		class: className,
		loading = false,
		holdToConfirm = false,
		onconfirm,
		spinnerClass,
		ref = $bindable(),
		children,
		...rest
	}: Props = $props();

	const classes = $derived(
		button({
			variant,
			size,
			square: square ?? Boolean(icon && !children),
			iconSide: icon && children ? iconSide : "none",
			hideLabelOnMobile: Boolean(icon && children && hideLabelOnMobile),
			class: className
		})
	);
	const spinnerClasses = $derived(buttonIcon({ size, class: spinnerClass }));
	const iconClasses = $derived(buttonIcon({ size }));
	const Icon = $derived(icon);
	const holdEnabled = $derived(holdToConfirm && !href && type === "button");
	const hintId = $props.id();
	let progress = $state(0);
	let holding = $state(false);
	let confirmed = $state(false);

	function spaceActivates(node: HTMLAnchorElement) {
		return on(node, "keydown", (event) => {
			if (event.key !== " " || event.defaultPrevented) return;
			event.preventDefault();
			node.click();
		});
	}

	function hold(node: HTMLButtonElement) {
		const unavailable = disabled || loading;
		const controller = new AbortController();
		const options = { signal: controller.signal, capture: true };
		let owner: number | string | null = null;
		let origin = { x: 0, y: 0 };
		let frame = 0;
		let resetTimer: ReturnType<typeof setTimeout> | undefined;
		let last = 0;
		let elapsed = 0;

		function release() {
			const pointer = typeof owner === "number" ? owner : null;
			owner = null;
			holding = false;
			if (pointer !== null && node.hasPointerCapture(pointer)) {
				node.releasePointerCapture(pointer);
			}
		}

		function tick(now: number) {
			frame = 0;
			if (disabled || loading || document.hidden || !holdEnabled) release();
			const delta = Math.max(0, now - last);
			last = now;
			elapsed = Math.max(0, Math.min(1800, elapsed + delta * (holding ? 1 : -2.5)));
			progress = elapsed / 1800;
			if (holding && elapsed === 1800) {
				holding = false;
				confirmed = true;
				resetTimer = setTimeout(reset, 1600);
				onconfirm?.();
				return;
			}
			if (elapsed > 0 || holding) frame = requestAnimationFrame(tick);
		}

		function begin(input: number | string) {
			if (
				unavailable ||
				disabled ||
				loading ||
				document.hidden ||
				owner !== null ||
				confirmed
			)
				return;
			owner = input;
			holding = true;
			if (!frame) {
				last = performance.now();
				frame = requestAnimationFrame(tick);
			}
		}

		function reset() {
			clearTimeout(resetTimer);
			resetTimer = undefined;
			cancelAnimationFrame(frame);
			frame = 0;
			release();
			elapsed = 0;
			progress = 0;
			confirmed = false;
		}

		node.addEventListener(
			"pointerdown",
			(event) => {
				if (!event.isPrimary || event.button !== 0 || owner !== null || unavailable) return;
				node.focus();
				begin(event.pointerId);
				if (owner !== event.pointerId) return;
				origin = { x: event.clientX, y: event.clientY };
				node.setPointerCapture(event.pointerId);
			},
			options
		);
		node.addEventListener(
			"pointermove",
			(event) => {
				if (owner !== event.pointerId) return;
				const bounds = node.getBoundingClientRect();
				if (
					Math.hypot(event.clientX - origin.x, event.clientY - origin.y) > 10 ||
					event.clientX < bounds.left ||
					event.clientX > bounds.right ||
					event.clientY < bounds.top ||
					event.clientY > bounds.bottom
				)
					release();
			},
			options
		);
		for (const name of [
			"pointerup",
			"pointercancel",
			"lostpointercapture",
			"pointerleave"
		] as const) {
			node.addEventListener(
				name,
				(event) => {
					if (owner === event.pointerId) release();
				},
				options
			);
		}
		node.addEventListener(
			"keydown",
			(event) => {
				if (event.key !== " " && event.key !== "Enter") return;
				event.preventDefault();
				if (!event.repeat) begin(event.key);
			},
			options
		);
		window.addEventListener(
			"keyup",
			(event) => {
				if (owner === event.key) {
					event.preventDefault();
					release();
				}
			},
			options
		);
		window.addEventListener(
			"keydown",
			(event) => {
				if (event.key === "Escape" && (holding || progress > 0)) {
					event.preventDefault();
					reset();
				}
			},
			options
		);
		node.addEventListener("blur", release, options);
		window.addEventListener("blur", release, options);
		document.addEventListener(
			"visibilitychange",
			() => {
				if (document.hidden) release();
			},
			options
		);
		node.addEventListener(
			"click",
			(event) => {
				event.preventDefault();
				event.stopImmediatePropagation();
			},
			options
		);
		node.addEventListener("contextmenu", (event) => event.preventDefault(), options);

		return () => {
			controller.abort();
			reset();
		};
	}

	setButtonIconSize(() => size);
</script>

{#snippet content()}
	<!-- the transition matches the ::before shadow's so both halves of the press ease together. -->
	<span
		class={[
			"inline-flex min-w-0 items-center justify-center gap-[inherit] transition-transform duration-150 ease-in-out",
			loading && "invisible"
		]}
	>
		{#if Icon && iconSide == "left"}
			<Icon class={iconClasses} aria-hidden="true" />
		{/if}
		{#if children && Icon && hideLabelOnMobile}
			<span class="mobile:sr-only">{@render children()}</span>
		{:else if children}
			{@render children()}
		{/if}
		{#if Icon && iconSide == "right"}
			<Icon class={iconClasses} aria-hidden="true" />
		{/if}
	</span>
	{#if loading}
		<span data-spinner class="absolute inset-0 flex items-center justify-center">
			<Spinner class={spinnerClasses} />
		</span>
	{/if}
{/snippet}

{#snippet faces()}
	<span class="hold-faces">
		<span class="hold-label">{@render content()}</span>
		<span class="hold-confirmed" aria-hidden="true">
			<Check class={iconClasses} />
			Confirmed
		</span>
	</span>
{/snippet}

{#if href}
	<a
		bind:this={ref}
		{href}
		class={classes}
		data-variant={variant}
		aria-disabled={disabled || loading ? "true" : undefined}
		tabindex={disabled ? -1 : undefined}
		{...rest}
		{@attach spaceActivates}
	>
		{@render content()}
	</a>
{:else if holdEnabled}
	<button
		bind:this={ref}
		{type}
		disabled={disabled || loading}
		class={[classes, "hold-button"]}
		data-variant={variant}
		data-holding={holding || confirmed}
		data-confirmed={confirmed}
		aria-busy={loading}
		{...rest}
		aria-describedby={[rest["aria-describedby"], hintId].filter(Boolean).join(" ")}
		{@attach hold}
	>
		{@render faces()}
		<span
			class="hold-fill"
			aria-hidden="true"
			style:clip-path={`inset(0 ${(1 - progress) * 100}% 0 0)`}
		>
			{@render faces()}
		</span>
	</button>
	<span id={hintId} class="sr-only">
		Press and hold the pointer, Space, or Enter for 1.8 seconds to confirm. Release early or
		press Escape to cancel.
	</span>
	<span role="status" class="sr-only">{confirmed ? "Confirmed" : ""}</span>
{:else}
	<button
		bind:this={ref}
		{type}
		disabled={disabled || loading}
		class={classes}
		data-variant={variant}
		aria-busy={loading}
		{...rest}
	>
		{@render content()}
	</button>
{/if}

<style>
	.hold-button {
		position: relative;
		isolation: isolate;
		touch-action: manipulation;
		-webkit-touch-callout: none;
	}

	.hold-fill {
		position: absolute;
		inset: 0;
		display: flex;
		align-items: center;
		justify-content: inherit;
		gap: inherit;
		padding: inherit;
		pointer-events: none;
		translate: none;
		background: var(--color-foreground-default);
		color: var(--color-background-default);
	}

	.hold-button[data-variant="danger"] .hold-fill {
		background: var(--color-background-danger-emphasis);
		color: var(--color-foreground-on-emphasis);
	}

	.hold-button[data-variant="destructive"] .hold-fill {
		background: linear-gradient(
			to bottom,
			color-mix(in oklch, var(--btn-from), black 0%),
			color-mix(in oklch, var(--btn-brand), black 10%)
		);
		box-shadow: inset 0 2px 0 0 color-mix(in oklch, var(--btn-highlight), black 5%);
		color: white;
	}

	.hold-button[data-variant="primary"] .hold-fill {
		background: var(--color-background-success-emphasis-hover);
		color: var(--color-foreground-on-emphasis);
	}

	.hold-faces {
		display: grid;
		gap: inherit;
	}

	.hold-label,
	.hold-confirmed {
		grid-area: 1 / 1;
		display: inline-flex;
		align-items: center;
		justify-content: center;
		gap: inherit;
		transition: opacity 150ms ease;
	}

	.hold-confirmed,
	.hold-button[data-confirmed="true"] .hold-label {
		opacity: 0;
	}

	.hold-button[data-confirmed="true"] .hold-confirmed {
		opacity: 1;
	}

	@media (prefers-reduced-motion: reduce) {
		.hold-button > span {
			transition: none;
			translate: none;
		}

		.hold-fill {
			clip-path: none !important;
			opacity: 0;
		}

		.hold-button[data-holding="true"] .hold-fill {
			opacity: 1;
		}

		.hold-label,
		.hold-confirmed {
			transition: none;
		}
	}
</style>