@r2digisolutions/components 0.21.28 → 0.21.30

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.
@@ -29,7 +29,7 @@
29
29
  }: BarChartProps = $props();
30
30
 
31
31
  const W = 420;
32
- const pad = { t: 24, r: 12, b: 32, l: 12 };
32
+ const pad = { t: 24, r: 12, b: 40, l: 12 };
33
33
  const uid = `bc-${Math.random().toString(36).slice(2, 8)}`;
34
34
  const color = 'var(--color-brand-500, #6366f1)';
35
35
 
@@ -66,6 +66,10 @@
66
66
  };
67
67
  });
68
68
 
69
+ function axisLabel(label: string): string {
70
+ return label.length > 14 ? `${label.slice(0, 14)}…` : label;
71
+ }
72
+
69
73
  function select(i: number) {
70
74
  if (!interactive) return;
71
75
  tipIndex = tipIndex === i ? null : i;
@@ -109,8 +113,8 @@
109
113
  {formatTick(b.value, 0)}{unit}
110
114
  </text>
111
115
  {/if}
112
- <text x={b.cx} y={height - 8} text-anchor="middle" class="fill-muted" font-size="10">
113
- {b.label}
116
+ <text x={b.cx} y={height - 8} text-anchor="middle" class="fill-muted" font-size="12">
117
+ {axisLabel(b.label)}
114
118
  </text>
115
119
  {#if interactive}
116
120
  <!-- svelte-ignore a11y_no_static_element_interactions -->
@@ -13,6 +13,8 @@
13
13
  showLegend?: boolean;
14
14
  /** Max tick labels on the X axis (line/bar). */
15
15
  maxLabels?: number;
16
+ /** Draw numeric values on bars / near line points. */
17
+ showValues?: boolean;
16
18
  emptyLabel?: string;
17
19
  class?: string;
18
20
  }
@@ -23,6 +25,7 @@
23
25
  height = 180,
24
26
  showLegend = true,
25
27
  maxLabels = 7,
28
+ showValues = true,
26
29
  emptyLabel = 'No data',
27
30
  class: className = ''
28
31
  }: ChartProps = $props();
@@ -36,6 +39,9 @@
36
39
  '#a855f7'
37
40
  ];
38
41
 
42
+ /** Extra top space so bar value labels are not clipped. */
43
+ const valuePad = 16;
44
+
39
45
  const max = $derived(Math.max(...data.map((d) => d.value), 1));
40
46
  const total = $derived(data.reduce((s, d) => s + d.value, 0));
41
47
  const isEmpty = $derived(data.length === 0 || total === 0);
@@ -46,10 +52,11 @@
46
52
  if (data.length < 2) return '';
47
53
  const w = 320;
48
54
  const h = height - 28;
55
+ const top = showValues && data.length <= 14 ? 12 + valuePad : 12;
49
56
  return data
50
57
  .map((d, i) => {
51
58
  const x = 16 + (i / (data.length - 1)) * (w - 32);
52
- const y = 12 + (1 - d.value / max) * (h - 16);
59
+ const y = top + (1 - d.value / max) * (h - 16 - (top - 12));
53
60
  return `${x},${y}`;
54
61
  })
55
62
  .join(' ');
@@ -63,14 +70,23 @@
63
70
  const denom = total || 1;
