-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathwizard.svelte
More file actions
222 lines (201 loc) · 6.74 KB
/
wizard.svelte
File metadata and controls
222 lines (201 loc) · 6.74 KB
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
<script context="module" lang="ts">
export type WizardStepsType = Map<
number,
{
label: string;
component: typeof SvelteComponent<unknown>;
optional?: boolean;
disabled?: boolean;
actions?: {
label: string;
onClick: () => Promise<void>;
}[];
}
>;
</script>
<script lang="ts">
import { trackEvent } from '$lib/actions/analytics';
import { Steps } from '$lib/components';
import { Button, Form } from '$lib/elements/forms';
import { addNotification } from '$lib/stores/notifications';
import { wizard } from '$lib/stores/wizard';
import { createEventDispatcher, type SvelteComponent } from 'svelte';
import WizardExitModal from './wizardExitModal.svelte';
export let title: string;
export let steps: WizardStepsType;
export let confirmExit = false;
export let finalAction = 'Create';
const dispatch = createEventDispatcher();
let showExitModal = false;
function handleKeydown(event: KeyboardEvent) {
const openDialog = document.querySelectorAll('dialog[open]');
if (event.key === 'Escape' && !showExitModal && !openDialog?.length) {
event.preventDefault();
trackEvent('wizard_exit', {
from: 'escape'
});
dispatch('exit');
wizard.hide();
}
}
function handleExit() {
if (confirmExit) {
showExitModal = true;
} else {
trackEvent('wizard_exit', {
from: 'button'
});
dispatch('exit');
wizard.hide();
}
}
function handleStepClick(e: CustomEvent<number>) {
const step = e.detail;
if (step < $wizard.step) {
$wizard.step = step;
}
}
async function submit() {
if ($wizard.interceptor) {
$wizard.nextDisabled = true;
try {
await $wizard.interceptor();
} catch (error) {
if (!$wizard.interceptorNotificationEnabled) return;
addNotification({
message: error.message,
type: 'error'
});
return;
} finally {
$wizard.nextDisabled = false;
}
}
wizard.setInterceptor(null);
if (isLastStep) {
if ($wizard.finalAction) {
$wizard.nextDisabled = true;
try {
await $wizard.finalAction();
trackEvent('wizard_finish');
} catch (error) {
addNotification({
message: error.message,
type: 'error'
});
} finally {
$wizard.nextDisabled = false;
}
} else {
$wizard.nextDisabled = true;
trackEvent('wizard_finish');
dispatch('finish');
setTimeout(() => {
$wizard.nextDisabled = false;
}, 2000);
}
} else {
if (steps.get($wizard.step + 1)?.disabled) {
$wizard.step++;
while (steps.get($wizard.step)?.disabled) {
$wizard.step++;
}
} else {
$wizard.step++;
}
trackEvent('wizard_next');
}
}
function previousStep() {
if (steps.get($wizard.step - 1)?.disabled) {
$wizard.step--;
while (steps.get($wizard.step)?.disabled) {
$wizard.step--;
}
} else {
$wizard.step--;
}
trackEvent('wizard_back');
}
$: sortedSteps = [...steps].sort(([a], [b]) => (a > b ? 1 : -1));
$: isLastStep = $wizard.step === steps.size;
$: currentStep = steps.get($wizard.step);
</script>
<svelte:window on:keydown={handleKeydown} />
<section class="wizard">
<div class="wizard-header-strip" />
<div class="wizard-start-bg" />
<div class="wizard-end-bg" />
<header class="wizard-header">
<div class="body-text-1 u-bold">{title}</div>
<slot name="header" />
<button
class="button is-text is-only-icon u-margin-inline-start-auto"
style="--button-size:1.5rem;"
aria-label="close wizard"
on:click={handleExit}>
<span class="icon-x" aria-hidden="true" />
</button>
</header>
<aside class="wizard-side">
<slot name="aside">
<Steps
on:step={handleStepClick}
steps={sortedSteps.map(([, { label, optional, disabled }]) => ({
text: label,
optional,
disabled
}))}
currentStep={$wizard.step} />
</slot>
</aside>
<div class="wizard-media">
{#if $wizard.media}
<img src={$wizard.media} alt="wizard media" loading="lazy" />
{/if}
</div>
<div class="wizard-main">
<Form noStyle onSubmit={submit}>
{#each sortedSteps as [step, { component }]}
{#if $wizard.step === step}
<svelte:component this={component} />
{/if}
{/each}
<div class="form-footer">
<div class="u-flex u-main-end u-gap-12">
{#if !isLastStep && currentStep?.optional}
<Button text on:click={() => dispatch('finish')}>
Skip optional steps
</Button>
{/if}
{#if $wizard.step === 1}
<Button secondary on:click={handleExit}>Cancel</Button>
{:else}
<Button secondary on:click={previousStep}>Back</Button>
{/if}
{#if currentStep?.actions}
{#each currentStep.actions as action}
<Button secondary on:click={action.onClick}>
{action.label}</Button>
{/each}
{/if}
<Button submit disabled={$wizard.nextDisabled}>
{isLastStep ? finalAction : 'Next'}
</Button>
</div>
</div>
</Form>
</div>
</section>
{#if showExitModal}
<WizardExitModal
bind:show={showExitModal}
on:exit={() => {
trackEvent('wizard_exit', {
from: 'prompt'
});
wizard.hide();
}}>
<slot name="exit">this process</slot>
</WizardExitModal>
{/if}