@torpor/ui 0.3.6 → 0.3.7

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.
@@ -0,0 +1,215 @@
1
+ import type { ClassValue, StyleValue } from "@torpor/view";
2
+ import XAxis from "./XAxis.torp";
3
+ import YAxis from "./YAxis.torp";
4
+ import GridLines from "./GridLines.torp";
5
+ import {
6
+ calculateStepValue,
7
+ calculateStepLabels,
8
+ calculateValueHeight,
9
+ calculateChartBottom,
10
+ calculateChartLeft
11
+ } from "../utils/chartUtils";
12
+ import { chartColors } from "../utils/chartColors";
13
+
14
+ interface Pair {
15
+ x: number;
16
+ y: number;
17
+ label: string;
18
+ }
19
+
20
+ interface DataSeries {
21
+ name: string;
22
+ color: string;
23
+ data: Pair[];
24
+ }
25
+
26
+ interface ScatterChartProps {
27
+ /** An ID for the root element */
28
+ id?: string;
29
+ /** Classes for the root element */
30
+ class?: ClassValue,
31
+ /** Styles for the root element */
32
+ style?: StyleValue,
33
+ series: DataSeries[],
34
+ height?: number,
35
+ width?: number,
36
+ xLabel?: string,
37
+ yLabel?: string,
38
+ xStepCount?: number,
39
+ xStepValue?: number,
40
+ yStepCount?: number,
41
+ yStepValue?: number,
42
+ hideXAxis?: boolean,
43
+ hideYAxis?: boolean,
44
+ showHLines?: boolean,
45
+ showVLines?: boolean,
46
+ }
47
+
48
+ export default function ScatterChart($props: ScatterChartProps) {
49
+ const defaultHeight = 200;
50
+ const defaultWidth = 400;
51
+
52
+ let container: HTMLElement;
53
+ let measurer: SVGTextElement;
54
+
55
+ let $state = $watch({
56
+ textWidth: 0,
57
+ textHeight: 0,
58
+ get calculatedHeight() {
59
+ return $props.height || defaultHeight;
60
+ },
61
+ get calculatedWidth() {
62
+ return $props.width || container?.clientWidth || defaultWidth;
63
+ },
64
+ get maxXValue() {
65
+ return calculateMaxValue($props.series, "x");
66
+ },
67
+ get calculatedXStepValue() {
68
+ return $props.xStepValue || calculateStepValue($state.maxXValue, $props.xStepCount);
69
+ },
70
+ get xStepLabels() {
71
+ return calculateStepLabels($props.xStepCount || 2, $state.calculatedXStepValue);
72
+ },
73
+ get maxYValue() {
74
+ return calculateMaxValue($props.series, "y");
75
+ },
76
+ get calculatedYStepValue() {
77
+ return $props.yStepValue || calculateStepValue($state.maxYValue, $props.yStepCount);
78
+ },
79
+ get yStepLabels() {
80
+ return calculateStepLabels($props.yStepCount || 2, $state.calculatedYStepValue);
81
+ },
82
+ get itemWidth() {
83
+ return calculateValueHeight(
84
+ $state.calculatedWidth - $state.chartLeft,
85
+ $state.textHeight,
86
+ $state.calculatedXStepValue,
87
+ $props.xStepCount || 2
88
+ );
89
+ },
90
+ get valueHeight() {
91
+ return calculateValueHeight($state.chartBottom, $state.textHeight, $state.calculatedYStepValue, $props.yStepCount || 2);
92
+ },
93
+ get chartBottom() {
94
+ return calculateChartBottom($props.xLabel ?? "", $state.calculatedHeight, $state.textHeight);
95
+ },
96
+ get chartLeft() {
97
+ return calculateChartLeft($state.yStepLabels, $props.yLabel ?? "", $state.textHeight, $state.textWidth);
98
+ },
99
+ get pointSeries() {
100
+ return buildPointSeries(
101
+ $props.series,
102
+ $state.chartLeft,
103
+ $state.chartBottom,
104
+ $state.itemWidth,
105
+ $state.valueHeight
106
+ );
107
+ },
108
+ });
109
+
110
+ $mount(() => {
111
+ // HACK: Wait until the measurer is really available
112
+ setTimeout(() => {
113
+ const bbox = measurer.getBBox();
114
+ $state.textWidth = bbox.width;
115
+ $state.textHeight = bbox.height * 1.5;
116
+ measurer.remove();
117
+ }, 1);
118
+ });
119
+
120
+ function calculateMaxValue(series: DataSeries[], field: "x" | "y") {
121
+ // HACK: Yeah, nested reduces
122
+ return series.reduce((a, b) => {
123
+ return Math.max(
124
+ a,
125
+ b.data.reduce((c, d) => Math.max(c, field === "x" ? d.x : d.y), 0)
126
+ );
127
+ }, 0);
128
+ }
129
+
130
+ function buildPointSeries(
131
+ theSeries: DataSeries[],
132
+ theChartLeft: number,
133
+ theChartBottom: number,
134
+ theItemWidth: number,
135
+ theValueHeight: number
136
+ ) {
137
+ return theSeries.map((series) => {
138
+ return series.data.map((item) => {
139
+ const x = +(theChartLeft + item.x * theItemWidth).toFixed(2);
140
+ const y = +(theChartBottom - item.y * theValueHeight).toFixed(2);
141
+ return { x, y };
142
+ });
143
+ });
144
+ }
145
+
146
+ @render {
147
+ <div
148
+ {$props?.id}
149
+ class={["torp-scatter-chart", $props?.class]}
150
+ {$props?.style}
151
+ &ref={container}
152
+ >
153
+ <svg version="1.1" width={$state.calculatedWidth} height={$state.calculatedHeight}>
154
+ <g>
155
+ <text &ref={measurer} x="-999" y="-999">8</text>
156
+
157
+ @if ($state.textWidth && $state.textHeight) {
158
+ <XAxis
159
+ showAxis={!$props.hideXAxis}
160
+ height={$state.calculatedHeight}
161
+ width={$state.calculatedWidth}
162
+ {$state.itemWidth}
163
+ {$state.textHeight}
164
+ xLabel={$props.xLabel ?? ""}
165
+ labels={[]}
166
+ stepLabels={$state.xStepLabels}
167
+ stepValue={$state.calculatedXStepValue}
168
+ {$state.chartLeft}
169
+ {$state.chartBottom}
170
+ />
171
+ <YAxis
172
+ showAxis={!$props.hideYAxis}
173
+ {$state.valueHeight}
174
+ {$state.textHeight}
175
+ yLabel={$props.yLabel ?? ""}
176
+ stepLabels={$state.yStepLabels}
177
+ stepValue={$state.calculatedYStepValue}
178
+ {$state.chartLeft}
179
+ {$state.chartBottom}
180
+ />
181
+ <GridLines
182
+ {$props.showHLines}
183
+ {$props.showVLines}
184
+ width={$state.calculatedWidth}
185
+ labels={[]}
186
+ xStepValue={$state.calculatedXStepValue}
187
+ {$state.xStepLabels}
188
+ yStepValue={$state.calculatedYStepValue}
189
+ {$state.yStepLabels}
190
+ {$state.itemWidth}
191
+ {$state.valueHeight}
192
+ {$state.chartLeft}
193
+ {$state.chartBottom}
194
+ type="line"
195
+ />
196
+ @for (let [i, series] of $props.series.entries()) {
197
+ @for (let [j, point] of $state.pointSeries[i].entries()) {
198
+ <circle
199
+ cx={point.x}
200
+ cy={point.y}
201
+ r="4"
202
+ fill={series.color || chartColors[i % chartColors.length]}
203
+ >
204
+ <title>{
205
+ `${[series.name, series.data[j].label, series.data[j].x].filter(Boolean).join("\n")}, ${series.data[j].y}`
206
+ }</title>
207
+ </circle>
208
+ }
209
+ }
210
+ }
211
+ </g>
212
+ </svg>
213
+ </div>
214
+ }
215
+ }
@@ -0,0 +1,71 @@
1
+ import type { ClassValue, StyleValue } from "@torpor/view";
2
+ import { calculateMaxValue } from "../utils/chartUtils";
3
+ import { chartColors } from "../utils/chartColors";
4
+
5
+ interface SparkLineProps {
6
+ /** An ID for the root element */
7
+ id?: string;
8
+ /** Classes for the root element */
9
+ class?: ClassValue,
10
+ /** Styles for the root element */
11
+ style?: StyleValue,
12
+ data: number[];
13
+ color?: string,
14
+ height?: number,
15
+ width?: number,
16
+ }
17
+
18
+ export default function SparkLine($props: SparkLineProps) {
19
+ const defaultHeight = 20;
20
+ const defaultWidth = 100;
21
+
22
+ let container: HTMLElement;
23
+
24
+ let $state = $watch({
25
+ get calculatedHeight() {
26
+ return $props.height || defaultHeight;
27
+ },
28
+ get calculatedWidth() {
29
+ return $props.width || container?.clientWidth || defaultWidth;
30
+ },
31
+ get calculatedSeries() {
32
+ return [{ name: "", color: $props.color ?? "", data: $props.data }];
33
+ },
34
+ get maxValue() {
35
+ return calculateMaxValue($state.calculatedSeries);
36
+ },
37
+ get itemWidth() {
38
+ return $state.calculatedWidth / $props.data.length;
39
+ },
40
+ get valueHeight() {
41
+ return ($state.calculatedHeight - 4) / $state.maxValue;
42
+ },
43
+ get pointSeries() {
44
+ return $props.data.map((value, i) => {
45
+ const x = +(i * $state.itemWidth).toFixed(2);
46
+ const y = +($state.calculatedHeight - 2 - value * $state.valueHeight).toFixed(2);
47
+ return { x, y };
48
+ });
49
+ }
50
+ });
51
+
52
+ @render {
53
+ <div
54
+ {$props?.id}
55
+ class={["torp-spark-line", $props?.class]}
56
+ {$props?.style}
57
+ &ref={container}
58
+ >
59
+ <svg version="1.1" width={$state.calculatedWidth} height={$state.calculatedHeight}>
60
+ <g>
61
+ <polyline
62
+ points={$state.pointSeries.map((p) => `${p.x},${p.y}`).join(" ")}
63
+ stroke={$props.color || chartColors[0]}
64
+ stroke-width="2"
65
+ fill="none"
66
+ />
67
+ </g>
68
+ </svg>
69
+ </div>
70
+ }
71
+ }
@@ -0,0 +1,60 @@
1
+ interface XAxisProps {
2
+ showAxis: boolean,
3
+ height: number,
4
+ width: number,
5
+ itemWidth: number,
6
+ textHeight: number,
7
+ xLabel: string,
8
+ labels: string[] ,
9
+ stepLabels:number[] ,
10
+ stepValue: number,
11
+ chartLeft: number,
12
+ chartBottom: number,
13
+ }
14
+
15
+ export default function XAxis($props: XAxisProps) {
16
+ let $state = $watch({
17
+ chartLabelBottom: $props.xLabel ? $props.height - $props.textHeight : $props.height,
18
+ });
19
+
20
+ @render {
21
+ <svg>
22
+ @if ($props.showAxis) {
23
+ <line x1={$props.chartLeft} y1={$props.chartBottom} x2={$props.width} y2={$props.chartBottom} />
24
+ }
25
+ @if ($props.xLabel) {
26
+ <text
27
+ text-anchor="middle"
28
+ dominant-baseline="text-after-edge"
29
+ x={$props.width - ($props.width - $props.chartLeft) / 2}
30
+ y={$props.height}
31
+ >
32
+ {$props.xLabel}
33
+ </text>
34
+ }
35
+ @if ($props.stepLabels.length && $props.stepValue) {
36
+ @for (let [i, label] of $props.stepLabels.entries()) {
37
+ <text
38
+ text-anchor="middle"
39
+ dominant-baseline="text-after-edge"
40
+ x={$props.chartLeft + i * $props.stepValue * $props.itemWidth}
41
+ y={$state.chartLabelBottom}
42
+ >
43
+ {label}
44
+ </text>
45
+ }
46
+ } else {
47
+ @for (let [i, label] of $props.labels.entries()) {
48
+ <text
49
+ text-anchor="middle"
50
+ dominant-baseline="text-after-edge"
51
+ x={$props.chartLeft + $props.itemWidth * i + $props.itemWidth / 2}
52
+ y={$state.chartLabelBottom}
53
+ >
54
+ {label}
55
+ </text>
56
+ }
57
+ }
58
+ </svg>
59
+ }
60
+ }
@@ -0,0 +1,41 @@
1
+ interface YAxisProps {
2
+ showAxis: boolean,
3
+ valueHeight: number,
4
+ textHeight: number,
5
+ yLabel: string,
6
+ stepLabels: number[],
7
+ stepValue: number,
8
+ chartLeft: number,
9
+ chartBottom: number,
10
+ }
11
+
12
+ export default function YAxis($props: YAxisProps) {
13
+ @render {
14
+ <svg>
15
+ @if ($props.showAxis) {
16
+ <line x1={$props.chartLeft} y1={$props.chartBottom} x2={$props.chartLeft} y2={0} />
17
+ }
18
+ @if ($props.yLabel) {
19
+ <text
20
+ text-anchor="middle"
21
+ dominant-baseline="hanging"
22
+ x={0}
23
+ y={$props.chartBottom / 2}
24
+ transform={`rotate(-90, ${0}, ${$props.chartBottom / 2})`}
25
+ >
26
+ {$props.yLabel}
27
+ </text>
28
+ }
29
+ @for (let [i, label] of $props.stepLabels.entries()) {
30
+ <text
31
+ text-anchor="end"
32
+ dominant-baseline="middle"
33
+ x={$props.chartLeft - $props.textHeight / 4}
34
+ y={$props.chartBottom - i * $props.stepValue * $props.valueHeight}
35
+ >
36
+ {label}
37
+ </text>
38
+ }
39
+ </svg>
40
+ }
41
+ }
@@ -0,0 +1,220 @@
1
+ import { type SlotRender } from "@torpor/view";
2
+
3
+
4
+ interface BarChartProps {
5
+ /** An ID for the root element */
6
+ id?: string;
7
+ /** Classes for the root element */
8
+ class?: ClassValue,
9
+ /** Styles for the root element */
10
+ style?: StyleValue,
11
+ series: DataSeries[],
12
+ height?: number,
13
+ width?: number,
14
+ xLabel?: string,
15
+ yLabel?: string,
16
+ maxValue?: number,
17
+ stepCount?: number,
18
+ stepValue?: number,
19
+ hideXAxis?: boolean,
20
+ hideYAxis?: boolean,
21
+ showHLines?: boolean,
22
+ showVLines?: boolean,
23
+ }
24
+
25
+ declare function BarChart(
26
+ $parent: ParentNode,
27
+ $anchor: Node | null,
28
+ $props: BarChartProps,
29
+ $context?: Record<PropertyKey, any>,
30
+ $slots?: Record<string, SlotRender>,
31
+ ): void;
32
+
33
+
34
+
35
+ interface GridLinesProps {
36
+ /** An ID for the root element */
37
+ id?: string;
38
+ /** Classes for the root element */
39
+ class?: ClassValue,
40
+ /** Styles for the root element */
41
+ style?: StyleValue,
42
+ showHLines?: boolean,
43
+ showVLines?: boolean,
44
+ width: number;
45
+ labels: string[];
46
+ xStepValue: number;
47
+ xStepLabels: number[];
48
+ yStepValue: number;
49
+ yStepLabels: number[];
50
+ itemWidth: number;
51
+ valueHeight: number;
52
+ chartLeft: number;
53
+ chartBottom: number;
54
+ type: string;
55
+ }
56
+
57
+ declare function GridLines(
58
+ $parent: ParentNode,
59
+ $anchor: Node | null,
60
+ $props: GridLinesProps,
61
+ $context?: Record<PropertyKey, any>,
62
+ $slots?: Record<string, SlotRender>,
63
+ ): void;
64
+
65
+
66
+
67
+ interface LineChartProps {
68
+ /** An ID for the root element */
69
+ id?: string;
70
+ /** Classes for the root element */
71
+ class?: ClassValue,
72
+ /** Styles for the root element */
73
+ style?: StyleValue,
74
+ series: DataSeries[],
75
+ height?: number,
76
+ width?: number,
77
+ xLabel?: string,
78
+ yLabel?: string,
79
+ maxValue?: number,
80
+ stepCount?: number,
81
+ stepValue?: number,
82
+ showPoints?: boolean,
83
+ hideXAxis?: boolean,
84
+ hideYAxis?: boolean,
85
+ showHLines?: boolean,
86
+ showVLines?: boolean,
87
+ }
88
+
89
+ declare function LineChart(
90
+ $parent: ParentNode,
91
+ $anchor: Node | null,
92
+ $props: LineChartProps,
93
+ $context?: Record<PropertyKey, any>,
94
+ $slots?: Record<string, SlotRender>,
95
+ ): void;
96
+
97
+
98
+
99
+ interface PieChartProps {
100
+ /** An ID for the root element */
101
+ id?: string;
102
+ /** Classes for the root element */
103
+ class?: ClassValue,
104
+ /** Styles for the root element */
105
+ style?: StyleValue,
106
+ data: PieData[],
107
+ size?: number,
108
+ radius?: number,
109
+ cutout?: number,
110
+ }
111
+
112
+ declare function PieChart(
113
+ $parent: ParentNode,
114
+ $anchor: Node | null,
115
+ $props: PieChartProps,
116
+ $context?: Record<PropertyKey, any>,
117
+ $slots?: Record<string, SlotRender>,
118
+ ): void;
119
+
120
+
121
+
122
+ interface ScatterChartProps {
123
+ /** An ID for the root element */
124
+ id?: string;
125
+ /** Classes for the root element */
126
+ class?: ClassValue,
127
+ /** Styles for the root element */
128
+ style?: StyleValue,
129
+ series: DataSeries[],
130
+ height?: number,
131
+ width?: number,
132
+ xLabel?: string,
133
+ yLabel?: string,
134
+ xStepCount?: number,
135
+ xStepValue?: number,
136
+ yStepCount?: number,
137
+ yStepValue?: number,
138
+ hideXAxis?: boolean,
139
+ hideYAxis?: boolean,
140
+ showHLines?: boolean,
141
+ showVLines?: boolean,
142
+ }
143
+
144
+ declare function ScatterChart(
145
+ $parent: ParentNode,
146
+ $anchor: Node | null,
147
+ $props: ScatterChartProps,
148
+ $context?: Record<PropertyKey, any>,
149
+ $slots?: Record<string, SlotRender>,
150
+ ): void;
151
+
152
+
153
+
154
+ interface SparkLineProps {
155
+ /** An ID for the root element */
156
+ id?: string;
157
+ /** Classes for the root element */
158
+ class?: ClassValue,
159
+ /** Styles for the root element */
160
+ style?: StyleValue,
161
+ data: number[];
162
+ color?: string,
163
+ height?: number,
164
+ width?: number,
165
+ }
166
+
167
+ declare function SparkLine(
168
+ $parent: ParentNode,
169
+ $anchor: Node | null,
170
+ $props: SparkLineProps,
171
+ $context?: Record<PropertyKey, any>,
172
+ $slots?: Record<string, SlotRender>,
173
+ ): void;
174
+
175
+
176
+
177
+ interface XAxisProps {
178
+ showAxis: boolean,
179
+ height: number,
180
+ width: number,
181
+ itemWidth: number,
182
+ textHeight: number,
183
+ xLabel: string,
184
+ labels: string[] ,
185
+ stepLabels:number[] ,
186
+ stepValue: number,
187
+ chartLeft: number,
188
+ chartBottom: number,
189
+ }
190
+
191
+ declare function XAxis(
192
+ $parent: ParentNode,
193
+ $anchor: Node | null,
194
+ $props: XAxisProps,
195
+ $context?: Record<PropertyKey, any>,
196
+ $slots?: Record<string, SlotRender>,
197
+ ): void;
198
+
199
+
200
+
201
+ interface YAxisProps {
202
+ showAxis: boolean,
203
+ valueHeight: number,
204
+ textHeight: number,
205
+ yLabel: string,
206
+ stepLabels: number[],
207
+ stepValue: number,
208
+ chartLeft: number,
209
+ chartBottom: number,
210
+ }
211
+
212
+ declare function YAxis(
213
+ $parent: ParentNode,
214
+ $anchor: Node | null,
215
+ $props: YAxisProps,
216
+ $context?: Record<PropertyKey, any>,
217
+ $slots?: Record<string, SlotRender>,
218
+ ): void;
219
+
220
+ export { BarChart, GridLines, LineChart, PieChart, ScatterChart, SparkLine, XAxis, YAxis };
@@ -0,0 +1,34 @@
1
+ // src/Chart/BarChart.torp
2
+ import BarChart_default from "./BarChart.torp";
3
+
4
+ // src/Chart/GridLines.torp
5
+ import GridLines_default from "./GridLines.torp";
6
+
7
+ // src/Chart/LineChart.torp
8
+ import LineChart_default from "./LineChart.torp";
9
+
10
+ // src/Chart/PieChart.torp
11
+ import PieChart_default from "./PieChart.torp";
12
+
13
+ // src/Chart/ScatterChart.torp
14
+ import ScatterChart_default from "./ScatterChart.torp";
15
+
16
+ // src/Chart/SparkLine.torp
17
+ import SparkLine_default from "./SparkLine.torp";
18
+
19
+ // src/Chart/XAxis.torp
20
+ import XAxis_default from "./XAxis.torp";
21
+
22
+ // src/Chart/YAxis.torp
23
+ import YAxis_default from "./YAxis.torp";
24
+ export {
25
+ BarChart_default as BarChart,
26
+ GridLines_default as GridLines,
27
+ LineChart_default as LineChart,
28
+ PieChart_default as PieChart,
29
+ ScatterChart_default as ScatterChart,
30
+ SparkLine_default as SparkLine,
31
+ XAxis_default as XAxis,
32
+ YAxis_default as YAxis
33
+ };
34
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -19,6 +19,8 @@ interface TextAreaProps {
19
19
  placeholder?: string,
20
20
  /** Whether the input should be readonly */
21
21
  readonly?: boolean,
22
+ /** Whether the input's value is required */
23
+ required?: boolean,
22
24
  /** The maximum number of characters allowed for the input value */
23
25
  maxlength?: number,
24
26
  /** An ARIA label for describing the input to screen readers when there is no associated label element */
@@ -94,6 +96,7 @@ export default function TextArea($props: TextAreaProps | undefined) {
94
96
  {$props?.rows}
95
97
  {$props?.placeholder}
96
98
  {$props?.readonly}
99
+ {$props?.required}
97
100
  {$props?.maxlength}
98
101
  aria-label={$props?.ariaLabel}
99
102
  data-valid={$state.valid ? "valid" : "invalid"}
@@ -322,6 +322,8 @@ interface TextAreaProps {
322
322
  placeholder?: string,
323
323
  /** Whether the input should be readonly */
324
324
  readonly?: boolean,
325
+ /** Whether the input's value is required */
326
+ required?: boolean,
325
327
  /** The maximum number of characters allowed for the input value */
326
328
  maxlength?: number,
327
329
  /** An ARIA label for describing the input to screen readers when there is no associated label element */