tgui-core 1.1.7 → 1.1.9

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 (291) hide show
  1. package/lib/common/assets.ts +38 -0
  2. package/lib/common/collections.ts +27 -0
  3. package/lib/common/color.ts +88 -0
  4. package/lib/common/constants.ts +349 -0
  5. package/lib/common/events.ts +262 -0
  6. package/{dist/common/exhaustive.d.ts → lib/common/exhaustive.ts} +3 -1
  7. package/lib/common/format.ts +167 -0
  8. package/{dist/common/fp.d.ts → lib/common/fp.ts} +16 -2
  9. package/lib/common/hotkeys.ts +207 -0
  10. package/lib/common/http.ts +16 -0
  11. package/lib/common/keycodes.ts +86 -0
  12. package/{dist/common/keys.d.ts → lib/common/keys.ts} +24 -21
  13. package/lib/common/math.ts +76 -0
  14. package/lib/common/perf.ts +72 -0
  15. package/lib/common/random.ts +32 -0
  16. package/lib/common/react.ts +59 -0
  17. package/lib/common/redux.ts +187 -0
  18. package/lib/common/storage.ts +207 -0
  19. package/lib/common/string.ts +169 -0
  20. package/lib/common/timer.ts +63 -0
  21. package/lib/common/type-utils.ts +41 -0
  22. package/lib/common/types.d.ts +12 -0
  23. package/lib/common/uuid.ts +18 -0
  24. package/lib/components/AnimatedNumber.tsx +180 -0
  25. package/lib/components/Autofocus.tsx +23 -0
  26. package/lib/components/Blink.tsx +91 -0
  27. package/lib/components/BlockQuote.tsx +9 -0
  28. package/lib/components/BodyZoneSelector.tsx +149 -0
  29. package/lib/components/Box.tsx +252 -0
  30. package/lib/components/Button.tsx +425 -0
  31. package/lib/components/ByondUi.jsx +110 -0
  32. package/lib/components/Chart.tsx +155 -0
  33. package/lib/components/Collapsible.tsx +43 -0
  34. package/lib/components/ColorBox.tsx +29 -0
  35. package/lib/components/Dialog.tsx +81 -0
  36. package/lib/components/Dimmer.tsx +13 -0
  37. package/lib/components/Divider.tsx +20 -0
  38. package/lib/components/DmIcon.tsx +86 -0
  39. package/lib/components/DraggableControl.jsx +276 -0
  40. package/lib/components/Dropdown.tsx +246 -0
  41. package/lib/components/FakeTerminal.jsx +52 -0
  42. package/lib/components/FitText.tsx +99 -0
  43. package/lib/components/Flex.tsx +159 -0
  44. package/lib/components/Icon.tsx +95 -0
  45. package/lib/components/Image.tsx +54 -0
  46. package/lib/components/InfinitePlane.jsx +192 -0
  47. package/lib/components/Input.tsx +176 -0
  48. package/lib/components/KeyListener.tsx +40 -0
  49. package/lib/components/Knob.tsx +178 -0
  50. package/lib/components/LabeledControls.tsx +44 -0
  51. package/lib/components/LabeledList.tsx +154 -0
  52. package/lib/components/MenuBar.tsx +228 -0
  53. package/lib/components/Modal.tsx +23 -0
  54. package/lib/components/NoticeBox.tsx +45 -0
  55. package/lib/components/NumberInput.tsx +328 -0
  56. package/lib/components/Popper.tsx +100 -0
  57. package/lib/components/ProgressBar.tsx +105 -0
  58. package/lib/components/RestrictedInput.jsx +301 -0
  59. package/lib/components/RoundGauge.tsx +180 -0
  60. package/lib/components/Section.tsx +120 -0
  61. package/lib/components/Slider.tsx +169 -0
  62. package/lib/components/Stack.tsx +96 -0
  63. package/lib/components/StyleableSection.tsx +33 -0
  64. package/lib/components/Table.tsx +84 -0
  65. package/lib/components/Tabs.tsx +89 -0
  66. package/lib/components/TextArea.tsx +182 -0
  67. package/lib/components/TimeDisplay.jsx +64 -0
  68. package/lib/components/Tooltip.tsx +152 -0
  69. package/lib/components/TrackOutsideClicks.tsx +35 -0
  70. package/lib/components/VirtualList.tsx +69 -0
  71. package/lib/styles/atomic/candystripe.scss +8 -0
  72. package/lib/styles/atomic/centered-image.scss +7 -0
  73. package/lib/styles/atomic/color.scss +21 -0
  74. package/lib/styles/atomic/debug-layout.scss +17 -0
  75. package/lib/styles/atomic/fit-text.scss +14 -0
  76. package/lib/styles/atomic/links.scss +12 -0
  77. package/lib/styles/atomic/outline.scss +47 -0
  78. package/lib/styles/atomic/text.scss +44 -0
  79. package/lib/styles/base.scss +32 -0
  80. package/lib/styles/colors.scss +92 -0
  81. package/lib/styles/components/BlockQuote.module.scss +20 -0
  82. package/lib/styles/components/BlockQuote.module.scss.d.ts +4 -0
  83. package/lib/styles/components/Button.module.scss +157 -0
  84. package/lib/styles/components/Button.module.scss.d.ts +46 -0
  85. package/lib/styles/components/ColorBox.module.scss +12 -0
  86. package/lib/styles/components/ColorBox.module.scss.d.ts +4 -0
  87. package/lib/styles/components/Dialog.module.scss +60 -0
  88. package/lib/styles/components/Dialog.module.scss.d.ts +10 -0
  89. package/lib/styles/components/Dimmer.module.scss +22 -0
  90. package/lib/styles/components/Dimmer.module.scss.d.ts +4 -0
  91. package/lib/styles/components/Divider.module.scss +27 -0
  92. package/lib/styles/components/Divider.module.scss.d.ts +6 -0
  93. package/lib/styles/components/Dropdown.scss +72 -0
  94. package/lib/styles/components/Flex.module.scss +13 -0
  95. package/lib/styles/components/Flex.module.scss.d.ts +5 -0
  96. package/lib/styles/components/Icon.module.scss +25 -0
  97. package/lib/styles/components/Icon.module.scss.d.ts +5 -0
  98. package/lib/styles/components/Input.module.scss +64 -0
  99. package/lib/styles/components/Input.module.scss.d.ts +8 -0
  100. package/lib/styles/components/Knob.module.scss +131 -0
  101. package/lib/styles/components/Knob.module.scss.d.ts +33 -0
  102. package/lib/styles/components/LabeledList.module.scss +49 -0
  103. package/lib/styles/components/LabeledList.module.scss.d.ts +8 -0
  104. package/lib/styles/components/MenuBar.module.scss +75 -0
  105. package/lib/styles/components/MenuBar.module.scss.d.ts +14 -0
  106. package/lib/styles/components/Modal.module.scss +14 -0
  107. package/lib/styles/components/Modal.module.scss.d.ts +4 -0
  108. package/lib/styles/components/NoticeBox.module.scss +65 -0
  109. package/lib/styles/components/NoticeBox.module.scss.d.ts +27 -0
  110. package/lib/styles/components/NumberInput.module.scss +71 -0
  111. package/lib/styles/components/NumberInput.module.scss.d.ts +9 -0
  112. package/lib/styles/components/ProgressBar.module.scss +63 -0
  113. package/lib/styles/components/ProgressBar.module.scss.d.ts +27 -0
  114. package/lib/styles/components/RoundGauge.module.scss +85 -0
  115. package/lib/styles/components/RoundGauge.module.scss.d.ts +49 -0
  116. package/lib/styles/components/Section.module.scss +130 -0
  117. package/lib/styles/components/Section.module.scss.d.ts +13 -0
  118. package/lib/styles/components/Slider.module.scss +54 -0
  119. package/lib/styles/components/Slider.module.scss.d.ts +8 -0
  120. package/lib/styles/components/Stack.module.scss +60 -0
  121. package/lib/styles/components/Stack.module.scss.d.ts +12 -0
  122. package/lib/styles/components/Table.module.scss +44 -0
  123. package/lib/styles/components/Table.module.scss.d.ts +10 -0
  124. package/lib/styles/components/Tabs.module.scss +144 -0
  125. package/lib/styles/components/Tabs.module.scss.d.ts +35 -0
  126. package/lib/styles/components/TextArea.module.scss +86 -0
  127. package/lib/styles/components/TextArea.module.scss.d.ts +11 -0
  128. package/lib/styles/components/Tooltip.module.scss +24 -0
  129. package/lib/styles/components/Tooltip.module.scss.d.ts +4 -0
  130. package/lib/styles/functions.scss +79 -0
  131. package/lib/styles/input.scss +9 -0
  132. package/lib/styles/main.scss +20 -0
  133. package/lib/styles/reset.scss +68 -0
  134. package/package.json +6 -6
  135. package/dist/ProgressBar.module-BkAFfFy0.js +0 -29
  136. package/dist/Section.module-CLVHJ4yA.js +0 -15
  137. package/dist/assets/BlockQuote.css +0 -1
  138. package/dist/assets/Button.css +0 -1
  139. package/dist/assets/ColorBox.css +0 -1
  140. package/dist/assets/Dialog.css +0 -1
  141. package/dist/assets/Dimmer.css +0 -1
  142. package/dist/assets/Divider.css +0 -1
  143. package/dist/assets/Flex.css +0 -1
  144. package/dist/assets/Icon.css +0 -6
  145. package/dist/assets/Input.css +0 -1
  146. package/dist/assets/Knob.css +0 -1
  147. package/dist/assets/LabeledList.css +0 -1
  148. package/dist/assets/MenuBar.css +0 -1
  149. package/dist/assets/Modal.css +0 -1
  150. package/dist/assets/NoticeBox.css +0 -1
  151. package/dist/assets/NumberInput.css +0 -1
  152. package/dist/assets/ProgressBar.css +0 -1
  153. package/dist/assets/RoundGauge.css +0 -1
  154. package/dist/assets/Section.css +0 -1
  155. package/dist/assets/Slider.css +0 -1
  156. package/dist/assets/Stack.css +0 -1
  157. package/dist/assets/Table.css +0 -1
  158. package/dist/assets/Tabs.css +0 -1
  159. package/dist/assets/TextArea.css +0 -1
  160. package/dist/assets/Tooltip.css +0 -1
  161. package/dist/common/assets.d.ts +0 -4
  162. package/dist/common/assets.js +0 -21
  163. package/dist/common/collections.d.ts +0 -10
  164. package/dist/common/collections.js +0 -15
  165. package/dist/common/color.d.ts +0 -25
  166. package/dist/common/color.js +0 -69
  167. package/dist/common/constants.d.ts +0 -102
  168. package/dist/common/constants.js +0 -312
  169. package/dist/common/events.d.ts +0 -33
  170. package/dist/common/events.js +0 -147
  171. package/dist/common/exhaustive.js +0 -6
  172. package/dist/common/format.d.ts +0 -11
  173. package/dist/common/format.js +0 -114
  174. package/dist/common/fp.js +0 -9
  175. package/dist/common/hotkeys.d.ts +0 -25
  176. package/dist/common/hotkeys.js +0 -112
  177. package/dist/common/http.d.ts +0 -4
  178. package/dist/common/http.js +0 -10
  179. package/dist/common/keycodes.d.ts +0 -85
  180. package/dist/common/keycodes.js +0 -88
  181. package/dist/common/keys.js +0 -8
  182. package/dist/common/math.d.ts +0 -39
  183. package/dist/common/math.js +0 -41
  184. package/dist/common/perf.d.ts +0 -24
  185. package/dist/common/perf.js +0 -33
  186. package/dist/common/random.d.ts +0 -16
  187. package/dist/common/random.js +0 -18
  188. package/dist/common/react.d.ts +0 -23
  189. package/dist/common/react.js +0 -30
  190. package/dist/common/redux.d.ts +0 -64
  191. package/dist/common/redux.js +0 -72
  192. package/dist/common/storage.js +0 -124
  193. package/dist/common/string.d.ts +0 -65
  194. package/dist/common/string.js +0 -83
  195. package/dist/common/timer.d.ts +0 -18
  196. package/dist/common/timer.js +0 -28
  197. package/dist/common/type-utils.d.ts +0 -9
  198. package/dist/common/type-utils.js +0 -25
  199. package/dist/common/uuid.d.ts +0 -9
  200. package/dist/common/uuid.js +0 -10
  201. package/dist/components/AnimatedNumber.d.ts +0 -60
  202. package/dist/components/AnimatedNumber.js +0 -76
  203. package/dist/components/Autofocus.d.ts +0 -4
  204. package/dist/components/Autofocus.js +0 -17
  205. package/dist/components/Blink.d.ts +0 -26
  206. package/dist/components/Blink.js +0 -56
  207. package/dist/components/BlockQuote.d.ts +0 -3
  208. package/dist/components/BlockQuote.js +0 -13
  209. package/dist/components/BodyZoneSelector.d.ts +0 -28
  210. package/dist/components/BodyZoneSelector.js +0 -115
  211. package/dist/components/Box.d.ts +0 -91
  212. package/dist/components/Box.js +0 -133
  213. package/dist/components/Button.d.ts +0 -93
  214. package/dist/components/Button.js +0 -298
  215. package/dist/components/ByondUi.js +0 -73
  216. package/dist/components/Chart.d.ts +0 -28
  217. package/dist/components/Chart.js +0 -95
  218. package/dist/components/Collapsible.d.ts +0 -15
  219. package/dist/components/Collapsible.js +0 -27
  220. package/dist/components/ColorBox.d.ts +0 -8
  221. package/dist/components/ColorBox.js +0 -24
  222. package/dist/components/Dialog.d.ts +0 -24
  223. package/dist/components/Dialog.js +0 -67
  224. package/dist/components/Dimmer.d.ts +0 -3
  225. package/dist/components/Dimmer.js +0 -13
  226. package/dist/components/Divider.d.ts +0 -6
  227. package/dist/components/Divider.js +0 -22
  228. package/dist/components/DmIcon.d.ts +0 -31
  229. package/dist/components/DmIcon.js +0 -31
  230. package/dist/components/DraggableControl.js +0 -176
  231. package/dist/components/Dropdown.d.ts +0 -48
  232. package/dist/components/Dropdown.js +0 -152
  233. package/dist/components/FakeTerminal.js +0 -38
  234. package/dist/components/FitText.d.ts +0 -22
  235. package/dist/components/FitText.js +0 -63
  236. package/dist/components/Flex.d.ts +0 -93
  237. package/dist/components/Flex.js +0 -72
  238. package/dist/components/Icon.d.ts +0 -30
  239. package/dist/components/Icon.js +0 -51
  240. package/dist/components/Image.d.ts +0 -14
  241. package/dist/components/Image.js +0 -35
  242. package/dist/components/InfinitePlane.js +0 -139
  243. package/dist/components/Input.d.ts +0 -61
  244. package/dist/components/Input.js +0 -89
  245. package/dist/components/KeyListener.d.ts +0 -15
  246. package/dist/components/KeyListener.js +0 -23
  247. package/dist/components/Knob.d.ts +0 -49
  248. package/dist/components/Knob.js +0 -162
  249. package/dist/components/LabeledControls.d.ts +0 -11
  250. package/dist/components/LabeledControls.js +0 -39
  251. package/dist/components/LabeledList.d.ts +0 -57
  252. package/dist/components/LabeledList.js +0 -94
  253. package/dist/components/MenuBar.d.ts +0 -28
  254. package/dist/components/MenuBar.js +0 -174
  255. package/dist/components/Modal.d.ts +0 -3
  256. package/dist/components/Modal.js +0 -25
  257. package/dist/components/NoticeBox.d.ts +0 -20
  258. package/dist/components/NoticeBox.js +0 -49
  259. package/dist/components/NumberInput.d.ts +0 -45
  260. package/dist/components/NumberInput.js +0 -221
  261. package/dist/components/Popper.d.ts +0 -27
  262. package/dist/components/Popper.js +0 -177
  263. package/dist/components/ProgressBar.d.ts +0 -46
  264. package/dist/components/ProgressBar.js +0 -37
  265. package/dist/components/RestrictedInput.js +0 -155
  266. package/dist/components/RoundGauge.d.ts +0 -53
  267. package/dist/components/RoundGauge.js +0 -147
  268. package/dist/components/Section.d.ts +0 -63
  269. package/dist/components/Section.js +0 -62
  270. package/dist/components/Slider.d.ts +0 -46
  271. package/dist/components/Slider.js +0 -124
  272. package/dist/components/Stack.d.ts +0 -27
  273. package/dist/components/Stack.js +0 -67
  274. package/dist/components/StyleableSection.d.ts +0 -11
  275. package/dist/components/StyleableSection.js +0 -16
  276. package/dist/components/Table.d.ts +0 -29
  277. package/dist/components/Table.js +0 -67
  278. package/dist/components/Tabs.d.ts +0 -23
  279. package/dist/components/Tabs.js +0 -89
  280. package/dist/components/TextArea.d.ts +0 -39
  281. package/dist/components/TextArea.js +0 -118
  282. package/dist/components/TimeDisplay.js +0 -34
  283. package/dist/components/Tooltip.d.ts +0 -29
  284. package/dist/components/Tooltip.js +0 -83
  285. package/dist/components/TrackOutsideClicks.d.ts +0 -13
  286. package/dist/components/TrackOutsideClicks.js +0 -24
  287. package/dist/components/VirtualList.d.ts +0 -8
  288. package/dist/components/VirtualList.js +0 -34
  289. package/dist/components/index.js +0 -92
  290. package/dist/popper-CiqSDJTE.js +0 -906
  291. /package/{dist/components/index.d.ts → lib/components/index.ts} +0 -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
