@koniwa/core 0.0.0-stage → 0.1.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Trần Thành Phương Đăng
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,187 @@
1
- # Temporary Holding Version
1
+ <p align="center">
2
+ <img src="assets/logo.svg" width="112" alt="koniwa logo: a bonsai tree in a small pot">
3
+ </p>
2
4
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
5
+ <h1 align="center">koniwa 小庭</h1>
6
+
7
+ <p align="center">A tiny JSX renderer that turns JSX straight into real DOM nodes.</p>
8
+
9
+ - **No virtual DOM diffing, no state, no hooks.** Components are plain functions that run once.
10
+ - **No dependencies.** About 100 lines of TypeScript.
11
+ - **Works with any JSX compiler** through the automatic runtime (`jsxImportSource`) or the
12
+ classic `createVirtualNode` factory.
13
+
14
+ It is a good fit for mostly static pages (portfolios, landing pages, CVs) where you want to
15
+ write markup as components but don't need React. If you know React, you already know most of
16
+ koniwa (see [Coming from React](#coming-from-react)). React-style hooks are planned (see
17
+ [Roadmap](#roadmap)); until they land, use React or Preact if you need state or re-rendering.
18
+
19
+ ## The name
20
+
21
+ _Koniwa_ (小庭) means "small garden": the little garden inside a traditional Japanese
22
+ house, the part guests actually see. A small garden is small but curated, and every stone,
23
+ plant and lantern has its place. That is the idea here: a handful of pages built from
24
+ components and laid out with care, for school projects, GitHub pages and personal sites of
25
+ about ten pages or fewer.
26
+
27
+ Koniwa is one part of a bigger house:
28
+
29
+ - [**watari**](https://github.com/dangttp97/watari) (渡り), from _watari-rōka_, the covered
30
+ corridor that connects the rooms. It fetches data and carries it where it is needed.
31
+ - **jubako** (重箱), the stacked lunch boxes kept in the kitchen. It stores things in layers.
32
+ - **koniwa** (小庭), the garden. It puts everything on display.
33
+
34
+ _Jubako keeps things, watari carries them, koniwa shows them._
35
+
36
+ ## Install
37
+
38
+ ```bash
39
+ npm install @koniwa/core
40
+ # or
41
+ yarn add @koniwa/core
42
+ ```
43
+
44
+ ## Setup
45
+
46
+ ### TypeScript
47
+
48
+ ```jsonc
49
+ // tsconfig.json
50
+ {
51
+ "compilerOptions": {
52
+ "jsx": "react-jsx",
53
+ "jsxImportSource": "@koniwa/core",
54
+ },
55
+ }
56
+ ```
57
+
58
+ ### Vite
59
+
60
+ ```ts
61
+ // vite.config.ts
62
+ import { defineConfig } from "vite";
63
+
64
+ export default defineConfig({
65
+ oxc: {
66
+ jsx: { runtime: "automatic", importSource: "@koniwa/core" },
67
+ },
68
+ });
69
+ ```
70
+
71
+ With the automatic runtime the compiler imports what it needs, so components never import
72
+ `createVirtualNode` themselves.
73
+
74
+ ## Usage
75
+
76
+ ```tsx
77
+ import { render } from "@koniwa/core";
78
+
79
+ const Greeting = ({ name }: { name: string }) => <h1>Hello, {name}!</h1>;
80
+
81
+ const App = () => (
82
+ <>
83
+ <Greeting name="Ken Musashi" />
84
+ <button onClick={() => alert("Hi")}>Say hi</button>
85
+ </>
86
+ );
87
+
88
+ render(<App />, document.getElementById("root")!);
89
+ ```
90
+
91
+ ## Coming from React
92
+
93
+ The parts you write every day work the same way:
94
+
95
+ | React | koniwa |
96
+ | -------------------------------------------- | -------------------------------------------- |
97
+ | Function components with `props`, `children` | Same |
98
+ | `<>...</>` / `Fragment` | Same |
99
+ | `className`, `htmlFor`, `style={{ ... }}` | Same |
100
+ | `onClick`, `onInput`, `onSubmit`, … | Same names, attached with `addEventListener` |
101
+ | `"jsxImportSource": "react"` | `"jsxImportSource": "@koniwa/core"` |
102
+ | `React.createElement(type, props, ...kids)` | `createVirtualNode(type, props, ...kids)` |
103
+ | `createRoot(root).render(<App />)` | `mount(<App />, root)` |
104
+
105
+ What is different:
106
+
107
+ - **Components run once.** Nothing re-renders; to update the page, call `mount` again or change
108
+ the DOM in an event handler.
109
+ - **Events are native DOM events**, not React's synthetic events. `onChange` fires on the
110
+ browser's `change` event (when an input loses focus); use `onInput` for every keystroke.
111
+ - **Numbers in `style` get no unit.** Write `{ width: "10px" }`, not `{ width: 10 }`.
112
+ - **Every prop is an attribute.** `value` and `checked` set the initial HTML attribute, and
113
+ `dangerouslySetInnerHTML` is not supported.
114
+ - **No class components**, no `key`-based reordering (`key` is ignored), no hooks yet.
115
+
116
+ ## API
117
+
118
+ | Export | Description |
119
+ | --------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
120
+ | `render(child, container)` | Builds DOM from `child` and **appends** it to `container`. Accepts a VirtualNode, a string or number, an array, or a component function (`render(App, root)`). |
121
+ | `mount(child, container)` | Clears `container`, then renders into it. |
122
+ | `createVirtualNode(type, props, ...children)` | Classic JSX factory. Returns a VirtualNode `{ type, props, children }`. |
123
+ | `Fragment` | `<>...</>`: renders children without a wrapper element. |
124
+ | `@koniwa/core/jsx-runtime` | Automatic runtime (`jsx`, `jsxs`, `Fragment`) used by the compiler. |
125
+
126
+ Types: `Child`, `Component`, `Props`, `VirtualNode`, `JSX`.
127
+
128
+ ### How props are applied
129
+
130
+ | Prop | Result |
131
+ | ----------------------------------- | ----------------------------------- |
132
+ | `className` / `class` | `class` attribute |
133
+ | `htmlFor` | `for` attribute |
134
+ | `onClick`, `onInput`, … (functions) | `addEventListener("click", …)` |
135
+ | `style={{ color: "red" }}` | assigned to `element.style` |
136
+ | `true` | empty attribute (`hidden=""`) |
137
+ | `false`, `null`, `undefined` | attribute is not set |
138
+ | `key` | ignored |
139
+ | anything else | `setAttribute(name, String(value))` |
140
+
141
+ `<svg>` and its children are created in the SVG namespace; children of `<foreignObject>` go back
142
+ to HTML.
143
+
144
+ ## Limitations
145
+
146
+ - **Render once.** There is no diffing: calling `render` again appends a second copy. Use
147
+ `mount` to replace the content, or update the DOM directly (for example through `data-*`
148
+ attributes that your CSS reacts to).
149
+ - **No state, effects, context or refs** yet (see [Roadmap](#roadmap)).
150
+ - **Loose attribute types.** Intrinsic elements accept any attribute; there is no per-element
151
+ type checking like `@types/react` provides.
152
+ - **Custom CSS properties in `style` objects** (`{ "--size": "10px" }`) are not applied; set
153
+ them with CSS classes instead.
154
+
155
+ ## Roadmap
156
+
157
+ The next step is React-style hooks, with the same names and call signatures as React, so
158
+ code and habits carry over:
159
+
160
+ | Hook | What it is for |
161
+ | --------------------- | ----------------------------------------------------------- |
162
+ | `useState` | Local state; changing it re-renders the component |
163
+ | `useEffect` | Run code after render, with cleanup |
164
+ | `useRef` | Hold a DOM element or a value across renders |
165
+ | `useMemo` | Cache a computed value until its dependencies change |
166
+ | `useCallback` | Keep the same function between renders |
167
+ | `useContext` | Read a value from a `createContext` provider up the tree |
168
+ | `useImperativeHandle` | Expose methods from a component to its parent through `ref` |
169
+
170
+ These are **planned, not available yet**; details may change while they are built.
171
+
172
+ Out of scope, on purpose: server-side rendering, client-side routing (SPA), server
173
+ components, Suspense and concurrent rendering. koniwa stays a small tool for static sites of
174
+ about ten pages-ish.
175
+
176
+ ## Development
177
+
178
+ ```bash
179
+ yarn install
180
+ yarn test # Vitest + happy-dom
181
+ yarn typecheck
182
+ yarn build # emits dist/
183
+ ```
184
+
185
+ ## License
186
+
187
+ [MIT](LICENSE)
@@ -0,0 +1,4 @@
1
+ export { Fragment, createVirtualNode } from "./virtual-node.js";
2
+ export type { JSX } from "./jsx.js";
3
+ export { mount, render } from "./render.js";
4
+ export type { Child, Component, Props, VirtualNode } from "./types.js";
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { Fragment, createVirtualNode } from "./virtual-node.js";
2
+ export { mount, render } from "./render.js";
@@ -0,0 +1,2 @@
1
+ export { Fragment, jsx as jsxDEV } from "./jsx-runtime.js";
2
+ export type { JSX } from "./jsx.js";
@@ -0,0 +1 @@
1
+ export { Fragment, jsx as jsxDEV } from "./jsx-runtime.js";
@@ -0,0 +1,6 @@
1
+ import { Fragment } from "./virtual-node.js";
2
+ import type { Component, Props, VirtualNode } from "./types.js";
3
+ export { Fragment };
4
+ export type { JSX } from "./jsx.js";
5
+ export declare const jsx: (type: string | Component, props: Props, _key?: unknown) => VirtualNode;
6
+ export declare const jsxs: (type: string | Component, props: Props, _key?: unknown) => VirtualNode;
@@ -0,0 +1,9 @@
1
+ import { Fragment, createVirtualNode } from "./virtual-node.js";
2
+ export { Fragment };
3
+ export const jsx = (type, props, _key) => {
4
+ const { children, ...rest } = props;
5
+ return children === undefined
6
+ ? createVirtualNode(type, rest)
7
+ : createVirtualNode(type, rest, children);
8
+ };
9
+ export const jsxs = jsx;
package/dist/jsx.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { VirtualNode } from "./types.js";
2
+ export declare namespace JSX {
3
+ type Element = VirtualNode;
4
+ interface IntrinsicElements {
5
+ [elemName: string]: any;
6
+ }
7
+ interface ElementChildrenAttribute {
8
+ children: unknown;
9
+ }
10
+ }
package/dist/jsx.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { Child, Component } from "./types.js";
2
+ export declare const render: (child: Child | Component, container: Node) => void;
3
+ export declare const mount: (child: Child | Component, container: Element) => void;
package/dist/render.js ADDED
@@ -0,0 +1,65 @@
1
+ import { createVirtualNode } from "./virtual-node.js";
2
+ const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
3
+ export const render = (child, container) => {
4
+ const node = typeof child === "function" ? createVirtualNode(child, null) : child;
5
+ appendChild(container, node, false);
6
+ };
7
+ export const mount = (child, container) => {
8
+ container.replaceChildren();
9
+ render(child, container);
10
+ };
11
+ const appendChild = (parent, child, insideSvg) => {
12
+ if (child == null || typeof child === "boolean")
13
+ return;
14
+ if (Array.isArray(child)) {
15
+ for (const item of child)
16
+ appendChild(parent, item, insideSvg);
17
+ }
18
+ else if (typeof child === "string" || typeof child === "number") {
19
+ parent.appendChild(document.createTextNode(String(child)));
20
+ }
21
+ else if (typeof child.type === "function") {
22
+ appendChild(parent, callComponent(child.type, child), insideSvg);
23
+ }
24
+ else {
25
+ parent.appendChild(createDomElement(child.type, child, insideSvg));
26
+ }
27
+ };
28
+ const callComponent = (component, node) => component({ ...node.props, children: node.children });
29
+ const createDomElement = (tag, node, insideSvg) => {
30
+ const isSvg = insideSvg || tag === "svg";
31
+ const element = isSvg
32
+ ? document.createElementNS(SVG_NAMESPACE, tag)
33
+ : document.createElement(tag);
34
+ setProps(element, node.props);
35
+ // Children of <foreignObject> are HTML again.
36
+ const childrenInsideSvg = isSvg && tag !== "foreignObject";
37
+ for (const child of node.children)
38
+ appendChild(element, child, childrenInsideSvg);
39
+ return element;
40
+ };
41
+ const setProps = (element, props) => {
42
+ for (const [name, value] of Object.entries(props)) {
43
+ if (name === "key" || name === "children")
44
+ continue;
45
+ if (value == null || value === false)
46
+ continue;
47
+ if (name.startsWith("on") && typeof value === "function") {
48
+ const eventName = name.slice(2).toLowerCase();
49
+ element.addEventListener(eventName, value);
50
+ }
51
+ else if (name === "style" && typeof value === "object") {
52
+ Object.assign(element.style, value);
53
+ }
54
+ else {
55
+ element.setAttribute(toAttributeName(name), value === true ? "" : String(value));
56
+ }
57
+ }
58
+ };
59
+ const toAttributeName = (propName) => {
60
+ if (propName === "className")
61
+ return "class";
62
+ if (propName === "htmlFor")
63
+ return "for";
64
+ return propName;
65
+ };
@@ -0,0 +1,8 @@
1
+ export type Child = VirtualNode | string | number | boolean | null | undefined | Child[];
2
+ export type Props = Record<string, unknown>;
3
+ export type Component<P = any> = (props: P) => Child;
4
+ export interface VirtualNode {
5
+ type: string | Component;
6
+ props: Props;
7
+ children: Child[];
8
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import type { Child, Component, Props, VirtualNode } from "./types.js";
2
+ export declare const createVirtualNode: (type: string | Component, props: Props | null, ...children: Child[]) => VirtualNode;
3
+ export declare const Fragment: (props: {
4
+ children?: Child[];
5
+ }) => Child;
@@ -0,0 +1,6 @@
1
+ export const createVirtualNode = (type, props, ...children) => ({
2
+ type,
3
+ props: props ?? {},
4
+ children: children.flat(Infinity),
5
+ });
6
+ export const Fragment = (props) => props.children ?? [];
package/package.json CHANGED
@@ -1,6 +1,55 @@
1
1
  {
2
2
  "name": "@koniwa/core",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "A tiny JSX renderer that turns JSX straight into real DOM. No virtual DOM diffing, no state, no dependencies.",
5
+ "keywords": [
6
+ "jsx",
7
+ "dom",
8
+ "renderer",
9
+ "jsx-runtime",
10
+ "vite"
11
+ ],
12
+ "license": "MIT",
13
+ "author": "Trần Thành Phương Đăng",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/dangttp97/koniwa.git"
17
+ },
18
+ "type": "module",
19
+ "sideEffects": false,
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "default": "./dist/index.js"
27
+ },
28
+ "./jsx-runtime": {
29
+ "types": "./dist/jsx-runtime.d.ts",
30
+ "default": "./dist/jsx-runtime.js"
31
+ },
32
+ "./jsx-dev-runtime": {
33
+ "types": "./dist/jsx-dev-runtime.d.ts",
34
+ "default": "./dist/jsx-dev-runtime.js"
35
+ }
36
+ },
37
+ "types": "./dist/index.d.ts",
38
+ "packageManager": "yarn@4.18.0",
39
+ "scripts": {
40
+ "build": "tsc -p tsconfig.build.json",
41
+ "typecheck": "tsc --noEmit",
42
+ "test": "vitest run",
43
+ "test:watch": "vitest",
44
+ "format": "prettier --write .",
45
+ "format:check": "prettier --check .",
46
+ "prepack": "yarn build"
47
+ },
48
+ "devDependencies": {
49
+ "happy-dom": "^21.0.4",
50
+ "prettier": "^3.10.1",
51
+ "typescript": "~6.0.3",
52
+ "vite": "^8.3.8",
53
+ "vitest": "^5.1.0"
54
+ }
55
+ }