sercrod

:style

Summary

:style is an attribute binding directive that controls an element’s inline style through a Sercrod expression. It evaluates the expression and assigns the result directly to element.style.cssText. The binding is one way: changes in data update the inline styles, but changing styles in the DOM does not modify data.

Unlike *style or n-style, :style does not use the style filter. It is a thin, direct bridge between your expression and the element’s inline CSS string.

Basic example

Simple inline style binding:

<serc-rod id="app" data='{
  "highlight": true
}'>
  <p :style="highlight ? 'color: red; font-weight: bold;' : ''">
    This paragraph is red when highlight is true.
  </p>
</serc-rod>

Behavior:

Behavior

:style follows the general colon attribute-binding pipeline, with :style specific handling:

Evaluation timing

:style is evaluated as part of the element render pass, alongside other colon bindings:

Execution model

Conceptually, Sercrod’s execution model for :style looks like this:

  1. Detect the colon attribute:

    • When rendering an element node, inspect its attributes.
    • For each attribute with name starting with :, compute key = name.slice(1).
    • If key is "style", treat it as a :style binding.
  2. Evaluate the expression:

    • Let exprSource be the attribute value string (for example "highlight ? 'color: red;' : ''").
    • Call eval_expr(exprSource, scope, { el: node, mode: "attr:style" }).
    • If this call throws, jump to the error path.
  3. Apply the result:

    • Let val be the result of evaluation.
    • Set element.style.cssText = val || "".
  4. Error path:

    • If evaluation throws, set element.style.cssText = "".
  5. Cleanup of the binding attribute:

    • If _config.cleanup.handlers is enabled, remove the :style attribute from the element after it has been processed.

There is no additional filter step and no special handling for objects or arrays in :style; the expression result is used directly as the style string (subject to val || "").

Interaction with *style and n-style

Sercrod also provides *style and n-style directives that assign inline styles using a separate, filter aware pipeline:

Order and precedence on the same element:

This means:

Use with conditionals and loops

Because :style only binds an attribute, it composes freely with structural directives and loops around it:

Best practices

Additional examples

Using a computed style string in data:

<serc-rod id="app" data='{
  "styles": {
    "warning": "color: #b45309; background-color: #fffbeb; padding: 0.5rem;",
    "normal": ""
  },
  "mode": "warning"
}'>
  <p :style="styles[mode]">
    This paragraph style is controlled via data.styles[mode].
  </p>
</serc-rod>

Combining multiple conditions into one style string:

<serc-rod id="app" data='{
  "isActive": true,
  "isDisabled": false
}'>
  <button :style="(isActive ? 'border: 2px solid #2563eb;' : '') +
                  (isDisabled ? ' opacity: 0.5; pointer-events: none;' : '')">
    Click me
  </button>
</serc-rod>

Notes