Skip to content

Commit

Permalink
Merge pull request #243 from cqh963852/refactor/van_jsx
Browse files Browse the repository at this point in the history
refactor: update van_jsx
  • Loading branch information
Tao-VanJS authored Jan 20, 2024
2 parents 2c6536e + 15d8649 commit 426063f
Show file tree
Hide file tree
Showing 13 changed files with 1,537 additions and 121 deletions.
7 changes: 4 additions & 3 deletions addons/van_jsx/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "vanjs-jsx",
"version": "0.1.0",
"version": "0.2.0",
"type": "module",
"types": "./src/index.d.ts",
"description": "jsx-runtime for vanjs",
"types": "./src/index.d.ts",
"exports": {
".": {
"require": "./src/index.js",
"import": "./src/index.js"
"import": "./src/index.js",
"types": "./src/index.d.ts"
},
"./jsx-runtime": {
"require": "./src/jsx-runtime.js",
Expand Down
13 changes: 13 additions & 0 deletions addons/van_jsx/src/createElement.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as CSS from "csstype";
import { State } from "vanjs-core";
import { Primitive } from "./type";
export type VanElement = Element;
export type JSXElementType<P> = (props: P) => VanNode | VanElement;
export type PrimitiveChild = Primitive | State<Primitive>;
export type VanNode = VanElement | PrimitiveChild | VanNode[] | (() => VanNode) | null;
declare const createElement: (jsxTag: string | Function, { children, style, ref, ...props }: {
children?: VanNode | undefined;
style?: CSS.Properties<0 | (string & {}), string & {}> | (() => CSS.Properties) | undefined;
ref?: State<Element> | undefined;
}) => any;
export default createElement;
33 changes: 33 additions & 0 deletions addons/van_jsx/src/createElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import van from "vanjs-core";
import { setAttribute } from "./hyper";
const createElement = (jsxTag, { children, style, ref, ...props }) => {
if (typeof jsxTag === "string") {
// TODO VanNode to VanElement
const ele = van.tags[jsxTag](children);
for (const [key, value] of Object.entries(props ?? {})) {
// Auto Update Attribute
if (typeof value === "function" && !key.startsWith("on")) {
van.derive(() => {
let attr = value();
setAttribute(ele, key, attr);
});
continue;
}
// Add Event Listener
if (typeof value === "function" && key.startsWith("on")) {
ele.addEventListener(key.replace("on", "").toLowerCase(), value);
continue;
}
setAttribute(ele, key, value);
continue;
}
if (ref != null) {
ref.val = ele;
}
return ele;
}
if (typeof jsxTag === "function") {
return jsxTag({ ...props, ref, style, children });
}
};
export default createElement;
3 changes: 3 additions & 0 deletions addons/van_jsx/src/hyper.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as CSS from "csstype";
export declare const styleToString: (style: CSS.Properties) => string;
export declare const setAttribute: (element: Element, key: string, value: unknown) => void;
37 changes: 37 additions & 0 deletions addons/van_jsx/src/hyper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export const styleToString = (style) => {
return Object.entries(style).reduce((acc, key) => acc +
key[0]
.split(/(?=[A-Z])/)
.join("-")
.toLowerCase() +
":" +
key[1] +
";", "");
};
export const setAttribute = (element, key, value) => {
// Convert Style Object
if (key === "style") {
const attr = styleToString(value);
element.setAttribute(key, attr);
return;
}
if (typeof value === "number") {
if (key === "tabIndex") {
element.setAttribute("tabindex", value.toString());
return;
}
}
// Set String Attribute
if (typeof value === "string") {
if (key === "className") {
element.setAttribute("class", value);
return;
}
if (key === "htmlFor") {
element.setAttribute("for", value);
return;
}
element.setAttribute(key, value);
return;
}
};
3 changes: 1 addition & 2 deletions addons/van_jsx/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { State, StateView } from "vanjs-core";

export declare function createState<T>(initialValue: T): State<T>;
export declare function createState<T>(initialValue: T | null): StateView<T>;
export declare function createState<T = undefined>(): State<T | undefined>;
export * from "./jsx-runtime";
export { default as createElement, default as jsx, default as jsxDEV, } from "./createElement";
export * from "./type";
7 changes: 3 additions & 4 deletions addons/van_jsx/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import van from "vanjs-core";

export function createState(v) {
return van.state(v);
return van.state(v);
}

export * from "./jsx-runtime";
export { default as createElement, default as jsx, default as jsxDEV, } from "./createElement";
export * from "./type";
1 change: 1 addition & 0 deletions addons/van_jsx/src/jsx-dev-runtime.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./index";
export * from "./jsx-runtime";
1 change: 1 addition & 0 deletions addons/van_jsx/src/jsx-dev-runtime.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./index";
export * from "./jsx-runtime";
31 changes: 0 additions & 31 deletions addons/van_jsx/src/jsx-internal.d.ts

This file was deleted.

40 changes: 19 additions & 21 deletions addons/van_jsx/src/jsx-runtime.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import * as CSS from "csstype";
import { State, ChildDom } from "vanjs-core";

export declare const jsx: (
jsxTag: string | Function,
{
children,
style,
ref,
...props
}: {
children?: ChildDom;
style?:
| CSS.Properties<0 | (string & {}), string & {}>
| (() => CSS.Properties)
| undefined;
ref?: State<Element> | undefined;
}
) => any;
export { jsx as jsxDEV, jsx as jsxs };
export type { JSX } from "./jsx-internal";
import { JSXElementType, VanElement } from "./createElement";
import { InnerElement, Key, TagOption } from "./type";
export declare namespace JSX {
type ElementType = string | JSXElementType<any>;
interface ElementAttributesProperty {
props: object;
}
interface ElementChildrenAttribute {
children: object;
}
interface Element extends VanElement {
}
interface IntrinsicAttributes {
key?: Key;
}
type IntrinsicElements = {
[K in keyof InnerElement]: TagOption<K>;
};
}
49 changes: 0 additions & 49 deletions addons/van_jsx/src/jsx-runtime.js

This file was deleted.

Loading

0 comments on commit 426063f

Please sign in to comment.