glacia-native 0.3.2 → 0.3.4

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 (69) hide show
  1. package/LICENSE +1 -7
  2. package/README.md +178 -0
  3. package/lib/module/Dialog.js +128 -0
  4. package/lib/module/Form.js +125 -0
  5. package/lib/module/Input.js +64 -0
  6. package/lib/module/Table.js +81 -0
  7. package/lib/module/ai.js +246 -0
  8. package/lib/module/charts/AreaChart.js +32 -0
  9. package/lib/module/charts/BarChart.js +65 -0
  10. package/lib/module/charts/FunnelChart.js +48 -0
  11. package/lib/module/charts/HeatmapChart.js +69 -0
  12. package/lib/module/charts/LineChart.js +79 -0
  13. package/lib/module/charts/PlotChart.js +44 -0
  14. package/lib/module/charts/RadarChart.js +76 -0
  15. package/lib/module/charts/RingChart.js +50 -0
  16. package/lib/module/charts/ScatterChart.js +78 -0
  17. package/lib/module/charts/WaterfallChart.js +76 -0
  18. package/lib/module/charts/advanced.js +307 -0
  19. package/lib/module/charts/extra.js +105 -0
  20. package/lib/module/charts/index.js +50 -0
  21. package/lib/module/charts/primitives.js +212 -0
  22. package/lib/module/charts/scales.js +140 -0
  23. package/lib/module/index.js +48 -1
  24. package/lib/module/internal/tokens.js +3 -0
  25. package/lib/typescript/Dialog.d.ts +71 -0
  26. package/lib/typescript/Form.d.ts +81 -0
  27. package/lib/typescript/Input.d.ts +47 -0
  28. package/lib/typescript/Table.d.ts +55 -0
  29. package/lib/typescript/ai.d.ts +163 -0
  30. package/lib/typescript/charts/AreaChart.d.ts +44 -0
  31. package/lib/typescript/charts/BarChart.d.ts +19 -0
  32. package/lib/typescript/charts/FunnelChart.d.ts +25 -0
  33. package/lib/typescript/charts/HeatmapChart.d.ts +23 -0
  34. package/lib/typescript/charts/LineChart.d.ts +30 -0
  35. package/lib/typescript/charts/PlotChart.d.ts +31 -0
  36. package/lib/typescript/charts/RadarChart.d.ts +27 -0
  37. package/lib/typescript/charts/RingChart.d.ts +28 -0
  38. package/lib/typescript/charts/ScatterChart.d.ts +34 -0
  39. package/lib/typescript/charts/WaterfallChart.d.ts +22 -0
  40. package/lib/typescript/charts/advanced.d.ts +107 -0
  41. package/lib/typescript/charts/extra.d.ts +63 -0
  42. package/lib/typescript/charts/index.d.ts +13 -0
  43. package/lib/typescript/charts/primitives.d.ts +23 -0
  44. package/lib/typescript/charts/scales.d.ts +35 -0
  45. package/lib/typescript/index.d.ts +6 -0
  46. package/lib/typescript/internal/tokens.d.ts +2 -0
  47. package/package.json +10 -3
  48. package/src/Dialog.tsx +345 -0
  49. package/src/Form.tsx +360 -0
  50. package/src/Input.tsx +172 -0
  51. package/src/Table.tsx +207 -0
  52. package/src/ai.tsx +655 -0
  53. package/src/charts/AreaChart.tsx +73 -0
  54. package/src/charts/BarChart.tsx +181 -0
  55. package/src/charts/FunnelChart.tsx +123 -0
  56. package/src/charts/HeatmapChart.tsx +153 -0
  57. package/src/charts/LineChart.tsx +245 -0
  58. package/src/charts/PlotChart.tsx +148 -0
  59. package/src/charts/RadarChart.tsx +206 -0
  60. package/src/charts/RingChart.tsx +131 -0
  61. package/src/charts/ScatterChart.tsx +202 -0
  62. package/src/charts/WaterfallChart.tsx +183 -0
  63. package/src/charts/advanced.tsx +670 -0
  64. package/src/charts/extra.tsx +239 -0
  65. package/src/charts/index.ts +86 -0
  66. package/src/charts/primitives.ts +262 -0
  67. package/src/charts/scales.ts +163 -0
  68. package/src/index.ts +68 -0
  69. package/src/internal/tokens.ts +3 -0
