Skip to content

Commit e835625

Browse files
committed
docs: add documentation for new hooks
1 parent 4f0af56 commit e835625

File tree

6 files changed

+366
-4
lines changed

6 files changed

+366
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ Pre‑render your UI once, flip a `data-*` attribute to update — that's it.
2020

2121
## 🚀 Live Demo
2222

23-
| Example | Link | What it shows | Link to Code |
24-
| --------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
25-
| Interactive menu with render tracker | <a href="https://zero-ui.dev/" target="_blank" rel="noopener noreferrer"><strong>Main Demo↗</strong></a> | Compare Zero‑UI vs. React side‑by‑side while toggling a menu. | <a href="https://github.com/react-zero-ui/core/tree/main/examples/demo" target="_blank" rel="noopener noreferrer">Github</a> |
26-
| React benchmark (10 000 nested nodes) | <a href="https://zero-ui.dev/react" target="_blank" rel="noopener noreferrer"><strong>React 10k↗</strong></a> | How long the traditional React render path takes. | <a href="https://github.com/react-zero-ui/core/tree/main/examples/demo/src/app/react" target="_blank" rel="noopener noreferrer">Github</a> |
23+
| Example | Link | What it shows | Link to Code |
24+
| --- | --- | --- | --- |
25+
| Interactive menu with render tracker | <a href="https://zero-ui.dev/" target="_blank" rel="noopener noreferrer"><strong>Main Demo↗</strong></a> | Compare Zero‑UI vs. React side‑by‑side while toggling a menu. | <a href="https://github.com/react-zero-ui/core/tree/main/examples/demo" target="_blank" rel="noopener noreferrer">Github</a> |
26+
| React benchmark (10 000 nested nodes) | <a href="https://zero-ui.dev/react" target="_blank" rel="noopener noreferrer"><strong>React 10k↗</strong></a> | How long the traditional React render path takes. | <a href="https://github.com/react-zero-ui/core/tree/main/examples/demo/src/app/react" target="_blank" rel="noopener noreferrer">Github</a> |
2727
| Zero‑UI benchmark (10 000 nested nodes) | <a href="https://zero-ui.dev/zero-ui" target="_blank" rel="noopener noreferrer"><strong style="text-align: nowrap;">Zero‑UI 10k↗</strong></a> | Identical DOM, but powered by Zero‑UI's `data-*` switch. | <a href="https://github.com/react-zero-ui/core/tree/main/examples/demo/src/app/zero-ui" target="_blank" rel="noopener noreferrer">Github</a> |
2828

2929
---

