@bindtty/jsx-runtime 0.1.0-alpha.1

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/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @bindtty/jsx-runtime
2
+
3
+ JSX runtime for BindTTY.
4
+
5
+ It converts TypeScript automatic JSX runtime calls into `@bindtty/vnode` `ViewTemplate` values.
@@ -0,0 +1,4 @@
1
+ export { Fragment, jsx, jsxs } from "./jsx-runtime.js";
2
+ export { jsxDEV } from "./jsx-dev-runtime.js";
3
+ export type { JSX } from "./jsx-runtime.js";
4
+ export type { ElementProps, ForProps, JsxProps, JsxType, ShowProps } from "./types.js";
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { Fragment, jsx, jsxs } from "./jsx-runtime.js";
2
+ export { jsxDEV } from "./jsx-dev-runtime.js";
@@ -0,0 +1,3 @@
1
+ export { Fragment, jsx, jsxs } from "./jsx-runtime.js";
2
+ export { jsx as jsxDEV } from "./jsx-runtime.js";
3
+ export type { JSX } from "./jsx-runtime.js";
@@ -0,0 +1,2 @@
1
+ export { Fragment, jsx, jsxs } from "./jsx-runtime.js";
2
+ export { jsx as jsxDEV } from "./jsx-runtime.js";
@@ -0,0 +1,113 @@
1
+ import type { PublicTextWrapMode } from "@bindtty/text";
2
+ import type { BindingValue, MountedElementRefHandler, Template, TemplateChildren } from "@bindtty/vnode";
3
+ import type { JsxProps, JsxType } from "./types.js";
4
+ export declare const Fragment: unique symbol;
5
+ type InteractionFocusChangeReason = "initial" | "next" | "previous" | "programmatic" | "clear" | "refresh";
6
+ interface InteractionNodeFocusChangeEvent {
7
+ id: string;
8
+ node: unknown;
9
+ focused: boolean;
10
+ reason: InteractionFocusChangeReason;
11
+ }
12
+ interface InteractionKeyEvent {
13
+ input: string;
14
+ name?: string;
15
+ ctrl: boolean;
16
+ meta: boolean;
17
+ shift: boolean;
18
+ sequence?: string;
19
+ }
20
+ interface InteractionKeyContext {
21
+ node: unknown;
22
+ isFocused: true;
23
+ }
24
+ type InteractionKeyHandler = (event: InteractionKeyEvent, context: InteractionKeyContext) => boolean | void;
25
+ type InteractionKeyBinding = boolean | InteractionKeyHandler | null | undefined;
26
+ interface IntrinsicInteractionProps {
27
+ id?: BindingValue<string | number>;
28
+ ref?: MountedElementRefHandler | null | undefined;
29
+ onKey?: BindingValue<InteractionKeyBinding>;
30
+ onFocusChange?: (event: InteractionNodeFocusChangeEvent) => void;
31
+ }
32
+ interface IntrinsicPaintProps {
33
+ color?: BindingValue<string>;
34
+ background?: BindingValue<string>;
35
+ bold?: BindingValue<boolean>;
36
+ }
37
+ type IntrinsicYogaAlignItems = "stretch" | "flex-start" | "center" | "flex-end" | "baseline";
38
+ type IntrinsicYogaJustifyContent = "flex-start" | "center" | "flex-end" | "space-between" | "space-around" | "space-evenly";
39
+ type IntrinsicYogaFlexWrap = "nowrap" | "wrap" | "wrap-reverse";
40
+ interface IntrinsicYogaItemProps {
41
+ flexGrow?: BindingValue<number>;
42
+ "flex-grow"?: BindingValue<number>;
43
+ flexShrink?: BindingValue<number>;
44
+ "flex-shrink"?: BindingValue<number>;
45
+ }
46
+ interface IntrinsicYogaContainerProps {
47
+ gap?: BindingValue<number>;
48
+ flexWrap?: BindingValue<IntrinsicYogaFlexWrap>;
49
+ "flex-wrap"?: BindingValue<IntrinsicYogaFlexWrap>;
50
+ alignItems?: BindingValue<IntrinsicYogaAlignItems>;
51
+ "align-items"?: BindingValue<IntrinsicYogaAlignItems>;
52
+ justifyContent?: BindingValue<IntrinsicYogaJustifyContent>;
53
+ "justify-content"?: BindingValue<IntrinsicYogaJustifyContent>;
54
+ }
55
+ interface IntrinsicBoxStyleProps {
56
+ border?: BindingValue<boolean | number>;
57
+ padding?: BindingValue<number>;
58
+ height?: BindingValue<number>;
59
+ width?: BindingValue<number>;
60
+ overflow?: BindingValue<"visible" | "clip">;
61
+ scrollX?: BindingValue<number>;
62
+ scrollY?: BindingValue<number>;
63
+ }
64
+ export declare function jsx(type: JsxType, rawProps: JsxProps | null, jsxKey?: unknown): Template;
65
+ export declare const jsxs: typeof jsx;
66
+ export declare namespace JSX {
67
+ type Element = Template;
68
+ interface IntrinsicElements {
69
+ screen: IntrinsicInteractionProps & IntrinsicYogaItemProps & IntrinsicYogaContainerProps & {
70
+ children?: TemplateChildren;
71
+ };
72
+ box: IntrinsicInteractionProps & IntrinsicBoxStyleProps & IntrinsicPaintProps & IntrinsicYogaItemProps & IntrinsicYogaContainerProps & {
73
+ children?: TemplateChildren;
74
+ };
75
+ vstack: IntrinsicInteractionProps & IntrinsicYogaItemProps & IntrinsicYogaContainerProps & {
76
+ children?: TemplateChildren;
77
+ };
78
+ hstack: IntrinsicInteractionProps & IntrinsicYogaItemProps & IntrinsicYogaContainerProps & {
79
+ children?: TemplateChildren;
80
+ };
81
+ text: IntrinsicInteractionProps & IntrinsicPaintProps & IntrinsicYogaItemProps & {
82
+ value: BindingValue<string | number>;
83
+ wrap?: BindingValue<PublicTextWrapMode>;
84
+ children?: never;
85
+ };
86
+ button: IntrinsicInteractionProps & IntrinsicYogaItemProps & {
87
+ value: BindingValue<string | number>;
88
+ disabled?: BindingValue<boolean>;
89
+ onPress?: () => void;
90
+ children?: never;
91
+ };
92
+ input: IntrinsicInteractionProps & IntrinsicYogaItemProps & {
93
+ value?: BindingValue<string>;
94
+ placeholder?: BindingValue<string>;
95
+ children?: never;
96
+ };
97
+ spacer: IntrinsicInteractionProps & IntrinsicYogaItemProps & {
98
+ size?: BindingValue<number>;
99
+ children?: never;
100
+ };
101
+ show: {
102
+ when: BindingValue<boolean>;
103
+ fallback?: Template;
104
+ children?: TemplateChildren;
105
+ };
106
+ for: {
107
+ each: BindingValue<readonly unknown[]>;
108
+ key?: (item: unknown, index: number) => string | number;
109
+ children: (item: unknown, index: number) => Template;
110
+ };
111
+ }
112
+ }
113
+ export {};
@@ -0,0 +1,70 @@
1
+ import { componentTemplate, elementSchemas, elementTemplate, forTemplate, fragmentTemplate, showTemplate } from "@bindtty/vnode";
2
+ export const Fragment = Symbol.for("bindtty.fragment");
3
+ export function jsx(type, rawProps, jsxKey) {
4
+ const props = normalizeProps(rawProps);
5
+ if (type === Fragment) {
6
+ return fragmentTemplate(props.children);
7
+ }
8
+ if (typeof type === "function") {
9
+ return componentTemplate(type, props);
10
+ }
11
+ if (type === "show") {
12
+ return createShowTemplate(props);
13
+ }
14
+ if (type === "for") {
15
+ if (jsxKey !== undefined && !("key" in props)) {
16
+ props.key = jsxKey;
17
+ }
18
+ return createForTemplate(props);
19
+ }
20
+ if (typeof type === "string") {
21
+ return createElementTemplate(type, props);
22
+ }
23
+ throw new TypeError("Unsupported JSX type.");
24
+ }
25
+ export const jsxs = jsx;
26
+ function normalizeProps(rawProps) {
27
+ return rawProps ? { ...rawProps } : {};
28
+ }
29
+ function takeChildren(props) {
30
+ const children = props.children;
31
+ delete props.children;
32
+ return children;
33
+ }
34
+ function createElementTemplate(type, props) {
35
+ if (!isIntrinsicElementTag(type)) {
36
+ throw new TypeError(`Unknown intrinsic element <${type}>.`);
37
+ }
38
+ const children = takeChildren(props);
39
+ return elementTemplate(type, props, children);
40
+ }
41
+ function createShowTemplate(props) {
42
+ if (!("when" in props)) {
43
+ throw new TypeError("<show> requires prop \"when\".");
44
+ }
45
+ const showProps = props;
46
+ const children = takeChildren(showProps);
47
+ return showTemplate({
48
+ when: showProps.when,
49
+ fallback: showProps.fallback,
50
+ children: children
51
+ });
52
+ }
53
+ function createForTemplate(props) {
54
+ if (!("each" in props)) {
55
+ throw new TypeError("<for> requires prop \"each\".");
56
+ }
57
+ const forProps = props;
58
+ const children = takeChildren(forProps);
59
+ if (typeof children !== "function") {
60
+ throw new TypeError("<for> children must be a render function.");
61
+ }
62
+ return forTemplate({
63
+ each: forProps.each,
64
+ key: forProps.key,
65
+ renderItem: children
66
+ });
67
+ }
68
+ function isIntrinsicElementTag(type) {
69
+ return type in elementSchemas;
70
+ }
@@ -0,0 +1,16 @@
1
+ import type { BindingValue, Template, TemplateChildren, TemplateProps } from "@bindtty/vnode";
2
+ export type JsxType = string | symbol | ((props: Record<string, unknown>) => Template);
3
+ export type JsxProps = Record<string, unknown>;
4
+ export interface ShowProps extends JsxProps {
5
+ when: BindingValue<boolean>;
6
+ fallback?: Template;
7
+ children?: TemplateChildren;
8
+ }
9
+ export interface ForProps<T = unknown> extends JsxProps {
10
+ each: BindingValue<readonly T[]>;
11
+ key?: (item: T, index: number) => string | number;
12
+ children: (item: T, index: number) => Template;
13
+ }
14
+ export type ElementProps = TemplateProps & {
15
+ children?: TemplateChildren;
16
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@bindtty/jsx-runtime",
3
+ "version": "0.1.0-alpha.1",
4
+ "type": "module",
5
+ "description": "JSX runtime for BindTTY ViewTemplate.",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ },
13
+ "./jsx-runtime": {
14
+ "types": "./dist/jsx-runtime.d.ts",
15
+ "import": "./dist/jsx-runtime.js"
16
+ },
17
+ "./jsx-dev-runtime": {
18
+ "types": "./dist/jsx-dev-runtime.d.ts",
19
+ "import": "./dist/jsx-dev-runtime.js"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "README.md"
25
+ ],
26
+ "scripts": {
27
+ "build": "tsc -p tsconfig.json",
28
+ "prepublishOnly": "npm run build",
29
+ "test": "npm run build --workspace @bindtty/text && npm run build --workspace @bindtty/vnode && npm run build && tsc -p test/tsconfig.json && tsc -p test/tsconfig.runtime.json && node --test test/dist/*.test.js"
30
+ },
31
+ "dependencies": {
32
+ "@bindtty/text": "0.1.0-alpha.1",
33
+ "@bindtty/vnode": "0.1.0-alpha.1"
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": "^22.0.0",
37
+ "typescript": "^5.5.0"
38
+ },
39
+ "engines": {
40
+ "node": ">=18"
41
+ },
42
+ "publishConfig": {
43
+ "access": "public"
44
+ },
45
+ "license": "MIT",
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "git+https://github.com/lithdoo/BindTTY.git",
49
+ "directory": "packages/jsx-runtime"
50
+ },
51
+ "bugs": {
52
+ "url": "https://github.com/lithdoo/BindTTY/issues"
53
+ },
54
+ "homepage": "https://github.com/lithdoo/BindTTY/tree/main/packages/jsx-runtime#readme"
55
+ }