@svadmin/ui 0.119.0 → 0.121.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.
Files changed (37) hide show
  1. package/dist/app.css +139 -0
  2. package/dist/app.theme.css +139 -0
  3. package/dist/components/AboutSettings.svelte +1 -1
  4. package/dist/components/Barcode.svelte +219 -0
  5. package/dist/components/Barcode.svelte.d.ts +15 -0
  6. package/dist/components/ContextMenu.svelte +268 -0
  7. package/dist/components/ContextMenu.svelte.d.ts +22 -0
  8. package/dist/components/JsonViewer.svelte +323 -0
  9. package/dist/components/JsonViewer.svelte.d.ts +20 -0
  10. package/dist/components/KpiCard.svelte +250 -0
  11. package/dist/components/KpiCard.svelte.d.ts +44 -0
  12. package/dist/components/NumberRoll.svelte +149 -0
  13. package/dist/components/NumberRoll.svelte.d.ts +17 -0
  14. package/dist/components/OtpInput.svelte +209 -0
  15. package/dist/components/OtpInput.svelte.d.ts +17 -0
  16. package/dist/components/RecordCompareModal.svelte +428 -0
  17. package/dist/components/RecordCompareModal.svelte.d.ts +42 -0
  18. package/dist/components/ResultPage.svelte +166 -0
  19. package/dist/components/ResultPage.svelte.d.ts +28 -0
  20. package/dist/components/SignaturePad.svelte +230 -66
  21. package/dist/components/SignaturePad.svelte.d.ts +6 -5
  22. package/dist/components/Tour.svelte +71 -13
  23. package/dist/components/Tour.svelte.d.ts +1 -1
  24. package/dist/components/TrendIndicator.svelte +21 -13
  25. package/dist/components/TrendIndicator.svelte.d.ts +4 -2
  26. package/dist/components/UnsavedChangesGuard.svelte +138 -0
  27. package/dist/components/UnsavedChangesGuard.svelte.d.ts +32 -0
  28. package/dist/components/VirtualList.svelte +165 -0
  29. package/dist/components/VirtualList.svelte.d.ts +43 -0
  30. package/dist/components/WatermarkContainer.svelte +196 -0
  31. package/dist/components/WatermarkContainer.svelte.d.ts +20 -0
  32. package/dist/components/useUnsavedChanges.d.svelte.ts +1 -0
  33. package/dist/components/useUnsavedChanges.svelte.d.ts +22 -0
  34. package/dist/components/useUnsavedChanges.svelte.js +105 -0
  35. package/dist/index.d.ts +30 -35
  36. package/dist/index.js +18 -2
  37. package/package.json +1 -1