README2.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
2+
## ⚡️ React Zero-UI
3+
*The ZERO re-render UI state library*
4+
5+
**The fastest possible UI updates in React. Period.**
6+
Zero runtime, zero React re-renders, and the simplest developer experience ever.
7+
8+
See the proof in [here](/docs/demo)
9+
10+
<a href="https://bundlephobia.com/package/@react-zero-ui/core@0.2.6" target="_blank" rel="noopener noreferrer"><img src="https://badgen.net/bundlephobia/minzip/@react-zero-ui/core@0.2.6" alt="npm version" /> </a><a href="https://www.npmjs.com/package/@austinserb/react-zero-ui" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/npm/v/@react-zero-ui/core" alt="npm version" /></a> <a href="https://opensource.org/licenses/MIT" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License: MIT" /></a> ![CI](https://github.com/react-zero-ui/core/actions/workflows/ci.yml/badge.svg?branch=main)
11+
12+
---
13+
14+
15+
## 🔥 Core Concept: *"Pre-Rendering"*
16+
17+
Why re-render UI if all states are known at build time? React Zero-UI **pre-renders** UI states once, and flips `data-*` attribute to update - that's it.
18+
19+
**Example:**
20+
21+
```tsx
22+
const [, setTheme] = useUI("theme", "dark");
23+
24+
// Flip theme to "light"
25+
setTheme("light"); // data-theme="light" on body
26+
```
27+
28+
Tailwind usage:
29+
30+
```html
31+
<div class="theme-dark:bg-black theme-light:bg-white">Fast & Reactive</div>
32+
```
33+
34+
---
35+
36+
## 🚀 How it Works (Build-Time Magic)
37+
38+
React Zero-UI uses a hyper-optimized AST resolver in development/build-time that scans your codebase for:
39+
40+
* `useUI` and `useScopedUI` hook usage.
41+
* Any variables resolving to strings (e.g., `'theme'`, `'modal-open'`).
42+
* Tailwind variant classes (e.g., `theme-dark:bg-black`).
43+
44+
This generates:
45+
46+
* Optimal CSS with scoped variant selectors.
47+
* Initial data-attributes injected onto the body (zero FOUC).
48+
* **Zero runtime overhead in production**.
49+
50+
---
51+
52+
## ⚙️ Installation (Zero-UI CLI)
53+
54+
pre-requisites:
55+
- Tailwind CSS v4 must already be initialized in your project.
56+
- Vite or Next.js (App Router)
57+
58+
59+
```bash
60+
npx create-zero-ui
61+
```
62+
> for manual configuration, see [manual installation.](https://github.com/react-zero-ui/core)
63+
64+
---
65+
66+
## 🔨 API: `useUI` Hook (Global UI state)
67+
68+
Simple hook mirroring React's `useState`:
69+
70+
```tsx
71+
import { useUI } from '@react-zero-ui/core';
72+
73+
const [theme, setTheme] = useUI("theme", "dark");
74+
```
75+
76+
* Flips global `data-theme` attribute on `<body>`.
77+
* Zero React re-renders.
78+
* Initial state pre-rendered at build time (no FOUC).
79+
80+
---
81+
82+
## 🎯 API: `useScopedUI` Hook (Scoped UI state)
83+
84+
Control UI states at the element-level:
85+
86+
```tsx
87+
import { useScopedUI } from '@react-zero-ui/core';
88+
89+
const [theme, setTheme] = useScopedUI("theme", "dark");
90+
91+
// Flips data-theme attribute on the specific ref element
92+
<div ref={setTheme.ref} data-theme={theme}>
93+
Scoped UI Here
94+
</div>
95+
```
96+
97+
* Data attribute flips on specific target element.
98+
* Generates scoped CSS selectors only applying within the target element.
99+
100+
---
101+
102+
## 🌈 API: CSS Variables Support
103+
104+
Sometimes CSS variables are more efficient. React Zero-UI makes it trivial using the `CssVar` option:
105+
106+
```tsx
107+
import { useUI, CssVar } from '@react-zero-ui/core';
108+
109+
const [blur, setBlur] = useUI("blur", "0px", CssVar);
110+
111+
// Flips CSS variable --blur on body
112+
setBlur("5px"); // body { --blur: 5px }
113+
```
114+
115+
**Scoped CSS Variable Example:**
116+
117+
```tsx
118+
const [blur, setBlur] = useScopedUI("blur", "0px", CssVar);
119+
120+
<div ref={setBlur.ref} style={{ "--blur": blur }}>
121+
Scoped blur effect.
122+
</div>
123+
```
124+
125+
---
126+
127+
## 🧪 Experimental: SSR-safe `zeroOnClick`
128+
129+
Enable client-side interactivity **without leaving server components**.
130+
Just 300 bytes of runtime overhead.
131+
132+
See [experimental](./docs/assets/experimental.md) for more details.
133+
134+
## 📦 Summary of Benefits
135+
136+
* **Zero React re-renders:** Pure CSS-driven UI state.
137+
* **Pre-rendered UI:** All states injected at build-time. and only loaded when needed.
138+
* **Tiny footprint:** <350 bytes runtime, zero overhead for CSS states.
139+
* **SSR-safe interaction:** Static server components, fully interactive.
140+
* **Amazing DX:** Simple hooks, auto-generated Tailwind variants.
141+
* **Highly optimized AST resolver:** Fast, cached build process.
142+
143+
React Zero-UI delivers the fastest, simplest, most performant way to handle global and scoped UI state in modern React applications.
144+
145+
146+
Made with ❤️ for the React community by [@austinserb](https://github.com/austin1serb)

docs/assets/demo.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## 🚀 Demo + Benchmarks
2+
3+
| Example | Link | What it shows | Link to Code |
4+
| -- | -- | -- | -- |
5+
| Interactive menu with render tracker | [Main Demo](https://zero-ui.dev/) | Compare Zero‑UI vs. React side‑by‑side while toggling a menu. | [Github](https://zero-ui.dev/react) |
6+
| React benchmark (10 000 nested nodes) | [React 10k](https://zero-ui.dev/react) | How long the traditional React render path takes. | [Github](https://github.com/react-zero-ui/core/tree/main/examples/demo/src/app/react) |
7+
| Zero‑UI benchmark (10 000 nested nodes) | [Zero‑UI 10k](https://zero-ui.dev/zero-ui) | Identical DOM, but powered by Zero‑UI's `data-*` switch. | [Github](https://github.com/react-zero-ui/core/tree/main/examples/demo/src/app/zero-ui) |
8+
9+
source code for the demo: [Zero Rerender Demo](/examples/demo/)
10+
11+
---
12+
13+
## 🧐 Why Zero‑UI?
14+
15+
Every `setState` in React triggers the full VDOM → Diff → Reconciliation → Paint pipeline. For _pure UI state_ (themes, menus, toggles) that work is wasted.
16+
17+
**Zero‑UI introduces "_PRE‑rendering_":**
18+
19+
1. Tailwind variants for every state are **generated at build‑time**.
20+
2. The app **pre‑renders once**.
21+
3. Runtime state changes only **flip a `data-*`**.
22+
23+
Result → **5-10× faster visual updates** with **ZERO additional bundle cost**.
24+
25+
### 📊 Micro‑benchmarks (Apple M1)
26+
27+
| Nodes updated | React state | Zero‑UI | Speed‑up |
28+
| ------------- | ----------- | ------- | -------- |
29+
| 10,000 | \~50 ms | \~5 ms | **10×** |
30+
| 25,000 | \~180 ms | \~15 ms | **12×** |
31+
| 50,000 | \~300 ms | \~20 ms | **15×** |
32+
33+
Re‑run these numbers yourself via the links above with chrome dev tools.
34+
35+
---

docs/assets/experimental.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
## 🧪 Experimental: SSR-safe `zeroOnClick`
3+
4+
Enable client-side interactivity **without leaving server components**.
5+
Just 300 bytes of runtime overhead.
6+
7+
### ⚙️ Installation (Zero-UI Experimental)
8+
9+
```bash
10+
npm install @react-zero-ui/core@0.3.1-beta.2
11+
```
12+
13+
Initialize once in your root:
14+
15+
```tsx
16+
"use client"
17+
18+
/* ① import the generated defaults */
19+
import { variantKeyMap } from "../.zero-ui/attributes"
20+
/* ② activate the runtime shipped in the package */
21+
import { activateZeroUiRuntime } from "@react-zero-ui/core/experimental/runtime"
22+
23+
activateZeroUiRuntime(variantKeyMap)
24+
25+
export const ZeroUiRuntime = () => null // this component just runs the side effect
26+
27+
```
28+
29+
### Usage
30+
31+
```tsx
32+
import { zeroSSR } from "@react-zero-ui/core/experimental"
33+
34+
<div {...zeroSSR.onClick("theme", ["dark", "light", "spanish"])} >
35+
Click me to cycle themes!
36+
</div>
37+
```
38+
39+
Usage is the same as the `useUI` hooks:
40+
```html
41+
<div class="theme-dark:bg-black theme-light:bg-white">
42+
Interactive Server Component!
43+
</div>
44+
```
45+
46+
**Scoped Version:**
47+
48+
```tsx
49+
import { zeroScopedOnClick } from '@react-zero-ui/experimental';
50+
51+
<div data-model="open"> // set the scope with a data-attribute that matches the key
52+
<button {...scopedZeroSSR.onClick("modal", ["open", "closed"])}>
53+
Toggle Modal
54+
</button>
55+
</div>
56+
```
57+
58+
see the source code for the `zeroSSR` object, see [experimental](/packages/core/src/experimental)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
### Manual Install Vite
3+
4+
```bash
5+
npm install @react-zero-ui/core @tailwindcss/postcss
6+
```
7+
8+
---
9+
10+
## 🔧 Setup
11+
12+
### Vite
13+
14+
```js
15+
// vite.config.*
16+
import zeroUI from "@react-zero-ui/core/vite";
17+
import { defineConfig } from 'vite';
18+
import react from '@vitejs/plugin-react';
19+
20+
export default defineConfig({
21+
// ❗️Remove the default `tailwindcss()` plugin — Zero‑UI extends it internally
22+
plugins: [zeroUI(), react()]
23+
});
24+
```
25+
### Next.js (App Router)
26+
27+
2. **Add the PostCSS plugin (must come _before_ Tailwind).**
28+
29+
```js
30+
// postcss.config.js
31+
module.exports = { plugins: { '@react-zero-ui/core/postcss': {}, tailwindcss: {} } };
32+
```
33+
34+
---
35+
36+
37+
38+
39+
1. **Spread `bodyAttributes` on `<body>`** in your root layout.
40+
41+
```tsx
42+
// app/layout.tsx
43+
import { bodyAttributes } from './.zero-ui/attributes';
44+
// or: import { bodyAttributes } from '../.zero-ui/attributes';
45+
46+
export default function RootLayout({ children }) {
47+
return (
48+
<html lang="en">
49+
<body {...bodyAttributes}>{children}</body>
50+
</html>
51+
);
52+
}
53+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 🧪 Experimental Runtime (Zero-UI)
2+
3+
This folder contains the **SSR-safe runtime logic** for handling interactivity in React server components without using `use client`. It is designed to be tiny (\~300 bytes), deterministic, and fully compatible with React Zero-UI's pre-rendered data-attribute model.
4+
5+
---
6+
7+
## 📦 What This Code Does
8+
9+
### `activateZeroUiRuntime()`
10+
11+
This is the core runtime entrypoint. When called:
12+
13+
1. **Registers a single global click event listener** on `document`.
14+
15+
2. **Listens for clicks** on any element (or ancestor) with a `data-ui` attribute.
16+
17+
3. **Parses the `data-ui` directive** with this format:
18+
19+
* `data-ui="global:key(val1,val2,...)"` → flips `data-key` on `document.body`
20+
* `data-ui="scoped:key(val1,val2,...)"` → flips `data-key` on closest matching ancestor or self
21+
22+
4. **Cycles the value** of the matching `data-*` attribute in a round-robin fashion.
23+
24+
5. **Updates the DOM instantly**, enabling Tailwind to respond via selectors (e.g. `theme-dark:bg-black`).
25+
26+
It guards against duplicate initialization using a `window.__zero` flag.
27+
28+
---
29+
30+
### `zeroSSR.onClick()` / `scopedZeroSSR.onClick()`
31+
32+
These utility functions generate valid `data-ui` attributes for use in JSX/TSX.
33+
34+
They return a prop like this:
35+
36+
```tsx
37+
{ 'data-ui': 'global:theme(dark,light)' }
38+
```
39+
40+
or
41+
42+
```tsx
43+
{ 'data-ui': 'scoped:modal(open,closed)' }
44+
```
45+
46+
**In development**, they also perform validation:
47+
48+
* Ensures the key is kebab-case.
49+
* Ensures at least one value is provided.
50+
51+
---
52+
53+
## 🧠 Design Notes
54+
55+
* **No React state or re-renders** involved.
56+
* Works entirely via DOM `data-*` mutations.
57+
* Compatible with all server components.
58+
* Fully tree-shakable and side-effect-free unless `activateZeroUiRuntime()` is called.
59+
60+
This design makes it ideal for pairing with Tailwind-style conditional classes in static components.
61+
62+
---
63+
64+
## 🧼 Summary
65+
66+
* `activateZeroUiRuntime()` → enables click handling on static components via `data-ui`
67+
* `zeroSSR` / `scopedZeroSSR` → emit valid click handlers as JSX props
68+
* No state. No runtime overhead. Works in server components.
69+
70+
This runtime is the bridge between **static HTML** and **interactive UX**, while keeping everything **server-rendered** and blazing fast.

0 commit comments

Comments
 (0)