@milktop/react-ui 0.3.28 → 0.3.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.
@@ -12,7 +12,7 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
12
12
  * - `zero`: the value axis starts at 0 (default); false fits it to the values, so a small change
13
13
  * on a large value shows (a weight); stacked charts always start at 0
14
14
  * - `time`: `x` holds dates (Date, or '2024-03-08'), rows in date order: each point sits by its
15
- * date, so a year's gap looks like one, and the axis is marked in months and years
15
+ * date, so a year's gap looks like one, and the axis is marked in months or years (days when short)
16
16
  * - `formatX(value)`: the axis' labels (a Date when `time`); `tooltipLabel(row)`: the tooltip's heading
17
17
  * - `xTicks`: where `time` marks its axis (dates), in place of its months and years: a birthday a year
18
18
  * - `height` (240 px), `format(value)` for ticks and tooltips, `grid` (lines; true)
@@ -23,7 +23,7 @@ import { useId, useState } from 'react';
23
23
  import { scalePoint, scaleTime } from 'd3-scale';
24
24
  import { area, line, curveMonotoneX, curveLinear } from 'd3-shape';
25
25
  import { ChartFrame } from './ChartFrame.js';
26
- import { seriesOf, formatter, pad, yScale, fitScale, leftFor, labelStep, num, toDate } from './chart.js';
26
+ import { seriesOf, formatter, pad, yScale, fitScale, leftFor, labelStep, num, toDate, timeTicks, timeLabel } from './chart.js';
27
27
  import { partsOf } from '../utils.js';
