@opentui/react 0.1.6
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.
- package/LICENSE +21 -0
- package/README.md +464 -0
- package/index.js +418 -0
- package/jsx-dev-runtime.d.ts +2 -0
- package/jsx-dev-runtime.js +1 -0
- package/jsx-namespace.d.ts +25 -0
- package/jsx-runtime.d.ts +2 -0
- package/jsx-runtime.js +1 -0
- package/package.json +50 -0
- package/src/components/app.d.ts +8 -0
- package/src/components/index.d.ts +27 -0
- package/src/hooks/use-keyboard.d.ts +2 -0
- package/src/hooks/use-renderer.d.ts +1 -0
- package/src/hooks/use-resize.d.ts +1 -0
- package/src/index.d.ts +8 -0
- package/src/reconciler/host-config.d.ts +9 -0
- package/src/reconciler/reconciler.d.ts +5 -0
- package/src/reconciler/renderer.d.ts +3 -0
- package/src/reconciler/renderer.js +374 -0
- package/src/types/components.d.ts +32 -0
- package/src/types/extend.d.ts +15 -0
- package/src/types/host.d.ts +9 -0
- package/src/utils/id.d.ts +2 -0
- package/src/utils/index.d.ts +3 -0
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/reconciler/renderer.ts
|
|
3
|
+
import { createCliRenderer, getKeyHandler } from "@opentui/core";
|
|
4
|
+
import React from "react";
|
|
5
|
+
|
|
6
|
+
// src/components/app.tsx
|
|
7
|
+
import { createContext, useContext } from "react";
|
|
8
|
+
var AppContext = createContext({
|
|
9
|
+
keyHandler: null,
|
|
10
|
+
renderer: null
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// src/reconciler/reconciler.ts
|
|
14
|
+
import ReactReconciler from "react-reconciler";
|
|
15
|
+
import { ConcurrentRoot } from "react-reconciler/constants";
|
|
16
|
+
|
|
17
|
+
// src/reconciler/host-config.ts
|
|
18
|
+
import { createContext as createContext2 } from "react";
|
|
19
|
+
import { DefaultEventPriority, NoEventPriority } from "react-reconciler/constants";
|
|
20
|
+
|
|
21
|
+
// src/components/index.ts
|
|
22
|
+
import {
|
|
23
|
+
BoxRenderable,
|
|
24
|
+
GroupRenderable,
|
|
25
|
+
InputRenderable,
|
|
26
|
+
SelectRenderable,
|
|
27
|
+
TabSelectRenderable,
|
|
28
|
+
TextRenderable
|
|
29
|
+
} from "@opentui/core";
|
|
30
|
+
var baseComponents = {
|
|
31
|
+
box: BoxRenderable,
|
|
32
|
+
text: TextRenderable,
|
|
33
|
+
group: GroupRenderable,
|
|
34
|
+
input: InputRenderable,
|
|
35
|
+
select: SelectRenderable,
|
|
36
|
+
"tab-select": TabSelectRenderable
|
|
37
|
+
};
|
|
38
|
+
var componentCatalogue = { ...baseComponents };
|
|
39
|
+
function getComponentCatalogue() {
|
|
40
|
+
return componentCatalogue;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// src/utils/id.ts
|
|
44
|
+
var idCounter = new Map;
|
|
45
|
+
function getNextId(type) {
|
|
46
|
+
if (!idCounter.has(type)) {
|
|
47
|
+
idCounter.set(type, 0);
|
|
48
|
+
}
|
|
49
|
+
const value = idCounter.get(type) + 1;
|
|
50
|
+
idCounter.set(type, value);
|
|
51
|
+
return `${type}-${value}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/utils/index.ts
|
|
55
|
+
import {
|
|
56
|
+
InputRenderable as InputRenderable2,
|
|
57
|
+
InputRenderableEvents,
|
|
58
|
+
SelectRenderable as SelectRenderable2,
|
|
59
|
+
SelectRenderableEvents,
|
|
60
|
+
StyledText,
|
|
61
|
+
TabSelectRenderable as TabSelectRenderable2,
|
|
62
|
+
TabSelectRenderableEvents,
|
|
63
|
+
TextRenderable as TextRenderable2,
|
|
64
|
+
stringToStyledText
|
|
65
|
+
} from "@opentui/core";
|
|
66
|
+
function initEventListeners(instance, eventName, listener, previousListener) {
|
|
67
|
+
if (previousListener) {
|
|
68
|
+
instance.off(eventName, previousListener);
|
|
69
|
+
}
|
|
70
|
+
if (listener) {
|
|
71
|
+
instance.on(eventName, listener);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function handleTextChildren(textInstance, children) {
|
|
75
|
+
if (children == null) {
|
|
76
|
+
textInstance.content = stringToStyledText("");
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (Array.isArray(children)) {
|
|
80
|
+
const chunks = [];
|
|
81
|
+
for (const child of children) {
|
|
82
|
+
if (typeof child === "string") {
|
|
83
|
+
chunks.push({
|
|
84
|
+
__isChunk: true,
|
|
85
|
+
text: new TextEncoder().encode(child),
|
|
86
|
+
plainText: child
|
|
87
|
+
});
|
|
88
|
+
} else if (child && typeof child === "object" && "__isChunk" in child) {
|
|
89
|
+
chunks.push(child);
|
|
90
|
+
} else if (child instanceof StyledText) {
|
|
91
|
+
chunks.push(...child.chunks);
|
|
92
|
+
} else if (child != null) {
|
|
93
|
+
const stringValue = String(child);
|
|
94
|
+
chunks.push({
|
|
95
|
+
__isChunk: true,
|
|
96
|
+
text: new TextEncoder().encode(stringValue),
|
|
97
|
+
plainText: stringValue
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
textInstance.content = new StyledText(chunks);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (typeof children === "string") {
|
|
105
|
+
textInstance.content = stringToStyledText(children);
|
|
106
|
+
} else if (children && typeof children === "object" && "__isChunk" in children) {
|
|
107
|
+
textInstance.content = new StyledText([children]);
|
|
108
|
+
} else if (children instanceof StyledText) {
|
|
109
|
+
textInstance.content = children;
|
|
110
|
+
} else {
|
|
111
|
+
textInstance.content = stringToStyledText(String(children));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function setStyle(instance, styles, oldStyles) {
|
|
115
|
+
if (styles && typeof styles === "object") {
|
|
116
|
+
if (oldStyles != null) {
|
|
117
|
+
for (const styleName in styles) {
|
|
118
|
+
const value = styles[styleName];
|
|
119
|
+
if (styles.hasOwnProperty(styleName) && oldStyles[styleName] !== value) {
|
|
120
|
+
instance[styleName] = value;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
for (const styleName in styles) {
|
|
125
|
+
if (styles.hasOwnProperty(styleName)) {
|
|
126
|
+
const value = styles[styleName];
|
|
127
|
+
instance[styleName] = value;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function setProperty(instance, type, propKey, propValue, oldPropValue) {
|
|
134
|
+
switch (propKey) {
|
|
135
|
+
case "onChange":
|
|
136
|
+
if (instance instanceof InputRenderable2) {
|
|
137
|
+
initEventListeners(instance, InputRenderableEvents.CHANGE, propValue, oldPropValue);
|
|
138
|
+
} else if (instance instanceof SelectRenderable2) {
|
|
139
|
+
initEventListeners(instance, SelectRenderableEvents.SELECTION_CHANGED, propValue, oldPropValue);
|
|
140
|
+
} else if (instance instanceof TabSelectRenderable2) {
|
|
141
|
+
initEventListeners(instance, TabSelectRenderableEvents.SELECTION_CHANGED, propValue, oldPropValue);
|
|
142
|
+
}
|
|
143
|
+
break;
|
|
144
|
+
case "onInput":
|
|
145
|
+
if (instance instanceof InputRenderable2) {
|
|
146
|
+
initEventListeners(instance, InputRenderableEvents.INPUT, propValue, oldPropValue);
|
|
147
|
+
}
|
|
148
|
+
break;
|
|
149
|
+
case "onSubmit":
|
|
150
|
+
if (instance instanceof InputRenderable2) {
|
|
151
|
+
initEventListeners(instance, InputRenderableEvents.ENTER, propValue, oldPropValue);
|
|
152
|
+
}
|
|
153
|
+
break;
|
|
154
|
+
case "onSelect":
|
|
155
|
+
if (instance instanceof SelectRenderable2) {
|
|
156
|
+
initEventListeners(instance, SelectRenderableEvents.ITEM_SELECTED, propValue, oldPropValue);
|
|
157
|
+
} else if (instance instanceof TabSelectRenderable2) {
|
|
158
|
+
initEventListeners(instance, TabSelectRenderableEvents.ITEM_SELECTED, propValue, oldPropValue);
|
|
159
|
+
}
|
|
160
|
+
break;
|
|
161
|
+
case "focused":
|
|
162
|
+
if (!!propValue) {
|
|
163
|
+
instance.focus();
|
|
164
|
+
} else {
|
|
165
|
+
instance.blur();
|
|
166
|
+
}
|
|
167
|
+
break;
|
|
168
|
+
case "style":
|
|
169
|
+
setStyle(instance, propValue, oldPropValue);
|
|
170
|
+
break;
|
|
171
|
+
case "children":
|
|
172
|
+
if (type === "text" && instance instanceof TextRenderable2) {
|
|
173
|
+
handleTextChildren(instance, propValue);
|
|
174
|
+
}
|
|
175
|
+
break;
|
|
176
|
+
default:
|
|
177
|
+
instance[propKey] = propValue;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function setInitialProperties(instance, type, props) {
|
|
181
|
+
for (const propKey in props) {
|
|
182
|
+
if (!props.hasOwnProperty(propKey)) {
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
const propValue = props[propKey];
|
|
186
|
+
if (propValue == null) {
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
setProperty(instance, type, propKey, propValue);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
function updateProperties(instance, type, oldProps, newProps) {
|
|
193
|
+
for (const propKey in oldProps) {
|
|
194
|
+
const oldProp = oldProps[propKey];
|
|
195
|
+
if (oldProps.hasOwnProperty(propKey) && oldProp != null && !newProps.hasOwnProperty(propKey)) {
|
|
196
|
+
setProperty(instance, type, propKey, null, oldProp);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
for (const propKey in newProps) {
|
|
200
|
+
const newProp = newProps[propKey];
|
|
201
|
+
const oldProp = oldProps[propKey];
|
|
202
|
+
if (newProps.hasOwnProperty(propKey) && newProp !== oldProp && (newProp != null || oldProp != null)) {
|
|
203
|
+
setProperty(instance, type, propKey, newProp, oldProp);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// src/reconciler/host-config.ts
|
|
209
|
+
var currentUpdatePriority = NoEventPriority;
|
|
210
|
+
var hostConfig = {
|
|
211
|
+
supportsMutation: true,
|
|
212
|
+
supportsPersistence: false,
|
|
213
|
+
supportsHydration: false,
|
|
214
|
+
createInstance(type, props, rootContainerInstance, hostContext) {
|
|
215
|
+
const id = getNextId(type);
|
|
216
|
+
const components = getComponentCatalogue();
|
|
217
|
+
if (!components[type]) {
|
|
218
|
+
throw new Error(`[Reconciler] Unknown component type: ${type}`);
|
|
219
|
+
}
|
|
220
|
+
return new components[type](id, {});
|
|
221
|
+
},
|
|
222
|
+
appendChild(parent, child) {
|
|
223
|
+
parent.add(child);
|
|
224
|
+
},
|
|
225
|
+
removeChild(parent, child) {
|
|
226
|
+
parent.remove(child.id);
|
|
227
|
+
},
|
|
228
|
+
insertBefore(parent, child, beforeChild) {
|
|
229
|
+
parent.insertBefore(child, beforeChild);
|
|
230
|
+
},
|
|
231
|
+
insertInContainerBefore(parent, child, beforeChild) {
|
|
232
|
+
parent.insertBefore(child, beforeChild);
|
|
233
|
+
},
|
|
234
|
+
removeChildFromContainer(parent, child) {
|
|
235
|
+
parent.remove(child.id);
|
|
236
|
+
},
|
|
237
|
+
prepareForCommit(containerInfo) {
|
|
238
|
+
return null;
|
|
239
|
+
},
|
|
240
|
+
resetAfterCommit(containerInfo) {
|
|
241
|
+
containerInfo.needsUpdate();
|
|
242
|
+
},
|
|
243
|
+
getRootHostContext(rootContainerInstance) {
|
|
244
|
+
return {};
|
|
245
|
+
},
|
|
246
|
+
getChildHostContext(parentHostContext, type, rootContainerInstance) {
|
|
247
|
+
return parentHostContext;
|
|
248
|
+
},
|
|
249
|
+
shouldSetTextContent(type, props) {
|
|
250
|
+
if (type === "text") {
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
return false;
|
|
254
|
+
},
|
|
255
|
+
createTextInstance(text, rootContainerInstance, hostContext) {
|
|
256
|
+
const components = getComponentCatalogue();
|
|
257
|
+
return new components["text"](getNextId("text"), {
|
|
258
|
+
content: text
|
|
259
|
+
});
|
|
260
|
+
},
|
|
261
|
+
scheduleTimeout: setTimeout,
|
|
262
|
+
cancelTimeout: clearTimeout,
|
|
263
|
+
noTimeout: -1,
|
|
264
|
+
shouldAttemptEagerTransition() {
|
|
265
|
+
return false;
|
|
266
|
+
},
|
|
267
|
+
finalizeInitialChildren(instance, type, props, rootContainerInstance, hostContext) {
|
|
268
|
+
setInitialProperties(instance, type, props);
|
|
269
|
+
return false;
|
|
270
|
+
},
|
|
271
|
+
commitMount(instance, type, props, internalInstanceHandle) {},
|
|
272
|
+
commitUpdate(instance, type, oldProps, newProps, internalInstanceHandle) {
|
|
273
|
+
updateProperties(instance, type, oldProps, newProps);
|
|
274
|
+
instance.needsUpdate();
|
|
275
|
+
},
|
|
276
|
+
commitTextUpdate(textInstance, oldText, newText) {
|
|
277
|
+
textInstance.content = newText;
|
|
278
|
+
textInstance.needsUpdate();
|
|
279
|
+
},
|
|
280
|
+
appendChildToContainer(container, child) {
|
|
281
|
+
container.add(child);
|
|
282
|
+
},
|
|
283
|
+
appendInitialChild(parent, child) {
|
|
284
|
+
parent.add(child);
|
|
285
|
+
},
|
|
286
|
+
hideInstance(instance) {
|
|
287
|
+
instance.visible = false;
|
|
288
|
+
instance.needsUpdate();
|
|
289
|
+
},
|
|
290
|
+
unhideInstance(instance, props) {
|
|
291
|
+
instance.visible = true;
|
|
292
|
+
instance.needsUpdate();
|
|
293
|
+
},
|
|
294
|
+
hideTextInstance(textInstance) {
|
|
295
|
+
textInstance.visible = false;
|
|
296
|
+
textInstance.needsUpdate();
|
|
297
|
+
},
|
|
298
|
+
unhideTextInstance(textInstance, text) {
|
|
299
|
+
textInstance.visible = true;
|
|
300
|
+
textInstance.needsUpdate();
|
|
301
|
+
},
|
|
302
|
+
clearContainer(container) {
|
|
303
|
+
const children = container.getChildren();
|
|
304
|
+
children.forEach((child) => container.remove(child.id));
|
|
305
|
+
},
|
|
306
|
+
setCurrentUpdatePriority(newPriority) {
|
|
307
|
+
currentUpdatePriority = newPriority;
|
|
308
|
+
},
|
|
309
|
+
getCurrentUpdatePriority: () => currentUpdatePriority,
|
|
310
|
+
resolveUpdatePriority() {
|
|
311
|
+
if (currentUpdatePriority !== NoEventPriority) {
|
|
312
|
+
return currentUpdatePriority;
|
|
313
|
+
}
|
|
314
|
+
return DefaultEventPriority;
|
|
315
|
+
},
|
|
316
|
+
maySuspendCommit() {
|
|
317
|
+
return false;
|
|
318
|
+
},
|
|
319
|
+
NotPendingTransition: null,
|
|
320
|
+
HostTransitionContext: createContext2(null),
|
|
321
|
+
resetFormInstance() {},
|
|
322
|
+
requestPostPaintCallback() {},
|
|
323
|
+
trackSchedulerEvent() {},
|
|
324
|
+
resolveEventType() {
|
|
325
|
+
return null;
|
|
326
|
+
},
|
|
327
|
+
resolveEventTimeStamp() {
|
|
328
|
+
return -1.1;
|
|
329
|
+
},
|
|
330
|
+
preloadInstance() {
|
|
331
|
+
return true;
|
|
332
|
+
},
|
|
333
|
+
startSuspendingCommit() {},
|
|
334
|
+
suspendInstance() {},
|
|
335
|
+
waitForCommitToBeReady() {
|
|
336
|
+
return null;
|
|
337
|
+
},
|
|
338
|
+
detachDeletedInstance(instance) {
|
|
339
|
+
if (!instance.parent) {
|
|
340
|
+
instance.destroy();
|
|
341
|
+
}
|
|
342
|
+
},
|
|
343
|
+
getPublicInstance(instance) {
|
|
344
|
+
return instance;
|
|
345
|
+
},
|
|
346
|
+
preparePortalMount(containerInfo) {},
|
|
347
|
+
isPrimaryRenderer: true,
|
|
348
|
+
getInstanceFromNode() {
|
|
349
|
+
return null;
|
|
350
|
+
},
|
|
351
|
+
beforeActiveInstanceBlur() {},
|
|
352
|
+
afterActiveInstanceBlur() {},
|
|
353
|
+
prepareScopeUpdate() {},
|
|
354
|
+
getInstanceFromScope() {
|
|
355
|
+
return null;
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
// src/reconciler/reconciler.ts
|
|
360
|
+
var reconciler = ReactReconciler(hostConfig);
|
|
361
|
+
function _render(element, root) {
|
|
362
|
+
const container = reconciler.createContainer(root, ConcurrentRoot, null, false, null, "", console.error, console.error, console.error, console.error, null);
|
|
363
|
+
reconciler.updateContainer(element, container, null, () => {});
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// src/reconciler/renderer.ts
|
|
367
|
+
var keyHandler = getKeyHandler();
|
|
368
|
+
async function render(node, rendererConfig = {}) {
|
|
369
|
+
const renderer = await createCliRenderer(rendererConfig);
|
|
370
|
+
_render(React.createElement(AppContext.Provider, { value: { keyHandler, renderer } }, node), renderer.root);
|
|
371
|
+
}
|
|
372
|
+
export {
|
|
373
|
+
render
|
|
374
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { BoxOptions, InputRenderableOptions, RenderableOptions, SelectOption, SelectRenderableOptions, StyledText, TabSelectOption, TabSelectRenderableOptions, TextChunk, TextOptions } from "@opentui/core";
|
|
2
|
+
import type React from "react";
|
|
3
|
+
type NonStyledProps = "buffered";
|
|
4
|
+
type ContainerProps<T> = T & {
|
|
5
|
+
children?: React.ReactNode;
|
|
6
|
+
};
|
|
7
|
+
type ComponentProps<T extends RenderableOptions, K extends keyof T = NonStyledProps> = T & {
|
|
8
|
+
style?: Partial<Omit<T, K | NonStyledProps>>;
|
|
9
|
+
};
|
|
10
|
+
type TextChildren = (string & {}) | number | boolean | null | undefined;
|
|
11
|
+
export type TextProps = ComponentProps<TextOptions, "content"> & {
|
|
12
|
+
children?: TextChildren | StyledText | TextChunk | Array<TextChildren | StyledText | TextChunk>;
|
|
13
|
+
};
|
|
14
|
+
export type BoxProps = ComponentProps<ContainerProps<BoxOptions>, "title">;
|
|
15
|
+
export type GroupProps = ComponentProps<ContainerProps<RenderableOptions>>;
|
|
16
|
+
export type InputProps = ComponentProps<InputRenderableOptions> & {
|
|
17
|
+
focused?: boolean;
|
|
18
|
+
onInput?: (value: string) => void;
|
|
19
|
+
onChange?: (value: string) => void;
|
|
20
|
+
onSubmit?: (value: string) => void;
|
|
21
|
+
};
|
|
22
|
+
export type SelectProps = ComponentProps<SelectRenderableOptions> & {
|
|
23
|
+
focused?: boolean;
|
|
24
|
+
onChange?: (index: number, option: SelectOption | null) => void;
|
|
25
|
+
onSelect?: (index: number, option: SelectOption | null) => void;
|
|
26
|
+
};
|
|
27
|
+
export type TabSelectProps = ComponentProps<TabSelectRenderableOptions> & {
|
|
28
|
+
focused?: boolean;
|
|
29
|
+
onChange?: (index: number, option: TabSelectOption | null) => void;
|
|
30
|
+
onSelect?: (index: number, option: TabSelectOption | null) => void;
|
|
31
|
+
};
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Renderable } from "@opentui/core";
|
|
2
|
+
import type React from "react";
|
|
3
|
+
export type RenderableConstructor<T extends Renderable = Renderable> = new (id: string, options: any) => T;
|
|
4
|
+
type ExtractRenderableOptions<T> = T extends new (id: string, options: infer O) => any ? O : never;
|
|
5
|
+
export type ExtendedComponentProps<T extends RenderableConstructor> = ExtractRenderableOptions<T> & {
|
|
6
|
+
children?: React.ReactNode;
|
|
7
|
+
style?: Partial<ExtractRenderableOptions<T>>;
|
|
8
|
+
};
|
|
9
|
+
export type ExtendedIntrinsicElements<T extends Record<string, RenderableConstructor>> = {
|
|
10
|
+
[K in keyof T]: ExtendedComponentProps<T[K]>;
|
|
11
|
+
};
|
|
12
|
+
export interface OpenTUIComponents {
|
|
13
|
+
[key: string]: RenderableConstructor;
|
|
14
|
+
}
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Renderable, RootRenderable, TextRenderable } from "@opentui/core";
|
|
2
|
+
import { baseComponents } from "../components";
|
|
3
|
+
export type Type = keyof typeof baseComponents;
|
|
4
|
+
export type Props = Record<string, any>;
|
|
5
|
+
export type Container = RootRenderable;
|
|
6
|
+
export type Instance = Renderable;
|
|
7
|
+
export type TextInstance = TextRenderable;
|
|
8
|
+
export type PublicInstance = Instance;
|
|
9
|
+
export type HostContext = Record<string, any>;
|