@@ -0,0 +1,323 @@
1
+ <script lang="ts">
2
+ import { ChevronRight, ChevronDown, Copy, Check, Search } from '@lucide/svelte';
3
+ import { cn } from '../utils.js';
4
+
5
+ export interface JsonViewerProps {
6
+ /** 待解析展示的数据,可为对象、数组或合法的 JSON 字符串 */
7
+ data: unknown;
8
+ /** 初始展开深度,默认 2 */
9
+ initialDepth?: number | undefined;
10
+ /** 是否显示一键复制完整 JSON 按钮,默认 true */
11
+ copyable?: boolean | undefined;
12
+ /** 是否显示关键词搜索过滤栏,默认 true */
13
+ searchable?: boolean | undefined;
14
+ /** 字体大小:'sm' | 'md',默认 'sm' */
15
+ size?: 'sm' | 'md' | undefined;
16
+ /** 自定义最大高度类名,如 'max-h-96' */
17
+ maxHeight?: string | undefined;
18
+ /** 复制指定属性路径时的回调 */
19
+ onCopyPath?: ((path: string, value: unknown) => void) | undefined;
20
+ class?: string | undefined;
21
+ }
22
+
23
+ let {
24
+ data,
25
+ initialDepth = 2,
26
+ copyable = true,
27
+ searchable = true,
28
+ size = 'sm',
29
+ maxHeight = 'max-h-[32rem]',
30
+ onCopyPath,
31
+ class: className = '',
32
+ }: JsonViewerProps = $props();
33
+
34
+ let searchQuery = $state('');
35
+ let copiedAll = $state(false);
36
+ let copiedPath = $state<string | null>(null);
37
+ let collapsedPaths = $state<Set<string>>(new Set());
38
+
39
+ // 解析并规范化根对象
40
+ const parsedData = $derived.by(() => {
41
+ if (typeof data === 'string') {
42
+ try {
43
+ return JSON.parse(data);
44
+ } catch {
45
+ return data;
46
+ }
47
+ }
48
+ return data;
49
+ });
50
+
51
+ const fullJsonString = $derived.by(() => {
52
+ try {
53
+ return JSON.stringify(parsedData, null, 2);
54
+ } catch {
55
+ return String(parsedData);
56
+ }
57
+ });
58
+
59
+ function isObject(val: unknown): val is Record<string, unknown> {
60
+ return val !== null && typeof val === 'object' && !Array.isArray(val);
61
+ }
62
+
63
+ function isArray(val: unknown): val is unknown[] {
64
+ return Array.isArray(val);
65
+ }
66
+
67
+ function isPathCollapsed(path: string, depth: number): boolean {
68
+ if (collapsedPaths.has(path)) return true;
69
+ // 如果没有被显式收起,且层级超过初始深度,则默认收起
70
+ return depth > initialDepth && !collapsedPaths.has(`expanded:${path}`);
71
+ }
72
+
73
+ function handleToggle(path: string, depth: number) {
74
+ const isCurrentlyCollapsed = isPathCollapsed(path, depth);
75
+ const next = new Set(collapsedPaths);
76
+ if (isCurrentlyCollapsed) {
77
+ next.delete(path);
78
+ next.add(`expanded:${path}`);
79
+ } else {
80
+ next.add(path);
81
+ next.delete(`expanded:${path}`);
82
+ }
83
+ collapsedPaths = next;
84
+ }
85
+
86
+ function copyAll() {
87
+ if (typeof navigator !== 'undefined' && navigator.clipboard) {
88
+ navigator.clipboard.writeText(fullJsonString);
89
+ copiedAll = true;
90
+ setTimeout(() => {
91
+ copiedAll = false;
92
+ }, 1800);
93
+ }
94
+ }
95
+
96
+ function copySinglePath(path: string, value: unknown) {
97
+ if (typeof navigator !== 'undefined' && navigator.clipboard) {
98
+ navigator.clipboard.writeText(path);
99
+ copiedPath = path;
100
+ onCopyPath?.(path, value);
101
+ setTimeout(() => {
102
+ if (copiedPath === path) copiedPath = null;
103
+ }, 1500);
104
+ }
105
+ }
106
+
107
+ function matchesSearch(key: string, val: unknown): boolean {
108
+ if (!searchQuery.trim()) return true;
109
+ const q = searchQuery.toLowerCase();
110
+ if (key.toLowerCase().includes(q)) return true;
111
+ if (typeof val === 'string' && val.toLowerCase().includes(q)) return true;
112
+ if (typeof val === 'number' && String(val).includes(q)) return true;
113
+ return false;
114
+ }
115
+ </script>
116
+
117
+ <div
118
+ data-svadmin-component="JsonViewer"
119
+ class={cn(
120
+ 'rounded-xl border border-border bg-card shadow-xs flex flex-col font-mono text-xs overflow-hidden',
121
+ className
122
+ )}
123
+ >
124
+ <!-- Top Toolbar -->
125
+ <div class="flex items-center justify-between gap-2 px-3 py-2 border-b border-border/70 bg-muted/30">
126
+ <div class="flex items-center gap-2 flex-1 max-w-sm">
127
+ {#if searchable}
128
+ <div class="relative w-full flex items-center">
129
+ <Search class="absolute left-2.5 h-3.5 w-3.5 text-muted-foreground pointer-events-none" />
130
+ <input
131
+ type="text"
132
+ bind:value={searchQuery}
133
+ placeholder="搜索 Key 或 Value..."
134
+ class="w-full pl-8 pr-2.5 py-1 text-xs rounded-md border border-border bg-background text-foreground placeholder:text-muted-foreground focus:outline-2 focus:outline-primary"
135
+ />
136
+ </div>
137
+ {/if}
138
+ </div>
139
+
140
+ <div class="flex items-center gap-1.5 shrink-0">
141
+ {#if copyable}
142
+ <button
143
+ type="button"
144
+ onclick={copyAll}
145
+ class="inline-flex items-center gap-1 px-2 py-1 rounded-md text-xs font-sans border border-border bg-background text-muted-foreground hover:text-foreground transition-colors cursor-pointer"
146
+ title="复制完整 JSON"
147
+ >
148
+ {#if copiedAll}
149
+ <Check class="h-3 w-3 text-success" />
150
+ <span class="text-success text-[11px]">已复制</span>
151
+ {:else}
152
+ <Copy class="h-3 w-3" />
153
+ <span class="text-[11px]">复制 JSON</span>
154
+ {/if}
155
+ </button>
156
+ {/if}
157
+ </div>
158
+ </div>
159
+
160
+ <!-- JSON Content Tree -->
161
+ <div class={cn('p-3.5 overflow-auto select-text space-y-1', maxHeight, size === 'sm' ? 'text-xs' : 'text-sm')}>
162
+ {#if parsedData === null}
163
+ <span class="text-muted-foreground italic">null</span>
164
+ {:else if typeof parsedData !== 'object'}
165
+ <span class="text-foreground">{String(parsedData)}</span>
166
+ {:else if isArray(parsedData)}
167
+ <!-- Array Root -->
168
+ <div class="space-y-0.5">
169
+ <span class="text-muted-foreground">[</span>
170
+ <div class="pl-4 border-l border-border/40 space-y-1 my-0.5">
171
+ {#each parsedData as item, idx (idx)}
172
+ {@const itemPath = `[${idx}]`}
173
+ {#if matchesSearch(String(idx), item)}
174
+ <div class="flex items-start gap-1 group py-0.5">
175
+ <span class="text-muted-foreground/60 select-none text-[10px] w-5 text-right">{idx}:</span>
176
+ {#if isObject(item) || isArray(item)}
177
+ {@const collapsed = isPathCollapsed(itemPath, 1)}
178
+ <button
179
+ type="button"
180
+ onclick={() => handleToggle(itemPath, 1)}
181
+ class="inline-flex items-center p-0.5 text-muted-foreground hover:text-foreground cursor-pointer"
182
+ aria-label="Toggle node"
183
+ >
184
+ {#if collapsed}
185
+ <ChevronRight class="h-3 w-3" />
186
+ {:else}
187
+ <ChevronDown class="h-3 w-3" />
188
+ {/if}
189
+ </button>
190
+ {#if collapsed}
191
+ <button
192
+ type="button"
193
+ class="text-muted-foreground text-[11px] cursor-pointer hover:underline bg-transparent border-0 p-0 text-left font-mono"
194
+ onclick={() => handleToggle(itemPath, 1)}
195
+ >
196
+ {isArray(item) ? `Array(${item.length})` : `{...}`}
197
+ </button>
198
+ {:else}
199
+ <div class="flex-1">
200
+ <div class="text-muted-foreground text-[11px]">{isArray(item) ? '[' : '{'}</div>
201
+ <div class="pl-4 border-l border-border/30 my-0.5 space-y-1">
202
+ {#each Object.entries(item) as [subKey, subVal] (subKey)}
203
+ <div class="flex items-baseline gap-1 py-0.5">
204
+ <span class="text-foreground font-semibold">"{subKey}":</span>
205
+ <span class={cn(
206
+ typeof subVal === 'string' && 'text-success',
207
+ typeof subVal === 'number' && 'text-primary font-medium',
208
+ typeof subVal === 'boolean' && 'text-warning font-medium',
209
+ subVal === null && 'text-muted-foreground italic'
210
+ )}>
211
+ {typeof subVal === 'string' ? `"${subVal}"` : String(subVal)}
212
+ </span>
213
+ </div>
214
+ {/each}
215
+ </div>
216
+ <div class="text-muted-foreground text-[11px]">{isArray(item) ? ']' : '}'}</div>
217
+ </div>
218
+ {/if}
219
+ {:else}
220
+ <span class={cn(
221
+ typeof item === 'string' && 'text-success',
222
+ typeof item === 'number' && 'text-primary font-medium',
223
+ typeof item === 'boolean' && 'text-warning font-medium',
224
+ item === null && 'text-muted-foreground italic'
225
+ )}>
226
+ {typeof item === 'string' ? `"${item}"` : String(item)}
227
+ </span>
228
+ {/if}
229
+ </div>
230
+ {/if}
231
+ {/each}
232
+ </div>
233
+ <span class="text-muted-foreground">]</span>
234
+ </div>
235
+ {:else if isObject(parsedData)}
236
+ <!-- Object Root -->
237
+ <div class="space-y-0.5">
238
+ <span class="text-muted-foreground">&#123;</span>
239
+ <div class="pl-4 border-l border-border/40 space-y-1 my-0.5">
240
+ {#each Object.entries(parsedData) as [key, val] (key)}
241
+ {@const nodePath = key}
242
+ {#if matchesSearch(key, val)}
243
+ <div class="flex items-start gap-1 group py-0.5 hover:bg-muted/20 rounded px-1 -mx-1">
244
+ {#if isObject(val) || isArray(val)}
245
+ {@const collapsed = isPathCollapsed(nodePath, 1)}
246
+ <button
247
+ type="button"
248
+ onclick={() => handleToggle(nodePath, 1)}
249
+ class="inline-flex items-center p-0.5 text-muted-foreground hover:text-foreground cursor-pointer mt-0.5"
250
+ aria-label={`Toggle ${key}`}
251
+ >
252
+ {#if collapsed}
253
+ <ChevronRight class="h-3 w-3" />
254
+ {:else}
255
+ <ChevronDown class="h-3 w-3" />
256
+ {/if}
257
+ </button>
258
+ <span class="text-foreground font-semibold">"{key}":</span>
259
+ {#if collapsed}
260
+ <button
261
+ type="button"
262
+ class="text-muted-foreground text-[11px] cursor-pointer hover:underline bg-transparent border-0 p-0 text-left font-mono"
263
+ onclick={() => handleToggle(nodePath, 1)}
264
+ >
265
+ {isArray(val) ? `Array(${val.length})` : `{...}`}
266
+ </button>
267
+ {:else}
268
+ <div class="flex-1">
269
+ <span class="text-muted-foreground text-[11px]">{isArray(val) ? '[' : '{'}</span>
270
+ <div class="pl-4 border-l border-border/30 my-0.5 space-y-1">
271
+ {#each Object.entries(val) as [subK, subV] (subK)}
272
+ <div class="flex items-baseline gap-1 py-0.5">
273
+ <span class="text-foreground/90 font-medium">"{subK}":</span>
274
+ <span class={cn(
275
+ typeof subV === 'string' && 'text-success',
276
+ typeof subV === 'number' && 'text-primary font-medium',
277
+ typeof subV === 'boolean' && 'text-warning font-medium',
278
+ subV === null && 'text-muted-foreground italic'
279
+ )}>
280
+ {typeof subV === 'string' ? `"${subV}"` : typeof subV === 'object' && subV !== null ? (Array.isArray(subV) ? `Array(${subV.length})` : '{...}') : String(subV)}
281
+ </span>
282
+ </div>
283
+ {/each}
284
+ </div>
285
+ <span class="text-muted-foreground text-[11px]">{isArray(val) ? ']' : '}'}</span>
286
+ </div>
287
+ {/if}
288
+ {:else}
289
+ <div class="flex items-baseline gap-1 flex-1">
290
+ <span class="text-foreground font-semibold">"{key}":</span>
291
+ <span class={cn(
292
+ typeof val === 'string' && 'text-success',
293
+ typeof val === 'number' && 'text-primary font-medium',
294
+ typeof val === 'boolean' && 'text-warning font-medium',
295
+ val === null && 'text-muted-foreground italic'
296
+ )}>
297
+ {typeof val === 'string' ? `"${val}"` : String(val)}
298
+ </span>
299
+ </div>
300
+ {/if}
301
+
302
+ <!-- Quick Path Copy Button on Hover -->
303
+ <button
304
+ type="button"
305
+ onclick={() => copySinglePath(nodePath, val)}
306
+ class="opacity-0 group-hover:opacity-100 transition-opacity p-0.5 text-muted-foreground hover:text-foreground cursor-pointer"
307
+ title={`复制路径: ${nodePath}`}
308
+ >
309
+ {#if copiedPath === nodePath}
310
+ <Check class="h-3 w-3 text-success" />
311
+ {:else}
312
+ <Copy class="h-3 w-3" />
313
+ {/if}
314
+ </button>
315
+ </div>
316
+ {/if}
317
+ {/each}
318
+ </div>
319
+ <span class="text-muted-foreground">&#125;</span>
320
+ </div>
321
+ {/if}
322
+ </div>
323
+ </div>
@@ -0,0 +1,20 @@
1
+ export interface JsonViewerProps {
2
+ /** 待解析展示的数据,可为对象、数组或合法的 JSON 字符串 */
3
+ data: unknown;
4
+ /** 初始展开深度,默认 2 */
5
+ initialDepth?: number | undefined;
6
+ /** 是否显示一键复制完整 JSON 按钮,默认 true */
7
+ copyable?: boolean | undefined;
8
+ /** 是否显示关键词搜索过滤栏,默认 true */
9
+ searchable?: boolean | undefined;
10
+ /** 字体大小:'sm' | 'md',默认 'sm' */
11
+ size?: 'sm' | 'md' | undefined;
12
+ /** 自定义最大高度类名,如 'max-h-96' */
13
+ maxHeight?: string | undefined;
14
+ /** 复制指定属性路径时的回调 */
15
+ onCopyPath?: ((path: string, value: unknown) => void) | undefined;
16
+ class?: string | undefined;
17
+ }
18
+ declare const JsonViewer: import("svelte").Component<JsonViewerProps, {}, "">;
19
+ type JsonViewer = ReturnType<typeof JsonViewer>;
20
+ export default JsonViewer;
@@ -0,0 +1,250 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from 'svelte';
3
+ import { AlertTriangle, AlertCircle } from '@lucide/svelte';
4
+ import TrendIndicator from './TrendIndicator.svelte';
5
+ import { cn } from '../utils.js';
6
+
7
+ export interface KpiCardProps {
8
+ /** KPI 标题 / 指标名称,如 "季度总营收"、"API 响应成功率" */
9
+ title: string;
10
+ /** 主指标数值,如 1284500 或 "99.98%" */
11
+ value: number | string;
12
+ /** 数值前缀,如 "¥"、"$" */
13
+ prefix?: string | undefined;
14
+ /** 数值后缀,如 "元"、"次"、"ms" */
15
+ suffix?: string | undefined;
16
+ /** 辅助副标题或时间跨度,如 "近 30 天"、"统计周期: Q3" */
17
+ subtitle?: string | undefined;
18
+ /** 上一期数值,若提供则自动使用 TrendIndicator 计算并展示同环比 */
19
+ prevValue?: number | undefined;
20
+ /** 同环比前缀,如 "YoY" 或 "环比" */
21
+ trendPrefix?: string | undefined;
22
+ /** 是否反转同环比色彩极性(如报错率降低为优) */
23
+ reverseTrendColor?: boolean | undefined;
24
+ /** 目标达成率:当前达成数值与目标数值,如 { current: 85, target: 100 } */
25
+ targetProgress?: { current: number; target: number; label?: string } | undefined;
26
+ /** 走势微图数据点(如近 7 天或 12 个月的数值数组,自动渲染纯 SVG 平滑 Sparkline) */
27
+ sparklineData?: number[] | undefined;
28
+ /** 走势微图色调:'primary' | 'success' | 'warning' | 'destructive' */
29
+ sparklineTone?: 'primary' | 'success' | 'warning' | 'destructive' | undefined;
30
+ /** 加载中状态(骨架屏态) */
31
+ loading?: boolean | undefined;
32
+ /** 预警状态:'normal' | 'warning' | 'danger' */
33
+ alertStatus?: 'normal' | 'warning' | 'danger' | undefined;
34
+ /** 异常预警提示信息 */
35
+ alertMessage?: string | undefined;
36
+ /** 徽标标签文本 */
37
+ badge?: string | undefined;
38
+ /** 自定义卡片右上角操作区或底部插槽 */
39
+ extra?: Snippet | undefined;
40
+ children?: Snippet | undefined;
41
+ class?: string | undefined;
42
+ }
43
+
44
+ let {
45
+ title,
46
+ value,
47
+ prefix = '',
48
+ suffix = '',
49
+ subtitle,
50
+ prevValue,
51
+ trendPrefix = '环比',
52
+ reverseTrendColor = false,
53
+ targetProgress,
54
+ sparklineData,
55
+ sparklineTone = 'primary',
56
+ loading = false,
57
+ alertStatus = 'normal',
58
+ alertMessage,
59
+ badge,
60
+ extra,
61
+ children,
62
+ class: className = '',
63
+ }: KpiCardProps = $props();
64
+
65
+ const formattedValue = $derived.by(() => {
66
+ if (typeof value === 'number') {
67
+ return value.toLocaleString('en-US');
68
+ }
69
+ return String(value);
70
+ });
71
+
72
+ const progressPercent = $derived.by(() => {
73
+ if (!targetProgress || targetProgress.target <= 0) return 0;
74
+ const pct = (targetProgress.current / targetProgress.target) * 100;
75
+ return Math.max(0, Math.min(100, Math.round(pct)));
76
+ });
77
+
78
+ const sparklinePoints = $derived.by(() => {
79
+ if (!sparklineData || sparklineData.length < 2) return '';
80
+ const min = Math.min(...sparklineData);
81
+ const max = Math.max(...sparklineData);
82
+ const range = max - min || 1;
83
+ const width = 120;
84
+ const height = 36;
85
+ const padding = 2;
86
+
87
+ const points = sparklineData.map((val, idx) => {
88
+ const x = padding + (idx / (sparklineData.length - 1)) * (width - padding * 2);
89
+ const y = height - padding - ((val - min) / range) * (height - padding * 2);
90
+ return `${x.toFixed(1)},${y.toFixed(1)}`;
91
+ });
92
+
93
+ return points.join(' ');
94
+ });
95
+
96
+ const sparklineStroke = $derived.by(() => {
97
+ switch (sparklineTone) {
98
+ case 'success':
99
+ return 'var(--success)';
100
+ case 'warning':
101
+ return 'var(--warning)';
102
+ case 'destructive':
103
+ return 'var(--destructive)';
104
+ case 'primary':
105
+ default:
106
+ return 'var(--primary)';
107
+ }
108
+ });
109
+
110
+ const alertClasses = $derived.by(() => {
111
+ if (alertStatus === 'danger') {
112
+ return 'border-destructive/40 bg-destructive/5 ring-1 ring-destructive/20';
113
+ }
114
+ if (alertStatus === 'warning') {
115
+ return 'border-warning/40 bg-warning/5 ring-1 ring-warning/20';
116
+ }
117
+ return 'border-border bg-card';
118
+ });
119
+ </script>
120
+
121
+ <div
122
+ data-svadmin-component="KpiCard"
123
+ class={cn(
124
+ 'relative flex flex-col justify-between rounded-xl border p-5 shadow-xs transition-all hover:shadow-sm',
125
+ alertClasses,
126
+ className
127
+ )}
128
+ >
129
+ {#if loading}
130
+ <!-- Loading skeleton state -->
131
+ <div class="space-y-4 animate-pulse">
132
+ <div class="flex items-center justify-between">
133
+ <div class="h-4 w-28 bg-muted rounded-md"></div>
134
+ <div class="h-4 w-12 bg-muted rounded-md"></div>
135
+ </div>
136
+ <div class="h-8 w-44 bg-muted rounded-md"></div>
137
+ <div class="h-3 w-32 bg-muted rounded-md"></div>
138
+ </div>
139
+ {:else}
140
+ <!-- Header -->
141
+ <div class="flex items-start justify-between gap-3">
142
+ <div class="space-y-1">
143
+ <div class="flex items-center gap-2">
144
+ <span class="text-xs font-medium text-muted-foreground tracking-wide">{title}</span>
145
+ {#if badge}
146
+ <span class="rounded-full bg-primary/10 px-2 py-0.5 text-[10px] font-medium text-primary">
147
+ {badge}
148
+ </span>
149
+ {/if}
150
+ </div>
151
+ {#if subtitle}
152
+ <p class="text-[11px] text-muted-foreground/80">{subtitle}</p>
153
+ {/if}
154
+ </div>
155
+
156
+ {#if extra}
157
+ <div class="shrink-0 text-xs">
158
+ {@render extra()}
159
+ </div>
160
+ {/if}
161
+ </div>
162
+
163
+ <!-- Main Metric Value & Trend / Sparkline -->
164
+ <div class="my-3 flex items-baseline justify-between gap-4">
165
+ <div class="flex items-baseline gap-1 font-mono tracking-tight">
166
+ {#if prefix}
167
+ <span class="text-base font-semibold text-muted-foreground">{prefix}</span>
168
+ {/if}
169
+ <span class="text-3xl font-bold text-foreground">{formattedValue}</span>
170
+ {#if suffix}
171
+ <span class="text-xs font-medium text-muted-foreground">{suffix}</span>
172
+ {/if}
173
+ </div>
174
+
175
+ <!-- Sparkline Mini Chart -->
176
+ {#if sparklinePoints}
177
+ <div class="shrink-0 flex items-center" title="历史走势">
178
+ <svg class="h-9 w-28 overflow-visible" viewBox="0 0 120 36">
179
+ <polyline
180
+ fill="none"
181
+ stroke={sparklineStroke}
182
+ stroke-width="2"
183
+ stroke-linecap="round"
184
+ stroke-linejoin="round"
185
+ points={sparklinePoints}
186
+ />
187
+ </svg>
188
+ </div>
189
+ {/if}
190
+ </div>
191
+
192
+ <!-- Trend, Progress & Alerts -->
193
+ <div class="space-y-2.5 pt-1">
194
+ {#if typeof prevValue === 'number' && typeof value === 'number'}
195
+ <div class="flex items-center gap-2 text-xs">
196
+ <TrendIndicator
197
+ {value}
198
+ {prevValue}
199
+ prefix={trendPrefix}
200
+ reverseColor={reverseTrendColor}
201
+ variant="badge"
202
+ size="sm"
203
+ />
204
+ <span class="text-[11px] text-muted-foreground">对比上一周期</span>
205
+ </div>
206
+ {/if}
207
+
208
+ <!-- Target Completion Progress Bar -->
209
+ {#if targetProgress}
210
+ <div class="space-y-1">
211
+ <div class="flex items-center justify-between text-[11px] text-muted-foreground font-mono">
212
+ <span>{targetProgress.label ?? '目标达成'}</span>
213
+ <span class="font-semibold text-foreground">{progressPercent}%</span>
214
+ </div>
215
+ <div class="h-1.5 w-full overflow-hidden rounded-full bg-muted">
216
+ <div
217
+ class="h-full rounded-full bg-primary transition-all duration-500 ease-out"
218
+ style:width={`${progressPercent}%`}
219
+ ></div>
220
+ </div>
221
+ </div>
222
+ {/if}
223
+
224
+ <!-- Alert warning banner -->
225
+ {#if alertMessage}
226
+ <div
227
+ class={cn(
228
+ 'flex items-center gap-1.5 rounded-lg px-2.5 py-1.5 text-xs',
229
+ alertStatus === 'danger'
230
+ ? 'bg-destructive/10 text-destructive'
231
+ : 'bg-warning/10 text-warning'
232
+ )}
233
+ >
234
+ {#if alertStatus === 'danger'}
235
+ <AlertCircle class="h-3.5 w-3.5 shrink-0" />
236
+ {:else}
237
+ <AlertTriangle class="h-3.5 w-3.5 shrink-0" />
238
+ {/if}
239
+ <span class="truncate">{alertMessage}</span>
240
+ </div>
241
+ {/if}
242
+
243
+ {#if children}
244
+ <div class="pt-2 border-t border-border/50 text-xs">
245
+ {@render children()}
246
+ </div>
247
+ {/if}
248
+ </div>
249
+ {/if}
250
+ </div>
@@ -0,0 +1,44 @@
1
+ import type { Snippet } from 'svelte';
2
+ export interface KpiCardProps {
3
+ /** KPI 标题 / 指标名称,如 "季度总营收"、"API 响应成功率" */
4
+ title: string;
5
+ /** 主指标数值,如 1284500 或 "99.98%" */
6
+ value: number | string;
7
+ /** 数值前缀,如 "¥"、"$" */
8
+ prefix?: string | undefined;
9
+ /** 数值后缀,如 "元"、"次"、"ms" */
10
+ suffix?: string | undefined;
11
+ /** 辅助副标题或时间跨度,如 "近 30 天"、"统计周期: Q3" */
12
+ subtitle?: string | undefined;
13
+ /** 上一期数值,若提供则自动使用 TrendIndicator 计算并展示同环比 */
14
+ prevValue?: number | undefined;
15
+ /** 同环比前缀,如 "YoY" 或 "环比" */
16
+ trendPrefix?: string | undefined;
17
+ /** 是否反转同环比色彩极性(如报错率降低为优) */
18
+ reverseTrendColor?: boolean | undefined;
19
+ /** 目标达成率:当前达成数值与目标数值,如 { current: 85, target: 100 } */
20
+ targetProgress?: {
21
+ current: number;
22
+ target: number;
23
+ label?: string;
24
+ } | undefined;
25
+ /** 走势微图数据点(如近 7 天或 12 个月的数值数组,自动渲染纯 SVG 平滑 Sparkline) */
26
+ sparklineData?: number[] | undefined;
27
+ /** 走势微图色调:'primary' | 'success' | 'warning' | 'destructive' */
28
+ sparklineTone?: 'primary' | 'success' | 'warning' | 'destructive' | undefined;
29
+ /** 加载中状态(骨架屏态) */
30
+ loading?: boolean | undefined;
31
+ /** 预警状态:'normal' | 'warning' | 'danger' */
32
+ alertStatus?: 'normal' | 'warning' | 'danger' | undefined;
33
+ /** 异常预警提示信息 */
34
+ alertMessage?: string | undefined;
35
+ /** 徽标标签文本 */
36
+ badge?: string | undefined;
37
+ /** 自定义卡片右上角操作区或底部插槽 */
38
+ extra?: Snippet | undefined;
39
+ children?: Snippet | undefined;
40
+ class?: string | undefined;
41
+ }
42
+ declare const KpiCard: import("svelte").Component<KpiCardProps, {}, "">;
43
+ type KpiCard = ReturnType<typeof KpiCard>;
44
+ export default KpiCard;