@startsimpli/ui 0.4.90 → 0.4.91

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@startsimpli/ui",
3
- "version": "0.4.90",
3
+ "version": "0.4.91",
4
4
  "description": "Shared UI components package for StartSimpli applications",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -22,6 +22,15 @@ export interface DonutChartProps {
22
22
  showLegend?: boolean
23
23
  /** Formats the right-hand column of a legend row. Defaults to the raw value. */
24
24
  formatLegendValue?: (segment: DonutSegment) => React.ReactNode
25
+ /**
26
+ * Narrowest a legend column may get, in px. The legend flows into as many
27
+ * columns as fit, so on a wide card it packs rows side by side instead of
28
+ * stretching each one — a legend row is a label and a number that belong
29
+ * together, and a 1400px row separates them by a metre of empty card.
30
+ */
31
+ legendColumnWidth?: number
32
+ /** Extra classes for the legend list — e.g. a `max-w-*` cap. */
33
+ legendClassName?: string
25
34
  /** Overrides the generated description. */
26
35
  ariaLabel?: string
27
36
  className?: string
@@ -81,6 +90,8 @@ export function DonutChart({
81
90
  thickness = 18,
82
91
  showLegend = false,
83
92
  formatLegendValue,
93
+ legendColumnWidth = 176,
94
+ legendClassName,
84
95
  ariaLabel,
85
96
  className,
86
97
  }: DonutChartProps) {
@@ -161,7 +172,22 @@ export function DonutChart({
161
172
  </div>
162
173
 
163
174
  {showLegend && segments.length > 0 && (
164
- <ul className="min-w-0 flex-1 space-y-2">
175
+ // Columns, not one tall list. A legend row pairs a label with its
176
+ // number, so the row has to stay narrow enough for the eye to carry one
177
+ // to the other; `auto-fill` spends extra card width on more columns
178
+ // instead of on the gap inside every row.
179
+ <ul
180
+ className={cn(
181
+ 'grid min-w-0 flex-1 gap-x-8 gap-y-2',
182
+ legendClassName,
183
+ )}
184
+ style={{
185
+ gridTemplateColumns: `repeat(auto-fill, minmax(${Math.max(
186
+ legendColumnWidth,
187
+ 1,
188
+ )}px, 1fr))`,
189
+ }}
190
+ >
165
191
  {segments.map((segment, i) => (
166
192
  <li
167
193
  key={`${segment.label}-${i}`}
@@ -441,6 +441,35 @@ describe('DonutChart', () => {
441
441
  expect(screen.getByText('$6,800')).toBeInTheDocument()
442
442
  })
443
443
 
444
+ it('flows the legend into columns rather than one full-width row per segment', () => {
445
+ // A legend row is a label and its number, and the eye has to carry one to
446
+ // the other. Stretched across a wide card the pair ends up a metre apart
447
+ // with nothing in between, so the list is a grid that spends spare width on
448
+ // more columns instead of on the gap inside each row.
449
+ const { container } = render(<DonutChart segments={segments} showLegend />)
450
+ const list = container.querySelector('ul')!
451
+ expect(list.className).toContain('grid')
452
+ expect(list.style.gridTemplateColumns).toBe(
453
+ 'repeat(auto-fill, minmax(176px, 1fr))',
454
+ )
455
+ })
456
+
457
+ it('lets the caller set the column width and add classes to the legend', () => {
458
+ const { container } = render(
459
+ <DonutChart
460
+ segments={segments}
461
+ showLegend
462
+ legendColumnWidth={240}
463
+ legendClassName="max-w-md"
464
+ />,
465
+ )
466
+ const list = container.querySelector('ul')!
467
+ expect(list.style.gridTemplateColumns).toBe(
468
+ 'repeat(auto-fill, minmax(240px, 1fr))',
469
+ )
470
+ expect(list.className).toContain('max-w-md')
471
+ })
472
+
444
473
  it('uses caller colours for the arcs and falls back to currentColor', () => {
445
474
  const { container } = render(
446
475
  <DonutChart segments={[{ label: 'a', value: 1, color: '#FF0000' }, { label: 'b', value: 1 }]} />,