28
28
  export function AreaChart({ data = [], x = 'label', y = null, series = null, height = 240, format = null, label: labelProp, grid = true, curve = 'smooth', stacked = false, zero = true, time = false, formatX = null, xTicks = null, tooltipLabel = null, classes, ...rest }) {
29
29
  const t = useMessages();
@@ -57,15 +57,13 @@ export function AreaChart({ data = [], x = 'label', y = null, series = null, hei
57
57
  const byDate = scaleTime().domain([dates[0] ?? new Date(), dates[dates.length - 1] ?? new Date()]).range([0, innerW]);
58
58
  const evenly = scalePoint().domain(rows.map((_, i) => i)).range([0, innerW]);
59
59
  const at = (i) => (time ? byDate(dates[i]) : evenly(i)) ?? 0;
60
- const tickLabel = formatX ?? (time
61
- ? (value) => toDate(value).toLocaleDateString(locale, { month: 'short', year: 'numeric' })
62
- : (value) => String(value ?? ''));
63
- const labelOf = tooltipLabel ?? ((row) => time ? toDate(row[x]).toLocaleDateString(locale, { dateStyle: 'medium' }) : tickLabel(row[x]));
64
- // About one date every 80px, on whole months and years
60
+ // About one date every 80px
65
61
  const [start, end] = byDate.domain();
66
- const timeTicks = !time || rows.length < 2 ? []
67
- : xTicks ? xTicks.map(toDate).filter((tick) => tick >= start && tick <= end)
68
- : byDate.ticks(Math.max(2, Math.floor(innerW / 80)));
62
+ const auto = timeTicks(start, end, Math.max(2, Math.floor(innerW / 80)), (count) => byDate.ticks(count));
63
+ const marks = !time || rows.length < 2 ? []
64
+ : xTicks ? xTicks.map(toDate).filter((tick) => tick >= start && tick <= end) : auto.ticks;
65
+ const tickLabel = formatX ?? (time ? timeLabel(auto.unit, locale) : (value) => String(value ?? ''));
66
+ const labelOf = tooltipLabel ?? ((row) => time ? toDate(row[x]).toLocaleDateString(locale, { dateStyle: 'medium' }) : tickLabel(row[x]));
69
67
  // A mark near an end is drawn inward from it, so its label stays inside the chart
70
68
  const anchorAt = (pos) => (pos < 40 ? 'start' : pos > innerW - 40 ? 'end' : 'middle');
71
69
  const shape = curve === 'linear' ? curveLinear : curveMonotoneX;
@@ -83,5 +81,5 @@ export function AreaChart({ data = [], x = 'label', y = null, series = null, hei
83
81
  nearest = i; });
84
82
  setHover(nearest);
85
83
  }
86
- return (_jsx(ChartFrame, { ...rest, rows: rows, x: x, labelOf: labelOf, list: list, fmt: fmt, height: height, label: label, classes: classes, hover: hover, onHover: setHover, onMove: move, width: width, onWidth: setWidth, tipLeft: hover == null ? 0 : left + at(hover), flip: hover != null && at(hover) > innerW * 0.6, children: _jsxs("svg", { ...part('svg'), width: width, height: height, "aria-hidden": "true", children: [_jsx("defs", { children: list.map((s, i) => (_jsxs("linearGradient", { id: `${id}-${i}`, x1: "0", x2: "0", y1: "0", y2: "1", children: [_jsx("stop", { offset: "0%", stopColor: s.color, stopOpacity: "0.28" }), _jsx("stop", { offset: "100%", stopColor: s.color, stopOpacity: "0.02" })] }, s.key))) }), _jsxs("g", { transform: `translate(${left},${pad.top})`, children: [ticks.map((tick) => (_jsxs("g", { children: [grid && _jsx("line", { ...part('grid'), x1: "0", x2: innerW, y1: ys(tick), y2: ys(tick) }), _jsx("text", { ...part('tick'), x: "-8", y: ys(tick), dy: "0.32em", textAnchor: "end", children: fmt(tick) })] }, tick))), time ? timeTicks.map((tick) => (_jsx("text", { ...part('tick'), x: byDate(tick), y: innerH + 18, textAnchor: anchorAt(byDate(tick)), children: tickLabel(tick) }, +tick))) : rows.map((row, i) => i % step === 0 && (_jsx("text", { ...part('tick'), x: at(i), y: innerH + 18, textAnchor: i === 0 ? 'start' : i === rows.length - 1 ? 'end' : 'middle', children: tickLabel(row[x]) }, i))), list.map((s, i) => (_jsxs("g", { children: [_jsx("path", { ...part('area'), d: fill(bands[i]) ?? '', fill: `url(#${id}-${i})` }), _jsx("path", { ...part('line'), d: stroke(bands[i]) ?? '', stroke: s.color })] }, s.key))), hover != null && _jsxs(_Fragment, { children: [_jsx("line", { ...part('cursor'), x1: at(hover), x2: at(hover), y1: "0", y2: innerH }), list.map((s, i) => _jsx("circle", { ...part('dot'), cx: at(hover), cy: ys(bands[i][hover][1]), r: "4", fill: s.color }, s.key))] })] })] }) }));
84
+ return (_jsx(ChartFrame, { ...rest, rows: rows, x: x, labelOf: labelOf, list: list, fmt: fmt, height: height, label: label, classes: classes, hover: hover, onHover: setHover, onMove: move, width: width, onWidth: setWidth, tipLeft: hover == null ? 0 : left + at(hover), flip: hover != null && at(hover) > innerW * 0.6, children: _jsxs("svg", { ...part('svg'), width: width, height: height, "aria-hidden": "true", children: [_jsx("defs", { children: list.map((s, i) => (_jsxs("linearGradient", { id: `${id}-${i}`, x1: "0", x2: "0", y1: "0", y2: "1", children: [_jsx("stop", { offset: "0%", stopColor: s.color, stopOpacity: "0.28" }), _jsx("stop", { offset: "100%", stopColor: s.color, stopOpacity: "0.02" })] }, s.key))) }), _jsxs("g", { transform: `translate(${left},${pad.top})`, children: [ticks.map((tick) => (_jsxs("g", { children: [grid && _jsx("line", { ...part('grid'), x1: "0", x2: innerW, y1: ys(tick), y2: ys(tick) }), _jsx("text", { ...part('tick'), x: "-8", y: ys(tick), dy: "0.32em", textAnchor: "end", children: fmt(tick) })] }, tick))), time ? marks.map((tick) => (_jsx("text", { ...part('tick'), x: byDate(tick), y: innerH + 18, textAnchor: anchorAt(byDate(tick)), children: tickLabel(tick) }, +tick))) : rows.map((row, i) => i % step === 0 && (_jsx("text", { ...part('tick'), x: at(i), y: innerH + 18, textAnchor: i === 0 ? 'start' : i === rows.length - 1 ? 'end' : 'middle', children: tickLabel(row[x]) }, i))), list.map((s, i) => (_jsxs("g", { children: [_jsx("path", { ...part('area'), d: fill(bands[i]) ?? '', fill: `url(#${id}-${i})` }), _jsx("path", { ...part('line'), d: stroke(bands[i]) ?? '', stroke: s.color })] }, s.key))), hover != null && _jsxs(_Fragment, { children: [_jsx("line", { ...part('cursor'), x1: at(hover), x2: at(hover), y1: "0", y2: innerH }), list.map((s, i) => _jsx("circle", { ...part('dot'), cx: at(hover), cy: ys(bands[i][hover][1]), r: "4", fill: s.color }, s.key))] })] })] }) }));
87
85
  }
@@ -23,3 +23,9 @@ export declare const leftFor: (ticks: number[], fmt: (value: unknown) => string)
23
23
  export declare const labelStep: (count: number, innerW: number) => number;
24
24
  export declare const num: (value: unknown) => number;
25
25
  export declare const toDate: (value: unknown) => Date;
26
+ export type TimeTicks = {
27
+ ticks: Date[];
28
+ unit: 'day' | 'month' | 'year';
29
+ };
30
+ export declare function timeTicks(start: Date, end: Date, max: number, days: (count: number) => Date[]): TimeTicks;
31
+ export declare const timeLabel: (unit: TimeTicks["unit"], locale?: string) => (value: unknown) => string;
@@ -28,3 +28,24 @@ export const num = (value) => +value || 0;
28
28
  export const toDate = (value) => value instanceof Date ? value
29
29
  : typeof value === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(value) ? new Date(`${value}T00:00`)
30
30
  : new Date(value);
31
+ // At most `max` marks between start and end: the first of every 1, 2, 3, 6, 12… months when the dates
32
+ // span two months or more, else days, as d3 picks them.
33
+ export function timeTicks(start, end, max, days) {
34
+ const from = start.getFullYear() * 12 + start.getMonth();
35
+ const to = end.getFullYear() * 12 + end.getMonth();
36
+ if (to - from < 2) {
37
+ const ticks = days(max);
38
+ const every = Math.ceil(ticks.length / max);
39
+ return { ticks: ticks.filter((_, i) => i % every === 0), unit: 'day' };
40
+ }
41
+ const step = [1, 2, 3, 6, 12, 24, 60, 120, 240].find((k) => Math.floor((to - from) / k) + 1 <= max) ?? 600;
42
+ const ticks = [];
43
+ for (let month = from; month <= to; month++) {
44
+ const tick = new Date(Math.floor(month / 12), month % 12, 1);
45
+ if (month % step === 0 && tick >= start)
46
+ ticks.push(tick);
47
+ }
48
+ return { ticks, unit: step >= 12 ? 'year' : 'month' };
49
+ }
50
+ export const timeLabel = (unit, locale) => (value) => toDate(value).toLocaleDateString(locale, unit === 'day' ? { day: 'numeric', month: 'short' }
51
+ : unit === 'month' ? { month: 'short', year: 'numeric' } : { year: 'numeric' });
@@ -13,6 +13,7 @@
13
13
  * - `allowMouseWheel`: scroll over the focused input to step
14
14
  * - `steppers`: false to hide the buttons
15
15
  * - `name`: for plain form posts
16
+ * - `autoFocus`: focuses the input, in a Dialog or Popover too
16
17
  *
17
18
  * By hand, with Ark's parts:
18
19
  *
@@ -43,7 +44,7 @@ export type NumberInputProps = Omit<Ark.RootProps, 'value' | 'defaultValue' | 'o
43
44
  placeholder?: string;
44
45
  classes?: Classes<'root' | 'label' | 'control' | 'icon' | 'prefix' | 'input' | 'suffix' | 'decrementTrigger' | 'incrementTrigger'>;
45
46
  };
