GitHub v1.3.1
Lesson 03

Attributes & Data

The bridge between HTML and Coralite. Attributes declare the inputs your component accepts and seed reactive state.

Declaring Inputs

Attributes are the entry point for data flowing into your component from HTML. In Coralite, you declare a strict schema using the attributes export. Only primitives are allowed.

html
Code copied!
  <template id="user-badge">
    <div class="badge">
      <span>{{ name }}</span>
      <span>{{ role }}</span>
    </div>
  </template>
  
  <script type="module">
    export const attributes = {
      name: { type: String, required: true },
      role: { type: String, default: 'Member' }
    }
  </script>

Coercion and State

When a user writes <user-badge name="Alice" role="Admin"> on your page, Coralite reads those DOM attributes, coerces them to their declared types, and exposes them as flat tokens in your template.

javascript
Code copied!
  // Host HTML:
  // <user-badge name="Alice" role="Admin"></user-badge>
  
  // Component state:
  state.name = 'Alice'  // coerce to String
  state.role = 'Admin'  // coerce to String

Diagnostic Safety

Coralite enforces two critical rules at build time to keep your components predictable and AI-readable.

CORALITE-E101: Invalid Attribute Type

Attribute types must be String, Number, or Boolean. Complex types like Object or Array are forbidden. Use server data or provide/inject for complex structures.

CORALITE-E102: Mutex Violation

required: true cannot be combined with a default value. A required attribute must be supplied by the consumer.

Reflection

By default, String and Number attributes do not reflect back to the host DOM. Boolean attributes do—they appear as attr="" when truthy and are removed when falsy. You can force reflection on other types with reflect: true.