# Properties

## Property initialization

For simple components, define properties as class fields — Symbiote picks them up automatically via fallback:
```js
class MyComponent extends Symbiote {
  myProp = 'some value';
  someOtherProp = 123;
  oneMoreProp = true;
}
```

For complex components with shared context, computed props, or many reactive properties, use `init$` to explicitly declare state:
```js
class MyComponent extends Symbiote {
  init$ = {
    myProp: 'some value',
    '*sharedProp': [],
    '+computed': () => this.$.myProp.length,
  }
}
```
The `init$` map should be a flat object that provides access to all values by top-level keys. Nested property key access (e.g. `'obj.prop'`) is **not supported** — use flat names instead.

## Reading and writing values

Use the `$` proxy (a standard JavaScript `Proxy` object) to access property values:
```js
class MyComponent extends Symbiote {
  time = Date.now();

  renderCallback() {
    // Write a new value:
    this.$.time = Date.now();

    // Read current value:
    console.log(this.$.time);
  }
}
```

## Merging state

Assign an object to `$` to add or replace several state properties:

```js
const card = document.createElement('profile-card');
card.$ = { name: 'Alex', role: 'Reader' };
const state = card.$;

card.$ = { role: 'Editor', online: true };
// state === card.$; name remains 'Alex', role is 'Editor', online is added.
```

The component class must be registered before creating the element. Local state can be assigned before connection; missing `init$` defaults are added when it connects. Assignments preserve the proxy and omitted keys. The merge is shallow: a supplied object or array replaces that property's value by reference. Every supplied key notifies subscribers, even if a primitive value is unchanged. Keys are applied sequentially, so subscribers can observe intermediate values.

The setter uses `add$(stateObj, true)` and resolves shared (`*`), parent (`^`), and named (`CTX/key`) context keys. Named contexts must already exist. Select allowed keys from external data before assigning it; arbitrary keys can address other contexts or replace handler properties.

This also enables [binding an object to a child component's state](./templates.md#binding-an-object-to-child-state):

```js
ProfilePanel.template = html`
  <profile-card ${{$: 'profile'}}></profile-card>
`;
```

Publishing `profile` merges its fields into the child. This is a one-way binding. Mutating a nested field alone does not publish; reassign the parent property or call `notify('profile')` after changing it.

## Bulk updates

To update existing local properties while skipping unchanged string, number, and boolean values, use the `set$` method:
```js
class MyComponent extends Symbiote {

  init$ = {
    firstProp: 'some value',
    secondProp: 'some value',
  }

  renderCallback() {
    this.set$({
      firstProp: 'new value',
      secondProp: 'new value',
    });
  }

}
```

The optional second argument `forcePrimitives` triggers callbacks even if the value hasn't changed (for primitive types):
```js
this.set$({ count: 0 }, true); // forces notification even though value may be the same
```

## Property subscription

To subscribe to property changes, use the `sub` method:
```js
class MyComponent extends Symbiote {

  init$ = {
    myProp: 'some value',
  }

  renderCallback() {
    this.sub('myProp', (newVal) => {
      console.log(newVal);
    });

    // This will invoke the subscription callback:
    this.$.myProp = 'Changed value';
  }

}
```

The third argument `init` (default `true`) controls whether the handler is called immediately with the current value:
```js
this.sub('myProp', handler, false); // don't call handler immediately
```

To trigger change notification manually:
```js
this.notify('myProp');
```

## Adding properties dynamically

Add properties to the context at runtime:
```js
this.add('newProp', 'initial value');     // single property
this.add$({ prop1: 'a', prop2: 'b' });   // bulk add
```

Check if a property exists:
```js
this.has('myProp'); // true / false
```

## Computed properties

Computed properties recalculate their values automatically when dependencies change. Use the `+` prefix for the property name.

### Local computed (auto-tracked)

Dependencies are recorded automatically when the function executes:
```js
class MyComponent extends Symbiote {

  init$ = {
    a: 1,
    b: 2,
    '+sum': () => this.$.a + this.$.b, // auto-tracks 'a' and 'b'
  }

}

MyComponent.template = html`<div>{{+sum}}</div>`;
```

> Computed values are recalculated asynchronously (via `queueMicrotask`). The computed doesn't make an excess re-render if the final value has not changed.

### Cross-context computed (explicit deps)

When a computed property depends on external named context properties, you must declare dependencies explicitly:
```js
init$ = {
  local: 0,
  '+total': {
    deps: ['GAME/score'],
    fn: () => this.$['GAME/score'] + this.$.local,
  },
};
```

To trigger manual recalculation:
```js
this.notify('+computedText');
```

## Property context and tokens

Symbiote components can interact with different types of properties, not just local ones. The context type is set by key prefixes:

| Prefix | Meaning | Description |
|--------|---------|-------------|
| _(none)_ | Local | Component's own local state |
| `^` | Pop-up | Direct access to upper level component property (must be in parent's `init$`) |
| `*` | Shared | Share properties between components in the same workflow context |
| `/` | Named | Access abstract named data context |
| `--` | CSS Data | Initiate property from CSS custom property value |
| `@` | Attribute | Bind state property to HTML attribute value |
| `+` | Computed | Auto-calculated derived value |

> More details in the [Context](./context.md) section.

---

Next: [Context →](./context.md)
