sercrod

*prevent

Summary

*prevent is a shorthand for *prevent-default. It attaches low-level listeners that call event.preventDefault() for specific events on the host element:

Unlike many other directives, the attribute value is not a Sercrod expression. It is a simple mode string such as enter, submit, or all.

Basic example

Prevent the browser’s default form submission, but still run your handler:

<serc-rod id="form-app" data='{"status": null}'>
  <form *prevent="submit" @submit="status = 'saved'">
    <input type="text" name="title">
    <button type="submit">Save</button>
    <p *if="status" *print="status"></p>
  </form>
</serc-rod>

In this example:

Behavior

*prevent and *prevent-default are handled by the same runtime branch:

Behavior by mode:

Any other mode string:

Relationship to *prevent-default

*prevent is purely syntactic sugar:

Typical usage patterns:

In both cases:

Modes in detail

Mode string handling:

Supported modes:

Evaluation timing

*prevent and *prevent-default are processed when Sercrod renders each element:

Important points:

Execution model

Conceptually, the runtime flow for *prevent looks like:

  1. Detect:

    • Check whether the node has *prevent-default or *prevent.
  2. Read mode:

    • raw = value of "*prevent-default" or "*prevent" (whichever exists).
    • mode = (raw || "enter").toLowerCase().
  3. Attach listeners:

    • If mode is enter or all:

      • Add a keydown listener like:

        • On keydown, if e.key === "Enter", call e.preventDefault().
    • If mode is submit or all and the host is a <form>:

      • Add a submit listener that always calls e.preventDefault().
  4. Continue:

    • Sercrod continues processing other directives and attributes as usual.
    • Event handlers registered via @... attributes are not replaced; they remain intact.

Interaction with Sercrod event handlers

*prevent is complementary to the @ event system:

Example: manual form handling

<serc-rod id="login-app" data='{"error": null}'>
  <form *prevent="submit" @submit="login()">
    <input type="text" name="user">
    <input type="password" name="pass">
    <button type="submit">Log in</button>
    <p *if="error" *print="error"></p>
  </form>
</serc-rod>

In this setup:

Key points:

Use cases

Typical scenarios where *prevent is useful:

Best practices

Additional examples

Block Enter on a text input:

<input type="text"
       *prevent="enter"
       @keydown="onKeydown($event)">

Block both Enter and submit on a form:

<form *prevent="all" @submit="save()">
  <input type="text" name="title">
  <button type="submit">Save</button>
</form>

Notes