@barocss/browser 0.4.0 → 0.6.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.
package/README.md CHANGED
@@ -8,6 +8,43 @@
8
8
 
9
9
  @barocss/browser provides a browser-specific runtime that automatically detects DOM changes and generates CSS in real-time. It includes DOM change detection, style injection, and performance optimizations for browser environments.
10
10
 
11
+ ## Recipe: BaroCSS next to a Tailwind/shadcn build (json-render)
12
+
13
+ Use this when the page already links a Tailwind 4 / shadcn build and a model sends json-render specs whose `className` values the build never saw. Copy it as is:
14
+
15
+ ```js
16
+ import { getRuntime, shadcnTheme, preloadJsonRenderClasses } from '@barocss/browser';
17
+ // CDN/UMD build: const { getRuntime, shadcnTheme, preloadJsonRenderClasses } = window.BaroCSS;
18
+
19
+ const runtime = getRuntime({
20
+ skipExisting: true, // only generate classes the build does not already define
21
+ config: {
22
+ cssVarPrefix: 'tw', // share --tw-* composite variables with the Tailwind build
23
+ theme: { extend: shadcnTheme }, // use the shadcn :root tokens (primary, muted-foreground, ...)
24
+ // preflight: leave unset. The layered preflight (@layer base) is the default and should stay on.
25
+ },
26
+ });
27
+ runtime.observe(document.body, { scan: true }); // also covers classes added later
28
+
29
+ const spec = validateResponse(response); // your catalog / class allowlist checks
30
+ preloadJsonRenderClasses(spec, runtime); // BEFORE mounting, so there is no unstyled flash
31
+ renderJsonUi(spec); // mount your json-render Renderer
32
+ ```
33
+
34
+ The five settings: `skipExisting: true`, `cssVarPrefix: 'tw'`, `theme: { extend: shadcnTheme }`, `preloadJsonRenderClasses(spec, runtime)` before mount, and the default layered preflight (don't set `preflight: false`).
35
+
36
+ **Non-shadcn site theme:** put the site's own tokens in `theme.extend` (e.g. `colors: { brand: { 600: '#2563eb' } }`). Literal values are safe. Pointing a token at the build's own var name (`brand: { 600: 'var(--color-brand-600)' }`) is also fine: BaroCSS skips that self-referencing `:root` var, so the build's value wins and `bg-brand-600` still uses it.
37
+
38
+ **Verify it rendered** (DevTools console, after mount):
39
+
40
+ ```js
41
+ runtime.getCss('bg-primary'); // a CSS string once generated (undefined if the build already had it)
42
+ getComputedStyle(document.querySelector('[class~="bg-primary"]')).backgroundColor; // not 'rgba(0, 0, 0, 0)'
43
+ document.querySelectorAll('style[id^="barocss-runtime"]').length; // > 0
44
+ ```
45
+
46
+ **Browser support:** Chrome/Edge 85+, Safari/iOS 16.4+, Firefox 128+. The runtime needs CSS `@property`; composite utilities (shadows, rings, transforms, filters) may not render on older engines.
47
+
11
48
  ## ✨ Key Features
12
49
 
13
50
  - **🚀 Real-time DOM Detection** - Automatically detects and processes class changes
@@ -36,7 +73,7 @@ yarn add @barocss/browser
36
73
  ```typescript
37
74
  import { BrowserRuntime } from '@barocss/browser';
38
75
 
39
- // Initialize runtime
76
+ // Standalone page with no Tailwind build: defaults are fine. Next to a build, use the recipe above.
40
77
  const runtime = new BrowserRuntime();
41
78
 
42
79
  // Watch DOM changes and auto-style
@@ -51,6 +88,25 @@ document.body.innerHTML = `
51
88
  `;
52
89
  ```
53
90
 
91
+ ### Preload classes from json-render
92
+
93
+ Call the preloader after validating the response and before mounting the renderer:
94
+
95
+ ```typescript
96
+ import { BrowserRuntime, preloadJsonRenderClasses, shadcnTheme } from '@barocss/browser';
97
+
98
+ // Same options as the recipe above; drop them only when there is no Tailwind/shadcn build on the page.
99
+ const runtime = new BrowserRuntime({
100
+ skipExisting: true,
101
+ config: { cssVarPrefix: 'tw', theme: { extend: shadcnTheme } },
102
+ });
103
+ const spec = validateResponse(response); // Your catalog and class allowlist checks
104
+ preloadJsonRenderClasses(spec, runtime);
105
+ renderJsonUi(spec); // Mount your json-render Renderer here
106
+ ```
107
+
108
+ The helper reads literal `props.className` strings in the flat `spec.elements` map. It splits class lists, removes duplicates, and calls `runtime.addClass` synchronously. It does not return a CSS readiness result. The application must validate the spec, response size, class allowlist, class support, and runtime state before this call. The helper reads every entry, including nodes that the renderer may not mount. State-derived classes and classes added inside registered components need a separate source of classes.
109
+
54
110
  ### CDN Usage
55
111
 
56
112
  ```html
@@ -70,7 +126,7 @@ document.body.innerHTML = `
70
126
  <script type="module">
71
127
  import { BrowserRuntime } from 'https://unpkg.com/@barocss/browser@latest/dist/cdn/barocss.js';
72
128
 
73
- const runtime = new BrowserRuntime();
129
+ const runtime = new BrowserRuntime(); // no Tailwind build here; next to one, use the recipe's options
74
130
  runtime.observe(document.body, { scan: true });
75
131
  </script>
76
132
  </body>
@@ -90,7 +146,7 @@ The browser runtime provides real-time CSS generation:
90
146
  ```typescript
91
147
  import { BrowserRuntime } from '@barocss/browser';
92
148
 
93
- const runtime = new BrowserRuntime();
149
+ const runtime = new BrowserRuntime(); // no Tailwind build here; next to one, use the recipe's options
94
150
 
95
151
  // Automatically detects and processes these changes:
96
152
  document.body.innerHTML = `
@@ -114,7 +170,7 @@ document.body.innerHTML = `
114
170
  ```typescript
115
171
  import { BrowserRuntime } from '@barocss/browser';
116
172
 
117
- const runtime = new BrowserRuntime();
173
+ const runtime = new BrowserRuntime(); // no Tailwind build here; next to one, use the recipe's options
118
174
 
119
175
  // Watch entire document
120
176
  runtime.observe(document.body, { scan: true });
@@ -129,6 +185,7 @@ runtime.observe(container, { scan: true });
129
185
  ```typescript
130
186
  import { BrowserRuntime } from '@barocss/browser';
131
187
 
188
+ // Custom theme, no Tailwind build; next to one, add the recipe's options too
132
189
  const runtime = new BrowserRuntime({
133
190
  config: {
134
191
  theme: {
@@ -154,7 +211,7 @@ const runtime = new BrowserRuntime({
154
211
  ```typescript
155
212
  import { BrowserRuntime } from '@barocss/browser';
156
213
 
157
- const runtime = new BrowserRuntime();
214
+ const runtime = new BrowserRuntime(); // no Tailwind build here; next to one, use the recipe's options
158
215
 
159
216
  // Get runtime statistics
160
217
  const stats = runtime.getStats();
@@ -167,6 +224,10 @@ runtime.clearCaches();
167
224
 
168
225
  ## 🔧 Configuration
169
226
 
227
+ `getRuntime()` / `baroStart()` share one runtime. Passing a `config` when that runtime
228
+ already exists applies it with `updateConfig` (replacing the whole config), so calling
229
+ `getRuntime()` before `baroStart({ config })` does not lose the config.
230
+
170
231
  ### Runtime Options
171
232
 
172
233
  ```typescript
@@ -271,3 +332,29 @@ This project is licensed under the MIT License - see the [LICENSE](../../LICENSE
271
332
  ---
272
333
 
273
334
  **@barocss/browser** - Real-time CSS generation for browsers.
335
+
336
+ ## BaroCSS next to a shadcn build
337
+
338
+ A shadcn app built with Tailwind 4 can make the runtime use its theme without a duplicate JS config:
339
+
340
+ ```js
341
+ import { baroStart, shadcnTheme } from '@barocss/browser';
342
+
343
+ baroStart({ config: { theme: { extend: shadcnTheme } } }); // theme only; with a Tailwind build + json-render use the full recipe at the top
344
+ ```
345
+
346
+ `shadcnTheme` maps the shadcn colours (`background`, `primary`, `muted-foreground`, `border`, `ring`, `chart-1..5`, `sidebar-*`, ...) and `rounded-sm/md/lg/xl` to the raw `:root` variables (`var(--primary)`, `calc(var(--radius) - 2px)`). It does not use `--color-*`, because `@theme inline` doesn't emit those to the page. Opacity modifiers such as `bg-primary/90` work. It expects full colour values in `:root`, as shadcn v4 ships them (e.g. `--primary: oklch(0.205 0 0)`). Older shadcn v3 themes that store bare HSL channels (`--primary: 222 47% 11%`) won't resolve through `var(--primary)`; map those tokens to `hsl(var(--primary))` in your own `theme.extend` instead.
347
+
348
+ Custom tokens (for example `--brand`) are not included. Add them yourself: `theme: { extend: { ...shadcnTheme, colors: { ...shadcnTheme.colors, brand: 'var(--brand)' } } }`.
349
+
350
+ ## BaroCSS next to a Tailwind build (companion mode)
351
+
352
+ When the page already links a Tailwind 4 build and the runtime only fills in classes the build did not see, set `cssVarPrefix: 'tw'` so the runtime writes its composite variables with the build's names (`--tw-shadow`, `--tw-ring-shadow`, `--tw-translate-x`, `--tw-skew-x`, `--tw-blur`, `--tw-border-style`, ...):
353
+
354
+ ```js
355
+ baroStart({ skipExisting: true, config: { cssVarPrefix: 'tw' } }); // add theme: { extend: shadcnTheme } for shadcn (recipe at the top)
356
+ ```
357
+
358
+ The rename applies to every `--baro-` name in the generated CSS, including `--baro-*` names you write in your own arbitrary or custom-property values.
359
+
360
+ A build class and a runtime class on one element then compose: build `ring-2` + runtime `shadow-md` gives both layers, build `translate-x-2` + runtime `translate-y-4` gives `8px 16px`, build `border-dashed` + runtime `border-2` stays dashed. Without it the runtime uses `--baro-*` names, and the two halves overwrite each other. Leave it unset when there is no Tailwind build. Gradient stops (`from-*`/`via-*`/`to-*` with `bg-linear-*`) do not yet follow Tailwind's variable protocol, so mixing them between build and runtime is not supported.