64
71
  return data.map((d, i) => {
65
72
  const slice = (d.value / denom) * Math.PI * 2;
66
- const x1 = cx + r * Math.cos(angle);
67
- const y1 = cy + r * Math.sin(angle);
73
+ const start = angle;
74
+ const x1 = cx + r * Math.cos(start);
75
+ const y1 = cy + r * Math.sin(start);
68
76
  angle += slice;
69
77
  const x2 = cx + r * Math.cos(angle);
70
78
  const y2 = cy + r * Math.sin(angle);
71
79
  const large = slice > Math.PI ? 1 : 0;
80
+ // Full circle: start===end so a single arc is invisible — use two semicircles.
81
+ const full = slice >= Math.PI * 2 - 1e-6;
82
+ const mid = start + Math.PI;
83
+ const xMid = cx + r * Math.cos(mid);
84
+ const yMid = cy + r * Math.sin(mid);
85
+ const dPath = full
86
+ ? `M ${cx} ${cy} L ${x1} ${y1} A ${r} ${r} 0 1 1 ${xMid} ${yMid} A ${r} ${r} 0 1 1 ${x1} ${y1} Z`
87
+ : `M ${cx} ${cy} L ${x1} ${y1} A ${r} ${r} 0 ${large} 1 ${x2} ${y2} Z`;
72
88
  return {
73
- d: `M ${cx} ${cy} L ${x1} ${y1} A ${r} ${r} 0 ${large} 1 ${x2} ${y2} Z`,
89
+ d: dPath,
74
90
  color: d.color ?? palette[i % palette.length],
75
91
  label: d.label,
76
92
  value: d.value
@@ -82,6 +98,10 @@
82
98
  if (data.length <= maxLabels) return true;
83
99
  return i % labelStep === 0 || i === data.length - 1;
84
100
  }
101
+
102
+ function axisLabel(label: string): string {
103
+ return label.length > 12 ? `${label.slice(0, 11)}…` : label;
104
+ }
85
105
  </script>
86
106
 
87
107
  <div class={['relative w-full space-y-3', className]}>
@@ -101,10 +121,10 @@
101
121
  role="img"
102
122
  aria-label="Bar chart"
103
123
  >
104
- {#each data as d, i}
124
+ {#each data as d, i (i)}
105
125
  {@const bw = Math.max(8, (320 - 32) / Math.max(data.length, 1) - 6)}
106
126
  {@const x = 16 + i * ((320 - 32) / Math.max(data.length, 1))}
107
- {@const bh = ((d.value / max) * (height - 36)) || 0}
127
+ {@const bh = (d.value / max) * (height - 36 - valuePad) || 0}
108
128
  {@const y = height - 20 - bh}
109
129
  <rect
110
130
  {x}
@@ -115,6 +135,16 @@
115
135
  fill={d.color ?? palette[i % palette.length]}
116
136
  opacity="0.9"
117
137
  />
138
+ {#if showValues}
139
+ <text
140
+ x={x + bw / 2}
141
+ y={y - 4}
142
+ text-anchor="middle"
143
+ class="fill-secondary text-[10px] font-semibold"
144
+ >
145
+ {d.value}
146
+ </text>
147
+ {/if}
118
148
  {#if showLabel(i)}
119
149
  <text
120
150
  x={x + bw / 2}
@@ -122,7 +152,7 @@
122
152
  text-anchor="middle"
123
153
  class="fill-muted text-[9px]"
124
154
  >
125
- {d.label}
155
+ {axisLabel(d.label)}
126
156
  </text>
127
157
  {/if}
128
158
  {/each}
@@ -154,21 +184,34 @@
154
184
  stroke-linecap="round"
155
185
  stroke-linejoin="round"
156
186
  />
157
- {#each data as d, i}
187
+ {#each data as d, i (i)}
188
+ {@const top = showValues && data.length <= 14 ? 12 + valuePad : 12}
158
189
  {@const x = 16 + (i / Math.max(data.length - 1, 1)) * 288}
159
- {@const y = 12 + (1 - d.value / max) * (height - 44)}
190
+ {@const y = top + (1 - d.value / max) * (height - 32 - top)}
160
191
  {#if data.length <= 14}
161
192
  <circle cx={x} cy={y} r="2.5" fill={palette[0]} />
162
193
  {/if}
194
+ {#if showValues && data.length <= 14}
195
+ <text
196
+ x={x}
197
+ y={y - 8}
198
+ text-anchor="middle"
199
+ class="fill-secondary text-[10px] font-semibold"
200
+ >
201
+ {d.value}
202
+ </text>
203
+ {/if}
163
204
  {#if showLabel(i)}
164
- <text x={x} y={height - 4} text-anchor="middle" class="fill-muted text-[9px]">{d.label}</text>
205
+ <text x={x} y={height - 4} text-anchor="middle" class="fill-muted text-[9px]"
206
+ >{axisLabel(d.label)}</text
207
+ >
165
208
  {/if}
166
209
  {/each}
167
210
  </svg>
168
211
  {:else}
169
212
  <div class="flex flex-wrap items-center gap-4">
170
213
  <svg viewBox="0 0 180 180" class="h-44 w-44" role="img" aria-label="Donut chart">
171
- {#each arcs as a}
214
+ {#each arcs as a (a.label)}
172
215
  <path d={a.d} fill={a.color} />
173
216
  {/each}
174
217
  <circle cx="90" cy="90" r="42" class="fill-surface-elevated" />
@@ -182,7 +225,7 @@
182
225
 
183
226
  {#if showLegend && !isEmpty}
184
227
  <ul class="flex flex-wrap gap-3">
185
- {#each data as d, i}
228
+ {#each data as d, i (i)}
186
229
  <li class="text-secondary inline-flex items-center gap-1.5 text-xs">
187
230
  <span
188
231
  class="h-2.5 w-2.5 rounded-full"
@@ -11,6 +11,8 @@ interface ChartProps {
11
11
  showLegend?: boolean;
12
12
  /** Max tick labels on the X axis (line/bar). */
13
13
  maxLabels?: number;
14
+ /** Draw numeric values on bars / near line points. */
15
+ showValues?: boolean;
14
16
  emptyLabel?: string;
15
17
  class?: string;
16
18
  }
@@ -64,16 +64,33 @@
64
64
  const iy1 = cy + innerR * Math.sin(angle);
65
65
  const ix2 = cx + innerR * Math.cos(start);
66
66
  const iy2 = cy + innerR * Math.sin(start);
67
+ // Full circle: start===end so a single arc is invisible — use two semicircles.
68
+ const full = slice >= Math.PI * 2 - 1e-6;
69
+ const mid = start + Math.PI;
70
+ const oxMid = cx + r * Math.cos(mid);
71
+ const oyMid = cy + r * Math.sin(mid);
72
+ const ixMid = cx + innerR * Math.cos(mid);
73
+ const iyMid = cy + innerR * Math.sin(mid);
67
74
  const path =
68
75
  slice <= 0
69
76
  ? ''
70
- : [
71
- `M ${x1} ${y1}`,
72
- `A ${r} ${r} 0 ${large} 1 ${x2} ${y2}`,
73
- `L ${ix1} ${iy1}`,
74
- `A ${innerR} ${innerR} 0 ${large} 0 ${ix2} ${iy2}`,
75
- 'Z'
76
- ].join(' ');
77
+ : full
78
+ ? [
79
+ `M ${x1} ${y1}`,
80
+ `A ${r} ${r} 0 1 1 ${oxMid} ${oyMid}`,
81
+ `A ${r} ${r} 0 1 1 ${x1} ${y1}`,
82
+ `L ${ix2} ${iy2}`,
83
+ `A ${innerR} ${innerR} 0 1 0 ${ixMid} ${iyMid}`,
84
+ `A ${innerR} ${innerR} 0 1 0 ${ix2} ${iy2}`,
85
+ 'Z'
86
+ ].join(' ')
87
+ : [
88
+ `M ${x1} ${y1}`,
89
+ `A ${r} ${r} 0 ${large} 1 ${x2} ${y2}`,
90
+ `L ${ix1} ${iy1}`,
91
+ `A ${innerR} ${innerR} 0 ${large} 0 ${ix2} ${iy2}`,
92
+ 'Z'
93
+ ].join(' ');
77
94
  return {
78
95
  ...d,
79
96
  value,
@@ -17,7 +17,12 @@
17
17
  label?: string;
18
18
  hint?: string;
19
19
  accept?: string;
20
- /** Form field name for the hidden file input (default `'files'`). */
20
+ /**
21
+ * Form field name for the file input.
22
+ * Leave unset for picker-only usage (`bind:files` / `onchange`) so the input is
23
+ * ignored by parent remote forms. When set with `multiple`, a `[]` suffix is
24
+ * added automatically unless the name already ends with `[]`.
25
+ */
21
26
  name?: string;
22
27
  /** Allow selecting more than one file (default true). */
23
28
  multiple?: boolean;
@@ -40,7 +45,7 @@
40
45
  label = 'Drop files here',
41
46
  hint = 'or click to browse',
42
47
  accept,
43
- name = 'files',
48
+ name,
44
49
  multiple = true,
45
50
  maxFiles,
46
51
  disabled = false,
@@ -63,6 +68,13 @@
63
68
  const previewUrls = new Set<string>();
64
69
 
65
70
  const maxBytes = $derived(maxSizeMb * 1024 * 1024);
71
+ const inputName = $derived(
72
+ !name
73
+ ? undefined
74
+ : multiple && !name.endsWith('[]')
75
+ ? `${name}[]`
76
+ : name
77
+ );
66
78
 
67
79
  function formatBytes(bytes: number): string {
68
80
  if (bytes === 0) return '0 B';
@@ -263,7 +275,7 @@
263
275
  <input
264
276
  bind:this={inputEl}
265
277
  id={inputId}
266
- name={name || undefined}
278
+ name={inputName}
267
279
  type="file"
268
280
  class="sr-only"
269
281
  aria-label={label}
@@ -8,7 +8,12 @@ interface DropZoneProps {
8
8
  label?: string;
9
9
  hint?: string;
10
10
  accept?: string;
11
- /** Form field name for the hidden file input (default `'files'`). */
11
+ /**
12
+ * Form field name for the file input.
13
+ * Leave unset for picker-only usage (`bind:files` / `onchange`) so the input is
14
+ * ignored by parent remote forms. When set with `multiple`, a `[]` suffix is
15
+ * added automatically unless the name already ends with `[]`.
16
+ */
12
17
  name?: string;
13
18
  /** Allow selecting more than one file (default true). */
14
19
  multiple?: boolean;
@@ -77,6 +77,7 @@
77
77
 
78
78
  <div class={['w-full space-y-1', className]}>
79
79
  <DropZone
80
+ {name}
80
81
  {label}
81
82
  {hint}
82
83
  {accept}
@@ -11,6 +11,8 @@
11
11
  data?: HBarPoint[];
12
12
  /** Bar track width in viewBox units */
13
13
  barWidth?: number;
14
+ /** Left label column width in viewBox units */
15
+ labelWidth?: number;
14
16
  rowHeight?: number;
15
17
  interactive?: boolean;
16
18
  showValues?: boolean;
@@ -21,6 +23,7 @@
21
23
  const {
22
24
  data = [],
23
25
  barWidth = 280,
26
+ labelWidth = 88,
24
27
  rowHeight = 36,
25
28
  interactive = true,
26
29
  showValues = true,
@@ -29,10 +32,10 @@
29
32
  }: HorizontalBarChartProps = $props();
30
33
 
31
34
  const color = 'var(--color-brand-500, #6366f1)';
32
- const labelW = 88;
33
35
  const padR = 48;
34
- const W = $derived(labelW + barWidth + padR);
36
+ const W = $derived(labelWidth + barWidth + padR);
35
37
  const H = $derived(Math.max(data.length, 1) * rowHeight + 8);
38
+ const maxLabelChars = $derived(Math.floor(labelWidth / 6.5));
36
39
 
37
40
  let tipIndex = $state<number | null>(null);
38
41
 
@@ -47,6 +50,12 @@
47
50
  })
48
51
  );
49
52
 
53
+ function displayLabel(label: string): string {
54
+ const maxChars = maxLabelChars;
55
+ if (label.length <= maxChars) return label;
56
+ return `${label.slice(0, Math.max(0, maxChars - 1))}…`;
57
+ }
58
+
50
59
  function select(i: number) {
51
60
  if (!interactive) return;
52
61
  tipIndex = tipIndex === i ? null : i;
@@ -60,18 +69,18 @@
60
69
  role="img"
61
70
  aria-label="Horizontal bar chart"
62
71
  >
63
- {#each rows as r, i}
72
+ {#each rows as r, i (i)}
64
73
  <text
65
- x={labelW - 8}
74
+ x={labelWidth - 8}
66
75
  y={r.midY + 4}
67
76
  text-anchor="end"
68
77
  class="fill-secondary"
69
78
  font-size="11"
70
79
  >
71
- {r.label}
80
+ {displayLabel(r.label)}
72
81
  </text>
73
82
  <rect
74
- x={labelW}
83
+ x={labelWidth}
75
84
  y={r.y + 4}
76
85
  width={barWidth}
77
86
  height={rowHeight - 14}
@@ -80,7 +89,7 @@
80
89
  opacity="0.6"
81
90
  />
82
91
  <rect
83
- x={labelW}
92
+ x={labelWidth}
84
93
  y={r.y + 4}
85
94
  width={r.bw}
86
95
  height={rowHeight - 14}
@@ -91,7 +100,7 @@
91
100
  />
92
101
  {#if showValues}
93
102
  <text
94
- x={labelW + r.bw + 6}
103
+ x={labelWidth + r.bw + 6}
95
104
  y={r.midY + 4}
96
105
  class="fill-muted"
97
106
  font-size="10"
@@ -102,6 +111,7 @@
102
111
  {/if}
103
112
  {#if interactive}
104
113
  <!-- svelte-ignore a11y_no_static_element_interactions -->
114
+ <!-- svelte-ignore a11y_click_events_have_key_events -->
105
115
  <rect
106
116
  x={0}
107
117
  y={r.y}
@@ -119,7 +129,7 @@
119
129
  {@const t = rows[tipIndex]}
120
130
  <div
121
131
  class="pointer-events-none absolute z-10 rounded-lg border border-border bg-surface-elevated px-2.5 py-1.5 shadow-lg"
122
- style:left={`${((labelW + t.bw) / W) * 100}%`}
132
+ style:left={`${((labelWidth + t.bw) / W) * 100}%`}
123
133
  style:top={`${(t.y / H) * 100}%`}
124
134
  role="status"
125
135
  >
@@ -7,6 +7,8 @@ interface HorizontalBarChartProps {
7
7
  data?: HBarPoint[];
8
8
  /** Bar track width in viewBox units */
9
9
  barWidth?: number;
10
+ /** Left label column width in viewBox units */
11
+ labelWidth?: number;
10
12
  rowHeight?: number;
11
13
  interactive?: boolean;
12
14
  showValues?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@r2digisolutions/components",
3
- "version": "0.21.28",
3
+ "version": "0.21.30",
4
4
  "private": false,
5
5
  "description": "R2DigiSolutions Svelte 5 component library — Atomic Design, Tailwind 4, Light/Dark mode",
6
6
  "license": "MIT",