@svadmin/ui 0.50.0 → 0.52.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 (45) hide show
  1. package/dist/components/AutoTable.svelte +223 -106
  2. package/dist/components/AutoTable.svelte.d.ts +5 -0
  3. package/dist/components/ListPage.svelte +16 -2
  4. package/dist/components/ListPage.svelte.d.ts +7 -1
  5. package/dist/components/ShowPage.svelte +65 -23
  6. package/dist/components/ShowPage.svelte.d.ts +3 -0
  7. package/dist/components/buttons/CloneButton.svelte +23 -2
  8. package/dist/components/buttons/CloneButton.svelte.d.ts +3 -0
  9. package/dist/components/buttons/CreateButton.svelte +22 -2
  10. package/dist/components/buttons/CreateButton.svelte.d.ts +3 -0
  11. package/dist/components/buttons/DeleteButton.svelte +23 -3
  12. package/dist/components/buttons/DeleteButton.svelte.d.ts +3 -0
  13. package/dist/components/buttons/EditButton.svelte +23 -2
  14. package/dist/components/buttons/EditButton.svelte.d.ts +3 -0
  15. package/dist/components/buttons/ExportButton.svelte +22 -3
  16. package/dist/components/buttons/ExportButton.svelte.d.ts +3 -0
  17. package/dist/components/buttons/ImportButton.svelte +39 -17
  18. package/dist/components/buttons/ImportButton.svelte.d.ts +3 -0
  19. package/dist/components/buttons/ListButton.svelte +13 -2
  20. package/dist/components/buttons/ListButton.svelte.d.ts +2 -0
  21. package/dist/components/buttons/RefreshButton.svelte +22 -2
  22. package/dist/components/buttons/RefreshButton.svelte.d.ts +3 -0
  23. package/dist/components/buttons/SaveButton.svelte +23 -2
  24. package/dist/components/buttons/SaveButton.svelte.d.ts +3 -0
  25. package/dist/components/buttons/ShowButton.svelte +24 -3
  26. package/dist/components/buttons/ShowButton.svelte.d.ts +3 -0
  27. package/dist/components/content/AuditTimeline.svelte +86 -0
  28. package/dist/components/content/AuditTimeline.svelte.d.ts +20 -0
  29. package/dist/components/content/DescriptionList.svelte +125 -15
  30. package/dist/components/content/DescriptionList.svelte.d.ts +6 -2
  31. package/dist/components/content/index.d.ts +2 -0
  32. package/dist/components/content/index.js +1 -0
  33. package/dist/components/fields/BooleanField.svelte +90 -8
  34. package/dist/components/fields/BooleanField.svelte.d.ts +8 -0
  35. package/dist/components/fields/DateField.svelte +74 -4
  36. package/dist/components/fields/DateField.svelte.d.ts +4 -0
  37. package/dist/components/fields/NumberField.svelte +72 -4
  38. package/dist/components/fields/NumberField.svelte.d.ts +9 -0
  39. package/dist/components/fields/TagField.svelte +90 -6
  40. package/dist/components/fields/TagField.svelte.d.ts +9 -0
  41. package/dist/components/fields/UrlField.svelte +43 -7
  42. package/dist/components/fields/UrlField.svelte.d.ts +5 -0
  43. package/dist/index.d.ts +5 -0
  44. package/dist/index.js +1 -0
  45. package/package.json +2 -2
