tgui-core 1.0.0

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 (152) hide show
  1. package/.editorconfig +10 -0
  2. package/.eslintrc.cjs +78 -0
  3. package/.gitattributes +4 -0
  4. package/.prettierrc.yml +1 -0
  5. package/.vscode/extensions.json +6 -0
  6. package/.vscode/settings.json +5 -0
  7. package/README.md +1 -0
  8. package/global.d.ts +173 -0
  9. package/package.json +25 -0
  10. package/src/assets.ts +43 -0
  11. package/src/backend.ts +369 -0
  12. package/src/common/collections.ts +349 -0
  13. package/src/common/color.ts +94 -0
  14. package/src/common/events.ts +45 -0
  15. package/src/common/exhaustive.ts +19 -0
  16. package/src/common/fp.ts +38 -0
  17. package/src/common/keycodes.ts +86 -0
  18. package/src/common/keys.ts +39 -0
  19. package/src/common/math.ts +98 -0
  20. package/src/common/perf.ts +72 -0
  21. package/src/common/random.ts +32 -0
  22. package/src/common/react.ts +65 -0
  23. package/src/common/redux.ts +196 -0
  24. package/src/common/storage.js +196 -0
  25. package/src/common/string.ts +173 -0
  26. package/src/common/timer.ts +68 -0
  27. package/src/common/type-utils.ts +41 -0
  28. package/src/common/types.ts +9 -0
  29. package/src/common/uuid.ts +24 -0
  30. package/src/common/vector.ts +51 -0
  31. package/src/components/AnimatedNumber.tsx +185 -0
  32. package/src/components/Autofocus.tsx +23 -0
  33. package/src/components/Blink.jsx +69 -0
  34. package/src/components/BlockQuote.tsx +15 -0
  35. package/src/components/BodyZoneSelector.tsx +149 -0
  36. package/src/components/Box.tsx +255 -0
  37. package/src/components/Button.tsx +415 -0
  38. package/src/components/ByondUi.jsx +121 -0
  39. package/src/components/Chart.tsx +160 -0
  40. package/src/components/Collapsible.tsx +45 -0
  41. package/src/components/ColorBox.tsx +30 -0
  42. package/src/components/Dialog.tsx +85 -0
  43. package/src/components/Dimmer.tsx +19 -0
  44. package/src/components/Divider.tsx +26 -0
  45. package/src/components/DmIcon.tsx +72 -0
  46. package/src/components/DraggableControl.jsx +282 -0
  47. package/src/components/Dropdown.tsx +246 -0
  48. package/src/components/FakeTerminal.jsx +52 -0
  49. package/src/components/FitText.tsx +99 -0
  50. package/src/components/Flex.tsx +105 -0
  51. package/src/components/Grid.tsx +44 -0
  52. package/src/components/Icon.tsx +91 -0
  53. package/src/components/Image.tsx +63 -0
  54. package/src/components/InfinitePlane.jsx +192 -0
  55. package/src/components/Input.tsx +181 -0
  56. package/src/components/KeyListener.tsx +40 -0
  57. package/src/components/Knob.tsx +185 -0
  58. package/src/components/LabeledControls.tsx +50 -0
  59. package/src/components/LabeledList.tsx +130 -0
  60. package/src/components/MenuBar.tsx +238 -0
  61. package/src/components/Modal.tsx +25 -0
  62. package/src/components/NoticeBox.tsx +48 -0
  63. package/src/components/NumberInput.tsx +328 -0
  64. package/src/components/Popper.tsx +100 -0
  65. package/src/components/ProgressBar.tsx +79 -0
  66. package/src/components/RestrictedInput.jsx +301 -0
  67. package/src/components/RoundGauge.tsx +189 -0
  68. package/src/components/Section.tsx +125 -0
  69. package/src/components/Slider.tsx +173 -0
  70. package/src/components/Stack.tsx +101 -0
  71. package/src/components/StyleableSection.tsx +30 -0
  72. package/src/components/Table.tsx +90 -0
  73. package/src/components/Tabs.tsx +90 -0
  74. package/src/components/TextArea.tsx +198 -0
  75. package/src/components/TimeDisplay.jsx +64 -0
  76. package/src/components/Tooltip.tsx +147 -0
  77. package/src/components/TrackOutsideClicks.tsx +35 -0
  78. package/src/components/VirtualList.tsx +69 -0
  79. package/src/constants.ts +355 -0
  80. package/src/debug/KitchenSink.jsx +56 -0
  81. package/src/debug/actions.js +11 -0
  82. package/src/debug/hooks.js +10 -0
  83. package/src/debug/index.ts +10 -0
  84. package/src/debug/middleware.js +86 -0
  85. package/src/debug/reducer.js +22 -0
  86. package/src/debug/selectors.js +7 -0
  87. package/src/drag.ts +280 -0
  88. package/src/events.ts +237 -0
  89. package/src/focus.ts +25 -0
  90. package/src/format.ts +173 -0
  91. package/src/hotkeys.ts +212 -0
  92. package/src/http.ts +16 -0
  93. package/src/layouts/Layout.tsx +75 -0
  94. package/src/layouts/NtosWindow.tsx +162 -0
  95. package/src/layouts/Pane.tsx +56 -0
  96. package/src/layouts/Window.tsx +227 -0
  97. package/src/renderer.ts +50 -0
  98. package/styles/base.scss +32 -0
  99. package/styles/colors.scss +92 -0
  100. package/styles/components/BlockQuote.scss +20 -0
  101. package/styles/components/Button.scss +175 -0
  102. package/styles/components/ColorBox.scss +12 -0
  103. package/styles/components/Dialog.scss +105 -0
  104. package/styles/components/Dimmer.scss +22 -0
  105. package/styles/components/Divider.scss +27 -0
  106. package/styles/components/Dropdown.scss +72 -0
  107. package/styles/components/Flex.scss +31 -0
  108. package/styles/components/Icon.scss +25 -0
  109. package/styles/components/Input.scss +68 -0
  110. package/styles/components/Knob.scss +131 -0
  111. package/styles/components/LabeledList.scss +49 -0
  112. package/styles/components/MenuBar.scss +75 -0
  113. package/styles/components/Modal.scss +14 -0
  114. package/styles/components/NoticeBox.scss +65 -0
  115. package/styles/components/NumberInput.scss +76 -0
  116. package/styles/components/ProgressBar.scss +63 -0
  117. package/styles/components/RoundGauge.scss +88 -0
  118. package/styles/components/Section.scss +143 -0
  119. package/styles/components/Slider.scss +54 -0
  120. package/styles/components/Stack.scss +59 -0
  121. package/styles/components/Table.scss +44 -0
  122. package/styles/components/Tabs.scss +144 -0
  123. package/styles/components/TextArea.scss +84 -0
  124. package/styles/components/Tooltip.scss +24 -0
  125. package/styles/functions.scss +79 -0
  126. package/styles/layouts/Layout.scss +57 -0
  127. package/styles/layouts/NtosHeader.scss +20 -0
  128. package/styles/layouts/NtosWindow.scss +26 -0
  129. package/styles/layouts/TitleBar.scss +111 -0
  130. package/styles/layouts/Window.scss +103 -0
  131. package/styles/main.scss +97 -0
  132. package/styles/reset.scss +68 -0
  133. package/styles/themes/abductor.scss +68 -0
  134. package/styles/themes/admin.scss +38 -0
  135. package/styles/themes/cardtable.scss +57 -0
  136. package/styles/themes/hackerman.scss +70 -0
  137. package/styles/themes/malfunction.scss +67 -0
  138. package/styles/themes/neutral.scss +50 -0
  139. package/styles/themes/ntOS95.scss +166 -0
  140. package/styles/themes/ntos.scss +44 -0
  141. package/styles/themes/ntos_cat.scss +148 -0
  142. package/styles/themes/ntos_darkmode.scss +44 -0
  143. package/styles/themes/ntos_lightmode.scss +67 -0
  144. package/styles/themes/ntos_spooky.scss +69 -0
  145. package/styles/themes/ntos_synth.scss +99 -0
  146. package/styles/themes/ntos_terminal.scss +112 -0
  147. package/styles/themes/paper.scss +184 -0
  148. package/styles/themes/retro.scss +72 -0
  149. package/styles/themes/spookyconsole.scss +73 -0
  150. package/styles/themes/syndicate.scss +67 -0
  151. package/styles/themes/wizard.scss +68 -0
  152. package/tsconfig.json +34 -0