46
- declare function NumberInputShortcut({ label, value, defaultValue, onValueChange, prefix, suffix, icon, steppers, placeholder, locale: localeProp, formatOptions: formatProp, className, classes, ...rest }: NumberInputProps): import("react").JSX.Element;
47
+ declare function NumberInputShortcut({ label, value, defaultValue, onValueChange, prefix, suffix, icon, steppers, placeholder, locale: localeProp, formatOptions: formatProp, autoFocus, className, classes, ...rest }: NumberInputProps): import("react").JSX.Element;
47
48
  export declare const NumberInput: typeof NumberInputShortcut & {
48
49
  Root: import("react").ForwardRefExoticComponent<Ark.RootProps & import("react").RefAttributes<HTMLDivElement>>;
49
50
  Label: import("react").ForwardRefExoticComponent<Ark.LabelProps & import("react").RefAttributes<HTMLLabelElement>>;
@@ -14,6 +14,7 @@ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-run
14
14
  * - `allowMouseWheel`: scroll over the focused input to step
15
15
  * - `steppers`: false to hide the buttons
16
16
  * - `name`: for plain form posts
17
+ * - `autoFocus`: focuses the input, in a Dialog or Popover too
17
18
  *
18
19
  * By hand, with Ark's parts:
19
20
  *
@@ -83,7 +84,7 @@ function numbers(locale, given) {
83
84
  toText: (value) => (value == null ? '' : formatOptions ? format.format(value) : String(value)),
84
85
  };
85
86
  }
