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

File tree

13 files changed

+1537
-121
lines changed

13 files changed

+1537
-121
lines changed

addons/van_jsx/package.json

Lines changed: 4 additions & 3 deletions
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",
Lines changed: 13 additions & 0 deletions
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;
Lines changed: 33 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 37 additions & 0 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 3 additions & 4 deletions
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";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from "./index";
12
export * from "./jsx-runtime";
Lines changed: 1 addition & 0 deletions
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

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)