Skip to content

Commit 426063f

Browse files
authored
Merge pull request #243 from cqh963852/refactor/van_jsx
refactor: update van_jsx
2 parents 2c6536e + 15d8649 commit 426063f

13 files changed

+1537
-121
lines changed

addons/van_jsx/package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "vanjs-jsx",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"type": "module",
5-
"types": "./src/index.d.ts",
65
"description": "jsx-runtime for vanjs",
6+
"types": "./src/index.d.ts",
77
"exports": {
88
".": {
99
"require": "./src/index.js",
10-
"import": "./src/index.js"
10+
"import": "./src/index.js",
11+
"types": "./src/index.d.ts"
1112
},
1213
"./jsx-runtime": {
1314
"require": "./src/jsx-runtime.js",

addons/van_jsx/src/createElement.d.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as CSS from "csstype";
2+
import { State } from "vanjs-core";
3+
import { Primitive } from "./type";
4+
export type VanElement = Element;
5+
export type JSXElementType<P> = (props: P) => VanNode | VanElement;
6+
export type PrimitiveChild = Primitive | State<Primitive>;
7+
export type VanNode = VanElement | PrimitiveChild | VanNode[] | (() => VanNode) | null;
8+
declare const createElement: (jsxTag: string | Function, { children, style, ref, ...props }: {
9+
children?: VanNode | undefined;
10+
style?: CSS.Properties<0 | (string & {}), string & {}> | (() => CSS.Properties) | undefined;
11+
ref?: State<Element> | undefined;
12+
}) => any;
13+
export default createElement;

addons/van_jsx/src/createElement.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import van from "vanjs-core";
2+
import { setAttribute } from "./hyper";
3+
const createElement = (jsxTag, { children, style, ref, ...props }) => {
4+
if (typeof jsxTag === "string") {
5+
// TODO VanNode to VanElement
6+
const ele = van.tags[jsxTag](children);
7+
for (const [key, value] of Object.entries(props ?? {})) {
8+
// Auto Update Attribute
9+
if (typeof value === "function" && !key.startsWith("on")) {
10+
van.derive(() => {
11+
let attr = value();
12+
setAttribute(ele, key, attr);
13+
});
14+
continue;
15+
}
16+
// Add Event Listener
17+
if (typeof value === "function" && key.startsWith("on")) {
18+
ele.addEventListener(key.replace("on", "").toLowerCase(), value);
19+
continue;
20+
}
21+
setAttribute(ele, key, value);
22+
continue;
23+
}
24+
if (ref != null) {
25+
ref.val = ele;
26+
}
27+
return ele;
28+
}
29+
if (typeof jsxTag === "function") {
30+
return jsxTag({ ...props, ref, style, children });
31+
}
32+
};
33+
export default createElement;

addons/van_jsx/src/hyper.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as CSS from "csstype";
2+
export declare const styleToString: (style: CSS.Properties) => string;
3+
export declare const setAttribute: (element: Element, key: string, value: unknown) => void;

addons/van_jsx/src/hyper.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export const styleToString = (style) => {
2+
return Object.entries(style).reduce((acc, key) => acc +
3+
key[0]
4+
.split(/(?=[A-Z])/)
5+
.join("-")
6+
.toLowerCase() +
7+
":" +
8+
key[1] +
9+
";", "");
10+
};
11+
export const setAttribute = (element, key, value) => {
12+
// Convert Style Object
13+
if (key === "style") {
14+
const attr = styleToString(value);
15+
element.setAttribute(key, attr);
16+
return;
17+
}
18+
if (typeof value === "number") {
19+
if (key === "tabIndex") {
20+
element.setAttribute("tabindex", value.toString());
21+
return;
22+
}
23+
}
24+
// Set String Attribute
25+
if (typeof value === "string") {
26+
if (key === "className") {
27+
element.setAttribute("class", value);
28+
return;
29+
}
30+
if (key === "htmlFor") {
31+
element.setAttribute("for", value);
32+
return;
33+
}
34+
element.setAttribute(key, value);
35+
return;
36+
}
37+
};

addons/van_jsx/src/index.d.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { State, StateView } from "vanjs-core";
2-
32
export declare function createState<T>(initialValue: T): State<T>;
43
export declare function createState<T>(initialValue: T | null): StateView<T>;
54
export declare function createState<T = undefined>(): State<T | undefined>;
6-
export * from "./jsx-runtime";
5+
export { default as createElement, default as jsx, default as jsxDEV, } from "./createElement";
76
export * from "./type";

addons/van_jsx/src/index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import van from "vanjs-core";
2-
32
export function createState(v) {
4-
return van.state(v);
3+
return van.state(v);
54
}
6-
7-
export * from "./jsx-runtime";
5+
export { default as createElement, default as jsx, default as jsxDEV, } from "./createElement";
6+
export * from "./type";
+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from "./index";
12
export * from "./jsx-runtime";

addons/van_jsx/src/jsx-dev-runtime.js

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from "./index";
12
export * from "./jsx-runtime";

addons/van_jsx/src/jsx-internal.d.ts

-31
This file was deleted.

addons/van_jsx/src/jsx-runtime.d.ts

+19-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
import * as CSS from "csstype";
2-
import { State, ChildDom } from "vanjs-core";
3-
4-
export declare const jsx: (
5-
jsxTag: string | Function,
6-
{
7-
children,
8-
style,
9-
ref,
10-
...props
11-
}: {
12-
children?: ChildDom;
13-
style?:
14-
| CSS.Properties<0 | (string & {}), string & {}>
15-
| (() => CSS.Properties)
16-
| undefined;
17-
ref?: State<Element> | undefined;
18-
}
19-
) => any;
20-
export { jsx as jsxDEV, jsx as jsxs };
21-
export type { JSX } from "./jsx-internal";
1+
import { JSXElementType, VanElement } from "./createElement";
2+
import { InnerElement, Key, TagOption } from "./type";
3+
export declare namespace JSX {
4+
type ElementType = string | JSXElementType<any>;
5+
interface ElementAttributesProperty {
6+
props: object;
7+
}
8+
interface ElementChildrenAttribute {
9+
children: object;
10+
}
11+
interface Element extends VanElement {
12+
}
13+
interface IntrinsicAttributes {
14+
key?: Key;
15+
}
16+
type IntrinsicElements = {
17+
[K in keyof InnerElement]: TagOption<K>;
18+
};
19+
}

addons/van_jsx/src/jsx-runtime.js

-49
This file was deleted.

0 commit comments

Comments
 (0)