@milktop/react-ui 0.3.23 → 0.3.25

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.
@@ -18,6 +18,7 @@ export type AreaChartProps = ChartProps & {
18
18
  zero?: boolean;
19
19
  time?: boolean;
20
20
  formatX?: ((value: unknown) => string) | null;
21
+ xTicks?: (Date | string)[] | null;
21
22
  tooltipLabel?: ((row: ChartRow) => string) | null;
22
23
  };
23
- export declare function AreaChart({ data, x, y, series, height, format, label: labelProp, grid, curve, stacked, zero, time, formatX, tooltipLabel, classes, ...rest }: AreaChartProps): import("react").JSX.Element;
24
+ export declare function AreaChart({ data, x, y, series, height, format, label: labelProp, grid, curve, stacked, zero, time, formatX, xTicks, tooltipLabel, classes, ...rest }: AreaChartProps): import("react").JSX.Element;
@@ -14,6 +14,7 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
14
14
  * - `time`: `x` holds dates (Date, or '2024-03-08'), rows in date order: each point sits by its
15
15
  * date, so a year's gap looks like one, and the axis is marked in months and years
16
16
  * - `formatX(value)`: the axis' labels (a Date when `time`); `tooltipLabel(row)`: the tooltip's heading
17
+ * - `xTicks`: where `time` marks its axis (dates), in place of its months and years: a birthday a year
17
18
  * - `height` (240 px), `format(value)` for ticks and tooltips, `grid` (lines; true)
18
19
  * - `label`: what it shows, for assistive tech (a hidden table of the data)
19
20
  */
@@ -24,7 +25,7 @@ import { area, line, curveMonotoneX, curveLinear } from 'd3-shape';
24
25
  import { ChartFrame } from './ChartFrame.js';
25
26
  import { seriesOf, formatter, pad, yScale, fitScale, leftFor, labelStep, num, toDate } from './chart.js';
26
27
  import { partsOf } from '../utils.js';
27
- 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, tooltipLabel = null, classes, ...rest }) {
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 }) {
28
29
  const t = useMessages();
29
30
  const label = labelProp ?? t.chart.label;
30
31
  const part = partsOf('chart', classes);
@@ -61,7 +62,12 @@ export function AreaChart({ data = [], x = 'label', y = null, series = null, hei
61
62
  : (value) => String(value ?? ''));
62
63
  const labelOf = tooltipLabel ?? ((row) => time ? toDate(row[x]).toLocaleDateString(locale, { dateStyle: 'medium' }) : tickLabel(row[x]));
63
64
  // About one date every 80px, on whole months and years
64
- const timeTicks = time && rows.length > 1 ? byDate.ticks(Math.max(2, Math.floor(innerW / 80))) : [];
65
+ 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)));
69
+ // A mark near an end is drawn inward from it, so its label stays inside the chart
70
+ const anchorAt = (pos) => (pos < 40 ? 'start' : pos > innerW - 40 ? 'end' : 'middle');
65
71
  const shape = curve === 'linear' ? curveLinear : curveMonotoneX;
66
72
  // Fitted, the fill runs down to the axis' foot rather than to 0, which is off the chart
67
73
  const fill = area().x((_, i) => at(i)).y0((d) => (fitted ? innerH : ys(d[0]))).y1((d) => ys(d[1])).curve(shape);
@@ -77,5 +83,5 @@ export function AreaChart({ data = [], x = 'label', y = null, series = null, hei
77
83
  nearest = i; });
78
84
  setHover(nearest);
79
85
  }
80
- 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: "middle", 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))] })] })] }) }));
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))] })] })] }) }));
81
87
  }
@@ -1,3 +1,4 @@
1
+ import { type ButtonProps } from '../button/Button.js';
1
2
  import { type PopoverProps } from '../popover/Popover.js';
2
3
  import { type Classes } from '../utils.js';
3
4
  import '../theme.css';
