|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +title: DruidUI |
| 4 | +description: WebAssembly framework for sandboxed UIs |
| 5 | +--- |
| 6 | + |
| 7 | +# DruidUI |
| 8 | + |
| 9 | +DruidUI is a React-like framework that compiles to **WebAssembly** for building sandboxed user interfaces. Use it when you need to run untrusted UI code safely. |
| 10 | + |
| 11 | +## Why WebAssembly? |
| 12 | + |
| 13 | +Traditional approaches can't provide true sandboxing: |
| 14 | +- **JavaScript** - Can access all browser APIs |
| 15 | +- **iframes** - Limited state sharing, poor UX |
| 16 | + |
| 17 | +**WebAssembly** provides complete isolation - code can only access explicitly granted functions. |
| 18 | + |
| 19 | +## Quick Start |
| 20 | + |
| 21 | +```bash |
| 22 | +npx create-druid-ui my-app |
| 23 | +cd my-app |
| 24 | +npm install |
| 25 | +npm run dev |
| 26 | +``` |
| 27 | + |
| 28 | +### Hello World |
| 29 | + |
| 30 | +```tsx |
| 31 | +import { createComponent, Context } from '@druid-ui/component'; |
| 32 | + |
| 33 | +let count = 0; |
| 34 | + |
| 35 | +export const component = createComponent((ctx: Context) => { |
| 36 | + return ( |
| 37 | + <main> |
| 38 | + <h1>Hello Druid!</h1> |
| 39 | + <button onClick={() => { count++; }}> |
| 40 | + Clicked {count} times |
| 41 | + </button> |
| 42 | + </main> |
| 43 | + ); |
| 44 | +}); |
| 45 | +``` |
| 46 | + |
| 47 | +### Build & Deploy |
| 48 | + |
| 49 | +```bash |
| 50 | +# Build to WASM |
| 51 | +npm run build # → dist/component.wasm |
| 52 | + |
| 53 | +# Add to scroll |
| 54 | +ui: |
| 55 | + - name: dashboard |
| 56 | + path: ui/dashboard.wasm |
| 57 | + route: /admin |
| 58 | +``` |
| 59 | + |
| 60 | +## Core Concepts |
| 61 | + |
| 62 | +### Functional Components |
| 63 | + |
| 64 | +Similar to [React](https://react.dev) and [Mithril.js](https://mithril.js.org/): |
| 65 | + |
| 66 | +- JSX/TSX syntax |
| 67 | +- Every event triggers full rerender (no complex diffing) |
| 68 | +- Module-level state (no hooks) |
| 69 | + |
| 70 | +```tsx |
| 71 | +let items = ['todo 1', 'todo 2']; |
| 72 | + |
| 73 | +export const component = createComponent(() => { |
| 74 | + return ( |
| 75 | + <ul> |
| 76 | + {items.map(item => <li>{item}</li>)} |
| 77 | + </ul> |
| 78 | + ); |
| 79 | +}); |
| 80 | +``` |
| 81 | + |
| 82 | +### Context |
| 83 | + |
| 84 | +Access route data via context: |
| 85 | + |
| 86 | +```tsx |
| 87 | +export const component = createComponent((ctx: Context) => { |
| 88 | + return ( |
| 89 | + <div> |
| 90 | + <p>Path: {ctx.path}</p> |
| 91 | + <p>User ID: {ctx.params.userId}</p> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +}); |
| 95 | +``` |
| 96 | + |
| 97 | +### Shadow DOM |
| 98 | + |
| 99 | +Each component renders in isolated Shadow DOM - styles can't leak in/out. |
| 100 | + |
| 101 | +## Development Workflow |
| 102 | + |
| 103 | +### Raw Mode (Fast) |
| 104 | + |
| 105 | +Develop without WASM overhead: |
| 106 | + |
| 107 | +```html |
| 108 | +<druid-ui no-sandbox src="/src/index.tsx"></druid-ui> |
| 109 | +``` |
| 110 | + |
| 111 | +- Instant hot reload |
| 112 | +- Normal browser debugging |
| 113 | +- Not sandboxed (dev only!) |
| 114 | + |
| 115 | +### Sandbox Mode (Production) |
| 116 | + |
| 117 | +Full WASM sandboxing: |
| 118 | + |
| 119 | +```bash |
| 120 | +npm run build |
| 121 | +npm run preview |
| 122 | +``` |
| 123 | + |
| 124 | +- True isolation |
| 125 | +- Production-ready |
| 126 | +- Slower iteration |
| 127 | + |
| 128 | +## Extension System |
| 129 | + |
| 130 | +By default, WASM can only call `d()`, `log()`, and `rerender()`. Add custom APIs via extensions: |
| 131 | + |
| 132 | +**1. Define interface (WIT):** |
| 133 | + |
| 134 | +```wit |
| 135 | +package my:api; |
| 136 | +
|
| 137 | +interface fetch { |
| 138 | + get-data: func(url: string) -> string; |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +**2. Implement in host:** |
| 143 | + |
| 144 | +```js |
| 145 | +druidElement.extensionObject = { |
| 146 | + 'my:api/fetch': { |
| 147 | + getData: async (url) => { |
| 148 | + const res = await fetch(url); |
| 149 | + return res.text(); |
| 150 | + } |
| 151 | + } |
| 152 | +}; |
| 153 | +``` |
| 154 | + |
| 155 | +**3. Use in component:** |
| 156 | + |
| 157 | +```tsx |
| 158 | +import { getData } from 'my:api/fetch'; |
| 159 | + |
| 160 | +const data = await getData('/api/status'); |
| 161 | +``` |
| 162 | + |
| 163 | +See [package documentation](./packages/component.md) for full API reference. |
| 164 | + |
| 165 | +## Multi-Language Support |
| 166 | + |
| 167 | +**Current:** JavaScript/TypeScript ✅ |
| 168 | + |
| 169 | +**Planned:** Rust, C++ (waiting for WebAssembly Component Model maturity) |
| 170 | + |
| 171 | +## Limitations |
| 172 | + |
| 173 | +- No async/await yet (use callback workaround) |
| 174 | +- Full rerenders on every event (less efficient than React) |
| 175 | +- JavaScript only for now |
| 176 | +- No SSR |
| 177 | + |
| 178 | +## Learn More |
| 179 | + |
| 180 | +- [NPM Packages](./packages/component.md) - API reference |
| 181 | +- [Examples](https://github.com/highcard-dev/druid-ui/tree/main/examples) |
| 182 | +- [Source Code](https://github.com/highcard-dev/druid-ui) |
0 commit comments