zulip/README.md

147 lines
3.3 KiB
Markdown
Raw Normal View History

2026-03-04 17:45:32 -08:00
# zulip
2026-03-04 11:13:15 -08:00
2026-03-04 18:11:14 -08:00
A modern, no-build web application using Lit web components with Zustand state management.
2026-03-04 14:18:44 -08:00
2026-03-04 12:23:40 -08:00
## Architecture Overview
* **Lit-based web components** - Standards-based, framework-agnostic UI
* **No-build tooling** - Import maps only, no bundler required
* **Zustand state management** - Lightweight, vanilla JS store
2026-03-04 18:11:14 -08:00
* **StoreController** - Reactive controller that bridges Zustand and Lit
2026-03-04 12:23:40 -08:00
2026-03-04 18:11:14 -08:00
## Project Structure
2026-03-04 11:13:15 -08:00
```
2026-03-04 18:11:14 -08:00
components/ -> standard lit components, including top-level "app-root"
controllers/ -> lit reactive controllers (such as Zustand<->Lit bridge)
store/ -> defines specific accessor sets
index.html -> entry point with inline importmap and app-root
README.md
2026-03-04 11:13:15 -08:00
```
2026-03-04 18:11:14 -08:00
## Data Flow Diagram
2026-03-04 12:23:40 -08:00
```mermaid
sequenceDiagram
participant User
participant LitComponent
participant StoreController
participant ZustandStore
User->>LitComponent: Click button
LitComponent->>ZustandStore: Call action (e.g., addItem)
ZustandStore->>ZustandStore: Update state
ZustandStore->>StoreController: Notify subscribers
StoreController->>LitComponent: requestUpdate()
LitComponent->>LitComponent: Re-render
LitComponent->>User: Show updated UI
```
2026-03-04 18:11:14 -08:00
## Example
2026-03-04 12:23:40 -08:00
2026-03-04 18:11:14 -08:00
### 1. Define Store (Zustand Vanilla)
2026-03-04 12:23:40 -08:00
2026-03-04 18:11:14 -08:00
```javascript
// store/index.js
import { createStore } from 'zustand/vanilla'
2026-03-04 12:23:40 -08:00
2026-03-04 18:11:14 -08:00
export const store = createStore((set, get) => ({
count: 0,
increment: () => set(s => ({ count: s.count + 1 })),
decrement: () => set(s => ({ count: s.count - 1 })),
}))
2026-03-04 12:23:40 -08:00
```
2026-03-04 18:11:14 -08:00
### 2. Create StoreController (Bridge)
2026-03-04 12:23:40 -08:00
```javascript
2026-03-04 18:11:14 -08:00
// controllers/store.js
export class StoreController {
constructor(host, store, selector) {
this.host = host
this.store = store
this.selector = selector
host.addController(this)
}
hostConnected() {
this._unsub = this.store.subscribe((state) => {
const next = this.selector(state)
if (next !== this.value) {
this.value = next
this.host.requestUpdate()
}
})
this.value = this.selector(this.store.getState())
}
hostDisconnected() {
this._unsub?.()
}
}
2026-03-04 11:13:15 -08:00
```
2026-03-04 12:23:40 -08:00
2026-03-04 18:11:14 -08:00
### 3. Create Component (Lit + StoreController)
2026-03-04 12:23:40 -08:00
```javascript
// components/counter.js
import { LitElement, html, css } from 'lit'
class Counter extends LitElement {
2026-03-04 14:18:44 -08:00
static properties = {
count: { type: Number, state: true }
}
constructor() {
super()
this.count = 0
}
2026-03-04 12:23:40 -08:00
static styles = css`
2026-03-04 18:11:14 -08:00
button { padding: 1rem; font-size: 1.2rem; margin: 0.5rem; }
2026-03-04 12:23:40 -08:00
`
render() {
return html`
<div>
<p>Count: ${this.#count.value}</p>
2026-03-04 18:11:14 -08:00
<button @click=${() => store.getState().decrement()}>-</button>
<button @click=${() => store.getState().increment()}>+</button>
2026-03-04 12:23:40 -08:00
</div>
`
}
}
customElements.define('my-counter', Counter)
```
2026-03-04 18:11:14 -08:00
### 4. Instantiate in parent (such as app-root)
2026-03-04 14:18:44 -08:00
2026-03-04 17:45:32 -08:00
```js
2026-03-04 18:11:14 -08:00
import counter from "./counter.js":
2026-03-04 14:18:44 -08:00
class AppRoot extends LitElement {
2026-03-04 17:45:32 -08:00
render() {
2026-03-04 18:11:14 -08:00
return html`
<counter></counter>
`;
2026-03-04 14:18:44 -08:00
}
2026-03-04 17:45:32 -08:00
}
```
2026-03-04 14:18:44 -08:00
2026-03-04 17:45:32 -08:00
## Benefits Over TSX
1. **No build step** - Edit and refresh, instant feedback
1. **Smaller bundle** - No framework runtime, just standards
1. **Better encapsulation** - Shadow DOM, scoped styles
1. **Framework agnostic** - Works anywhere, even in React apps
1. **Future-proof** - Built on web standards
2026-03-04 18:11:14 -08:00
1. **Familiar patterns** - Zustand works like Redux/Context
2026-03-04 12:23:40 -08:00
2026-03-04 18:11:14 -08:00
1. **Type-safe** - Can add JSDoc or TypeScript without build step