86
- function NumberInputShortcut({ label = null, value, defaultValue, onValueChange, prefix = null, suffix = null, icon = null, steppers = true, placeholder = '', locale: localeProp, formatOptions: formatProp, className, classes, ...rest }) {
87
+ function NumberInputShortcut({ label = null, value, defaultValue, onValueChange, prefix = null, suffix = null, icon = null, steppers = true, placeholder = '', locale: localeProp, formatOptions: formatProp, autoFocus, className, classes, ...rest }) {
87
88
  const t = useMessages();
88
89
  const field = useFieldContext();
89
90
  const part = partsOf('number-input', classes);
@@ -110,7 +111,7 @@ function NumberInputShortcut({ label = null, value, defaultValue, onValueChange,
110
111
  onValueChange?.(number, details);
111
112
  }
112
113
  return (_jsxs(Ark.Root, { ...(t.zag.numberInput && { translations: t.zag.numberInput }), ...rest, ...part('root'), className: cx(className, classes?.root), locale: locale, ...(formatOptions && { formatOptions }), value: text, onValueChange: change, children: [label && !field && _jsx(Ark.Label, { ...part('label'), children: label }), _jsxs(Ark.Control, { ...part('control'), children: [icon ? _jsx("iconify-icon", { ...part('icon'), icon: icon, "aria-hidden": "true" })
113
- : prefix != null && _jsx("span", { ...part('prefix'), children: prefix }), _jsx(Ark.Input, { ...part('input'), placeholder: placeholder, ref: inputRef }), suffix != null && _jsx("span", { ...part('suffix'), children: suffix }), steppers && _jsxs(_Fragment, { children: [_jsx(Ark.DecrementTrigger, { ...part('decrement-trigger'), children: _jsx("iconify-icon", { icon: "lucide:minus" }) }), _jsx(Ark.IncrementTrigger, { ...part('increment-trigger'), children: _jsx("iconify-icon", { icon: "lucide:plus" }) })] })] })] }));
114
+ : prefix != null && _jsx("span", { ...part('prefix'), children: prefix }), _jsx(Ark.Input, { ...part('input'), placeholder: placeholder, ref: inputRef, autoFocus: autoFocus, "data-autofocus": autoFocus || undefined }), suffix != null && _jsx("span", { ...part('suffix'), children: suffix }), steppers && _jsxs(_Fragment, { children: [_jsx(Ark.DecrementTrigger, { ...part('decrement-trigger'), children: _jsx("iconify-icon", { icon: "lucide:minus" }) }), _jsx(Ark.IncrementTrigger, { ...part('increment-trigger'), children: _jsx("iconify-icon", { icon: "lucide:plus" }) })] })] })] }));
114
115
  }
115
116
  export const NumberInput = withParts(NumberInputShortcut, {
116
117
  Root: Ark.Root,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@milktop/react-ui",
3
- "version": "0.3.28",
3
+ "version": "0.3.30",
4
4
  "description": "React components on Ark UI (Zag state machines), with imba-ui's API and design",
5
5
  "license": "MIT",
6
6
  "author": "André Goldstein",