tgui-core 1.0.2 → 1.0.3

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 (158) hide show
  1. package/{src/components → components}/AnimatedNumber.tsx +185 -185
  2. package/{src/components → components}/BlockQuote.tsx +15 -15
  3. package/{src/components → components}/BodyZoneSelector.tsx +149 -149
  4. package/{src/components → components}/Box.tsx +255 -255
  5. package/{src/components → components}/Button.tsx +415 -415
  6. package/{src/components → components}/ByondUi.jsx +121 -121
  7. package/{src/components → components}/Chart.tsx +160 -160
  8. package/{src/components → components}/ColorBox.tsx +30 -30
  9. package/{src/components → components}/Dimmer.tsx +19 -19
  10. package/{src/components → components}/Divider.tsx +26 -26
  11. package/{src/components → components}/DmIcon.tsx +72 -72
  12. package/{src/components → components}/DraggableControl.jsx +282 -282
  13. package/{src/components → components}/Dropdown.tsx +246 -246
  14. package/{src/components → components}/Flex.tsx +105 -105
  15. package/{src/components → components}/Icon.tsx +91 -91
  16. package/{src/components → components}/Input.tsx +181 -181
  17. package/{src/components → components}/KeyListener.tsx +40 -40
  18. package/{src/components → components}/Knob.tsx +185 -185
  19. package/{src/components → components}/LabeledList.tsx +130 -130
  20. package/{src/components → components}/MenuBar.tsx +233 -238
  21. package/{src/components → components}/Modal.tsx +25 -25
  22. package/{src/components → components}/NoticeBox.tsx +48 -48
  23. package/{src/components → components}/NumberInput.tsx +328 -328
  24. package/{src/components → components}/ProgressBar.tsx +79 -79
  25. package/{src/components → components}/RestrictedInput.jsx +301 -301
  26. package/{src/components → components}/RoundGauge.tsx +189 -189
  27. package/{src/components → components}/Section.tsx +125 -125
  28. package/{src/components → components}/Slider.tsx +173 -173
  29. package/{src/components → components}/Stack.tsx +101 -101
  30. package/{src/components → components}/Table.tsx +90 -90
  31. package/{src/components → components}/Tabs.tsx +90 -90
  32. package/{src/components → components}/TextArea.tsx +198 -198
  33. package/{src/components → components}/TimeDisplay.jsx +64 -64
  34. package/components/index.ts +51 -0
  35. package/{src/debug/KitchenSink.jsx → debug/KitchenSink.tsx} +56 -56
  36. package/{src/debug/actions.js → debug/actions.ts} +11 -11
  37. package/{src/debug/hooks.js → debug/hooks.ts} +10 -10
  38. package/{src/debug/middleware.js → debug/middleware.ts} +86 -86
  39. package/{src/debug/reducer.js → debug/reducer.ts} +27 -22
  40. package/{src/debug/selectors.js → debug/selectors.ts} +7 -7
  41. package/{src/layouts → layouts}/Layout.tsx +75 -75
  42. package/{src/layouts → layouts}/NtosWindow.tsx +162 -162
  43. package/{src/layouts → layouts}/Pane.tsx +56 -56
  44. package/{src/layouts → layouts}/Window.tsx +227 -227
  45. package/layouts/index.ts +10 -0
  46. package/package.json +3 -2
  47. package/src/assets.ts +43 -43
  48. package/src/backend.ts +369 -369
  49. package/src/drag.ts +280 -280
  50. package/src/events.ts +237 -237
  51. package/src/hotkeys.ts +212 -212
  52. package/src/renderer.ts +50 -50
  53. package/stories/Blink.stories.tsx +20 -0
  54. package/stories/BlockQuote.stories.tsx +23 -0
  55. package/stories/Box.stories.tsx +27 -0
  56. package/stories/Button.stories.tsx +68 -0
  57. package/stories/ByondUi.stories.tsx +45 -0
  58. package/stories/Collapsible.stories.tsx +23 -0
  59. package/stories/Flex.stories.tsx +68 -0
  60. package/stories/Input.stories.tsx +124 -0
  61. package/stories/LabeledList.stories.tsx +73 -0
  62. package/stories/Popper.stories.tsx +58 -0
  63. package/stories/ProgressBar.stories.tsx +58 -0
  64. package/stories/Stack.stories.tsx +55 -0
  65. package/stories/Storage.stories.tsx +46 -0
  66. package/stories/Themes.stories.tsx +30 -0
  67. package/stories/Tooltip.stories.tsx +48 -0
  68. package/stories/common.tsx +19 -0
  69. package/tsconfig.json +0 -21
  70. package/src/components/Grid.tsx +0 -44
  71. /package/{src/common → common}/collections.ts +0 -0
  72. /package/{src/common → common}/color.ts +0 -0
  73. /package/{src/common → common}/events.ts +0 -0
  74. /package/{src/common → common}/exhaustive.ts +0 -0
  75. /package/{src/common → common}/fp.ts +0 -0
  76. /package/{src/common → common}/keycodes.ts +0 -0
  77. /package/{src/common → common}/keys.ts +0 -0
  78. /package/{src/common → common}/math.ts +0 -0
  79. /package/{src/common → common}/perf.ts +0 -0
  80. /package/{src/common → common}/random.ts +0 -0
  81. /package/{src/common → common}/react.ts +0 -0
  82. /package/{src/common → common}/redux.ts +0 -0
  83. /package/{src/common → common}/storage.js +0 -0
  84. /package/{src/common → common}/string.ts +0 -0
  85. /package/{src/common → common}/timer.ts +0 -0
  86. /package/{src/common → common}/type-utils.ts +0 -0
  87. /package/{src/common → common}/types.ts +0 -0
  88. /package/{src/common → common}/uuid.ts +0 -0
  89. /package/{src/common → common}/vector.ts +0 -0
  90. /package/{src/components → components}/Autofocus.tsx +0 -0
  91. /package/{src/components → components}/Blink.jsx +0 -0
  92. /package/{src/components → components}/Collapsible.tsx +0 -0
  93. /package/{src/components → components}/Dialog.tsx +0 -0
  94. /package/{src/components → components}/FakeTerminal.jsx +0 -0
  95. /package/{src/components → components}/FitText.tsx +0 -0
  96. /package/{src/components → components}/Image.tsx +0 -0
  97. /package/{src/components → components}/InfinitePlane.jsx +0 -0
  98. /package/{src/components → components}/LabeledControls.tsx +0 -0
  99. /package/{src/components → components}/Popper.tsx +0 -0
  100. /package/{src/components → components}/StyleableSection.tsx +0 -0
  101. /package/{src/components → components}/Tooltip.tsx +0 -0
  102. /package/{src/components → components}/TrackOutsideClicks.tsx +0 -0
  103. /package/{src/components → components}/VirtualList.tsx +0 -0
  104. /package/{src/debug → debug}/index.ts +0 -0
  105. /package/{src/styles → styles}/base.scss +0 -0
  106. /package/{src/styles → styles}/colors.scss +0 -0
  107. /package/{src/styles → styles}/components/BlockQuote.scss +0 -0
  108. /package/{src/styles → styles}/components/Button.scss +0 -0
  109. /package/{src/styles → styles}/components/ColorBox.scss +0 -0
  110. /package/{src/styles → styles}/components/Dialog.scss +0 -0
  111. /package/{src/styles → styles}/components/Dimmer.scss +0 -0
  112. /package/{src/styles → styles}/components/Divider.scss +0 -0
  113. /package/{src/styles → styles}/components/Dropdown.scss +0 -0
  114. /package/{src/styles → styles}/components/Flex.scss +0 -0
  115. /package/{src/styles → styles}/components/Icon.scss +0 -0
  116. /package/{src/styles → styles}/components/Input.scss +0 -0
  117. /package/{src/styles → styles}/components/Knob.scss +0 -0
  118. /package/{src/styles → styles}/components/LabeledList.scss +0 -0
  119. /package/{src/styles → styles}/components/MenuBar.scss +0 -0
  120. /package/{src/styles → styles}/components/Modal.scss +0 -0
  121. /package/{src/styles → styles}/components/NoticeBox.scss +0 -0
  122. /package/{src/styles → styles}/components/NumberInput.scss +0 -0
  123. /package/{src/styles → styles}/components/ProgressBar.scss +0 -0
  124. /package/{src/styles → styles}/components/RoundGauge.scss +0 -0
  125. /package/{src/styles → styles}/components/Section.scss +0 -0
  126. /package/{src/styles → styles}/components/Slider.scss +0 -0
  127. /package/{src/styles → styles}/components/Stack.scss +0 -0
  128. /package/{src/styles → styles}/components/Table.scss +0 -0
  129. /package/{src/styles → styles}/components/Tabs.scss +0 -0
  130. /package/{src/styles → styles}/components/TextArea.scss +0 -0
  131. /package/{src/styles → styles}/components/Tooltip.scss +0 -0
  132. /package/{src/styles → styles}/functions.scss +0 -0
  133. /package/{src/styles → styles}/layouts/Layout.scss +0 -0
  134. /package/{src/styles → styles}/layouts/NtosHeader.scss +0 -0
  135. /package/{src/styles → styles}/layouts/NtosWindow.scss +0 -0
  136. /package/{src/styles → styles}/layouts/TitleBar.scss +0 -0
  137. /package/{src/styles → styles}/layouts/Window.scss +0 -0
  138. /package/{src/styles → styles}/main.scss +0 -0
  139. /package/{src/styles → styles}/reset.scss +0 -0
  140. /package/{src/styles → styles}/themes/abductor.scss +0 -0
  141. /package/{src/styles → styles}/themes/admin.scss +0 -0
  142. /package/{src/styles → styles}/themes/cardtable.scss +0 -0
  143. /package/{src/styles → styles}/themes/hackerman.scss +0 -0
  144. /package/{src/styles → styles}/themes/malfunction.scss +0 -0
  145. /package/{src/styles → styles}/themes/neutral.scss +0 -0
  146. /package/{src/styles → styles}/themes/ntOS95.scss +0 -0
  147. /package/{src/styles → styles}/themes/ntos.scss +0 -0
  148. /package/{src/styles → styles}/themes/ntos_cat.scss +0 -0
  149. /package/{src/styles → styles}/themes/ntos_darkmode.scss +0 -0
  150. /package/{src/styles → styles}/themes/ntos_lightmode.scss +0 -0
  151. /package/{src/styles → styles}/themes/ntos_spooky.scss +0 -0
  152. /package/{src/styles → styles}/themes/ntos_synth.scss +0 -0
  153. /package/{src/styles → styles}/themes/ntos_terminal.scss +0 -0
  154. /package/{src/styles → styles}/themes/paper.scss +0 -0
  155. /package/{src/styles → styles}/themes/retro.scss +0 -0
  156. /package/{src/styles → styles}/themes/spookyconsole.scss +0 -0
  157. /package/{src/styles → styles}/themes/syndicate.scss +0 -0
  158. /package/{src/styles → styles}/themes/wizard.scss +0 -0
