# CSS Data Binding

Symbiote components can read CSS custom property values into component state. This enables CSS-driven configuration: theme values, layout parameters, localized strings — all settable from CSS without touching JavaScript.

## `cssInit$`

Use `cssInit$` to define properties initialized from CSS custom properties:
```js
class MyWidget extends Symbiote {
  cssInit$ = {
    '--columns': 1,       // fallback value if CSS prop not set
    '--label': '',
  }
}

MyWidget.template = html`
  <span>{{--label}}</span>
`;
```

```css
my-widget {
  --columns: 3;
  --label: 'Click me';
}
```

CSS values are parsed automatically — quoted strings become strings, numbers become numbers.

> Values should be valid JSON, parseable with `JSON.parse()`. Use numbers for boolean flags (`0`/`1`).

## `--` prefix in templates

You can bind to CSS custom properties directly in templates:
```js
class TestApp extends Symbiote {}

TestApp.template = html`
  <h1>{{--header}}</h1>
  <div>{{--text}}</div>
`;
```

```css
:root {
  --header: 'CSS Data';
  --text: 'Hello!';
}
```

> CSS custom properties are used for value initialization only. After that, they act like normal local context properties.

## Updating CSS data

Call `this.updateCssData()` to re-read CSS custom properties after runtime CSS changes.

Call `this.dropCssDataCache()` to clear the cached CSS data.

### Reacting to layout changes

CSS data is read once on initialization. To react dynamically — for example, when container queries change custom property values — use `ResizeObserver`:

```js
class AdaptiveWidget extends Symbiote {
  cssInit$ = {
    '--layout': 'stack',
  }

  renderCallback() {
    new ResizeObserver(() => this.updateCssData()).observe(this);
  }
}
```

```css
adaptive-widget {
  container-type: inline-size;
  --layout: 'stack';
}

@container (min-width: 600px) {
  adaptive-widget {
    --layout: 'grid';
  }
}
```

> [!WARNING]
> **CSS data bindings and SSR.** Computed styles (`getComputedStyle`) are not available during server-side rendering. In `ssrMode` / `isoMode` components, CSS data properties will use their init value (from `cssInit$` or empty string). Enable `devMode` to see warnings for this.

## Why CSS as data?

- **Browser-native**: CSS custom properties are a platform feature for passing context down the DOM cascade — no JS library needed.
- **Shadow DOM friendly**: CSS custom properties are accessible inside nested shadow roots, unlike other CSS properties.
- **Flexible**: Redefine values at any level using `style` or `class` attributes.
- **Framework-agnostic**: Works with any external framework, library, or meta-platform.
- **CSP-safe**: Unlike inline scripts, CSS is typically allowed by CSP policies — making it ideal for configuration.

---

Next: [Dev Mode →](./dev-mode.md)
