@yoyaflow/yoya-ui 0.3.0 → 0.3.2

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/README.md CHANGED
@@ -44,19 +44,22 @@ function HelloWorldExampleI18n() {
44
44
 
45
45
  ## Installation
46
46
 
47
- ### Scaffold a new project
47
+ ### Quick experience
48
48
 
49
49
  ```bash
50
- npx create-yoya-ui@latest my-app # basic template (SPA)
51
- npx create-yoya-ui@latest my-app --template ssr # SSR template (renderPage + hydrateOrMount)
52
- npx create-yoya-ui@latest my-app --template admin # admin console template (top nav + collapsible sidebar + RouterViews)
50
+ # Install the scaffold
51
+ npm install -g create-yoya-ui
52
+
53
+ # Create a project with the admin template
54
+ create-yoya-ui my-app --template admin
53
55
  cd my-app
54
56
  npm install
55
- npm run dev # basic / admin: dev server
56
- # SSR: npm run build && npm start
57
+ npm run dev
57
58
  ```
58
59
 
59
- `--template admin` scaffolds a standard admin console: top navigation, collapsible sidebar and a titled RouterViews content area, with feature examples for dashboard (data boards & charts), member / role / permission management, and dictionary management (type table with an item editor dialog).
60
+ `--template admin` scaffolds a standard admin console: top navigation, sidebar and a titled RouterViews content area, with feature examples for dashboard (data boards & charts), member / role / permission management, and dictionary management (type table with an item editor dialog).
61
+
62
+ yoya-ui has its own unique development paradigm — declarative node DSL, page composition, and feature-module organization. The admin template is the recommended way to get familiar with it: it demonstrates the full stack of a real admin console (shell, routing, tables / forms / dialogs, dashboard boards & charts) using the library's idiomatic patterns.
60
63
 
61
64
  ### Install into an existing project
62
65
 
@@ -82,24 +85,42 @@ The page only needs a `<div id="app"></div>` loaded with a module script.
82
85
 
83
86
  ## Server-Side Rendering (SSR)
84
87
 
85
- The same page factory switches between server rendering and client rendering:
88
+ The same page factory switches between server rendering and client rendering. For a full page, use the high-level entry points — `renderPage` builds a complete HTML document (head/body in DSL) and `hydrateOrMount` bootstraps the client in one call:
86
89
 
87
90
  ```js
88
- // Server
89
- import { renderToString } from '@yoyaflow/yoya-ui/ssr';
90
- const { html, state } = renderToString(createPage, { state: { path: '/home' } });
91
-
92
- // Client
93
- import { hydrate, mount, parseState } from '@yoyaflow/yoya-ui/ssr';
94
- const data = parseState(document.getElementById('__YOYA_DATA__').textContent);
95
- const app = document.getElementById('app');
96
- if (app.firstElementChild) {
97
- hydrate(createPage, app, data); // Server HTML exists: adopt DOM, bind events
98
- } else {
99
- mount(createPage, app, data); // Empty shell: full client render
100
- }
91
+ // Server — render a complete HTML document per request
92
+ import { renderPage } from '@yoyaflow/yoya-ui/ssr';
93
+ import { HomePage, messages } from './home-page.js';
94
+
95
+ const html = renderPage(
96
+ {
97
+ page: (page, state) => {
98
+ page.head((head) => {
99
+ head.title('SSR Example'.s('title'));
100
+ head.meta({ charset: 'utf-8' });
101
+ head.link({ rel: 'stylesheet', href: '/assets/yoya.ui.css' });
102
+ });
103
+ page.body((body) => {
104
+ body.vBody((shell) => {
105
+ shell.child(HomePage(state)); // state = { lang, path, mode }
106
+ });
107
+ });
108
+ }
109
+ },
110
+ { lang, mode: 'history', path },
111
+ { messages } // per-request i18n; .s() is scoped automatically
112
+ );
113
+
114
+ // Client — reads __YOYA_DATA__; hydrates when server HTML exists, otherwise mounts
115
+ import '@yoyaflow/yoya-ui/ui.css';
116
+ import { hydrateOrMount } from '@yoyaflow/yoya-ui/ssr';
117
+ import { HomePage, messages } from './home-page.js';
118
+
119
+ hydrateOrMount(HomePage, { messages });
101
120
  ```
102
121
 
122
+ Lower-level primitives (`renderToString` / `serializeState` / `parseState` / `mount` / `hydrate`) remain available for fine-grained control, e.g. embedding an HTML fragment into your own server template.
123
+
103
124
  Key points:
104
125
 
105
126
  - `vClientOnly(loader)`: non-SSR modules (e.g. ECharts) emit a placeholder on the server and load on the client after hydration
@@ -107,7 +128,7 @@ Key points:
107
128
  - Per-request i18n instance, render-context id allocator, auto-destroy after render — the server stays stateless
108
129
  - `maxNodes` falls back to client rendering automatically when exceeded
109
130
 
110
- Full integration guide: [docs/ssr.md](docs/ssr.md) (Chinese); runnable example:
131
+ Full integration guide: [docs/ssr.md](docs/ssr.md) (Chinese). Or run the in-repo example:
111
132
 
112
133
  ```bash
113
134
  npm run build
@@ -120,7 +141,7 @@ node src/examples/ssr/server-http.mjs
120
141
  import { div, svg, createI18n } from '@yoyaflow/yoya-ui/core'; // core HTML/SVG/state
121
142
  import { vButton, vCard, vForm, vTable } from '@yoyaflow/yoya-ui/ui'; // official component library
122
143
  import { vEchart } from '@yoyaflow/yoya-ui/echart'; // ECharts component (bring your own echarts)
123
- import { renderToString, hydrate } from '@yoyaflow/yoya-ui/ssr'; // server-side rendering
144
+ import { renderPage, hydrateOrMount } from '@yoyaflow/yoya-ui/ssr'; // server-side rendering
124
145
  import '@yoyaflow/yoya-ui/ui.css'; // default styles and theme variables
125
146
  ```
126
147
 
@@ -133,7 +154,7 @@ TypeScript projects get hints and type checking with no extra configuration:
133
154
  ```ts
134
155
  import { div, vButton, vCard, vTable, toast } from '@yoyaflow/yoya-ui';
135
156
  import { createI18n } from '@yoyaflow/yoya-ui/core';
136
- import { renderToString } from '@yoyaflow/yoya-ui/ssr';
157
+ import { renderPage } from '@yoyaflow/yoya-ui/ssr';
137
158
  import '@yoyaflow/yoya-ui/ui.css';
138
159
 
139
160
  div((page) => {
@@ -189,7 +210,7 @@ npm run build
189
210
 
190
211
  - `yoya.core.js` / `yoya.ui.js` — core and component library ESM entries
191
212
  - `yoya.echart.js` — ECharts component entry (does not bundle echarts itself)
192
- - `yoya.ssr.js` — server rendering entry (`renderToString` / `hydrate` / `mount`)
213
+ - `yoya.ssr.js` — server rendering entry (`renderPage` / `hydrateOrMount` / `renderToString` / `hydrate` / `mount`)
193
214
  - `echarts.min.js` — ECharts core (load globally via `<script>`)
194
215
  - `yoya.ui.css` — default styles and theme variables
195
216
  - `yoya-ui.umd.js` — UMD build (`window.YoyaUI`)
package/README.zh-CN.md CHANGED
@@ -45,19 +45,22 @@ function HelloWorldExampleI18n() {
45
45
 
46
46
  ## 安装
47
47
 
48
- ### 脚手架快速搭建
48
+ ### 快速体验
49
49
 
50
50
  ```bash
51
- npx create-yoya-ui@latest my-app # basic 模板(SPA)
52
- npx create-yoya-ui@latest my-app --template ssr # SSR 模板(renderPage + hydrateOrMount)
53
- npx create-yoya-ui@latest my-app --template admin # admin 后台管理模板(顶部导航 + 可折叠侧栏 + RouterViews)
51
+ # 安装脚手架
52
+ npm install -g create-yoya-ui
53
+
54
+ # 使用 admin 模板创建项目
55
+ create-yoya-ui my-app --template admin
54
56
  cd my-app
55
57
  npm install
56
- npm run dev # basic / admin:开发服务器
57
- # SSR:npm run build && npm start
58
+ npm run dev
58
59
  ```
59
60
 
60
- `--template admin` 生成标准后台管理模板:顶部导航 + 可折叠侧栏 + 带标题的 RouterViews 内容区,内置数据概览看板与图表,以及成员 / 角色 / 权限 / 字典管理等业务域示例。
61
+ `--template admin` 生成标准后台管理模板:顶部导航 + 左侧菜单 + 带标题的 RouterViews 内容区,内置数据概览看板与图表,以及成员 / 角色 / 权限 / 字典管理等业务域示例。
62
+
63
+ yoya-ui 拥有自己独特的开发范式:声明式节点 DSL、页面编排与 feature 模块组织。推荐使用 admin 模板创建项目来了解这些范式——它用库的原生写法完整展示了一个真实后台(页面壳、路由、表格/表单/弹窗、数据看板与图表)。
61
64
 
62
65
  ### 已有项目引入
63
66
 
@@ -83,24 +86,42 @@ div((page) => {
83
86
 
84
87
  ## 服务端渲染(SSR)
85
88
 
86
- 同一份页面工厂代码,服务端渲染与客户端渲染可切换:
89
+ 同一份页面工厂代码,服务端渲染与客户端渲染可切换。整页场景直接使用高层入口:`renderPage` 输出完整 HTML 文档(head/body 用 DSL 定义),`hydrateOrMount` 一行完成客户端接入:
87
90
 
88
91
  ```js
89
- // 服务端
90
- import { renderToString } from '@yoyaflow/yoya-ui/ssr';
91
- const { html, state } = renderToString(createPage, { state: { path: '/home' } });
92
-
93
- // 客户端
94
- import { hydrate, mount, parseState } from '@yoyaflow/yoya-ui/ssr';
95
- const data = parseState(document.getElementById('__YOYA_DATA__').textContent);
96
- const app = document.getElementById('app');
97
- if (app.firstElementChild) {
98
- hydrate(createPage, app, data); // 有服务端 HTML:收养 DOM、绑定事件
99
- } else {
100
- mount(createPage, app, data); // 空壳:全量客户端渲染
101
- }
92
+ // 服务端:按请求渲染完整 HTML 文档
93
+ import { renderPage } from '@yoyaflow/yoya-ui/ssr';
94
+ import { HomePage, messages } from './home-page.js';
95
+
96
+ const html = renderPage(
97
+ {
98
+ page: (page, state) => {
99
+ page.head((head) => {
100
+ head.title('SSR 示例'.s('title'));
101
+ head.meta({ charset: 'utf-8' });
102
+ head.link({ rel: 'stylesheet', href: '/assets/yoya.ui.css' });
103
+ });
104
+ page.body((body) => {
105
+ body.vBody((shell) => {
106
+ shell.child(HomePage(state)); // state = { lang, path, mode }
107
+ });
108
+ });
109
+ }
110
+ },
111
+ { lang, mode: 'history', path },
112
+ { messages } // 每请求 i18n,.s() 自动作用域
113
+ );
114
+
115
+ // 客户端:自动读取 __YOYA_DATA__,有服务端 HTML 走 hydrate,否则 mount
116
+ import '@yoyaflow/yoya-ui/ui.css';
117
+ import { hydrateOrMount } from '@yoyaflow/yoya-ui/ssr';
118
+ import { HomePage, messages } from './home-page.js';
119
+
120
+ hydrateOrMount(HomePage, { messages });
102
121
  ```
103
122
 
123
+ 底层原语(`renderToString` / `serializeState` / `parseState` / `mount` / `hydrate`)仍然可用,适合需要细粒度控制的场景,例如把 HTML 片段嵌入自有服务端模板。
124
+
104
125
  要点:
105
126
 
106
127
  - `vClientOnly(loader)`:非 SSR 模块(如 ECharts)服务端只出占位,hydration 后客户端加载
@@ -108,7 +129,7 @@ if (app.firstElementChild) {
108
129
  - 每请求 i18n 实例、渲染上下文 id 分配器、渲染后自动销毁——服务端保持无状态
109
130
  - `maxNodes` 超限自动回退客户端渲染
110
131
 
111
- 完整集成指南见 [docs/ssr.md](docs/ssr.md);可运行示例:
132
+ 完整集成指南见 [docs/ssr.md](docs/ssr.md)。或运行仓库内示例:
112
133
 
113
134
  ```bash
114
135
  npm run build
@@ -121,7 +142,7 @@ node src/examples/ssr/server-http.mjs
121
142
  import { div, svg, createI18n } from '@yoyaflow/yoya-ui/core'; // 核心 HTML/SVG/状态
122
143
  import { vButton, vCard, vForm, vTable } from '@yoyaflow/yoya-ui/ui'; // 官方组件库
123
144
  import { vEchart } from '@yoyaflow/yoya-ui/echart'; // ECharts 组件(需自行引入 echarts)
124
- import { renderToString, hydrate } from '@yoyaflow/yoya-ui/ssr'; // 服务端渲染
145
+ import { renderPage, hydrateOrMount } from '@yoyaflow/yoya-ui/ssr'; // 服务端渲染
125
146
  import '@yoyaflow/yoya-ui/ui.css'; // 默认样式与主题变量
126
147
  ```
127
148
 
@@ -134,7 +155,7 @@ TypeScript 项目无需额外配置即可获得提示与类型检查:
134
155
  ```ts
135
156
  import { div, vButton, vCard, vTable, toast } from '@yoyaflow/yoya-ui';
136
157
  import { createI18n } from '@yoyaflow/yoya-ui/core';
137
- import { renderToString } from '@yoyaflow/yoya-ui/ssr';
158
+ import { renderPage } from '@yoyaflow/yoya-ui/ssr';
138
159
  import '@yoyaflow/yoya-ui/ui.css';
139
160
 
140
161
  div((page) => {
@@ -190,7 +211,7 @@ npm run build
190
211
 
191
212
  - `yoya.core.js` / `yoya.ui.js` — 核心与组件库 ESM 入口
192
213
  - `yoya.echart.js` — ECharts 组件入口(不包含 echarts 本体)
193
- - `yoya.ssr.js` — 服务端渲染入口(`renderToString` / `hydrate` / `mount`)
214
+ - `yoya.ssr.js` — 服务端渲染入口(`renderPage` / `hydrateOrMount` / `renderToString` / `hydrate` / `mount`)
194
215
  - `echarts.min.js` — ECharts 本体(用 `<script>` 标签全局引入)
195
216
  - `yoya.ui.css` — 默认样式与主题变量
196
217
  - `yoya-ui.umd.js` — UMD 版(`window.YoyaUI`)