+ function 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
+ function 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
+ function 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
+ function 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
+ function 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,180 @@
1
+ /**
2
+ * @file
3
+ * @copyright 2020 bobbahbrown (https://github.com/bobbahbrown)
4
+ * @license MIT
5
+ */
6
+ import { clamp01, keyOfMatchingRange, scale } from '../common/math';
7
+ import { classes } from '../common/react';
8
+ import styles from '../styles/components/RoundGauge.module.scss';
9
+ import { AnimatedNumber } from './AnimatedNumber';
10
+ import { Box, BoxProps, computeBoxClassName, computeBoxProps } from './Box';
11
+
12
+ type Props = {
13
+ /** The current value of the metric. */
14
+ value: number;
15
+ } & Partial<{
16
+ /** 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`. */
17
+ alertAfter: number;
18
+ /** 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. */
19
+ alertBefore: number;
20
+ /** CSS style. */
21
+ className: string;
22
+ /** When provided, will be used to format the value of the metric for display. */
23
+ format: (value: number) => string;
24
+ /** The upper bound of the gauge. */
25
+ maxValue: number;
26
+ /** The lower bound of the gauge. */
27
+ minValue: number;
28
+ /** Provide regions of the gauge to color between two specified values of the metric. */
29
+ ranges: Record<string, [number, number]>;
30
+ /** When provided scales the gauge. */
31
+ size: number;
32
+ /** Custom css */
33
+ style: React.CSSProperties;
34
+ }> &
35
+ BoxProps;
36
+
37
+ /**
38
+ * ## RoundGauge
39
+ * The RoundGauge component provides a visual representation of a single metric, as well as being capable of showing
40
+ * informational or cautionary boundaries related to that metric.
41
+ *
42
+ * @example
43
+ * ```tsx
44
+ * <RoundGauge
45
+ * size={1.75}
46
+ * value={tankPressure}
47
+ * minValue={0}
48
+ * maxValue={pressureLimit}
49
+ * alertAfter={pressureLimit * 0.7}
50
+ * ranges={{
51
+ * good: [0, pressureLimit * 0.7],
52
+ * average: [pressureLimit * 0.7, pressureLimit * 0.85],
53
+ * bad: [pressureLimit * 0.85, pressureLimit],
54
+ * }}
55
+ * format={formatPressure}
56
+ * />
57
+ * ```
58
+ *
59
+ * The alert on the gauge is optional, and will only be shown if the `alertAfter` prop is defined. When defined, the alert
60
+ * will begin to flash the respective color upon which the needle currently rests, as defined in the `ranges` prop.
61
+ *
62
+ */
63
+ export function RoundGauge(props: Props) {
64
+ const {
65
+ alertAfter,
66
+ alertBefore,
67
+ className,
68
+ format,
69
+ maxValue = 1,
70
+ minValue = 1,
71
+ ranges,
72
+ size = 1,
73
+ style,
74
+ value,
75
+ ...rest
76
+ } = props;
77
+
78
+ const scaledValue = scale(value, minValue, maxValue);
79
+ const clampedValue = clamp01(scaledValue);
80
+ const scaledRanges = ranges ? {} : { primary: [0, 1] };
81
+
82
+ if (ranges) {
83
+ Object.keys(ranges).forEach((x) => {
84
+ const range = ranges[x];
85
+ scaledRanges[x] = [
86
+ scale(range[0], minValue, maxValue),
87
+ scale(range[1], minValue, maxValue),
88
+ ];
89
+ });
90
+ }
91
+
92
+ function shouldShowAlert() {
93
+ // If both after and before alert props are set, and value is between them
94
+ if (
95
+ alertAfter &&
96
+ alertBefore &&
97
+ value > alertAfter &&
98
+ value < alertBefore
99
+ ) {
100
+ return true;
101
+ }
102
+ // If only alertAfter is set and value is greater than alertAfter
103
+ else if (alertAfter && value > alertAfter) {
104
+ return true;
105
+ }
106
+ // If only alertBefore is set and value is less than alertBefore
107
+ else if (alertBefore && value < alertBefore) {
108
+ return true;
109
+ }
110
+ // If none of the above conditions are met
111
+ return false;
112
+ }
113
+
114
+ const alertColor =
115
+ shouldShowAlert() && keyOfMatchingRange(clampedValue, scaledRanges);
116
+
117
+ return (
118
+ <Box inline>
119
+ <div
120
+ className={classes([
121
+ styles.roundGauge,
122
+ className,
123
+ computeBoxClassName(rest),
124
+ ])}
125
+ {...computeBoxProps({
126
+ style: {
127
+ fontSize: size + 'em',
128
+ ...style,
129
+ },
130
+ ...rest,
131
+ })}
132
+ >
133
+ <svg viewBox="0 0 100 50">
134
+ {(alertAfter || alertBefore) && (
135
+ <g
136
+ className={classes([
137
+ styles.alert,
138
+ alertColor ? styles['alert__' + alertColor] : '',
139
+ ])}
140
+ >
141
+ <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" />
142
+ </g>
143
+ )}
144
+ <g>
145
+ <circle className={styles.ringTrack} cx="50" cy="50" r="45" />
146
+ </g>
147
+ <g>
148
+ {Object.keys(scaledRanges).map((x, i) => {
149
+ const col_ranges = scaledRanges[x];
150
+ return (
151
+ <circle
152
+ className={classes([styles.ringFill, styles['color__' + x]])}
153
+ key={i}
154
+ style={{
155
+ strokeDashoffset: Math.max(
156
+ (2.0 - (col_ranges[1] - col_ranges[0])) * Math.PI * 50,
157
+ 0,
158
+ ),
159
+ }}
160
+ transform={`rotate(${180 + 180 * col_ranges[0]} 50 50)`}
161
+ cx="50"
162
+ cy="50"
163
+ r="45"
164
+ />
165
+ );
166
+ })}
167
+ </g>
168
+ <g
169
+ className={styles.needle}
170
+ transform={`rotate(${clampedValue * 180 - 90} 50 50)`}
171
+ >
172
+ <polygon className={styles.needleLine} points="46,50 50,0 54,50" />
173
+ <circle className={styles.needleMiddle} cx="50" cy="50" r="8" />
174
+ </g>
175
+ </svg>
176
+ </div>
177
+ <AnimatedNumber value={value} format={format} />
178
+ </Box>
179
+ );
180
+ }
@@ -0,0 +1,120 @@
1
+ import { forwardRef, ReactNode, RefObject, useEffect } from 'react';
2
+
3
+ import { addScrollableNode, removeScrollableNode } from '../common/events';
4
+ import { canRender, classes } from '../common/react';
5
+ import styles from '../styles/components/Section.module.scss';
6
+ import { BoxProps, computeBoxClassName, computeBoxProps } from './Box';
7
+
8
+ type Props = Partial<{
9
+ /** Buttons to render aside the section title. */
10
+ buttons: ReactNode;
11
+ /** id to assosiate with the parent div element used by this section, for uses with procs like getElementByID */
12
+ container_id: string;
13
+ /** If true, fills all available vertical space. */
14
+ fill: boolean;
15
+ /** If true, removes all section padding. */
16
+ fitted: boolean;
17
+ /** @member Callback function for the `scroll` event */
18
+ onScroll: ((this: GlobalEventHandlers, ev: Event) => any) | null;
19
+ /** Shows or hides the scrollbar. */
20
+ scrollable: boolean;
21
+ /** Shows or hides the horizontal scrollbar. */
22
+ scrollableHorizontal: boolean;
23
+ /** Title of the section. */
24
+ title: ReactNode;
25
+ }> &
26
+ BoxProps;
27
+
28
+ /**
29
+ * ## Section
30
+ * Section is a surface that displays content and actions on a single topic.
31
+ *
32
+ * They should be easy to scan for relevant and actionable information.
33
+ * Elements, like text and images, should be placed in them in a way that
34
+ * clearly indicates hierarchy.
35
+ *
36
+ * Sections can now be nested, and will automatically font size of the
37
+ * header according to their nesting level. Previously this was done via `level`
38
+ * prop, but now it is automatically calculated.
39
+ *
40
+ * Section can also be titled to clearly define its purpose.
41
+ *
42
+ * ```tsx
43
+ * <Section title="Cargo">Here you can order supply crates.</Section>
44
+ * ```
45
+ *
46
+ * If you want to have a button on the right side of an section title
47
+ * (for example, to perform some sort of action), there is a way to do that:
48
+ *
49
+ * ```tsx
50
+ * <Section title="Cargo" buttons={<Button>Send shuttle</Button>}>
51
+ * Here you can order supply crates.
52
+ * </Section>
53
+ * ```
54
+ */
55
+ export const Section = forwardRef(
56
+ (props: Props, forwardedRef: RefObject<HTMLDivElement>) => {
57
+ const {
58
+ buttons,
59
+ children,
60
+ className,
61
+ fill,
62
+ fitted,
63
+ onScroll,
64
+ scrollable,
65
+ scrollableHorizontal,
66
+ title,
67
+ container_id,
68
+ ...rest
69
+ } = props;
70
+
71
+ const hasTitle = canRender(title) || canRender(buttons);
72
+
73
+ /** We want to be able to scroll on hover, but using focus will steal it from inputs */
74
+ useEffect(() => {
75
+ if (!forwardedRef?.current) return;
76
+ if (!scrollable && !scrollableHorizontal) return;
77
+
78
+ addScrollableNode(forwardedRef.current);
79
+
80
+ return () => {
81
+ if (!forwardedRef?.current) return;
82
+ removeScrollableNode(forwardedRef.current!);
83
+ };
84
+ }, []);
85
+
86
+ return (
87
+ <div
88
+ id={container_id || ''}
89
+ className={classes([
90
+ styles.section,
91
+ fill && styles.fill,
92
+ fitted && styles.fitted,
93
+ scrollable && styles.scrollable,
94
+ scrollableHorizontal && styles.scrollableHorizontal,
95
+ className,
96
+ computeBoxClassName(rest),
97
+ ])}
98
+ {...computeBoxProps(rest)}
99
+ >
100
+ {hasTitle && (
101
+ <div className={styles.title}>
102
+ <span className={styles.titleText}>{title}</span>
103
+ <div className={styles.buttons}>{buttons}</div>
104
+ </div>
105
+ )}
106
+ <div className={styles.rest}>
107
+ <div
108
+ className={styles.content}
109
+ onScroll={onScroll}
110
+ // For posterity: the forwarded ref needs to be here specifically
111
+ // to actually let things interact with the scrolling.
112
+ ref={forwardedRef}
113
+ >
114
+ {children}
115
+ </div>
116
+ </div>
117
+ </div>
118
+ );
119
+ },
120
+ );