~james/core

core/web/src/lib/components/welcome/WelcomeCard.svelte -rw-r--r-- 5.8 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 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
<script module lang="ts">
	import { tv, type VariantProps } from "tailwind-variants";

	export const welcomeCard = tv({
		slots: {
			root: "overflow-hidden rounded-md border border-border-default bg-background-default",
			header: "border-b border-border-default px-xl py-lg",
			body: "grid overflow-hidden",
			// self-start keeps the pane at its content height: once the body carries a
			// measured height the grid row would otherwise stretch the pane to match it,
			// and measuring that back is a loop that never moves
			pane: "col-start-1 row-start-1 min-w-0 self-start p-xl focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-border-focus",
			footer: "flex items-center justify-between gap-md border-t border-border-default px-xl py-md"
		}
	});

	export type WelcomeCardVariants = VariantProps<typeof welcomeCard>;
</script>

<script lang="ts">
	import type { Snippet } from "svelte";
	import { untrack } from "svelte";
	import { cubicIn } from "svelte/easing";
	import { prefersReducedMotion } from "svelte/motion";
	import { fade, fly } from "svelte/transition";
	import ArrowLeft from "$icon/arrow-left";
	import ArrowRight from "$icon/arrow-right";
	import Check from "$icon/check";
	import Button from "#lib/components/ui/Button.svelte";
	import {
		CROSSFADE_DURATION,
		CROSSFADE_EASING,
		crossfadeEasing,
		EXIT_DURATION
	} from "#lib/motion/spring.ts";
	import Steps from "#lib/components/welcome/Steps.svelte";

	interface Props {
		steps: readonly { key: string; label: string }[];
		step: number;
		furthest: number;
		direction: number;
		nextReady?: boolean;
		nextLoading?: boolean;
		onBack: () => void;
		onNext: () => void;
		onSelect: (index: number) => void;
		start?: Snippet;
		children: Snippet<[number]>;
	}

	let {
		steps,
		step,
		furthest,
		direction,
		nextReady = false,
		nextLoading = false,
		onBack,
		onNext,
		onSelect,
		start,
		children
	}: Props = $props();

	const slot = welcomeCard();
	const isLast = $derived(step === steps.length - 1);

	const DISTANCE = 22;
	const enter = $derived(
		prefersReducedMotion.current
			? { duration: CROSSFADE_DURATION, easing: crossfadeEasing }
			: { x: direction * DISTANCE, duration: CROSSFADE_DURATION, easing: crossfadeEasing }
	);
	const leave = $derived(
		prefersReducedMotion.current
			? { duration: EXIT_DURATION, easing: cubicIn }
			: { x: direction * -DISTANCE, duration: EXIT_DURATION, easing: cubicIn }
	);

	const stepDescription = (index: number) =>
		`Step ${index + 1} of ${steps.length}: ${steps[index].label}`;

	const mobileLabelTransition = $derived(
		prefersReducedMotion.current
			? ""
			: `transition: opacity ${CROSSFADE_DURATION}ms ${CROSSFADE_EASING};`
	);

	let height = $state<number | null>(null);
	let mounted = $state(false);

	$effect(() => {
		if (height !== null && !mounted) mounted = true;
	});

	const heightTransition = $derived(
		mounted && !prefersReducedMotion.current
			? `transition: height ${CROSSFADE_DURATION}ms ${CROSSFADE_EASING};`
			: ""
	);

	function measurePane(node: HTMLElement) {
		// a detached pane reports 0, and the outgoing one is still observed while it
		// fades out — taking that would collapse the card mid-transition
		const observer = new ResizeObserver(() => {
			if (node.isConnected) height = node.offsetHeight;
		});
		observer.observe(node);
		height = node.offsetHeight;
		return {
			destroy() {
				observer.disconnect();
			}
		};
	}

	let paneEl: HTMLElement | undefined = $state();
	let intent: "panel" | null = null;

	$effect(() => {
		void step;
		if (intent === "panel") paneEl?.focus();
		intent = null;
	});

	function handleBack() {
		intent = "panel";
		onBack();
	}

	function handleNext() {
		intent = "panel";
		onNext();
	}

	const labelFadeMs = $derived(prefersReducedMotion.current ? 0 : 160);
	const backFadeInMs = $derived(prefersReducedMotion.current ? 0 : 160);
	const backFadeOutMs = $derived(prefersReducedMotion.current ? 0 : 120);
</script>

<div class={slot.root()}>
	<p aria-live="polite" class="sr-only">{stepDescription(step)}</p>

	<div class={slot.header()}>
		<div class="relative grid sm:hidden" aria-hidden="true">
			{#each steps as item, index (item.key)}
				<span
					class="col-start-1 row-start-1 typography-paragraph-small whitespace-nowrap text-foreground-default"
					style="opacity: {index === step ? 1 : 0}; {mobileLabelTransition}"
				>
					{item.label}
				</span>
			{/each}
		</div>

		<Steps items={steps} current={step} {furthest} complete={isLast} {onSelect} />
	</div>

	<div
		class={slot.body()}
		style="{height === null ? '' : `height: ${height}px;`} {heightTransition}"
	>
		{#key step}
			{@const frozen = untrack(() => step)}
			<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
			<div
				bind:this={paneEl}
				use:measurePane
				class={slot.pane()}
				tabindex={-1}
				role="group"
				aria-label={stepDescription(frozen)}
				in:fly={enter}
				out:fly={leave}
			>
				{@render children(frozen)}
			</div>
		{/key}
	</div>

	<div class={slot.footer()}>
		<div class="flex items-center gap-2">
			{#if start && step === 0}
				{@render start()}
			{/if}
			{#if step > 0}
				<div in:fade={{ duration: backFadeInMs }} out:fade={{ duration: backFadeOutMs }}>
					<Button variant="ghost" icon={ArrowLeft} onclick={handleBack}>Back</Button>
				</div>
			{/if}
		</div>

		<Button
			variant={nextReady ? "primary" : "default"}
			icon={isLast ? Check : ArrowRight}
			iconSide="right"
			loading={nextLoading}
			disabled={nextLoading}
			onclick={handleNext}
		>
			<span class="grid">
				<span
					class="col-start-1 row-start-1"
					style="opacity: {isLast ? 0 : 1}; transition: opacity {labelFadeMs}ms;"
					aria-hidden={isLast}
				>
					Next
				</span>
				<span
					class="col-start-1 row-start-1"
					style="opacity: {isLast ? 1 : 0}; transition: opacity {labelFadeMs}ms;"
					aria-hidden={!isLast}
				>
					Finish
				</span>
			</span>
		</Button>
	</div>
</div>