@@ -0,0 +1,301 @@
1
+ import { KEY_ENTER, KEY_ESCAPE } from '../common/keycodes';
2
+ import { clamp } from '../common/math';
3
+ import { classes } from '../common/react';
4
+ import { Component, createRef } from 'react';
5
+
6
+ import { Box } from './Box';
7
+
8
+ const DEFAULT_MIN = 0;
9
+ const DEFAULT_MAX = 10000;
10
+
11
+ /**
12
+ * Sanitize a number without interfering with writing negative or floating point numbers.
13
+ * Handling dots and minuses in a user friendly way
14
+ * @param value {String}
15
+ * @param minValue {Number}
16
+ * @param maxValue {Number}
17
+ * @param allowFloats {Boolean}
18
+ * @returns {String}
19
+ */
20
+ const softSanitizeNumber = (value, minValue, maxValue, allowFloats) => {
21
+ const minimum = minValue || DEFAULT_MIN;
22
+ const maximum = maxValue || maxValue === 0 ? maxValue : DEFAULT_MAX;
23
+
24
+ let sanitizedString = allowFloats
25
+ ? value.replace(/[^\-\d.]/g, '')
26
+ : value.replace(/[^\-\d]/g, '');
27
+
28
+ if (allowFloats) {
29
+ sanitizedString = maybeLeadWithMin(sanitizedString, minimum);
30
+ sanitizedString = keepOnlyFirstOccurrence('.', sanitizedString);
31
+ }
32
+ if (minValue < 0) {
33
+ sanitizedString = maybeMoveMinusSign(sanitizedString);
34
+ sanitizedString = keepOnlyFirstOccurrence('-', sanitizedString);
35
+ } else {
36
+ sanitizedString = sanitizedString.replaceAll('-', '');
37
+ }
38
+ if (minimum <= 1 && maximum >= 0) {
39
+ return clampGuessedNumber(sanitizedString, minimum, maximum, allowFloats);
40
+ }
41
+ return sanitizedString;
42
+ };
43
+
44
+ /**
45
+ * Clamping the input to the restricted range, making the Input smart for min <= 1 and max >= 0
46
+ * @param softSanitizedNumber {String}
47
+ * @param allowFloats {Boolean}
48
+ * @returns {string}
49
+ */
50
+ const clampGuessedNumber = (
51
+ softSanitizedNumber,
52
+ minValue,
53
+ maxValue,
54
+ allowFloats
55
+ ) => {
56
+ let parsed = allowFloats
57
+ ? parseFloat(softSanitizedNumber)
58
+ : parseInt(softSanitizedNumber, 10);
59
+ if (
60
+ !isNaN(parsed) &&
61
+ (softSanitizedNumber.slice(-1) !== '.' || parsed < Math.floor(minValue))
62
+ ) {
63
+ let clamped = clamp(parsed, minValue, maxValue);
64
+ if (parsed !== clamped) {
65
+ return String(clamped);
66
+ }
67
+ }
68
+ return softSanitizedNumber;
69
+ };
70
+
71
+ /**
72
+ * Translate x- to -x and -x- to x
73
+ * @param string {String}
74
+ * @returns {string}
75
+ */
76
+ const maybeMoveMinusSign = (string) => {
77
+ let retString = string;
78
+ // if minus sign is present but not first
79
+ let minusIdx = string.indexOf('-');
80
+ if (minusIdx > 0) {
81
+ string = string.replace('-', '');
82
+ retString = '-'.concat(string);
83
+ } else if (minusIdx === 0) {
84
+ if (string.indexOf('-', minusIdx + 1) > 0) {
85
+ retString = string.replaceAll('-', '');
86
+ }
87
+ }
88
+ return retString;
89
+ };
90
+
91
+ /**
92
+ * Translate . to min. or .x to mim.x or -. to -min.
93
+ * @param string {String}
94
+ */
95
+ const maybeLeadWithMin = (string, min) => {
96
+ let retString = string;
97
+ let cuttedVal = Math.sign(min) * Math.floor(Math.abs(min));
98
+ if (string.indexOf('.') === 0) {
99
+ retString = String(cuttedVal).concat(string);
100
+ } else if (string.indexOf('-') === 0 && string.indexOf('.') === 1) {
101
+ retString = cuttedVal + '.'.concat(string.slice(2));
102
+ }
103
+ return retString;
104
+ };
105
+
106
+ /**
107
+ * Keep only the first occurrence of a string in another string.
108
+ * @param needle {String}
109
+ * @param haystack {String}
110
+ * @returns {string}
111
+ */
112
+ const keepOnlyFirstOccurrence = (needle, haystack) => {
113
+ const idx = haystack.indexOf(needle);
114
+ const len = haystack.length;
115
+ let newHaystack = haystack;
116
+ if (idx !== -1 && idx < len - 1) {
117
+ let trailingString = haystack.slice(idx + 1, len);
118
+ trailingString = trailingString.replaceAll(needle, '');
119
+ newHaystack = haystack.slice(0, idx + 1).concat(trailingString);
120
+ }
121
+ return newHaystack;
122
+ };
123
+
124
+ /**
125
+ * Takes a string input and parses integers or floats from it.
126
+ * If none: Minimum is set.
127
+ * Else: Clamps it to the given range.
128
+ */
129
+ const getClampedNumber = (value, minValue, maxValue, allowFloats) => {
130
+ const minimum = minValue || DEFAULT_MIN;
131
+ const maximum = maxValue || maxValue === 0 ? maxValue : DEFAULT_MAX;
132
+ if (!value || !value.length) {
133
+ return String(minimum);
134
+ }
135
+ let parsedValue = allowFloats
136
+ ? parseFloat(value.replace(/[^\-\d.]/g, ''))
137
+ : parseInt(value.replace(/[^\-\d]/g, ''), 10);
138
+ if (isNaN(parsedValue)) {
139
+ return String(minimum);
140
+ } else {
141
+ return String(clamp(parsedValue, minimum, maximum));
142
+ }
143
+ };
144
+
145
+ export class RestrictedInput extends Component {
146
+ constructor(props) {
147
+ super(props);
148
+ this.inputRef = createRef();
149
+ this.state = {
150
+ editing: false,
151
+ };
152
+ this.handleBlur = (e) => {
153
+ const { maxValue, minValue, onBlur, allowFloats } = this.props;
154
+ const { editing } = this.state;
155
+ if (editing) {
156
+ this.setEditing(false);
157
+ }
158
+ const safeNum = getClampedNumber(
159
+ e.target.value,
160
+ minValue,
161
+ maxValue,
162
+ allowFloats
163
+ );
164
+ if (onBlur) {
165
+ onBlur(e, +safeNum);
166
+ }
167
+ };
168
+ this.handleChange = (e) => {
169
+ const { maxValue, minValue, onChange, allowFloats } = this.props;
170
+ e.target.value = softSanitizeNumber(
171
+ e.target.value,
172
+ minValue,
173
+ maxValue,
174
+ allowFloats
175
+ );
176
+ if (onChange) {
177
+ onChange(e, +e.target.value);
178
+ }
179
+ };
180
+ this.handleFocus = (e) => {
181
+ const { editing } = this.state;
182
+ if (!editing) {
183
+ this.setEditing(true);
184
+ }
185
+ };
186
+ this.handleInput = (e) => {
187
+ const { editing } = this.state;
188
+ const { onInput } = this.props;
189
+ if (!editing) {
190
+ this.setEditing(true);
191
+ }
192
+ if (onInput) {
193
+ onInput(e, +e.target.value);
194
+ }
195
+ };
196
+ this.handleKeyDown = (e) => {
197
+ const { maxValue, minValue, onChange, onEnter, allowFloats } = this.props;
198
+ if (e.keyCode === KEY_ENTER) {
199
+ const safeNum = getClampedNumber(
200
+ e.target.value,
201
+ minValue,
202
+ maxValue,
203
+ allowFloats
204
+ );
205
+ this.setEditing(false);
206
+ if (onChange) {
207
+ onChange(e, +safeNum);
208
+ }
209
+ if (onEnter) {
210
+ onEnter(e, +safeNum);
211
+ }
212
+ e.target.blur();
213
+ return;
214
+ }
215
+ if (e.keyCode === KEY_ESCAPE) {
216
+ if (this.props.onEscape) {
217
+ this.props.onEscape(e);
218
+ return;
219
+ }
220
+ this.setEditing(false);
221
+ e.target.value = this.props.value;
222
+ e.target.blur();
223
+ return;
224
+ }
225
+ };
226
+ }
227
+
228
+ componentDidMount() {
229
+ const { maxValue, minValue, allowFloats } = this.props;
230
+ const nextValue = this.props.value?.toString();
231
+ const input = this.inputRef.current;
232
+ if (input) {
233
+ input.value = getClampedNumber(
234
+ nextValue,
235
+ minValue,
236
+ maxValue,
237
+ allowFloats
238
+ );
239
+ }
240
+ if (this.props.autoFocus || this.props.autoSelect) {
241
+ setTimeout(() => {
242
+ input.focus();
243
+
244
+ if (this.props.autoSelect) {
245
+ input.select();
246
+ }
247
+ }, 1);
248
+ }
249
+ }
250
+
251
+ componentDidUpdate(prevProps, _) {
252
+ const { maxValue, minValue, allowFloats } = this.props;
253
+ const { editing } = this.state;
254
+ const prevValue = prevProps.value?.toString();
255
+ const nextValue = this.props.value?.toString();
256
+ const input = this.inputRef.current;
257
+ if (input && !editing) {
258
+ if (nextValue !== prevValue && nextValue !== input.value) {
259
+ input.value = getClampedNumber(
260
+ nextValue,
261
+ minValue,
262
+ maxValue,
263
+ allowFloats
264
+ );
265
+ }
266
+ }
267
+ }
268
+
269
+ setEditing(editing) {
270
+ this.setState({ editing });
271
+ }
272
+
273
+ render() {
274
+ const { props } = this;
275
+ const { onChange, onEnter, onInput, onBlur, value, ...boxProps } = props;
276
+ const { className, fluid, monospace, ...rest } = boxProps;
277
+ return (
278
+ <Box
279
+ className={classes([
280
+ 'Input',
281
+ fluid && 'Input--fluid',
282
+ monospace && 'Input--monospace',
283
+ className,
284
+ ])}
285
+ {...rest}
286
+ >
287
+ <div className="Input__baseline">.</div>
288
+ <input
289
+ className="Input__input"
290
+ onChange={this.handleChange}
291
+ onInput={this.handleInput}
292
+ onFocus={this.handleFocus}
293
+ onBlur={this.handleBlur}
294
+ onKeyDown={this.handleKeyDown}
295
+ ref={this.inputRef}
296
+ type="number | string"
297
+ />
298
+ </Box>
299
+ );
300
+ }
301
+ }
@@ -0,0 +1,189 @@
1
+ /**
2
+ * @file
3
+ * @copyright 2020 bobbahbrown (https://github.com/bobbahbrown)
4
+ * @license MIT
5
+ */
6
+
7
+ import { clamp01, keyOfMatchingRange, scale } from '../common/math';
8
+ import { classes } from '../common/react';
9
+
10
+ import { AnimatedNumber } from './AnimatedNumber';
11
+ import { Box, BoxProps, computeBoxClassName, computeBoxProps } from './Box';
12
+
13
+ type Props = {
14
+ /** The current value of the metric. */
15
+ value: number;
16
+ } & Partial<{
17
+ /** When provided, will cause an alert symbol on the gauge to begin flashing in the color upon which the needle currently rests, as defined in `ranges`. */
18
+ alertAfter: number;
19
+ /** As with alertAfter, but alerts below a value. If both are set, and alertAfter comes earlier, the alert will only flash when the needle is between both values. Otherwise, the alert will flash when on the active side of either threshold. */
20
+ alertBefore: number;
21
+ /** CSS style. */
22
+ className: string;
23
+ /** When provided, will be used to format the value of the metric for display. */
24
+ format: (value: number) => string;
25
+ /** The upper bound of the gauge. */
26
+ maxValue: number;
27
+ /** The lower bound of the gauge. */
28
+ minValue: number;
29
+ /** Provide regions of the gauge to color between two specified values of the metric. */
30
+ ranges: Record<string, [number, number]>;
31
+ /** When provided scales the gauge. */
32
+ size: number;
33
+ /** Custom css */
34
+ style: React.CSSProperties;
35
+ }> &
36
+ BoxProps;
37
+
38
+ /**
39
+ * ## RoundGauge
40
+ * The RoundGauge component provides a visual representation of a single metric, as well as being capable of showing
41
+ * informational or cautionary boundaries related to that metric.
42
+ *
43
+ * @example
44
+ * ```tsx
45
+ * <RoundGauge
46
+ * size={1.75}
47
+ * value={tankPressure}
48
+ * minValue={0}
49
+ * maxValue={pressureLimit}
50
+ * alertAfter={pressureLimit * 0.7}
51
+ * ranges={{
52
+ * good: [0, pressureLimit * 0.7],
53
+ * average: [pressureLimit * 0.7, pressureLimit * 0.85],
54
+ * bad: [pressureLimit * 0.85, pressureLimit],
55
+ * }}
56
+ * format={formatPressure}
57
+ * />
58
+ * ```
59
+ *
60
+ * The alert on the gauge is optional, and will only be shown if the `alertAfter` prop is defined. When defined, the alert
61
+ * will begin to flash the respective color upon which the needle currently rests, as defined in the `ranges` prop.
62
+ *
63
+ */
64
+ export function RoundGauge(props: Props) {
65
+ const {
66
+ alertAfter,
67
+ alertBefore,
68
+ className,
69
+ format,
70
+ maxValue = 1,
71
+ minValue = 1,
72
+ ranges,
73
+ size = 1,
74
+ style,
75
+ value,
76
+ ...rest
77
+ } = props;
78
+
79
+ const scaledValue = scale(value, minValue, maxValue);
80
+ const clampedValue = clamp01(scaledValue);
81
+ const scaledRanges = ranges ? {} : { primary: [0, 1] };
82
+
83
+ if (ranges) {
84
+ Object.keys(ranges).forEach((x) => {
85
+ const range = ranges[x];
86
+ scaledRanges[x] = [
87
+ scale(range[0], minValue, maxValue),
88
+ scale(range[1], minValue, maxValue),
89
+ ];
90
+ });
91
+ }
92
+
93
+ function shouldShowAlert() {
94
+ // If both after and before alert props are set, and value is between them
95
+ if (
96
+ alertAfter &&
97
+ alertBefore &&
98
+ value > alertAfter &&
99
+ value < alertBefore
100
+ ) {
101
+ return true;
102
+ }
103
+ // If only alertAfter is set and value is greater than alertAfter
104
+ else if (alertAfter && value > alertAfter) {
105
+ return true;
106
+ }
107
+ // If only alertBefore is set and value is less than alertBefore
108
+ else if (alertBefore && value < alertBefore) {
109
+ return true;
110
+ }
111
+ // If none of the above conditions are met
112
+ return false;
113
+ }
114
+
115
+ const alertColor =
116
+ shouldShowAlert() && keyOfMatchingRange(clampedValue, scaledRanges);
117
+
118
+ return (
119
+ <Box inline>
120
+ <div
121
+ className={classes([
122
+ 'RoundGauge',
123
+ className,
124
+ computeBoxClassName(rest),
125
+ ])}
126
+ {...computeBoxProps({
127
+ style: {
128
+ fontSize: size + 'em',
129
+ ...style,
130
+ },
131
+ ...rest,
132
+ })}
133
+ >
134
+ <svg viewBox="0 0 100 50">
135
+ {(alertAfter || alertBefore) && (
136
+ <g
137
+ className={classes([
138
+ 'RoundGauge__alert',
139
+ alertColor ? `active RoundGauge__alert--${alertColor}` : '',
140
+ ])}
141
+ >
142
+ <path d="M48.211,14.578C48.55,13.9 49.242,13.472 50,13.472C50.758,13.472 51.45,13.9 51.789,14.578C54.793,20.587 60.795,32.589 63.553,38.106C63.863,38.726 63.83,39.462 63.465,40.051C63.101,40.641 62.457,41 61.764,41C55.996,41 44.004,41 38.236,41C37.543,41 36.899,40.641 36.535,40.051C36.17,39.462 36.137,38.726 36.447,38.106C39.205,32.589 45.207,20.587 48.211,14.578ZM50,34.417C51.426,34.417 52.583,35.574 52.583,37C52.583,38.426 51.426,39.583 50,39.583C48.574,39.583 47.417,38.426 47.417,37C47.417,35.574 48.574,34.417 50,34.417ZM50,32.75C50,32.75 53,31.805 53,22.25C53,20.594 51.656,19.25 50,19.25C48.344,19.25 47,20.594 47,22.25C47,31.805 50,32.75 50,32.75Z" />
143
+ </g>
144
+ )}
145
+ <g>
146
+ <circle className="RoundGauge__ringTrack" cx="50" cy="50" r="45" />
147
+ </g>
148
+ <g>
149
+ {Object.keys(scaledRanges).map((x, i) => {
150
+ const col_ranges = scaledRanges[x];
151
+ return (
152
+ <circle
153
+ className={`RoundGauge__ringFill RoundGauge--color--${x}`}
154
+ key={i}
155
+ style={{
156
+ strokeDashoffset: Math.max(
157
+ (2.0 - (col_ranges[1] - col_ranges[0])) * Math.PI * 50,
158
+ 0
159
+ ),
160
+ }}
161
+ transform={`rotate(${180 + 180 * col_ranges[0]} 50 50)`}
162
+ cx="50"
163
+ cy="50"
164
+ r="45"
165
+ />
166
+ );
167
+ })}
168
+ </g>
169
+ <g
170
+ className="RoundGauge__needle"
171
+ transform={`rotate(${clampedValue * 180 - 90} 50 50)`}
172
+ >
173
+ <polygon
174
+ className="RoundGauge__needleLine"
175
+ points="46,50 50,0 54,50"
176
+ />
177
+ <circle
178
+ className="RoundGauge__needleMiddle"
179
+ cx="50"
180
+ cy="50"
181
+ r="8"
182
+ />
183
+ </g>
184
+ </svg>
185
+ </div>
186
+ <AnimatedNumber value={value} format={format} />
187
+ </Box>
188
+ );
189
+ }
@@ -0,0 +1,125 @@
1
+ /**
2
+ * @file
3
+ * @copyright 2020 Aleksej Komarov
4
+ * @license MIT
5
+ */
6
+
7
+ import { canRender, classes } from '../common/react';
8
+ import { forwardRef, ReactNode, RefObject, useEffect } from 'react';
9
+
10
+ import { addScrollableNode, removeScrollableNode } from '../events';
11
+ import { BoxProps, computeBoxClassName, computeBoxProps } from './Box';
12
+
13
+ type Props = Partial<{
14
+ /** Buttons to render aside the section title. */
15
+ buttons: ReactNode;
16
+ /** If true, fills all available vertical space. */
17
+ fill: boolean;
18
+ /** If true, removes all section padding. */
19
+ fitted: boolean;
20
+ /** Shows or hides the scrollbar. */
21
+ scrollable: boolean;
22
+ /** Shows or hides the horizontal scrollbar. */
23
+ scrollableHorizontal: boolean;
24
+ /** Title of the section. */
25
+ title: ReactNode;
26
+ /** id to assosiate with the parent div element used by this section, for uses with procs like getElementByID */
27
+ container_id: string;
28
+ /** @member Callback function for the `scroll` event */
29
+ onScroll: ((this: GlobalEventHandlers, ev: Event) => any) | null;
30
+ }> &
31
+ BoxProps;
32
+
33
+ /**
34
+ * ## Section
35
+ * Section is a surface that displays content and actions on a single topic.
36
+ *
37
+ * They should be easy to scan for relevant and actionable information.
38
+ * Elements, like text and images, should be placed in them in a way that
39
+ * clearly indicates hierarchy.
40
+ *
41
+ * Sections can now be nested, and will automatically font size of the
42
+ * header according to their nesting level. Previously this was done via `level`
43
+ * prop, but now it is automatically calculated.
44
+ *
45
+ * Section can also be titled to clearly define its purpose.
46
+ *
47
+ * ```tsx
48
+ * <Section title="Cargo">Here you can order supply crates.</Section>
49
+ * ```
50
+ *
51
+ * If you want to have a button on the right side of an section title
52
+ * (for example, to perform some sort of action), there is a way to do that:
53
+ *
54
+ * ```tsx
55
+ * <Section title="Cargo" buttons={<Button>Send shuttle</Button>}>
56
+ * Here you can order supply crates.
57
+ * </Section>
58
+ * ```
59
+ */
60
+ export const Section = forwardRef(
61
+ (props: Props, forwardedRef: RefObject<HTMLDivElement>) => {
62
+ const {
63
+ buttons,
64
+ children,
65
+ className,
66
+ fill,
67
+ fitted,
68
+ onScroll,
69
+ scrollable,
70
+ scrollableHorizontal,
71
+ title,
72
+ container_id,
73
+ ...rest
74
+ } = props;
75
+
76
+ const hasTitle = canRender(title) || canRender(buttons);
77
+
78
+ /** We want to be able to scroll on hover, but using focus will steal it from inputs */
79
+ useEffect(() => {
80
+ if (!forwardedRef?.current) return;
81
+ if (!scrollable && !scrollableHorizontal) return;
82
+
83
+ addScrollableNode(forwardedRef.current);
84
+
85
+ return () => {
86
+ if (!forwardedRef?.current) return;
87
+ removeScrollableNode(forwardedRef.current!);
88
+ };
89
+ }, []);
90
+
91
+ return (
92
+ <div
93
+ id={container_id || ''}
94
+ className={classes([
95
+ 'Section',
96
+ fill && 'Section--fill',
97
+ fitted && 'Section--fitted',
98
+ scrollable && 'Section--scrollable',
99
+ scrollableHorizontal && 'Section--scrollableHorizontal',
100
+ className,
101
+ computeBoxClassName(rest),
102
+ ])}
103
+ {...computeBoxProps(rest)}
104
+ >
105
+ {hasTitle && (
106
+ <div className="Section__title">
107
+ <span className="Section__titleText">{title}</span>
108
+ <div className="Section__buttons">{buttons}</div>
109
+ </div>
110
+ )}
111
+ <div className="Section__rest">
112
+ <div
113
+ className="Section__content"
114
+ onScroll={onScroll}
115
+ // For posterity: the forwarded ref needs to be here specifically
116
+ // to actually let things interact with the scrolling.
117
+ ref={forwardedRef}
118
+ >
119
+ {children}
120
+ </div>
121
+ </div>
122
+ </div>
123
+ );
124
+ }
125
+ );