@@ -0,0 +1,73 @@
1
+ import { NativeLineChart, type NativeLineSeries } from "./LineChart";
2
+
3
+ export interface NativeAreaChartProps {
4
+ labels: string[];
5
+ series: NativeLineSeries[];
6
+ width?: number;
7
+ height?: number;
8
+ smooth?: boolean;
9
+ isDark?: boolean;
10
+ palette?: readonly string[];
11
+ style?: any;
12
+ onPointPress?: (index: number) => void;
13
+ }
14
+
15
+ /**
16
+ * A continuous series with its area filled.
17
+ *
18
+ * This is NativeLineChart drawing filled: the same scales, axis ticks and
19
+ * touch targets, so the two stay consistent when used side by side.
20
+ */
21
+ export function NativeAreaChart(props: NativeAreaChartProps) {
22
+ return <NativeLineChart {...props} area />;
23
+ }
24
+
25
+ export interface NativeMirrorAreaSeries {
26
+ name: string;
27
+ /** Positive values drawn upward. */
28
+ above: number[];
29
+ /** Positive values drawn downward. */
30
+ below: number[];
31
+ }
32
+
33
+ export interface NativeMirrorAreaChartProps {
34
+ labels: string[];
35
+ series: NativeMirrorAreaSeries;
36
+ width?: number;
37
+ height?: number;
38
+ aboveColor?: string;
39
+ belowColor?: string;
40
+ isDark?: boolean;
41
+ style?: any;
42
+ }
43
+
44
+ /**
45
+ * Two series mirrored across a shared centre line.
46
+ *
47
+ * The below series is negated and drawn on the same axis as the above series,
48
+ * so both directions are measured against one honest scale rather than two
49
+ * charts that could be scaled differently.
50
+ */
51
+ export function NativeMirrorAreaChart({
52
+ labels,
53
+ series,
54
+ aboveColor = "#0088ff",
55
+ belowColor = "#ff375f",
56
+ ...rest
57
+ }: NativeMirrorAreaChartProps) {
58
+ return (
59
+ <NativeLineChart
60
+ {...rest}
61
+ labels={labels}
62
+ area
63
+ series={[
64
+ { name: `${series.name} (above)`, values: series.above, color: aboveColor },
65
+ {
66
+ name: `${series.name} (below)`,
67
+ values: series.below.map((v) => -Math.abs(v)),
68
+ color: belowColor,
69
+ },
70
+ ]}
71
+ />
72
+ );
73
+ }
@@ -0,0 +1,181 @@
1
+ import { useMemo, useState } from "react";
2
+
3
+ import { colors } from "../internal/tokens";
4
+ import { primitives } from "./primitives";
5
+ import { bandScale, formatValue, linearScale, niceDomain, seriesColor, ticks } from "./scales";
6
+
7
+ export interface NativeBarSeries {
8
+ name: string;
9
+ values: number[];
10
+ color?: string;
11
+ }
12
+
13
+ export interface NativeBarChartProps {
14
+ labels: string[];
15
+ series: NativeBarSeries[];
16
+ width?: number;
17
+ height?: number;
18
+ stacked?: boolean;
19
+ radius?: number;
20
+ isDark?: boolean;
21
+ palette?: readonly string[];
22
+ style?: any;
23
+ onBarPress?: (index: number) => void;
24
+ }
25
+
26
+ const PADDING = { top: 12, right: 10, bottom: 24, left: 38 };
27
+
28
+ /** Grouped or stacked bars, drawn with react-native-svg. */
29
+ export function NativeBarChart({
30
+ labels,
31
+ series,
32
+ width = 320,
33
+ height = 200,
34
+ stacked = false,
35
+ radius = 4,
36
+ isDark = false,
37
+ palette,
38
+ style,
39
+ onBarPress,
40
+ }: NativeBarChartProps) {
41
+ const [active, setActive] = useState<number | null>(null);
42
+ const theme = isDark ? colors.dark : colors.light;
43
+ const { View, Text, Pressable, Svg, G, Line, Rect, SvgText } = primitives();
44
+
45
+ const plot = {
46
+ x: PADDING.left,
47
+ y: PADDING.top,
48
+ width: Math.max(0, width - PADDING.left - PADDING.right),
49
+ height: Math.max(0, height - PADDING.top - PADDING.bottom),
50
+ };
51
+ const bottom = plot.y + plot.height;
52
+ const right = plot.x + plot.width;
53
+
54
+ const totals = stacked
55
+ ? labels.map((_, i) => series.reduce((sum, s) => sum + (s.values[i] ?? 0), 0))
56
+ : series.flatMap((s) => s.values);
57
+
58
+ const domain = useMemo(() => {
59
+ const max = totals.length ? Math.max(...totals) : 1;
60
+ return niceDomain(0, max, 4);
61
+ }, [totals.join(",")]);
62
+
63
+ const y = linearScale(domain, [bottom, plot.y], { nice: false });
64
+ const bands = bandScale(labels.length, [plot.x, right], 0.32);
65
+ const barWidth = stacked ? bands.width : bands.width / Math.max(1, series.length);
66
+ const yTicks = ticks(domain, 4);
67
+
68
+ if (labels.length === 0 || series.length === 0) {
69
+ return (
70
+ <View style={[{ width, height, alignItems: "center", justifyContent: "center" }, style]}>
71
+ <Text style={{ color: theme.muted, fontSize: 12 }}>No bars to display</Text>
72
+ </View>
73
+ );
74
+ }
75
+
76
+ return (
77
+ <View style={style}>
78
+ <Svg width={width} height={height}>
79
+ {yTicks.map((tick, i) => (
80
+ <Line
81
+ key={`grid-${i}`}
82
+ x1={plot.x}
83
+ x2={right}
84
+ y1={y(tick)}
85
+ y2={y(tick)}
86
+ stroke={theme.axis}
87
+ strokeOpacity={0.24}
88
+ strokeWidth={1}
89
+ />
90
+ ))}
91
+
92
+ {labels.map((label, i) => {
93
+ let stackTop = bottom;
94
+
95
+ return (
96
+ <G key={label + i}>
97
+ {series.map((s, si) => {
98
+ const value = s.values[i] ?? 0;
99
+ const color = s.color ?? seriesColor(si, palette);
100
+ const barHeight = Math.max(0, bottom - y(value));
101
+
102
+ const barX = stacked
103
+ ? bands.start(i)
104
+ : bands.start(i) + si * barWidth + (si > 0 ? 1 : 0);
105
+ const barY = stacked ? stackTop - barHeight : y(value);
106
+ const w = stacked ? bands.width : Math.max(1, barWidth - (si > 0 ? 1 : 0));
107
+
108
+ if (stacked) stackTop -= barHeight;
109
+
110
+ return (
111
+ <Rect
112
+ key={s.name}
113
+ x={barX}
114
+ y={barY}
115
+ width={w}
116
+ height={barHeight}
117
+ rx={Math.min(radius, w / 2)}
118
+ fill={color}
119
+ opacity={active === null || active === i ? 1 : 0.45}
120
+ />
121
+ );
122
+ })}
123
+ </G>
124
+ );
125
+ })}
126
+
127
+ {yTicks.map((tick, i) => (
128
+ <SvgText
129
+ key={`y-${i}`}
130
+ x={plot.x - 6}
131
+ y={y(tick) + 3}
132
+ fill={theme.muted}
133
+ fontSize={9}
134
+ textAnchor="end"
135
+ >
136
+ {formatValue(tick, { compact: true })}
137
+ </SvgText>
138
+ ))}
139
+
140
+ {labels.map((label, i) => (
141
+ <SvgText
142
+ key={`x-${i}`}
143
+ x={bands.center(i)}
144
+ y={height - 8}
145
+ fill={theme.muted}
146
+ fontSize={9}
147
+ textAnchor="middle"
148
+ >
149
+ {label}
150
+ </SvgText>
151
+ ))}
152
+ </Svg>
153
+
154
+ <View
155
+ style={{
156
+ position: "absolute",
157
+ left: plot.x,
158
+ top: plot.y,
159
+ width: plot.width,
160
+ height: plot.height,
161
+ flexDirection: "row",
162
+ }}
163
+ >
164
+ {labels.map((label, i) => (
165
+ <Pressable
166
+ key={label + i}
167
+ accessibilityRole="button"
168
+ accessibilityLabel={`${label}: ${series
169
+ .map((s) => `${s.name} ${formatValue(s.values[i] ?? 0)}`)
170
+ .join(", ")}`}
171
+ style={{ flex: 1 }}
172
+ onPress={() => {
173
+ setActive((current) => (current === i ? null : i));
174
+ onBarPress?.(i);
175
+ }}
176
+ />
177
+ ))}
178
+ </View>
179
+ </View>
180
+ );
181
+ }
@@ -0,0 +1,123 @@
1
+ import { useState } from "react";
2
+
3
+ import { colors } from "../internal/tokens";
4
+ import { primitives } from "./primitives";
5
+ import { formatValue, seriesColor } from "./scales";
6
+
7
+ export interface NativeFunnelStage {
8
+ label: string;
9
+ value: number;
10
+ color?: string;
11
+ }
12
+
13
+ export interface NativeFunnelChartProps {
14
+ stages: NativeFunnelStage[];
15
+ width?: number;
16
+ stageHeight?: number;
17
+ /** Value the first stage is measured against. Defaults to its own value. */
18
+ baseline?: number;
19
+ isDark?: boolean;
20
+ palette?: readonly string[];
21
+ style?: any;
22
+ onStagePress?: (index: number) => void;
23
+ }
24
+
25
+ /**
26
+ * A sequence of stages where each narrows the one before it.
27
+ *
28
+ * Built from plain Views rather than SVG: a funnel is a set of proportional
29
+ * rectangles, so vector drawing would add a dependency for nothing. Each row
30
+ * shows its own value, its share of entry, and the step conversion — the three
31
+ * numbers a funnel is actually read for.
32
+ */
33
+ export function NativeFunnelChart({
34
+ stages,
35
+ width = 320,
36
+ stageHeight = 38,
37
+ baseline,
38
+ isDark = false,
39
+ palette,
40
+ style,
41
+ onStagePress,
42
+ }: NativeFunnelChartProps) {
43
+ const [active, setActive] = useState<number | null>(null);
44
+ const theme = isDark ? colors.dark : colors.light;
45
+ const { View, Text, Pressable } = primitives();
46
+
47
+ if (stages.length === 0) {
48
+ return (
49
+ <View style={[{ width, alignItems: "center", paddingVertical: 20 }, style]}>
50
+ <Text style={{ color: theme.muted, fontSize: 12 }}>No stages to display</Text>
51
+ </View>
52
+ );
53
+ }
54
+
55
+ const entry = baseline ?? stages[0]?.value ?? 1;
56
+ const widest = Math.max(entry, ...stages.map((s) => s.value)) || 1;
57
+
58
+ return (
59
+ <View style={[{ width }, style]}>
60
+ {stages.map((stage, i) => {
61
+ const previous = i === 0 ? null : stages[i - 1]!.value;
62
+ const stepRate = previous && previous > 0 ? stage.value / previous : null;
63
+ const share = entry > 0 ? stage.value / entry : 0;
64
+ const color = stage.color ?? seriesColor(i, palette);
65
+ const widthPercent = Math.max(0.02, stage.value / widest);
66
+
67
+ return (
68
+ <Pressable
69
+ key={stage.label}
70
+ accessibilityRole="button"
71
+ accessibilityLabel={`${stage.label}: ${formatValue(stage.value)}, ${Math.round(
72
+ share * 100,
73
+ )} percent of entry`}
74
+ onPress={() => {
75
+ setActive((current) => (current === i ? null : i));
76
+ onStagePress?.(i);
77
+ }}
78
+ style={{ marginBottom: i < stages.length - 1 ? 10 : 0 }}
79
+ >
80
+ <View style={{ flexDirection: "row", justifyContent: "space-between", marginBottom: 4 }}>
81
+ <Text style={{ color: theme.foreground, fontSize: 12, fontWeight: "500" }}>
82
+ {stage.label}
83
+ </Text>
84
+ <Text style={{ color: theme.muted, fontSize: 12 }}>
85
+ {formatValue(stage.value, { compact: true })}
86
+ </Text>
87
+ </View>
88
+
89
+ <View
90
+ style={{
91
+ height: stageHeight,
92
+ borderRadius: 10,
93
+ backgroundColor: isDark ? "rgba(235,235,245,0.08)" : "rgba(0,0,0,0.05)",
94
+ overflow: "hidden",
95
+ }}
96
+ >
97
+ <View
98
+ style={{
99
+ width: `${widthPercent * 100}%`,
100
+ height: "100%",
101
+ borderRadius: 10,
102
+ backgroundColor: color,
103
+ opacity: active === null || active === i ? 1 : 0.5,
104
+ }}
105
+ />
106
+ </View>
107
+
108
+ <View style={{ flexDirection: "row", gap: 10, marginTop: 3 }}>
109
+ <Text style={{ color: theme.muted, fontSize: 10 }}>
110
+ {Math.round(share * 100)}% of entry
111
+ </Text>
112
+ {stepRate !== null && (
113
+ <Text style={{ color: theme.muted, fontSize: 10 }}>
114
+ {Math.round(stepRate * 100)}% from previous
115
+ </Text>
116
+ )}
117
+ </View>
118
+ </Pressable>
119
+ );
120
+ })}
121
+ </View>
122
+ );
123
+ }
@@ -0,0 +1,153 @@
1
+ import { useState } from "react";
2
+
3
+ import { colors } from "../internal/tokens";
4
+ import { primitives } from "./primitives";
5
+ import { formatValue } from "./scales";
6
+
7
+ export interface NativeHeatmapChartProps {
8
+ /** One row per series, each holding one value per column. */
9
+ rows: { label: string; values: number[] }[];
10
+ columns: string[];
11
+ cellSize?: number;
12
+ cellGap?: number;
13
+ isDark?: boolean;
14
+ /** Colour for the lowest and highest values. Defaults to a blue ramp. */
15
+ minColor?: string;
16
+ maxColor?: string;
17
+ style?: any;
18
+ onCellPress?: (rowIndex: number, columnIndex: number) => void;
19
+ }
20
+
21
+ /** Parses "#rrggbb" into channels; anything else falls back to grey. */
22
+ function parseHex(hex: string): [number, number, number] {
23
+ const match = /^#?([0-9a-f]{6})$/i.exec(hex);
24
+ if (!match) return [128, 128, 128];
25
+ const int = parseInt(match[1]!, 16);
26
+ return [(int >> 16) & 255, (int >> 8) & 255, int & 255];
27
+ }
28
+
29
+ /** Linear blend between two hex colours, so a ramp needs no colour library. */
30
+ function mix(from: string, to: string, t: number): string {
31
+ const a = parseHex(from);
32
+ const b = parseHex(to);
33
+ const clamped = Math.max(0, Math.min(1, t));
34
+ const channels = a.map((channel, i) => Math.round(channel + (b[i]! - channel) * clamped));
35
+ return `rgb(${channels[0]}, ${channels[1]}, ${channels[2]})`;
36
+ }
37
+
38
+ /**
39
+ * A grid where colour encodes magnitude.
40
+ *
41
+ * Cells are Views rather than SVG rects: the grid is uniform, so native layout
42
+ * positions it exactly and text stays crisp without vector text.
43
+ */
44
+ export function NativeHeatmapChart({
45
+ rows,
46
+ columns,
47
+ cellSize = 28,
48
+ cellGap = 3,
49
+ isDark = false,
50
+ minColor = "#e8eef7",
51
+ maxColor = "#0088ff",
52
+ style,
53
+ onCellPress,
54
+ }: NativeHeatmapChartProps) {
55
+ const [active, setActive] = useState<{ r: number; c: number } | null>(null);
56
+ const theme = isDark ? colors.dark : colors.light;
57
+ const { View, Text, Pressable } = primitives();
58
+
59
+ if (rows.length === 0 || columns.length === 0) {
60
+ return (
61
+ <View style={[{ alignItems: "center", paddingVertical: 20 }, style]}>
62
+ <Text style={{ color: theme.muted, fontSize: 12 }}>No cells to display</Text>
63
+ </View>
64
+ );
65
+ }
66
+
67
+ const all = rows.flatMap((row) => row.values).filter(Number.isFinite);
68
+ const min = all.length ? Math.min(...all) : 0;
69
+ const max = all.length ? Math.max(...all) : 1;
70
+ const span = max - min;
71
+
72
+ // A flat grid would otherwise tint every cell the same; centre it instead.
73
+ const position = (value: number) => (span === 0 ? 0.5 : (value - min) / span);
74
+ const labelWidth = 54;
75
+
76
+ return (
77
+ <View style={style}>
78
+ <View style={{ flexDirection: "row", marginLeft: labelWidth }}>
79
+ {columns.map((column) => (
80
+ <View key={column} style={{ width: cellSize + cellGap, alignItems: "center" }}>
81
+ <Text style={{ color: theme.muted, fontSize: 9 }} numberOfLines={1}>
82
+ {column}
83
+ </Text>
84
+ </View>
85
+ ))}
86
+ </View>
87
+
88
+ {rows.map((row, r) => (
89
+ <View key={row.label} style={{ flexDirection: "row", alignItems: "center", marginTop: cellGap }}>
90
+ <View style={{ width: labelWidth }}>
91
+ <Text style={{ color: theme.foreground, fontSize: 10 }} numberOfLines={1}>
92
+ {row.label}
93
+ </Text>
94
+ </View>
95
+
96
+ {columns.map((column, c) => {
97
+ const value = row.values[c];
98
+ const isActive = active?.r === r && active?.c === c;
99
+ const tint = value == null ? "transparent" : mix(minColor, maxColor, position(value));
100
+
101
+ return (
102
+ <Pressable
103
+ key={column}
104
+ accessibilityRole="button"
105
+ accessibilityLabel={`${row.label} ${column}: ${
106
+ value == null ? "no data" : formatValue(value)
107
+ }`}
108
+ onPress={() => {
109
+ if (value == null) return;
110
+ setActive((current) => (current?.r === r && current?.c === c ? null : { r, c }));
111
+ onCellPress?.(r, c);
112
+ }}
113
+ style={{
114
+ width: cellSize,
115
+ height: cellSize,
116
+ marginRight: cellGap,
117
+ borderRadius: 6,
118
+ backgroundColor: tint,
119
+ borderWidth: isActive ? 2 : 0,
120
+ borderColor: theme.foreground,
121
+ }}
122
+ />
123
+ );
124
+ })}
125
+ </View>
126
+ ))}
127
+
128
+ <View style={{ flexDirection: "row", alignItems: "center", marginTop: 10, marginLeft: labelWidth }}>
129
+ <Text style={{ color: theme.muted, fontSize: 9, marginRight: 5 }}>
130
+ {formatValue(min, { compact: true })}
131
+ </Text>
132
+ <View
133
+ style={{
134
+ width: 70,
135
+ height: 6,
136
+ borderRadius: 3,
137
+ backgroundColor: mix(minColor, maxColor, 0.5),
138
+ }}
139
+ />
140
+ <Text style={{ color: theme.muted, fontSize: 9, marginLeft: 5 }}>
141
+ {formatValue(max, { compact: true })}
142
+ </Text>
143
+ </View>
144
+
145
+ {active !== null && rows[active.r]?.values[active.c] != null && (
146
+ <Text style={{ color: theme.foreground, fontSize: 11, marginTop: 8, marginLeft: labelWidth }}>
147
+ {rows[active.r]!.label} · {columns[active.c]}:{" "}
148
+ {formatValue(rows[active.r]!.values[active.c]!)}
149
+ </Text>
150
+ )}
151
+ </View>
152
+ );
153
+ }