@@ -14,6 +15,7 @@ export type DateRangeFilterProps = {
14
15
  presets?: (DateRangeKey | DateRangePreset)[];
15
16
  placeholder?: string | null;
16
17
  placement?: PopoverProps['placement'];
18
+ size?: ButtonProps['size'];
17
19
  locale?: string | null;
18
20
  className?: string;
19
21
  classes?: Classes<'trigger' | 'panel' | 'presets' | 'preset' | 'clear' | 'calendar'>;
@@ -37,6 +39,6 @@ export declare const dateRanges: {
37
39
  thisYear: () => Date[];
38
40
  lastYear: () => Date[];
39
41
  };
40
- declare function DateRangeFilterShortcut({ value, defaultValue, onValueChange, presets, placeholder, placement, locale: localeProp, className, classes, }: DateRangeFilterProps): import("react").JSX.Element;
42
+ declare function DateRangeFilterShortcut({ value, defaultValue, onValueChange, presets, placeholder, placement, size, locale: localeProp, className, classes, }: DateRangeFilterProps): import("react").JSX.Element;
41
43
  export declare const DateRangeFilter: typeof DateRangeFilterShortcut;
42
44
  export {};
@@ -12,6 +12,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
12
12
  * your own (below), mixed in any order
13
13
  * - `placeholder`: the button's text with no dates ("All dates")
14
14
  * - `placement`: where the panel opens ('bottom-end')
15
+ * - `size`: the button's, to sit beside other controls ('sm', 'md' or 'lg')
15
16
  * - `locale`: for the dates on the button and the calendar (the
16
17
  * LocaleProvider's, or 'en-GB')
17
18
  *
@@ -91,7 +92,7 @@ function firstDayOf(locale) {
91
92
  return 1;
92
93
  }
93
94
  }
94
- function DateRangeFilterShortcut({ value, defaultValue, onValueChange, presets = ['today', 'last7', 'last30', 'thisMonth', 'lastMonth', 'thisQuarter', 'lastQuarter', 'thisYear', 'lastYear'], placeholder = null, placement = 'bottom-end', locale: localeProp = null, className, classes, }) {
95
+ function DateRangeFilterShortcut({ value, defaultValue, onValueChange, presets = ['today', 'last7', 'last30', 'thisMonth', 'lastMonth', 'thisQuarter', 'lastQuarter', 'thisYear', 'lastYear'], placeholder = null, placement = 'bottom-end', size = 'md', locale: localeProp = null, className, classes, }) {
95
96
  const t = useMessages();
96
97
  const app = useLocale();
97
98
  const locale = localeProp ?? app ?? 'en-GB';
@@ -130,6 +131,6 @@ function DateRangeFilterShortcut({ value, defaultValue, onValueChange, presets =
130
131
  return (_jsx(Popover, { placement: placement, "data-date-range-filter": "", onOpenChange: (open) => { if (open) {
131
132
  setPicking(dates);
132
133
  setShown((n) => n + 1);
133
- } }, trigger: _jsx(Button, { icon: "lucide:calendar", iconEnd: "lucide:chevron-down", className: cx(className, classes?.trigger), children: label }), children: _jsxs("div", { ...part('panel'), children: [_jsxs("div", { ...part('presets'), children: [items.map((item, i) => (_jsx(Button, { block: true, size: "sm", variant: "ghost", className: classes?.preset, "aria-pressed": matches(item), onClick: () => change(datesOf(item)), children: item.label }, i))), _jsx(Button, { block: true, size: "sm", icon: "lucide:x", className: classes?.clear, "data-clear": "", disabled: !dates.length, onClick: () => change([]), children: t.dateRangeFilter.clear })] }), _jsx(DatePicker, { inline: true, range: true, locale: locale, value: picking, onValueChange: picked, className: classes?.calendar }, shown)] }) }));
134
+ } }, trigger: _jsx(Button, { size: size, icon: "lucide:calendar", iconEnd: "lucide:chevron-down", className: cx(className, classes?.trigger), children: label }), children: _jsxs("div", { ...part('panel'), children: [_jsxs("div", { ...part('presets'), children: [items.map((item, i) => (_jsx(Button, { block: true, size: "sm", variant: "ghost", className: classes?.preset, "aria-pressed": matches(item), onClick: () => change(datesOf(item)), children: item.label }, i))), _jsx(Button, { block: true, size: "sm", icon: "lucide:x", className: classes?.clear, "data-clear": "", disabled: !dates.length, onClick: () => change([]), children: t.dateRangeFilter.clear })] }), _jsx(DatePicker, { inline: true, range: true, locale: locale, value: picking, onValueChange: picked, className: classes?.calendar }, shown)] }) }));
134
135
  }
135
136
  export const DateRangeFilter = DateRangeFilterShortcut;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@milktop/react-ui",
3
- "version": "0.3.23",
3
+ "version": "0.3.25",
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",