nine-9 1.0.0 → 1.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.
Files changed (2) hide show
  1. package/README.md +146 -0
  2. package/package.json +3 -2
package/README.md ADDED
@@ -0,0 +1,146 @@
1
+ # nine-9
2
+
3
+ 一个轻量、高性能、类型安全的 Vanilla DOM 响应式 UI 框架。
4
+
5
+ 融合了 Vue 模板指令和 React Hooks 的优点,取两者之长。
6
+
7
+ ## 特性
8
+
9
+ - **响应式** - 数据变化时自动更新 UI
10
+ - **差量更新** - 只更新变化的 DOM,速度更快
11
+ - **类型安全** - 完整的 TypeScript 支持,类型推断优秀
12
+ - **轻量级** - 无依赖,体积小巧
13
+ - **链式 API** - 流畅的 DOM 操作
14
+ - **Vue 风格指令** - 熟悉的 v-if、v-for 模式
15
+ - **JSX 风格表达式** - 响应式表达式可以放在模板任意位置
16
+
17
+ ## 安装
18
+
19
+ ```bash
20
+ npm install nine-9
21
+ ```
22
+
23
+ ## 快速开始
24
+
25
+ ```typescript
26
+ import { createComponent, wrap, sync, when, tree, styleSet } from "nine-9";
27
+
28
+ const counter = createComponent((props: { initial: number }) => {
29
+ const count = wrap(props.initial); // 类似 Vue 的 ref
30
+ const doubled = sync(() => count.get() * 2); // 类似 computed
31
+
32
+ return tree("div")
33
+ .use(styleSet()
34
+ .fontSize("20px")
35
+ .padding("10px")
36
+ )
37
+ .append(
38
+ tree("button")
39
+ .on("click", () => count.set(count.get() + 1))
40
+ .append("点击我"),
41
+ tree("p")
42
+ .append(count), // 数据变化时自动更新
43
+ when(count, tree("p").append("count > 0 时显示")), // 类似 v-if
44
+ tree("p").append(doubled) // 计算属性,类似 v-for
45
+ );
46
+ });
47
+
48
+ counter({ initial: 0 }).mount("#app");
49
+ ```
50
+
51
+ ## 与 Vue 对比
52
+
53
+ | nine-9 | Vue | 说明 |
54
+ |------------------------------|----------|----------------|
55
+ | `wrap()` | `ref()` | 创建响应式引用 |
56
+ | `sync()` | computed | 响应式计算值 |
57
+ | `when(condition, tree)` | `v-if` | 条件渲染 |
58
+ | `sync(() => items.map(...))` | `v-for` | 列表渲染 |
59
+
60
+ ## API
61
+
62
+ ### `wrap<T>(initialData: T)`
63
+
64
+ 创建响应式数据包装器。类似 Vue 的 `ref()`。
65
+
66
+ ```typescript
67
+ const data = wrap({ name: "nine-9" });
68
+ data.set({ name: "updated" });
69
+ data.event.subscribe((newVal, oldVal) => {
70
+ console.log("变化了:", oldVal, "->", newVal);
71
+ });
72
+ ```
73
+
74
+ ### `sync<R>(effectRenderer: () => R, dependencies?: Wrapper[])`
75
+
76
+ 创建计算值,当依赖变化时自动更新。类似 Vue 的 computed 或 React 的 useMemo。返回 `Wrapper<R>`,可以用 `.get()` 读取值或订阅变化。
77
+
78
+ ```typescript
79
+ const doubled = sync(() => count.get() * 2, [count]);
80
+ // 直接在树中使用
81
+ tree("p").append(doubled);
82
+
83
+ // 或订阅变化
84
+ doubled.event.subscribe((newVal, oldVal) => {
85
+ console.log("doubled 变化了:", oldVal, "->", newVal);
86
+ });
87
+ ```
88
+
89
+ ### `when(condition: Wrapper<boolean> | (() => boolean), tree: TreeResult, dependencies?: Wrapper[])`
90
+
91
+ 条件渲染。类似 Vue 的 `v-if`。返回 `Wrapper<TreeResult>`,可以直接放在树中。
92
+
93
+ ```typescript
94
+ // 使用 Wrapper<boolean>
95
+ const isVisible = wrap(true);
96
+ when(isVisible, tree("div").append("我可见!"));
97
+
98
+ // 使用函数
99
+ when(() => count.get() > 5, tree("p").append("count > 5"), [count]);
100
+
101
+ // 直接在树中使用
102
+ tree("div").append(
103
+ when(isVisible, tree("span").append("显示我!"))
104
+ );
105
+ ```
106
+
107
+ ### `createComponent<T>(renderer)`
108
+
109
+ 创建组件,返回带有 `mount` 方法的渲染结果。
110
+
111
+ ```typescript
112
+ const MyComponent = createComponent((props: { title: string }) => {
113
+ return tree("div").append(props.title);
114
+ });
115
+
116
+ MyComponent({ title: "你好" }).mount("#app");
117
+ ```
118
+
119
+ ### `tree(tagName)`
120
+
121
+ 创建 DOM 元素,支持链式调用。
122
+
123
+ ```typescript
124
+ tree("div")
125
+ .class("container")
126
+ .id("main")
127
+ .on("click", handler)
128
+ .append(child1, child2);
129
+ ```
130
+
131
+ ### `styleSet()`
132
+
133
+ 创建链式样式对象。
134
+
135
+ ```typescript
136
+ tree("div")
137
+ .use(styleSet()
138
+ .color("red")
139
+ .fontSize("16px")
140
+ .display("flex")
141
+ )
142
+ ```
143
+
144
+ ## License
145
+
146
+ MIT
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "nine-9",
3
- "version": "1.0.0",
4
- "description": "A lightweight, high-performance, type-safe reactive UI framework for Vanilla JavaScript.",
3
+ "version": "1.1.0",
4
+ "description": "一个轻量、高性能、类型安全的 Vanilla DOM 响应式 UI 框架。",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "files": [
8
8
  "dist"
9
9
  ],
10
+ "readme": "readme.md",
10
11
  "repository": {
11
12
  "type": "git",
12
13
  "url": "https://github.com/Rundll86/nine.git"