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,173 @@
1
+ /**
2
+ * @file
3
+ * @copyright 2020 Aleksej Komarov
4
+ * @license MIT
5
+ */
6
+
7
+ import { clamp01, keyOfMatchingRange, scale } from '../common/math';
8
+ import { classes } from '../common/react';
9
+ import { PropsWithChildren } from 'react';
10
+
11
+ import { BoxProps, computeBoxClassName, computeBoxProps } from './Box';
12
+ import { DraggableControl } from './DraggableControl';
13
+
14
+ type Props = {
15
+ /** Highest possible value. */
16
+ maxValue: number;
17
+ /** Lowest possible value. */
18
+ minValue: number;
19
+ /** Value itself, controls the position of the cursor. */
20
+ value: number;
21
+ } & Partial<{
22
+ /** Animates the value if it was changed externally. */
23
+ animated: boolean;
24
+ /** Custom css */
25
+ className: string;
26
+ /** Color of the slider. */
27
+ color: string;
28
+ /** If set, this value will be used to set the fill percentage of the progress bar filler independently of the main value. */
29
+ fillValue: number;
30
+ /** Format value using this function before displaying it. */
31
+ format: (value: number) => string;
32
+ /** Adjust value by this amount when dragging the input. */
33
+ onChange: (event: Event, value: number) => void;
34
+ /** An event, which fires when you release the input, or successfully enter a number. */
35
+ onDrag: (event: Event, value: number) => void;
36
+ /** Applies a `color` to the slider based on whether the value lands in the range between `from` and `to`. */
37
+ ranges: Record<string, [number, number]>;
38
+ /** Screen distance mouse needs to travel to adjust value by one `step`. */
39
+ step: number;
40
+ /** A number in milliseconds, for which the input will hold off from updating while events propagate through the backend. Default is about 250ms, increase it if you still see flickering. */
41
+ stepPixelSize: number;
42
+ /** Adjust value by this amount when dragging the input. */
43
+ suppressFlicker: boolean;
44
+ /** Unit to display to the right of value. */
45
+ unit: string;
46
+ }> &
47
+ BoxProps &
48
+ PropsWithChildren;
49
+
50
+ /**
51
+ * ## Slider
52
+ * A horizontal, progressbar-like control which allows dialing
53
+ * in precise values by dragging it left and right.
54
+ *
55
+ * Single click opens an input box to manually type in a number.
56
+ *
57
+ */
58
+ export function Slider(props: Props) {
59
+ const {
60
+ // Draggable props (passthrough)
61
+ animated,
62
+ format,
63
+ maxValue,
64
+ minValue,
65
+ onChange,
66
+ onDrag,
67
+ step,
68
+ stepPixelSize,
69
+ suppressFlicker,
70
+ unit,
71
+ value,
72
+ // Own props
73
+ className,
74
+ fillValue,
75
+ color,
76
+ ranges = {},
77
+ children,
78
+ ...rest
79
+ } = props;
80
+
81
+ const hasContent = children !== undefined;
82
+
83
+ return (
84
+ <DraggableControl
85
+ dragMatrix={[1, 0]}
86
+ {...{
87
+ animated,
88
+ format,
89
+ maxValue,
90
+ minValue,
91
+ onChange,
92
+ onDrag,
93
+ step,
94
+ stepPixelSize,
95
+ suppressFlicker,
96
+ unit,
97
+ value,
98
+ }}
99
+ >
100
+ {(control) => {
101
+ const {
102
+ displayElement,
103
+ displayValue,
104
+ dragging,
105
+ handleDragStart,
106
+ inputElement,
107
+ value,
108
+ } = control;
109
+
110
+ const hasFillValue = fillValue !== undefined && fillValue !== null;
111
+
112
+ const scaledFillValue = scale(
113
+ fillValue ?? displayValue,
114
+ minValue,
115
+ maxValue
116
+ );
117
+ const scaledDisplayValue = scale(displayValue, minValue, maxValue);
118
+
119
+ const effectiveColor =
120
+ color || keyOfMatchingRange(fillValue ?? value, ranges) || 'default';
121
+
122
+ return (
123
+ <div
124
+ className={classes([
125
+ 'Slider',
126
+ 'ProgressBar',
127
+ 'ProgressBar--color--' + effectiveColor,
128
+ className,
129
+ computeBoxClassName(rest),
130
+ ])}
131
+ {...computeBoxProps(rest)}
132
+ onMouseDown={handleDragStart}
133
+ >
134
+ <div
135
+ className={classes([
136
+ 'ProgressBar__fill',
137
+ hasFillValue && 'ProgressBar__fill--animated',
138
+ ])}
139
+ style={{
140
+ width: clamp01(scaledFillValue) * 100 + '%',
141
+ opacity: 0.4,
142
+ }}
143
+ />
144
+ <div
145
+ className="ProgressBar__fill"
146
+ style={{
147
+ width:
148
+ clamp01(Math.min(scaledFillValue, scaledDisplayValue)) * 100 +
149
+ '%',
150
+ }}
151
+ />
152
+ <div
153
+ className="Slider__cursorOffset"
154
+ style={{
155
+ width: clamp01(scaledDisplayValue) * 100 + '%',
156
+ }}
157
+ >
158
+ <div className="Slider__cursor" />
159
+ <div className="Slider__pointer" />
160
+ {dragging && (
161
+ <div className="Slider__popupValue">{displayElement}</div>
162
+ )}
163
+ </div>
164
+ <div className="ProgressBar__content">
165
+ {hasContent ? children : displayElement}
166
+ </div>
167
+ {inputElement}
168
+ </div>
169
+ );
170
+ }}
171
+ </DraggableControl>
172
+ );
173
+ }
@@ -0,0 +1,101 @@
1
+ /**
2
+ * @file
3
+ * @copyright 2021 Aleksej Komarov
4
+ * @license MIT
5
+ */
6
+
7
+ import { classes } from '../common/react';
8
+ import { RefObject } from 'react';
9
+
10
+ import {
11
+ computeFlexClassName,
12
+ computeFlexItemClassName,
13
+ computeFlexItemProps,
14
+ computeFlexProps,
15
+ FlexItemProps,
16
+ FlexProps,
17
+ } from './Flex';
18
+
19
+ type Props = Partial<{
20
+ /** Fills available space. */
21
+ fill: boolean;
22
+ /** Reverses the stack. */
23
+ reverse: boolean;
24
+ /** Flex column */
25
+ vertical: boolean;
26
+ /** Adds zebra striping to the stack. */
27
+ zebra: boolean;
28
+ }> &
29
+ FlexProps;
30
+
31
+ export function Stack(props: Props) {
32
+ const { className, vertical, fill, reverse, zebra, ...rest } = props;
33
+
34
+ const directionPrefix = vertical ? 'column' : 'row';
35
+ const directionSuffix = reverse ? '-reverse' : '';
36
+
37
+ return (
38
+ <div
39
+ className={classes([
40
+ 'Stack',
41
+ fill && 'Stack--fill',
42
+ vertical ? 'Stack--vertical' : 'Stack--horizontal',
43
+ zebra && 'Stack--zebra',
44
+ reverse && `Stack--reverse${vertical ? '--vertical' : ''}`,
45
+ className,
46
+ computeFlexClassName(props),
47
+ ])}
48
+ {...computeFlexProps({
49
+ direction: `${directionPrefix}${directionSuffix}`,
50
+ ...rest,
51
+ })}
52
+ />
53
+ );
54
+ }
55
+
56
+ type StackItemProps = FlexItemProps &
57
+ Partial<{
58
+ innerRef: RefObject<HTMLDivElement>;
59
+ }>;
60
+
61
+ function StackItem(props: StackItemProps) {
62
+ const { className, innerRef, ...rest } = props;
63
+
64
+ return (
65
+ <div
66
+ className={classes([
67
+ 'Stack__item',
68
+ className,
69
+ computeFlexItemClassName(rest),
70
+ ])}
71
+ ref={innerRef}
72
+ {...computeFlexItemProps(rest)}
73
+ />
74
+ );
75
+ }
76
+
77
+ Stack.Item = StackItem;
78
+
79
+ type StackDividerProps = FlexItemProps &
80
+ Partial<{
81
+ hidden: boolean;
82
+ }>;
83
+
84
+ function StackDivider(props: StackDividerProps) {
85
+ const { className, hidden, ...rest } = props;
86
+
87
+ return (
88
+ <div
89
+ className={classes([
90
+ 'Stack__item',
91
+ 'Stack__divider',
92
+ hidden && 'Stack__divider--hidden',
93
+ className,
94
+ computeFlexItemClassName(rest),
95
+ ])}
96
+ {...computeFlexItemProps(rest)}
97
+ />
98
+ );
99
+ }
100
+
101
+ Stack.Divider = StackDivider;
@@ -0,0 +1,30 @@
1
+ import { PropsWithChildren, ReactNode } from 'react';
2
+
3
+ import { Box } from './Box';
4
+
5
+ type Props = Partial<{
6
+ style: Record<string, any>;
7
+ titleStyle: Record<string, any>;
8
+ textStyle: Record<string, any>;
9
+ title: ReactNode;
10
+ titleSubtext: string;
11
+ }> &
12
+ PropsWithChildren;
13
+
14
+ // The cost of flexibility and prettiness.
15
+ export const StyleableSection = (props: Props) => {
16
+ return (
17
+ <Box style={props.style}>
18
+ {/* Yes, this box (line above) is missing the "Section" class. This is very intentional, as the layout looks *ugly* with it.*/}
19
+ <Box className="Section__title" style={props.titleStyle}>
20
+ <Box className="Section__titleText" style={props.textStyle}>
21
+ {props.title}
22
+ </Box>
23
+ <div className="Section__buttons">{props.titleSubtext}</div>
24
+ </Box>
25
+ <Box className="Section__rest">
26
+ <Box className="Section__content">{props.children}</Box>
27
+ </Box>
28
+ </Box>
29
+ );
30
+ };
@@ -0,0 +1,90 @@
1
+ /**
2
+ * @file
3
+ * @copyright 2020 Aleksej Komarov
4
+ * @license MIT
5
+ */
6
+
7
+ import { classes } from '../common/react';
8
+
9
+ import { BoxProps, computeBoxClassName, computeBoxProps } from './Box';
10
+
11
+ type Props = Partial<{
12
+ /** Collapses table to the smallest possible size. */
13
+ collapsing: boolean;
14
+ }> &
15
+ BoxProps;
16
+
17
+ export function Table(props: Props) {
18
+ const { className, collapsing, children, ...rest } = props;
19
+
20
+ return (
21
+ <table
22
+ className={classes([
23
+ 'Table',
24
+ collapsing && 'Table--collapsing',
25
+ className,
26
+ computeBoxClassName(rest),
27
+ ])}
28
+ {...computeBoxProps(rest)}
29
+ >
30
+ <tbody>{children}</tbody>
31
+ </table>
32
+ );
33
+ }
34
+
35
+ type RowProps = Partial<{
36
+ /** Whether this is a header cell. */
37
+ header: boolean;
38
+ }> &
39
+ BoxProps;
40
+
41
+ export function TableRow(props: RowProps) {
42
+ const { className, header, ...rest } = props;
43
+
44
+ return (
45
+ <tr
46
+ className={classes([
47
+ 'Table__row',
48
+ header && 'Table__row--header',
49
+ className,
50
+ computeBoxClassName(props),
51
+ ])}
52
+ {...computeBoxProps(rest)}
53
+ />
54
+ );
55
+ }
56
+
57
+ Table.Row = TableRow;
58
+
59
+ type CellProps = Partial<{
60
+ /** Collapses table cell to the smallest possible size,
61
+ and stops any text inside from wrapping. */
62
+ collapsing: boolean;
63
+ /** Additional columns for this cell to expand, assuming there is room. */
64
+ colSpan: number;
65
+ /** Whether this is a header cell. */
66
+ header: boolean;
67
+ /** Rows for this cell to expand, assuming there is room. */
68
+ rowSpan: number;
69
+ }> &
70
+ BoxProps;
71
+
72
+ export function TableCell(props: CellProps) {
73
+ const { className, collapsing, colSpan, header, ...rest } = props;
74
+
75
+ return (
76
+ <td
77
+ className={classes([
78
+ 'Table__cell',
79
+ collapsing && 'Table__cell--collapsing',
80
+ header && 'Table__cell--header',
81
+ className,
82
+ computeBoxClassName(props),
83
+ ])}
84
+ colSpan={colSpan}
85
+ {...computeBoxProps(rest)}
86
+ />
87
+ );
88
+ }
89
+
90
+ Table.Cell = TableCell;
@@ -0,0 +1,90 @@
1
+ /**
2
+ * @file
3
+ * @copyright 2020 Aleksej Komarov
4
+ * @license MIT
5
+ */
6
+
7
+ import { canRender, classes } from '../common/react';
8
+ import { PropsWithChildren, ReactNode } from 'react';
9
+
10
+ import { BoxProps, computeBoxClassName, computeBoxProps } from './Box';
11
+ import { Icon } from './Icon';
12
+
13
+ type Props = Partial<{
14
+ className: string;
15
+ fill: boolean;
16
+ fluid: boolean;
17
+ vertical: boolean;
18
+ }> &
19
+ BoxProps &
20
+ PropsWithChildren;
21
+
22
+ type TabProps = Partial<{
23
+ className: string;
24
+ color: string;
25
+ icon: string;
26
+ leftSlot: ReactNode;
27
+ onClick: (e?) => void;
28
+ rightSlot: ReactNode;
29
+ selected: boolean;
30
+ }> &
31
+ BoxProps &
32
+ PropsWithChildren;
33
+
34
+ export const Tabs = (props: Props) => {
35
+ const { className, vertical, fill, fluid, children, ...rest } = props;
36
+
37
+ return (
38
+ <div
39
+ className={classes([
40
+ 'Tabs',
41
+ vertical ? 'Tabs--vertical' : 'Tabs--horizontal',
42
+ fill && 'Tabs--fill',
43
+ fluid && 'Tabs--fluid',
44
+ className,
45
+ computeBoxClassName(rest),
46
+ ])}
47
+ {...computeBoxProps(rest)}
48
+ >
49
+ {children}
50
+ </div>
51
+ );
52
+ };
53
+
54
+ const Tab = (props: TabProps) => {
55
+ const {
56
+ className,
57
+ selected,
58
+ color,
59
+ icon,
60
+ leftSlot,
61
+ rightSlot,
62
+ children,
63
+ ...rest
64
+ } = props;
65
+
66
+ return (
67
+ <div
68
+ className={classes([
69
+ 'Tab',
70
+ 'Tabs__Tab',
71
+ 'Tab--color--' + color,
72
+ selected && 'Tab--selected',
73
+ className,
74
+ computeBoxClassName(rest),
75
+ ])}
76
+ {...computeBoxProps(rest)}
77
+ >
78
+ {(canRender(leftSlot) && <div className="Tab__left">{leftSlot}</div>) ||
79
+ (!!icon && (
80
+ <div className="Tab__left">
81
+ <Icon name={icon} />
82
+ </div>
83
+ ))}
84
+ <div className="Tab__text">{children}</div>
85
+ {canRender(rightSlot) && <div className="Tab__right">{rightSlot}</div>}
86
+ </div>
87
+ );
88
+ };
89
+
90
+ Tabs.Tab = Tab;
@@ -0,0 +1,198 @@
1
+ /**
2
+ * @file
3
+ * @copyright 2020 Aleksej Komarov
4
+ * @author Warlockd
5
+ * @license MIT
6
+ */
7
+
8
+ import { KEY } from '../common/keys';
9
+ import { classes } from '../common/react';
10
+ import {
11
+ forwardRef,
12
+ RefObject,
13
+ useEffect,
14
+ useImperativeHandle,
15
+ useRef,
16
+ useState,
17
+ } from 'react';
18
+ import { KeyboardEvent, SyntheticEvent } from 'react';
19
+
20
+ import { Box, BoxProps } from './Box';
21
+ import { toInputValue } from './Input';
22
+
23
+ type Props = Partial<{
24
+ autoFocus: boolean;
25
+ autoSelect: boolean;
26
+ displayedValue: string;
27
+ dontUseTabForIndent: boolean;
28
+ fluid: boolean;
29
+ maxLength: number;
30
+ noborder: boolean;
31
+ /** Fires when user is 'done typing': Clicked out, blur, enter key (but not shift+enter) */
32
+ onChange: (event: SyntheticEvent<HTMLTextAreaElement>, value: string) => void;
33
+ /** Fires once the enter key is pressed */
34
+ onEnter: (event: SyntheticEvent<HTMLTextAreaElement>, value: string) => void;
35
+ /** Fires once the escape key is pressed */
36
+ onEscape: (event: SyntheticEvent<HTMLTextAreaElement>) => void;
37
+ /** Fires on each key press / value change. Used for searching */
38
+ onInput: (event: SyntheticEvent<HTMLTextAreaElement>, value: string) => void;
39
+ placeholder: string;
40
+ scrollbar: boolean;
41
+ selfClear: boolean;
42
+ value: string;
43
+ }> &
44
+ BoxProps;
45
+
46
+ export const TextArea = forwardRef(
47
+ (props: Props, forwardedRef: RefObject<HTMLTextAreaElement>) => {
48
+ const {
49
+ autoFocus,
50
+ autoSelect,
51
+ displayedValue,
52
+ dontUseTabForIndent,
53
+ maxLength,
54
+ noborder,
55
+ onChange,
56
+ onEnter,
57
+ onEscape,
58
+ onInput,
59
+ placeholder,
60
+ scrollbar,
61
+ selfClear,
62
+ value,
63
+ ...boxProps
64
+ } = props;
65
+ const { className, fluid, nowrap, ...rest } = boxProps;
66
+
67
+ const textareaRef = useRef<HTMLTextAreaElement>(null);
68
+ const [scrolledAmount, setScrolledAmount] = useState(0);
69
+
70
+ const handleKeyDown = (event: KeyboardEvent<HTMLTextAreaElement>) => {
71
+ if (event.key === KEY.Enter) {
72
+ if (event.shiftKey) {
73
+ event.currentTarget.focus();
74
+ return;
75
+ }
76
+
77
+ onEnter?.(event, event.currentTarget.value);
78
+ if (selfClear) {
79
+ event.currentTarget.value = '';
80
+ }
81
+ event.currentTarget.blur();
82
+ return;
83
+ }
84
+
85
+ if (event.key === KEY.Escape) {
86
+ onEscape?.(event);
87
+ if (selfClear) {
88
+ event.currentTarget.value = '';
89
+ } else {
90
+ event.currentTarget.value = toInputValue(value);
91
+ event.currentTarget.blur();
92
+ }
93
+
94
+ return;
95
+ }
96
+
97
+ if (!dontUseTabForIndent && event.key === KEY.Tab) {
98
+ event.preventDefault();
99
+ const { value, selectionStart, selectionEnd } = event.currentTarget;
100
+ event.currentTarget.value =
101
+ value.substring(0, selectionStart) +
102
+ '\t' +
103
+ value.substring(selectionEnd);
104
+ event.currentTarget.selectionEnd = selectionStart + 1;
105
+ }
106
+ };
107
+
108
+ useImperativeHandle(
109
+ forwardedRef,
110
+ () => textareaRef.current as HTMLTextAreaElement
111
+ );
112
+
113
+ /** Focuses the input on mount */
114
+ useEffect(() => {
115
+ if (!autoFocus && !autoSelect) return;
116
+
117
+ const input = textareaRef.current;
118
+ if (!input) return;
119
+
120
+ if (autoFocus || autoSelect) {
121
+ setTimeout(() => {
122
+ input.focus();
123
+
124
+ if (autoSelect) {
125
+ input.select();
126
+ }
127
+ }, 1);
128
+ }
129
+ }, []);
130
+
131
+ /** Updates the initial value on props change */
132
+ useEffect(() => {
133
+ const input = textareaRef.current;
134
+ if (!input) return;
135
+
136
+ const newValue = toInputValue(value);
137
+ if (input.value === newValue) return;
138
+
139
+ input.value = newValue;
140
+ }, [value]);
141
+
142
+ return (
143
+ <Box
144
+ className={classes([
145
+ 'TextArea',
146
+ fluid && 'TextArea--fluid',
147
+ noborder && 'TextArea--noborder',
148
+ className,
149
+ ])}
150
+ {...rest}
151
+ >
152
+ {!!displayedValue && (
153
+ <div
154
+ style={{
155
+ height: '100%',
156
+ overflow: 'hidden',
157
+ position: 'absolute',
158
+ width: '100%',
159
+ }}
160
+ >
161
+ <div
162
+ className={classes([
163
+ 'TextArea__textarea',
164
+ 'TextArea__textarea_custom',
165
+ ])}
166
+ style={{
167
+ transform: `translateY(-${scrolledAmount}px)`,
168
+ }}
169
+ >
170
+ {displayedValue}
171
+ </div>
172
+ </div>
173
+ )}
174
+ <textarea
175
+ className={classes([
176
+ 'TextArea__textarea',
177
+ scrollbar && 'TextArea__textarea--scrollable',
178
+ nowrap && 'TextArea__nowrap',
179
+ ])}
180
+ maxLength={maxLength}
181
+ onBlur={(event) => onChange?.(event, event.target.value)}
182
+ onChange={(event) => onInput?.(event, event.target.value)}
183
+ onKeyDown={handleKeyDown}
184
+ onScroll={() => {
185
+ if (displayedValue && textareaRef.current) {
186
+ setScrolledAmount(textareaRef.current.scrollTop);
187
+ }
188
+ }}
189
+ placeholder={placeholder}
190
+ ref={textareaRef}
191
+ style={{
192
+ color: displayedValue ? 'rgba(0, 0, 0, 0)' : 'inherit',
193
+ }}
194
+ />
195
+ </Box>
196
+ );
197
+ }
198
+ );