@@ -1,121 +1,121 @@
1
- /**
2
- * @file
3
- * @copyright 2020 Aleksej Komarov
4
- * @license MIT
5
- */
6
-
7
- import { shallowDiffers } from '../common/react';
8
- import { debounce } from '../common/timer';
9
- import { Component, createRef } from 'react';
10
-
11
- import { computeBoxProps } from './Box';
12
-
13
- const logger = createLogger('ByondUi');
14
-
15
- // Stack of currently allocated BYOND UI element ids.
16
- const byondUiStack = [];
17
-
18
- const createByondUiElement = (elementId) => {
19
- // Reserve an index in the stack
20
- const index = byondUiStack.length;
21
- byondUiStack.push(null);
22
- // Get a unique id
23
- const id = elementId || 'byondui_' + index;
24
- // Return a control structure
25
- return {
26
- render: (params) => {
27
- logger.log(`rendering '${id}'`);
28
- byondUiStack[index] = id;
29
- Byond.winset(id, params);
30
- },
31
- unmount: () => {
32
- logger.log(`unmounting '${id}'`);
33
- byondUiStack[index] = null;
34
- Byond.winset(id, {
35
- parent: '',
36
- });
37
- },
38
- };
39
- };
40
-
41
- window.addEventListener('beforeunload', () => {
42
- // Cleanly unmount all visible UI elements
43
- for (let index = 0; index < byondUiStack.length; index++) {
44
- const id = byondUiStack[index];
45
- if (typeof id === 'string') {
46
- logger.log(`unmounting '${id}' (beforeunload)`);
47
- byondUiStack[index] = null;
48
- Byond.winset(id, {
49
- parent: '',
50
- });
51
- }
52
- }
53
- });
54
-
55
- /**
56
- * Get the bounding box of the DOM element in display-pixels.
57
- */
58
- const getBoundingBox = (element) => {
59
- const pixelRatio = window.devicePixelRatio ?? 1;
60
- const rect = element.getBoundingClientRect();
61
-
62
- return {
63
- pos: [rect.left * pixelRatio, rect.top * pixelRatio],
64
- size: [
65
- (rect.right - rect.left) * pixelRatio,
66
- (rect.bottom - rect.top) * pixelRatio,
67
- ],
68
- };
69
- };
70
-
71
- export class ByondUi extends Component {
72
- constructor(props) {
73
- super(props);
74
- this.containerRef = createRef();
75
- this.byondUiElement = createByondUiElement(props.params?.id);
76
- this.handleResize = debounce(() => {
77
- this.forceUpdate();
78
- }, 100);
79
- }
80
-
81
- shouldComponentUpdate(nextProps) {
82
- const { params: prevParams = {}, ...prevRest } = this.props;
83
- const { params: nextParams = {}, ...nextRest } = nextProps;
84
- return (
85
- shallowDiffers(prevParams, nextParams) ||
86
- shallowDiffers(prevRest, nextRest)
87
- );
88
- }
89
-
90
- componentDidMount() {
91
- window.addEventListener('resize', this.handleResize);
92
- this.componentDidUpdate();
93
- this.handleResize();
94
- }
95
-
96
- componentDidUpdate() {
97
- const { params = {} } = this.props;
98
- const box = getBoundingBox(this.containerRef.current);
99
- this.byondUiElement.render({
100
- parent: Byond.windowId,
101
- ...params,
102
- pos: box.pos[0] + ',' + box.pos[1],
103
- size: box.size[0] + 'x' + box.size[1],
104
- });
105
- }
106
-
107
- componentWillUnmount() {
108
- window.removeEventListener('resize', this.handleResize);
109
- this.byondUiElement.unmount();
110
- }
111
-
112
- render() {
113
- const { params, ...rest } = this.props;
114
- return (
115
- <div ref={this.containerRef} {...computeBoxProps(rest)}>
116
- {/* Filler */}
117
- <div style={{ minHeight: '22px' }} />
118
- </div>
119
- );
120
- }
121
- }
1
+ /**
2
+ * @file
3
+ * @copyright 2020 Aleksej Komarov
4
+ * @license MIT
5
+ */
6
+
7
+ import { shallowDiffers } from '../common/react';
8
+ import { debounce } from '../common/timer';
9
+ import { Component, createRef } from 'react';
10
+
11
+ import { computeBoxProps } from './Box';
12
+
13
+ const logger = createLogger('ByondUi');
14
+
15
+ // Stack of currently allocated BYOND UI element ids.
16
+ const byondUiStack = [];
17
+
18
+ const createByondUiElement = (elementId) => {
19
+ // Reserve an index in the stack
20
+ const index = byondUiStack.length;
21
+ byondUiStack.push(null);
22
+ // Get a unique id
23
+ const id = elementId || 'byondui_' + index;
24
+ // Return a control structure
25
+ return {
26
+ render: (params) => {
27
+ logger.log(`rendering '${id}'`);
28
+ byondUiStack[index] = id;
29
+ Byond.winset(id, params);
30
+ },
31
+ unmount: () => {
32
+ logger.log(`unmounting '${id}'`);
33
+ byondUiStack[index] = null;
34
+ Byond.winset(id, {
35
+ parent: '',
36
+ });
37
+ },
38
+ };
39
+ };
40
+
41
+ window.addEventListener('beforeunload', () => {
42
+ // Cleanly unmount all visible UI elements
43
+ for (let index = 0; index < byondUiStack.length; index++) {
44
+ const id = byondUiStack[index];
45
+ if (typeof id === 'string') {
46
+ logger.log(`unmounting '${id}' (beforeunload)`);
47
+ byondUiStack[index] = null;
48
+ Byond.winset(id, {
49
+ parent: '',
50
+ });
51
+ }
52
+ }
53
+ });
54
+
55
+ /**
56
+ * Get the bounding box of the DOM element in display-pixels.
57
+ */
58
+ const getBoundingBox = (element) => {
59
+ const pixelRatio = window.devicePixelRatio ?? 1;
60
+ const rect = element.getBoundingClientRect();
61
+
62
+ return {
63
+ pos: [rect.left * pixelRatio, rect.top * pixelRatio],
64
+ size: [
65
+ (rect.right - rect.left) * pixelRatio,
66
+ (rect.bottom - rect.top) * pixelRatio,
67
+ ],
68
+ };
69
+ };
70
+
71
+ export class ByondUi extends Component {
72
+ constructor(props) {
73
+ super(props);
74
+ this.containerRef = createRef();
75
+ this.byondUiElement = createByondUiElement(props.params?.id);
76
+ this.handleResize = debounce(() => {
77
+ this.forceUpdate();
78
+ }, 100);
79
+ }
80
+
81
+ shouldComponentUpdate(nextProps) {
82
+ const { params: prevParams = {}, ...prevRest } = this.props;
83
+ const { params: nextParams = {}, ...nextRest } = nextProps;
84
+ return (
85
+ shallowDiffers(prevParams, nextParams) ||
86
+ shallowDiffers(prevRest, nextRest)
87
+ );
88
+ }
89
+
90
+ componentDidMount() {
91
+ window.addEventListener('resize', this.handleResize);
92
+ this.componentDidUpdate();
93
+ this.handleResize();
94
+ }
95
+
96
+ componentDidUpdate() {
97
+ const { params = {} } = this.props;
98
+ const box = getBoundingBox(this.containerRef.current);
99
+ this.byondUiElement.render({
100
+ parent: Byond.windowId,
101
+ ...params,
102
+ pos: box.pos[0] + ',' + box.pos[1],
103
+ size: box.size[0] + 'x' + box.size[1],
104
+ });
105
+ }
106
+
107
+ componentWillUnmount() {
108
+ window.removeEventListener('resize', this.handleResize);
109
+ this.byondUiElement.unmount();
110
+ }
111
+
112
+ render() {
113
+ const { params, ...rest } = this.props;
114
+ return (
115
+ <div ref={this.containerRef} {...computeBoxProps(rest)}>
116
+ {/* Filler */}
117
+ <div style={{ minHeight: '22px' }} />
118
+ </div>
119
+ );
120
+ }
121
+ }
@@ -1,160 +1,160 @@
1
- /**
2
- * @file
3
- * @copyright 2020 Aleksej Komarov
4
- * @license MIT
5
- */
6
-
7
- import { map, zip } from '../common/collections';
8
- import { Component, createRef, RefObject } from 'react';
9
-
10
- import { Box, BoxProps } from './Box';
11
-
12
- type Props = {
13
- data: number[][];
14
- } & Partial<{
15
- fillColor: string;
16
- rangeX: [number, number];
17
- rangeY: [number, number];
18
- strokeColor: string;
19
- strokeWidth: number;
20
- }> &
21
- BoxProps;
22
-
23
- type State = {
24
- viewBox: [number, number];
25
- };
26
-
27
- type Point = number[];
28
- type Range = [number, number];
29
-
30
- const normalizeData = (
31
- data: Point[],
32
- scale: number[],
33
- rangeX?: Range,
34
- rangeY?: Range
35
- ) => {
36
- if (data.length === 0) {
37
- return [];
38
- }
39
-
40
- const min = map(zip(...data), (p) => Math.min(...p));
41
- const max = map(zip(...data), (p) => Math.max(...p));
42
-
43
- if (rangeX !== undefined) {
44
- min[0] = rangeX[0];
45
- max[0] = rangeX[1];
46
- }
47
-
48
- if (rangeY !== undefined) {
49
- min[1] = rangeY[0];
50
- max[1] = rangeY[1];
51
- }
52
-
53
- const normalized = map(data, (point) =>
54
- map(
55
- zip(point, min, max, scale),
56
- ([value, min, max, scale]) => ((value - min) / (max - min)) * scale
57
- )
58
- );
59
-
60
- return normalized;
61
- };
62
-
63
- const dataToPolylinePoints = (data) => {
64
- let points = '';
65
- for (let i = 0; i < data.length; i++) {
66
- const point = data[i];
67
- points += point[0] + ',' + point[1] + ' ';
68
- }
69
- return points;
70
- };
71
-
72
- class LineChart extends Component<Props> {
73
- ref: RefObject<HTMLDivElement>;
74
- state: State;
75
-
76
- constructor(props: Props) {
77
- super(props);
78
- this.ref = createRef();
79
- this.state = {
80
- // Initial guess
81
- viewBox: [600, 200],
82
- };
83
- this.handleResize = this.handleResize.bind(this);
84
- }
85
-
86
- componentDidMount() {
87
- window.addEventListener('resize', this.handleResize);
88
- this.handleResize();
89
- }
90
-
91
- componentWillUnmount() {
92
- window.removeEventListener('resize', this.handleResize);
93
- }
94
-
95
- handleResize = () => {
96
- const element = this.ref.current;
97
- if (!element) {
98
- return;
99
- }
100
- this.setState({
101
- viewBox: [element.offsetWidth, element.offsetHeight],
102
- });
103
- };
104
-
105
- render() {
106
- const {
107
- data = [],
108
- rangeX,
109
- rangeY,
110
- fillColor = 'none',
111
- strokeColor = '#ffffff',
112
- strokeWidth = 2,
113
- ...rest
114
- } = this.props;
115
- const { viewBox } = this.state;
116
- const normalized = normalizeData(data, viewBox, rangeX, rangeY);
117
- // Push data outside viewBox and form a fillable polygon
118
- if (normalized.length > 0) {
119
- const first = normalized[0];
120
- const last = normalized[normalized.length - 1];
121
- normalized.push([viewBox[0] + strokeWidth, last[1]]);
122
- normalized.push([viewBox[0] + strokeWidth, -strokeWidth]);
123
- normalized.push([-strokeWidth, -strokeWidth]);
124
- normalized.push([-strokeWidth, first[1]]);
125
- }
126
- const points = dataToPolylinePoints(normalized);
127
- const divProps = { ...rest, className: '', ref: this.ref };
128
-
129
- return (
130
- <Box position="relative" {...rest}>
131
- <Box {...divProps}>
132
- <svg
133
- viewBox={`0 0 ${viewBox[0]} ${viewBox[1]}`}
134
- preserveAspectRatio="none"
135
- style={{
136
- position: 'absolute',
137
- top: 0,
138
- left: 0,
139
- right: 0,
140
- bottom: 0,
141
- overflow: 'hidden',
142
- }}
143
- >
144
- <polyline
145
- transform={`scale(1, -1) translate(0, -${viewBox[1]})`}
146
- fill={fillColor}
147
- stroke={strokeColor}
148
- strokeWidth={strokeWidth}
149
- points={points}
150
- />
151
- </svg>
152
- </Box>
153
- </Box>
154
- );
155
- }
156
- }
157
-
158
- export const Chart = {
159
- Line: LineChart,
160
- };
1
+ /**
2
+ * @file
3
+ * @copyright 2020 Aleksej Komarov
4
+ * @license MIT
5
+ */
6
+
7
+ import { map, zip } from '../common/collections';
8
+ import { Component, createRef, RefObject } from 'react';
9
+
10
+ import { Box, BoxProps } from './Box';
11
+
12
+ type Props = {
13
+ data: number[][];
14
+ } & Partial<{
15
+ fillColor: string;
16
+ rangeX: [number, number];
17
+ rangeY: [number, number];
18
+ strokeColor: string;
19
+ strokeWidth: number;
20
+ }> &
21
+ BoxProps;
22
+
23
+ type State = {
24
+ viewBox: [number, number];
25
+ };
26
+
27
+ type Point = number[];
28
+ type Range = [number, number];
29
+
30
+ const normalizeData = (
31
+ data: Point[],
32
+ scale: number[],
33
+ rangeX?: Range,
34
+ rangeY?: Range
35
+ ) => {
36
+ if (data.length === 0) {
37
+ return [];
38
+ }
39
+
40
+ const min = map(zip(...data), (p) => Math.min(...p));
41
+ const max = map(zip(...data), (p) => Math.max(...p));
42
+
43
+ if (rangeX !== undefined) {
44
+ min[0] = rangeX[0];
45
+ max[0] = rangeX[1];
46
+ }
47
+
48
+ if (rangeY !== undefined) {
49
+ min[1] = rangeY[0];
50
+ max[1] = rangeY[1];
51
+ }
52
+
53
+ const normalized = map(data, (point) =>
54
+ map(
55
+ zip(point, min, max, scale),
56
+ ([value, min, max, scale]) => ((value - min) / (max - min)) * scale
57
+ )
58
+ );
59
+
60
+ return normalized;
61
+ };
62
+
63
+ const dataToPolylinePoints = (data) => {
64
+ let points = '';
65
+ for (let i = 0; i < data.length; i++) {
66
+ const point = data[i];
67
+ points += point[0] + ',' + point[1] + ' ';
68
+ }
69
+ return points;
70
+ };
71
+
72
+ class LineChart extends Component<Props> {
73
+ ref: RefObject<HTMLDivElement>;
74
+ state: State;
75
+
76
+ constructor(props: Props) {
77
+ super(props);
78
+ this.ref = createRef();
79
+ this.state = {
80
+ // Initial guess
81
+ viewBox: [600, 200],
82
+ };
83
+ this.handleResize = this.handleResize.bind(this);
84
+ }
85
+
86
+ componentDidMount() {
87
+ window.addEventListener('resize', this.handleResize);
88
+ this.handleResize();
89
+ }
90
+
91
+ componentWillUnmount() {
92
+ window.removeEventListener('resize', this.handleResize);
93
+ }
94
+
95
+ handleResize = () => {
96
+ const element = this.ref.current;
97
+ if (!element) {
98
+ return;
99
+ }
100
+ this.setState({
101
+ viewBox: [element.offsetWidth, element.offsetHeight],
102
+ });
103
+ };
104
+
105
+ render() {
106
+ const {
107
+ data = [],
108
+ rangeX,
109
+ rangeY,
110
+ fillColor = 'none',
111
+ strokeColor = '#ffffff',
112
+ strokeWidth = 2,
113
+ ...rest
114
+ } = this.props;
115
+ const { viewBox } = this.state;
116
+ const normalized = normalizeData(data, viewBox, rangeX, rangeY);
117
+ // Push data outside viewBox and form a fillable polygon
118
+ if (normalized.length > 0) {
119
+ const first = normalized[0];
120
+ const last = normalized[normalized.length - 1];
121
+ normalized.push([viewBox[0] + strokeWidth, last[1]]);
122
+ normalized.push([viewBox[0] + strokeWidth, -strokeWidth]);
123
+ normalized.push([-strokeWidth, -strokeWidth]);
124
+ normalized.push([-strokeWidth, first[1]]);
125
+ }
126
+ const points = dataToPolylinePoints(normalized);
127
+ const divProps = { ...rest, className: '', ref: this.ref };
128
+
129
+ return (
130
+ <Box position="relative" {...rest}>
131
+ <Box {...divProps}>
132
+ <svg
133
+ viewBox={`0 0 ${viewBox[0]} ${viewBox[1]}`}
134
+ preserveAspectRatio="none"
135
+ style={{
136
+ position: 'absolute',
137
+ top: 0,
138
+ left: 0,
139
+ right: 0,
140
+ bottom: 0,
141
+ overflow: 'hidden',
142
+ }}
143
+ >
144
+ <polyline
145
+ transform={`scale(1, -1) translate(0, -${viewBox[1]})`}
146
+ fill={fillColor}
147
+ stroke={strokeColor}
148
+ strokeWidth={strokeWidth}
149
+ points={points}
150
+ />
151
+ </svg>
152
+ </Box>
153
+ </Box>
154
+ );
155
+ }
156
+ }
157
+
158
+ export const Chart = {
159
+ Line: LineChart,
160
+ };
@@ -1,30 +1,30 @@
1
- /**
2
- * @file
3
- * @copyright 2020 Aleksej Komarov
4
- * @license MIT
5
- */
6
-
7
- import { classes } from '../common/react';
8
- import { ReactNode } from 'react';
9
-
10
- import { BoxProps, computeBoxClassName, computeBoxProps } from './Box';
11
-
12
- type Props = {
13
- content?: ReactNode;
14
- } & BoxProps;
15
-
16
- export function ColorBox(props: Props) {
17
- const { content, children, className, ...rest } = props;
18
-
19
- rest.color = content ? null : 'default';
20
- rest.backgroundColor = props.color || 'default';
21
-
22
- return (
23
- <div
24
- className={classes(['ColorBox', className, computeBoxClassName(rest)])}
25
- {...computeBoxProps(rest)}
26
- >
27
- {content || '.'}
28
- </div>
29
- );
30
- }
1
+ /**
2
+ * @file
3
+ * @copyright 2020 Aleksej Komarov
4
+ * @license MIT
5
+ */
6
+
7
+ import { classes } from '../common/react';
8
+ import { ReactNode } from 'react';
9
+
10
+ import { BoxProps, computeBoxClassName, computeBoxProps } from './Box';
11
+
12
+ type Props = {
13
+ content?: ReactNode;
14
+ } & BoxProps;
15
+
16
+ export function ColorBox(props: Props) {
17
+ const { content, children, className, ...rest } = props;
18
+
19
+ rest.color = content ? null : 'default';
20
+ rest.backgroundColor = props.color || 'default';
21
+
22
+ return (
23
+ <div
24
+ className={classes(['ColorBox', className, computeBoxClassName(rest)])}
25
+ {...computeBoxProps(rest)}
26
+ >
27
+ {content || '.'}
28
+ </div>
29
+ );
30
+ }
@@ -1,19 +1,19 @@
1
- /**
2
- * @file
3
- * @copyright 2020 Aleksej Komarov
4
- * @license MIT
5
- */
6
-
7
- import { classes } from '../common/react';
8
-
9
- import { Box, BoxProps } from './Box';
10
-
11
- export function Dimmer(props: BoxProps) {
12
- const { className, children, ...rest } = props;
13
-
14
- return (
15
- <Box className={classes(['Dimmer', className])} {...rest}>
16
- <div className="Dimmer__inner">{children}</div>
17
- </Box>
18
- );
19
- }
1
+ /**
2
+ * @file
3
+ * @copyright 2020 Aleksej Komarov
4
+ * @license MIT
5
+ */
6
+
7
+ import { classes } from '../common/react';
8
+
9
+ import { Box, BoxProps } from './Box';
10
+
11
+ export function Dimmer(props: BoxProps) {
12
+ const { className, children, ...rest } = props;
13
+
14
+ return (
15
+ <Box className={classes(['Dimmer', className])} {...rest}>
16
+ <div className="Dimmer__inner">{children}</div>
17
+ </Box>
18
+ );
19
+ }