@joewinke/jatui 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +46 -0
- package/src/lib/components/AudioWaveform.svelte +694 -0
- package/src/lib/components/AvailabilityModal.svelte +173 -0
- package/src/lib/components/Badge.svelte +38 -0
- package/src/lib/components/BookingForm.svelte +276 -0
- package/src/lib/components/Button.svelte +72 -0
- package/src/lib/components/CalendarPicker.svelte +284 -0
- package/src/lib/components/Card.svelte +67 -0
- package/src/lib/components/CharacterCounter.svelte +82 -0
- package/src/lib/components/ChipInput.svelte +596 -0
- package/src/lib/components/ColorSelector.svelte +163 -0
- package/src/lib/components/ConfirmModal.svelte +75 -0
- package/src/lib/components/CountdownTimer.svelte +94 -0
- package/src/lib/components/DateRangePicker.svelte +192 -0
- package/src/lib/components/Drawer.svelte +110 -0
- package/src/lib/components/FilterDropdown.svelte +202 -0
- package/src/lib/components/ImageUpload.svelte +97 -0
- package/src/lib/components/InlineEdit.svelte +283 -0
- package/src/lib/components/LazyImage.svelte +122 -0
- package/src/lib/components/LoadingSpinner.svelte +102 -0
- package/src/lib/components/Modal.svelte +208 -0
- package/src/lib/components/PhoneInput.svelte +92 -0
- package/src/lib/components/ResizableDivider.svelte +305 -0
- package/src/lib/components/ResizablePanel.svelte +302 -0
- package/src/lib/components/SearchDropdown.svelte +341 -0
- package/src/lib/components/SelectInput.svelte +215 -0
- package/src/lib/components/SignaturePad.svelte +171 -0
- package/src/lib/components/SortDropdown.svelte +148 -0
- package/src/lib/components/Sparkline.svelte +107 -0
- package/src/lib/components/SpeechForm.svelte +114 -0
- package/src/lib/components/StatusBadge.svelte +155 -0
- package/src/lib/components/TextArea.svelte +143 -0
- package/src/lib/components/TextInput.svelte +108 -0
- package/src/lib/components/ThemeSelector.svelte +195 -0
- package/src/lib/components/TimeSlotPicker.svelte +162 -0
- package/src/lib/components/VoicePlayer.svelte +420 -0
- package/src/lib/components/messaging/Avatar.svelte +81 -0
- package/src/lib/components/messaging/ChannelInfoModal.svelte +163 -0
- package/src/lib/components/messaging/ChannelList.svelte +107 -0
- package/src/lib/components/messaging/ChannelMemberAvatarStack.svelte +69 -0
- package/src/lib/components/messaging/ChannelMembersModal.svelte +182 -0
- package/src/lib/components/messaging/CreateChannelModal.svelte +190 -0
- package/src/lib/components/messaging/DirectMessageList.svelte +145 -0
- package/src/lib/components/messaging/EmojiSelector.svelte +260 -0
- package/src/lib/components/messaging/MentionAutocomplete.svelte +193 -0
- package/src/lib/components/messaging/MessageAttachment.svelte +270 -0
- package/src/lib/components/messaging/MessageAttachmentUpload.svelte +243 -0
- package/src/lib/components/messaging/MessageInput.svelte +451 -0
- package/src/lib/components/messaging/MessageItem.svelte +338 -0
- package/src/lib/components/messaging/MessageThread.svelte +306 -0
- package/src/lib/components/messaging/NotificationSettingsModal.svelte +234 -0
- package/src/lib/components/messaging/QuotedMessageDisplay.svelte +118 -0
- package/src/lib/components/messaging/StartDMModal.svelte +100 -0
- package/src/lib/components/messaging/ThreadPanel.svelte +153 -0
- package/src/lib/index.ts +185 -0
- package/src/lib/types/booking.ts +143 -0
- package/src/lib/types/messaging.ts +459 -0
- package/src/lib/utils/currency.ts +20 -0
- package/src/lib/utils/daisyuiColors.ts +243 -0
- package/src/lib/utils/dateFormatters.ts +153 -0
- package/src/lib/utils/mentionParser.ts +188 -0
- package/src/lib/utils/phoneFormat.ts +74 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* CalendarPicker — Month-view calendar with availability dots, date constraints,
|
|
4
|
+
* and navigation. Generic scheduling calendar.
|
|
5
|
+
*
|
|
6
|
+
* Pass `getAvailability` callback to load availability data for the visible date range.
|
|
7
|
+
* Pass `isDateEnabled` to control which dates are selectable.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { AvailabilityCounts } from '../types/booking';
|
|
11
|
+
|
|
12
|
+
export type { AvailabilityCounts };
|
|
13
|
+
|
|
14
|
+
const DAYS_SHORT = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
|
15
|
+
|
|
16
|
+
let {
|
|
17
|
+
onSelect,
|
|
18
|
+
getAvailability,
|
|
19
|
+
isDateEnabled,
|
|
20
|
+
maxDaysInAdvance = 90,
|
|
21
|
+
minNoticeHours = 24,
|
|
22
|
+
weekdaysOnly = true,
|
|
23
|
+
accentColor = 'primary',
|
|
24
|
+
}: {
|
|
25
|
+
onSelect: (date: Date) => void;
|
|
26
|
+
getAvailability?: (startDate: Date, endDate: Date) => Promise<AvailabilityCounts>;
|
|
27
|
+
isDateEnabled?: (date: Date) => boolean;
|
|
28
|
+
maxDaysInAdvance?: number;
|
|
29
|
+
minNoticeHours?: number;
|
|
30
|
+
weekdaysOnly?: boolean;
|
|
31
|
+
accentColor?: string;
|
|
32
|
+
} = $props();
|
|
33
|
+
|
|
34
|
+
let viewDate = $state(new Date());
|
|
35
|
+
let availabilityCounts = $state(new Map<string, { available: number; total: number }>());
|
|
36
|
+
let isLoadingAvailability = $state(false);
|
|
37
|
+
|
|
38
|
+
const calendarDays = $derived(() => {
|
|
39
|
+
const year = viewDate.getFullYear();
|
|
40
|
+
const month = viewDate.getMonth();
|
|
41
|
+
|
|
42
|
+
const firstDay = new Date(year, month, 1);
|
|
43
|
+
const startDate = new Date(firstDay);
|
|
44
|
+
startDate.setDate(startDate.getDate() - startDate.getDay());
|
|
45
|
+
|
|
46
|
+
const days: Date[] = [];
|
|
47
|
+
const current = new Date(startDate);
|
|
48
|
+
|
|
49
|
+
for (let i = 0; i < 42; i++) {
|
|
50
|
+
days.push(new Date(current));
|
|
51
|
+
current.setDate(current.getDate() + 1);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return days;
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
function defaultIsDateEnabled(date: Date): boolean {
|
|
58
|
+
const today = new Date();
|
|
59
|
+
today.setHours(0, 0, 0, 0);
|
|
60
|
+
|
|
61
|
+
if (date < today) return false;
|
|
62
|
+
|
|
63
|
+
const maxDate = new Date(today);
|
|
64
|
+
maxDate.setDate(maxDate.getDate() + maxDaysInAdvance);
|
|
65
|
+
if (date > maxDate) return false;
|
|
66
|
+
|
|
67
|
+
const minNoticeDate = new Date();
|
|
68
|
+
minNoticeDate.setHours(minNoticeDate.getHours() + minNoticeHours);
|
|
69
|
+
minNoticeDate.setHours(0, 0, 0, 0);
|
|
70
|
+
if (date < minNoticeDate) return false;
|
|
71
|
+
|
|
72
|
+
if (weekdaysOnly) {
|
|
73
|
+
const dayOfWeek = date.getDay();
|
|
74
|
+
if (dayOfWeek === 0 || dayOfWeek === 6) return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function checkDateEnabled(date: Date): boolean {
|
|
81
|
+
if (isDateEnabled) return isDateEnabled(date);
|
|
82
|
+
return defaultIsDateEnabled(date);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function isToday(date: Date): boolean {
|
|
86
|
+
const today = new Date();
|
|
87
|
+
return (
|
|
88
|
+
date.getDate() === today.getDate() &&
|
|
89
|
+
date.getMonth() === today.getMonth() &&
|
|
90
|
+
date.getFullYear() === today.getFullYear()
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function isCurrentMonth(date: Date): boolean {
|
|
95
|
+
return date.getMonth() === viewDate.getMonth();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function prevMonth() {
|
|
99
|
+
const newDate = new Date(viewDate);
|
|
100
|
+
newDate.setMonth(newDate.getMonth() - 1);
|
|
101
|
+
viewDate = newDate;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function nextMonth() {
|
|
105
|
+
const newDate = new Date(viewDate);
|
|
106
|
+
newDate.setMonth(newDate.getMonth() + 1);
|
|
107
|
+
viewDate = newDate;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function handleDateClick(date: Date) {
|
|
111
|
+
if (checkDateEnabled(date)) {
|
|
112
|
+
onSelect(date);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const canGoPrev = $derived(() => {
|
|
117
|
+
const today = new Date();
|
|
118
|
+
return (
|
|
119
|
+
viewDate.getMonth() > today.getMonth() ||
|
|
120
|
+
viewDate.getFullYear() > today.getFullYear()
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const canGoNext = $derived(() => {
|
|
125
|
+
const today = new Date();
|
|
126
|
+
const maxDate = new Date(today);
|
|
127
|
+
maxDate.setDate(maxDate.getDate() + maxDaysInAdvance);
|
|
128
|
+
return (
|
|
129
|
+
viewDate.getMonth() < maxDate.getMonth() ||
|
|
130
|
+
viewDate.getFullYear() < maxDate.getFullYear()
|
|
131
|
+
);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
async function loadAvailability() {
|
|
135
|
+
if (!getAvailability) return;
|
|
136
|
+
|
|
137
|
+
isLoadingAvailability = true;
|
|
138
|
+
try {
|
|
139
|
+
const days = calendarDays();
|
|
140
|
+
const startDate = days[0];
|
|
141
|
+
const endDate = days[days.length - 1];
|
|
142
|
+
|
|
143
|
+
const counts = await getAvailability(startDate, endDate);
|
|
144
|
+
const map = new Map<string, { available: number; total: number }>();
|
|
145
|
+
for (const [key, value] of Object.entries(counts)) {
|
|
146
|
+
map.set(key, value);
|
|
147
|
+
}
|
|
148
|
+
availabilityCounts = map;
|
|
149
|
+
} catch {
|
|
150
|
+
// Silently ignore — dots won't show
|
|
151
|
+
} finally {
|
|
152
|
+
isLoadingAvailability = false;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
$effect(() => {
|
|
157
|
+
const _ = viewDate.getMonth();
|
|
158
|
+
loadAvailability();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
function getAvailabilityLevel(date: Date): 'none' | 'low' | 'medium' | 'high' | 'full' {
|
|
162
|
+
const dateStr = date.toISOString().split('T')[0];
|
|
163
|
+
const counts = availabilityCounts.get(dateStr);
|
|
164
|
+
|
|
165
|
+
if (!counts || counts.total === 0) return 'none';
|
|
166
|
+
|
|
167
|
+
const ratio = counts.available / counts.total;
|
|
168
|
+
if (ratio === 0) return 'full';
|
|
169
|
+
if (ratio <= 0.25) return 'low';
|
|
170
|
+
if (ratio <= 0.6) return 'medium';
|
|
171
|
+
return 'high';
|
|
172
|
+
}
|
|
173
|
+
</script>
|
|
174
|
+
|
|
175
|
+
<div class="calendar-picker">
|
|
176
|
+
<!-- Month Navigation -->
|
|
177
|
+
<div class="flex items-center justify-between mb-6">
|
|
178
|
+
<button
|
|
179
|
+
onclick={prevMonth}
|
|
180
|
+
disabled={!canGoPrev()}
|
|
181
|
+
class="btn btn-ghost btn-sm btn-circle disabled:opacity-30"
|
|
182
|
+
aria-label="Previous month"
|
|
183
|
+
>
|
|
184
|
+
<!-- ChevronLeft -->
|
|
185
|
+
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
186
|
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
|
|
187
|
+
</svg>
|
|
188
|
+
</button>
|
|
189
|
+
|
|
190
|
+
<h3 class="text-lg font-semibold">
|
|
191
|
+
{viewDate.toLocaleDateString('en-US', { month: 'long', year: 'numeric' })}
|
|
192
|
+
</h3>
|
|
193
|
+
|
|
194
|
+
<button
|
|
195
|
+
onclick={nextMonth}
|
|
196
|
+
disabled={!canGoNext()}
|
|
197
|
+
class="btn btn-ghost btn-sm btn-circle disabled:opacity-30"
|
|
198
|
+
aria-label="Next month"
|
|
199
|
+
>
|
|
200
|
+
<!-- ChevronRight -->
|
|
201
|
+
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
202
|
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
|
|
203
|
+
</svg>
|
|
204
|
+
</button>
|
|
205
|
+
</div>
|
|
206
|
+
|
|
207
|
+
<!-- Day Headers -->
|
|
208
|
+
<div class="grid grid-cols-7 gap-1 mb-2">
|
|
209
|
+
{#each DAYS_SHORT as day}
|
|
210
|
+
<div class="text-center text-sm font-medium text-base-content/50 py-2">
|
|
211
|
+
{day}
|
|
212
|
+
</div>
|
|
213
|
+
{/each}
|
|
214
|
+
</div>
|
|
215
|
+
|
|
216
|
+
<!-- Calendar Grid -->
|
|
217
|
+
<div class="grid grid-cols-7 gap-1">
|
|
218
|
+
{#each calendarDays() as date}
|
|
219
|
+
{@const available = checkDateEnabled(date)}
|
|
220
|
+
{@const today = isToday(date)}
|
|
221
|
+
{@const currentMonth = isCurrentMonth(date)}
|
|
222
|
+
{@const availLevel = getAvailabilityLevel(date)}
|
|
223
|
+
|
|
224
|
+
<button
|
|
225
|
+
onclick={() => handleDateClick(date)}
|
|
226
|
+
disabled={!available}
|
|
227
|
+
class="aspect-square flex flex-col items-center justify-center rounded-lg text-sm font-medium
|
|
228
|
+
transition-all duration-200 relative
|
|
229
|
+
{currentMonth ? '' : 'opacity-30'}
|
|
230
|
+
{available
|
|
231
|
+
? `hover:bg-${accentColor} hover:text-${accentColor}-content cursor-pointer bg-${accentColor}/15 text-${accentColor}`
|
|
232
|
+
: 'text-base-content/30 cursor-not-allowed'}
|
|
233
|
+
{today ? `ring-2 ring-${accentColor} ring-offset-2 ring-offset-base-100` : ''}"
|
|
234
|
+
>
|
|
235
|
+
<span>{date.getDate()}</span>
|
|
236
|
+
{#if currentMonth && available && !isLoadingAvailability}
|
|
237
|
+
<div class="flex gap-0.5 mt-0.5">
|
|
238
|
+
{#if availLevel === 'high'}
|
|
239
|
+
<span class="w-1.5 h-1.5 rounded-full bg-emerald-400"></span>
|
|
240
|
+
<span class="w-1.5 h-1.5 rounded-full bg-emerald-400"></span>
|
|
241
|
+
<span class="w-1.5 h-1.5 rounded-full bg-emerald-400"></span>
|
|
242
|
+
{:else if availLevel === 'medium'}
|
|
243
|
+
<span class="w-1.5 h-1.5 rounded-full bg-amber-400"></span>
|
|
244
|
+
<span class="w-1.5 h-1.5 rounded-full bg-amber-400"></span>
|
|
245
|
+
{:else if availLevel === 'low'}
|
|
246
|
+
<span class="w-1.5 h-1.5 rounded-full bg-rose-400"></span>
|
|
247
|
+
{:else if availLevel === 'full'}
|
|
248
|
+
<span class="w-1.5 h-1.5 rounded-full bg-base-content/20"></span>
|
|
249
|
+
{/if}
|
|
250
|
+
</div>
|
|
251
|
+
{/if}
|
|
252
|
+
</button>
|
|
253
|
+
{/each}
|
|
254
|
+
</div>
|
|
255
|
+
|
|
256
|
+
<!-- Legend -->
|
|
257
|
+
{#if getAvailability}
|
|
258
|
+
<div class="flex flex-wrap items-center gap-x-5 gap-y-2 mt-6 pt-4 border-t border-base-300 text-sm">
|
|
259
|
+
<div class="flex items-center gap-2">
|
|
260
|
+
<div class="flex gap-0.5">
|
|
261
|
+
<span class="w-1.5 h-1.5 rounded-full bg-emerald-400"></span>
|
|
262
|
+
<span class="w-1.5 h-1.5 rounded-full bg-emerald-400"></span>
|
|
263
|
+
<span class="w-1.5 h-1.5 rounded-full bg-emerald-400"></span>
|
|
264
|
+
</div>
|
|
265
|
+
<span class="text-base-content/60">Many slots</span>
|
|
266
|
+
</div>
|
|
267
|
+
<div class="flex items-center gap-2">
|
|
268
|
+
<div class="flex gap-0.5">
|
|
269
|
+
<span class="w-1.5 h-1.5 rounded-full bg-amber-400"></span>
|
|
270
|
+
<span class="w-1.5 h-1.5 rounded-full bg-amber-400"></span>
|
|
271
|
+
</div>
|
|
272
|
+
<span class="text-base-content/60">Some slots</span>
|
|
273
|
+
</div>
|
|
274
|
+
<div class="flex items-center gap-2">
|
|
275
|
+
<span class="w-1.5 h-1.5 rounded-full bg-rose-400"></span>
|
|
276
|
+
<span class="text-base-content/60">Few slots</span>
|
|
277
|
+
</div>
|
|
278
|
+
<div class="flex items-center gap-2">
|
|
279
|
+
<div class="w-4 h-4 rounded ring-2 ring-{accentColor}"></div>
|
|
280
|
+
<span class="text-base-content/60">Today</span>
|
|
281
|
+
</div>
|
|
282
|
+
</div>
|
|
283
|
+
{/if}
|
|
284
|
+
</div>
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* Card Component
|
|
4
|
+
*
|
|
5
|
+
* DaisyUI card container with optional header, footer, and click handler.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
interface Props {
|
|
9
|
+
class?: string;
|
|
10
|
+
children?: import('svelte').Snippet;
|
|
11
|
+
header?: import('svelte').Snippet;
|
|
12
|
+
footer?: import('svelte').Snippet;
|
|
13
|
+
onclick?: (event: MouseEvent) => void;
|
|
14
|
+
role?: string;
|
|
15
|
+
tabindex?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let { class: className = '', children, header, footer, onclick, role, tabindex }: Props = $props();
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
{#if onclick || role}
|
|
22
|
+
<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
|
|
23
|
+
<div
|
|
24
|
+
class="card bg-base-100 shadow-xl border border-base-300 {className}"
|
|
25
|
+
{onclick}
|
|
26
|
+
role={role || (onclick ? 'button' : undefined)}
|
|
27
|
+
tabindex={tabindex ?? 0}
|
|
28
|
+
>
|
|
29
|
+
{#if header}
|
|
30
|
+
<div class="card-body pb-4">
|
|
31
|
+
<div class="flex justify-between items-center">
|
|
32
|
+
{@render header()}
|
|
33
|
+
</div>
|
|
34
|
+
</div>
|
|
35
|
+
{/if}
|
|
36
|
+
{#if children}
|
|
37
|
+
<div class="card-body {header ? 'pt-0' : ''}">
|
|
38
|
+
{@render children()}
|
|
39
|
+
</div>
|
|
40
|
+
{/if}
|
|
41
|
+
{#if footer}
|
|
42
|
+
<div class="card-actions justify-end p-6 pt-0">
|
|
43
|
+
{@render footer()}
|
|
44
|
+
</div>
|
|
45
|
+
{/if}
|
|
46
|
+
</div>
|
|
47
|
+
{:else}
|
|
48
|
+
<div class="card bg-base-100 shadow-xl border border-base-300 {className}">
|
|
49
|
+
{#if header}
|
|
50
|
+
<div class="card-body pb-4">
|
|
51
|
+
<div class="flex justify-between items-center">
|
|
52
|
+
{@render header()}
|
|
53
|
+
</div>
|
|
54
|
+
</div>
|
|
55
|
+
{/if}
|
|
56
|
+
{#if children}
|
|
57
|
+
<div class="card-body {header ? 'pt-0' : ''}">
|
|
58
|
+
{@render children()}
|
|
59
|
+
</div>
|
|
60
|
+
{/if}
|
|
61
|
+
{#if footer}
|
|
62
|
+
<div class="card-actions justify-end p-6 pt-0">
|
|
63
|
+
{@render footer()}
|
|
64
|
+
</div>
|
|
65
|
+
{/if}
|
|
66
|
+
</div>
|
|
67
|
+
{/if}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { onMount } from 'svelte'
|
|
3
|
+
|
|
4
|
+
interface Props {
|
|
5
|
+
targetCount: number
|
|
6
|
+
duration?: number
|
|
7
|
+
prefix?: string
|
|
8
|
+
suffix?: string
|
|
9
|
+
class?: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let {
|
|
13
|
+
targetCount,
|
|
14
|
+
duration = 2000,
|
|
15
|
+
prefix = "",
|
|
16
|
+
suffix = "",
|
|
17
|
+
class: className = ""
|
|
18
|
+
}: Props = $props()
|
|
19
|
+
|
|
20
|
+
let count = $state(0)
|
|
21
|
+
let isAnimating = $state(false)
|
|
22
|
+
|
|
23
|
+
onMount(() => {
|
|
24
|
+
const timeout = setTimeout(() => {
|
|
25
|
+
animateCount()
|
|
26
|
+
}, 100)
|
|
27
|
+
|
|
28
|
+
return () => clearTimeout(timeout)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
function animateCount() {
|
|
32
|
+
if (isAnimating) return
|
|
33
|
+
|
|
34
|
+
isAnimating = true
|
|
35
|
+
const startTime = Date.now()
|
|
36
|
+
const startCount = 0
|
|
37
|
+
const endCount = targetCount
|
|
38
|
+
|
|
39
|
+
function updateCount() {
|
|
40
|
+
const now = Date.now()
|
|
41
|
+
const elapsed = now - startTime
|
|
42
|
+
const progress = Math.min(elapsed / duration, 1)
|
|
43
|
+
|
|
44
|
+
// easeOutQuart
|
|
45
|
+
const easeOutQuart = 1 - Math.pow(1 - progress, 4)
|
|
46
|
+
const currentCount = Math.floor(startCount + (endCount - startCount) * easeOutQuart)
|
|
47
|
+
|
|
48
|
+
count = currentCount
|
|
49
|
+
|
|
50
|
+
if (progress < 1) {
|
|
51
|
+
requestAnimationFrame(updateCount)
|
|
52
|
+
} else {
|
|
53
|
+
count = endCount
|
|
54
|
+
isAnimating = false
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
requestAnimationFrame(updateCount)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
$effect(() => {
|
|
62
|
+
if (targetCount !== count && !isAnimating) {
|
|
63
|
+
animateCount()
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
function formatNumber(num: number): string {
|
|
68
|
+
return num.toLocaleString()
|
|
69
|
+
}
|
|
70
|
+
</script>
|
|
71
|
+
|
|
72
|
+
<span class="inline-flex items-center {className}">
|
|
73
|
+
{#if prefix}
|
|
74
|
+
<span class="mr-1">{prefix}</span>
|
|
75
|
+
{/if}
|
|
76
|
+
<span class="tabular-nums font-semibold">
|
|
77
|
+
{formatNumber(count)}
|
|
78
|
+
</span>
|
|
79
|
+
{#if suffix}
|
|
80
|
+
<span class="ml-1">{suffix}</span>
|
|
81
|
+
{/if}
|
|
82
|
+
</span>
|