@@ -0,0 +1,86 @@
1
+ <script module lang="ts">
2
+ import type { Status } from './StatusBadge.svelte';
3
+
4
+ export interface TimelineItem {
5
+ id: string;
6
+ title: string;
7
+ description?: string;
8
+ timestamp?: string;
9
+ actor?: string;
10
+ status?: Status;
11
+ tag?: string;
12
+ meta?: Record<string, unknown>;
13
+ }
14
+ </script>
15
+
16
+ <script lang="ts">
17
+ import { cn } from '../../utils.js';
18
+ import StatusBadge from './StatusBadge.svelte';
19
+ import DataState from './DataState.svelte';
20
+
21
+ interface Props {
22
+ items?: TimelineItem[];
23
+ emptyTitle?: string;
24
+ emptyDescription?: string;
25
+ class?: string;
26
+ }
27
+
28
+ let {
29
+ items = [],
30
+ emptyTitle = 'No timeline events',
31
+ emptyDescription = 'Activity events and status transitions will appear here.',
32
+ class: className = '',
33
+ }: Props = $props();
34
+
35
+ const dotClass: Record<Status, string> = {
36
+ success: 'bg-success border-success/30 text-success',
37
+ warning: 'bg-warning border-warning/30 text-warning-foreground',
38
+ danger: 'bg-destructive border-destructive/30 text-destructive',
39
+ info: 'bg-primary border-primary/30 text-primary',
40
+ neutral: 'bg-muted-foreground border-border text-muted-foreground',
41
+ };
42
+ </script>
43
+
44
+ <div class={cn('space-y-4', className)}>
45
+ {#if items.length === 0}
46
+ <DataState state="empty" title={emptyTitle} description={emptyDescription} />
47
+ {:else}
48
+ <div class="relative pl-6 space-y-6 before:absolute before:left-2 before:top-2 before:bottom-2 before:w-0.5 before:bg-border">
49
+ {#each items as item (item.id)}
50
+ {@const statusTone = item.status ?? 'info'}
51
+ <div class="relative flex flex-col gap-1 text-sm group">
52
+ <span
53
+ class={cn(
54
+ 'absolute -left-6 top-1 flex h-4 w-4 items-center justify-center rounded-full border-2 bg-background transition-transform group-hover:scale-110',
55
+ item.status ? dotClass[statusTone] : 'border-border bg-muted'
56
+ )}
57
+ aria-hidden="true"
58
+ ></span>
59
+
60
+ <div class="flex flex-wrap items-center justify-between gap-2">
61
+ <div class="flex items-center gap-2">
62
+ <span class="font-medium text-foreground">{item.title}</span>
63
+ {#if item.actor}
64
+ <span class="text-xs text-muted-foreground">by {item.actor}</span>
65
+ {/if}
66
+ {#if item.status}
67
+ <StatusBadge status={item.status} label={item.tag ?? item.status} class="text-[11px] py-0 h-4" />
68
+ {:else if item.tag}
69
+ <span class="inline-flex items-center rounded border border-border px-1.5 py-0.5 text-[11px] font-medium text-muted-foreground bg-muted">
70
+ {item.tag}
71
+ </span>
72
+ {/if}
73
+ </div>
74
+ {#if item.timestamp}
75
+ <time class="text-xs tabular-nums text-muted-foreground font-mono">{item.timestamp}</time>
76
+ {/if}
77
+ </div>
78
+
79
+ {#if item.description}
80
+ <p class="text-xs text-muted-foreground leading-relaxed whitespace-pre-wrap">{item.description}</p>
81
+ {/if}
82
+ </div>
83
+ {/each}
84
+ </div>
85
+ {/if}
86
+ </div>
@@ -0,0 +1,20 @@
1
+ import type { Status } from './StatusBadge.svelte';
2
+ export interface TimelineItem {
3
+ id: string;
4
+ title: string;
5
+ description?: string;
6
+ timestamp?: string;
7
+ actor?: string;
8
+ status?: Status;
9
+ tag?: string;
10
+ meta?: Record<string, unknown>;
11
+ }
12
+ interface Props {
13
+ items?: TimelineItem[];
14
+ emptyTitle?: string;
15
+ emptyDescription?: string;
16
+ class?: string;
17
+ }
18
+ declare const AuditTimeline: import("svelte").Component<Props, {}, "">;
19
+ type AuditTimeline = ReturnType<typeof AuditTimeline>;
20
+ export default AuditTimeline;
@@ -1,27 +1,137 @@
1
1
  <script lang="ts">
2
+ import { cn } from '../../utils.js';
3
+
2
4
  export interface DescriptionItem {
3
5
  label: string;
4
- value: string | number;
6
+ value: unknown;
5
7
  description?: string;
6
8
  href?: string;
9
+ span?: number;
7
10
  }
11
+
8
12
  interface Props {
9
13
  items?: DescriptionItem[];
10
- columns?: 1 | 2;
14
+ columns?: 1 | 2 | 3 | 4;
15
+ layout?: 'vertical' | 'horizontal';
16
+ bordered?: boolean;
17
+ density?: 'compact' | 'comfortable';
11
18
  class?: string;
12
19
  }
13
- let { items = [], columns = 1, class: className = '' }: Props = $props();
14
- const columnClass = $derived(columns === 2 ? 'sm:grid-cols-2' : 'grid-cols-1');
20
+
21
+ let {
22
+ items = [],
23
+ columns = 1,
24
+ layout = 'vertical',
25
+ bordered = false,
26
+ density = 'comfortable',
27
+ class: className = '',
28
+ }: Props = $props();
29
+
30
+ const isCompact = $derived(density === 'compact');
31
+ const isHorizontal = $derived(layout === 'horizontal');
32
+
33
+ function spanClass(span: number | undefined): string {
34
+ if (!span || span <= 1) return '';
35
+ if (span >= 4) return 'sm:col-span-4';
36
+ if (span === 3) return 'sm:col-span-3';
37
+ return 'sm:col-span-2';
38
+ }
39
+
40
+ const columnClass = $derived.by(() => {
41
+ switch (columns) {
42
+ case 4:
43
+ return 'grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4';
44
+ case 3:
45
+ return 'grid-cols-1 sm:grid-cols-2 lg:grid-cols-3';
46
+ case 2:
47
+ return 'grid-cols-1 sm:grid-cols-2';
48
+ default:
49
+ return 'grid-cols-1';
50
+ }
51
+ });
15
52
  </script>
16
53
 
17
- <dl class={'grid gap-x-6 gap-y-5 ' + columnClass + ' ' + className}>
18
- {#each items as item (item.label)}
19
- <div class="min-w-0">
20
- <dt class="text-xs font-medium text-muted-foreground">{item.label}</dt>
21
- <dd class="mt-1 break-words text-sm font-medium text-foreground">
22
- {#if item.href}<a class="text-primary underline-offset-4 hover:underline" href={item.href}>{item.value}</a>{:else}{item.value}{/if}
23
- </dd>
24
- {#if item.description}<p class="mt-1 text-xs text-muted-foreground">{item.description}</p>{/if}
25
- </div>
26
- {/each}
27
- </dl>
54
+ {#if bordered}
55
+ <div class={cn('overflow-hidden rounded-lg border border-border bg-card text-foreground', className)}>
56
+ <dl class={cn('grid divide-y divide-border/60 sm:divide-y-0', columnClass)}>
57
+ {#each items as item (item.label)}
58
+ <div
59
+ class={cn(
60
+ 'flex flex-col border-b border-r border-border/40 last:border-b-0',
61
+ isHorizontal ? 'sm:flex-row' : '',
62
+ spanClass(Math.min(item.span ?? 1, columns)),
63
+ )}
64
+ >
65
+ <dt
66
+ class={cn(
67
+ 'bg-muted/30 font-medium text-muted-foreground shrink-0 border-b sm:border-b-0 sm:border-r border-border/30',
68
+ isCompact ? 'px-3 py-1.5 text-xs' : 'px-4 py-2.5 text-xs sm:text-sm',
69
+ isHorizontal ? 'sm:w-1/3' : '',
70
+ )}
71
+ >
72
+ {item.label}
73
+ </dt>
74
+ <dd
75
+ class={cn(
76
+ 'break-words font-medium text-foreground bg-card flex-1 min-w-0',
77
+ isCompact ? 'px-3 py-1.5 text-xs' : 'px-4 py-2.5 text-xs sm:text-sm',
78
+ )}
79
+ >
80
+ {#if item.href}
81
+ <a class="text-primary underline-offset-4 hover:underline" href={item.href}>{String(item.value ?? '—')}</a>
82
+ {:else}
83
+ {item.value != null ? String(item.value) : '—'}
84
+ {/if}
85
+ {#if item.description}
86
+ <p class="mt-0.5 text-xs font-normal text-muted-foreground">{item.description}</p>
87
+ {/if}
88
+ </dd>
89
+ </div>
90
+ {/each}
91
+ </dl>
92
+ </div>
93
+ {:else}
94
+ <dl
95
+ class={cn(
96
+ 'grid',
97
+ columnClass,
98
+ isCompact ? 'gap-x-4 gap-y-3' : 'gap-x-6 gap-y-5',
99
+ className,
100
+ )}
101
+ >
102
+ {#each items as item (item.label)}
103
+ <div
104
+ class={cn(
105
+ 'min-w-0',
106
+ isHorizontal ? 'flex items-baseline gap-2' : '',
107
+ spanClass(Math.min(item.span ?? 1, columns)),
108
+ )}
109
+ >
110
+ <dt
111
+ class={cn(
112
+ 'font-medium text-muted-foreground',
113
+ isCompact ? 'text-xs' : 'text-xs',
114
+ isHorizontal ? 'shrink-0 min-w-20' : '',
115
+ )}
116
+ >
117
+ {item.label}
118
+ </dt>
119
+ <dd
120
+ class={cn(
121
+ 'break-words font-medium text-foreground min-w-0 flex-1',
122
+ isHorizontal ? 'text-xs sm:text-sm' : isCompact ? 'mt-0.5 text-xs sm:text-sm' : 'mt-1 text-sm',
123
+ )}
124
+ >
125
+ {#if item.href}
126
+ <a class="text-primary underline-offset-4 hover:underline" href={item.href}>{String(item.value ?? '—')}</a>
127
+ {:else}
128
+ {item.value != null ? String(item.value) : '—'}
129
+ {/if}
130
+ {#if item.description}
131
+ <p class="mt-1 text-xs font-normal text-muted-foreground">{item.description}</p>
132
+ {/if}
133
+ </dd>
134
+ </div>
135
+ {/each}
136
+ </dl>
137
+ {/if}
@@ -1,12 +1,16 @@
1
1
  export interface DescriptionItem {
2
2
  label: string;
3
- value: string | number;
3
+ value: unknown;
4
4
  description?: string;
5
5
  href?: string;
6
+ span?: number;
6
7
  }
7
8
  interface Props {
8
9
  items?: DescriptionItem[];
9
- columns?: 1 | 2;
10
+ columns?: 1 | 2 | 3 | 4;
11
+ layout?: 'vertical' | 'horizontal';
12
+ bordered?: boolean;
13
+ density?: 'compact' | 'comfortable';
10
14
  class?: string;
11
15
  }
12
16
  declare const DescriptionList: import("svelte").Component<Props, {}, "">;
@@ -41,3 +41,5 @@ export { default as TwoFactorStepper } from './TwoFactorStepper.svelte';
41
41
  export { default as SystemErrorState } from './SystemErrorState.svelte';
42
42
  export { default as FeedbackNotice } from './FeedbackNotice.svelte';
43
43
  export type { FeedbackNoticeTone, FeedbackNoticePriority } from './FeedbackNotice.svelte';
44
+ export { default as AuditTimeline } from './AuditTimeline.svelte';
45
+ export type { TimelineItem } from './AuditTimeline.svelte';
@@ -29,3 +29,4 @@ export { default as OtpInput } from './OtpInput.svelte';
29
29
  export { default as TwoFactorStepper } from './TwoFactorStepper.svelte';
30
30
  export { default as SystemErrorState } from './SystemErrorState.svelte';
31
31
  export { default as FeedbackNotice } from './FeedbackNotice.svelte';
32
+ export { default as AuditTimeline } from './AuditTimeline.svelte';
@@ -1,13 +1,95 @@
1
+ <script module lang="ts">
2
+ export type BooleanTone = 'success' | 'warning' | 'danger' | 'info' | 'neutral';
3
+ </script>
4
+
1
5
  <script lang="ts">
2
- interface Props { value: boolean | null | undefined; trueIcon?: string; falseIcon?: string }
3
- let { value, trueIcon = '✓', falseIcon = '✗' }: Props = $props();
6
+ import { Badge } from '../ui/badge/index.js';
7
+ import { cn } from '../../utils.js';
8
+
9
+ interface Props {
10
+ value: boolean | null | undefined;
11
+ mode?: 'icon' | 'badge' | 'tag' | 'text';
12
+ trueIcon?: string;
13
+ falseIcon?: string;
14
+ trueLabel?: string;
15
+ falseLabel?: string;
16
+ nullLabel?: string;
17
+ trueTone?: BooleanTone;
18
+ falseTone?: BooleanTone;
19
+ class?: string;
20
+ }
21
+
22
+ let {
23
+ value,
24
+ mode = 'icon',
25
+ trueIcon = '✓',
26
+ falseIcon = '✗',
27
+ trueLabel = 'Yes',
28
+ falseLabel = 'No',
29
+ nullLabel = '—',
30
+ trueTone = 'success',
31
+ falseTone = 'danger',
32
+ class: className = '',
33
+ }: Props = $props();
34
+
35
+ const toneClass: Record<BooleanTone, string> = {
36
+ success: 'border-success/30 bg-success/10 text-success',
37
+ warning: 'border-warning/30 bg-warning/10 text-warning-foreground',
38
+ danger: 'border-destructive/30 bg-destructive/10 text-destructive',
39
+ info: 'border-primary/30 bg-primary/10 text-primary',
40
+ neutral: 'border-border bg-muted text-muted-foreground',
41
+ };
4
42
  </script>
5
- {#if value}
6
- <span class="inline-flex items-center justify-center w-6 h-6 rounded-full text-sm bg-success/10 text-success">
7
- {trueIcon}
43
+
44
+ {#if value == null}
45
+ <span class={cn('text-muted-foreground', className)}>{nullLabel}</span>
46
+ {:else if mode === 'badge' || mode === 'tag'}
47
+ <Badge
48
+ variant="outline"
49
+ class={cn(
50
+ 'font-medium text-xs',
51
+ value ? toneClass[trueTone] : toneClass[falseTone],
52
+ className
53
+ )}
54
+ >
55
+ {value ? trueLabel : falseLabel}
56
+ </Badge>
57
+ {:else if mode === 'text'}
58
+ <span
59
+ class={cn(
60
+ 'font-medium text-sm',
61
+ value
62
+ ? trueTone === 'success'
63
+ ? 'text-success'
64
+ : 'text-foreground'
65
+ : falseTone === 'danger'
66
+ ? 'text-destructive'
67
+ : 'text-muted-foreground',
68
+ className
69
+ )}
70
+ >
71
+ {value ? trueLabel : falseLabel}
8
72
  </span>
9
73
  {:else}
10
- <span class="inline-flex items-center justify-center w-6 h-6 rounded-full text-sm bg-destructive/10 text-destructive">
11
- {falseIcon}
12
- </span>
74
+ {#if value}
75
+ <span
76
+ class={cn(
77
+ 'inline-flex items-center justify-center w-5 h-5 rounded-full text-xs font-semibold',
78
+ toneClass[trueTone],
79
+ className
80
+ )}
81
+ >
82
+ {trueIcon}
83
+ </span>
84
+ {:else}
85
+ <span
86
+ class={cn(
87
+ 'inline-flex items-center justify-center w-5 h-5 rounded-full text-xs font-semibold',
88
+ toneClass[falseTone],
89
+ className
90
+ )}
91
+ >
92
+ {falseIcon}
93
+ </span>
94
+ {/if}
13
95
  {/if}
@@ -1,7 +1,15 @@
1
+ export type BooleanTone = 'success' | 'warning' | 'danger' | 'info' | 'neutral';
1
2
  interface Props {
2
3
  value: boolean | null | undefined;
4
+ mode?: 'icon' | 'badge' | 'tag' | 'text';
3
5
  trueIcon?: string;
4
6
  falseIcon?: string;
7
+ trueLabel?: string;
8
+ falseLabel?: string;
9
+ nullLabel?: string;
10
+ trueTone?: BooleanTone;
11
+ falseTone?: BooleanTone;
12
+ class?: string;
5
13
  }
6
14
  declare const BooleanField: import("svelte").Component<Props, {}, "">;
7
15
  type BooleanField = ReturnType<typeof BooleanField>;
@@ -1,6 +1,76 @@
1
+ <script module lang="ts">
2
+ export type DateFieldFormat = 'date' | 'time' | 'datetime' | 'relative' | 'iso';
3
+ </script>
4
+
1
5
  <script lang="ts">
2
- interface Props { value: string | number | Date | null | undefined; locale?: string; options?: Intl.DateTimeFormatOptions }
3
- let { value, locale = 'en-US', options = { dateStyle: 'medium' } }: Props = $props();
4
- const formatted = $derived(value != null ? new Intl.DateTimeFormat(locale, options).format(new Date(value as string | number | Date)) : '—');
6
+ import { cn } from '../../utils.js';
7
+
8
+ interface Props {
9
+ value: string | number | Date | null | undefined;
10
+ locale?: string;
11
+ format?: DateFieldFormat;
12
+ options?: Intl.DateTimeFormatOptions;
13
+ nullLabel?: string;
14
+ class?: string;
15
+ }
16
+
17
+ let {
18
+ value,
19
+ locale = 'en-US',
20
+ format = 'date',
21
+ options,
22
+ nullLabel = '—',
23
+ class: className = '',
24
+ }: Props = $props();
25
+
26
+ function formatRelative(date: Date, loc: string): string {
27
+ const now = Date.now();
28
+ const diffMs = date.getTime() - now;
29
+ const diffSec = Math.round(diffMs / 1000);
30
+ const diffMin = Math.round(diffSec / 60);
31
+ const diffHour = Math.round(diffMin / 60);
32
+ const diffDay = Math.round(diffHour / 24);
33
+
34
+ try {
35
+ const rtf = new Intl.RelativeTimeFormat(loc, { numeric: 'auto' });
36
+ if (Math.abs(diffSec) < 60) return rtf.format(diffSec, 'second');
37
+ if (Math.abs(diffMin) < 60) return rtf.format(diffMin, 'minute');
38
+ if (Math.abs(diffHour) < 24) return rtf.format(diffHour, 'hour');
39
+ if (Math.abs(diffDay) < 30) return rtf.format(diffDay, 'day');
40
+ } catch {
41
+ // Fallback
42
+ }
43
+ return new Intl.DateTimeFormat(loc, { dateStyle: 'medium' }).format(date);
44
+ }
45
+
46
+ const formatted = $derived.by(() => {
47
+ if (value == null || value === '') return nullLabel;
48
+ const d = value instanceof Date ? value : new Date(value);
49
+ if (isNaN(d.getTime())) return nullLabel;
50
+
51
+ if (format === 'iso') return d.toISOString();
52
+ if (format === 'relative') return formatRelative(d, locale);
53
+
54
+ const defaultOptions: Intl.DateTimeFormatOptions =
55
+ options ??
56
+ (format === 'datetime'
57
+ ? { dateStyle: 'medium', timeStyle: 'short' }
58
+ : format === 'time'
59
+ ? { timeStyle: 'medium' }
60
+ : { dateStyle: 'medium' });
61
+
62
+ try {
63
+ return new Intl.DateTimeFormat(locale, defaultOptions).format(d);
64
+ } catch {
65
+ return d.toLocaleDateString();
66
+ }
67
+ });
68
+
69
+ const fullIsoTitle = $derived.by(() => {
70
+ if (value == null || value === '') return undefined;
71
+ const d = value instanceof Date ? value : new Date(value);
72
+ return isNaN(d.getTime()) ? undefined : d.toISOString();
73
+ });
5
74
  </script>
6
- <span class="field-date">{formatted}</span>
75
+
76
+ <span class={cn('field-date text-sm', className)} title={fullIsoTitle}>{formatted}</span>
@@ -1,7 +1,11 @@
1
+ export type DateFieldFormat = 'date' | 'time' | 'datetime' | 'relative' | 'iso';
1
2
  interface Props {
2
3
  value: string | number | Date | null | undefined;
3
4
  locale?: string;
5
+ format?: DateFieldFormat;
4
6
  options?: Intl.DateTimeFormatOptions;
7
+ nullLabel?: string;
8
+ class?: string;
5
9
  }
6
10
  declare const DateField: import("svelte").Component<Props, {}, "">;
7
11
  type DateField = ReturnType<typeof DateField>;
@@ -1,6 +1,74 @@
1
1
  <script lang="ts">
2
- interface Props { value: number | string | null | undefined; locale?: string; options?: Intl.NumberFormatOptions }
3
- let { value, locale = 'en-US', options = {} }: Props = $props();
4
- const formatted = $derived(value != null ? new Intl.NumberFormat(locale, options).format(Number(value)) : '—');
2
+ import { cn } from '../../utils.js';
3
+
4
+ interface Props {
5
+ value: number | string | null | undefined;
6
+ locale?: string;
7
+ currency?: string;
8
+ unit?: string;
9
+ prefix?: string;
10
+ precision?: number;
11
+ compact?: boolean;
12
+ signed?: boolean;
13
+ tabular?: boolean;
14
+ options?: Intl.NumberFormatOptions;
15
+ nullLabel?: string;
16
+ class?: string;
17
+ }
18
+
19
+ let {
20
+ value,
21
+ locale = 'en-US',
22
+ currency,
23
+ unit,
24
+ prefix,
25
+ precision,
26
+ compact = false,
27
+ signed = false,
28
+ tabular = true,
29
+ options = {},
30
+ nullLabel = '—',
31
+ class: className = '',
32
+ }: Props = $props();
33
+
34
+ const formatted = $derived.by(() => {
35
+ if (value == null || value === '') return nullLabel;
36
+ const num = Number(value);
37
+ if (isNaN(num)) return nullLabel;
38
+
39
+ const opts: Intl.NumberFormatOptions = { ...options };
40
+
41
+ if (currency) {
42
+ opts.style = 'currency';
43
+ opts.currency = currency;
44
+ }
45
+ if (compact) {
46
+ opts.notation = 'compact';
47
+ opts.compactDisplay = 'short';
48
+ }
49
+ if (precision != null) {
50
+ opts.minimumFractionDigits = precision;
51
+ opts.maximumFractionDigits = precision;
52
+ }
53
+ if (signed && num > 0 && !currency) {
54
+ opts.signDisplay = 'always';
55
+ }
56
+
57
+ try {
58
+ let result = new Intl.NumberFormat(locale, opts).format(num);
59
+ if (prefix && !currency) {
60
+ result = `${prefix}${result}`;
61
+ }
62
+ if (unit) {
63
+ result = `${result} ${unit}`;
64
+ }
65
+ return result;
66
+ } catch {
67
+ return String(num);
68
+ }
69
+ });
5
70
  </script>
6
- <span class="field-number">{formatted}</span>
71
+
72
+ <span class={cn('field-number text-sm', tabular && 'font-mono tabular-nums', className)}>
73
+ {formatted}
74
+ </span>
@@ -1,7 +1,16 @@
1
1
  interface Props {
2
2
  value: number | string | null | undefined;
3
3
  locale?: string;
4
+ currency?: string;
5
+ unit?: string;
6
+ prefix?: string;
7
+ precision?: number;
8
+ compact?: boolean;
9
+ signed?: boolean;
10
+ tabular?: boolean;
4
11
  options?: Intl.NumberFormatOptions;
12
+ nullLabel?: string;
13
+ class?: string;
5
14
  }
6
15
  declare const NumberField: import("svelte").Component<Props, {}, "">;
7
16
  type NumberField = ReturnType<typeof NumberField>;