sercrod

*prevent-default

Summary

*prevent-default attaches passive helpers that call event.preventDefault() for common UI events on the host element:

It has a short alias *prevent, and both directives share the same behavior and options. n-prevent-default and n-prevent are attribute aliases for the same feature.

Use *prevent-default when you always want to suppress the browser’s default behavior for Enter and/or form submission on a specific element, regardless of what your own event handlers do.

Basic example

Prevent Enter from submitting a search form, but still handle the submit event in Sercrod:

<serc-rod id="search" data='{}'>
  <form *prevent-default="'submit'" @submit="runSearch()">
    <input type="search" name="q" placeholder="Search..." />
    <button type="submit">Search</button>
  </form>
</serc-rod>

Behavior:

Behavior

*prevent-default is not a structural directive; it does not change how the DOM is rendered. Instead, it wires extra event listeners on the rendered element:

Aliases:

Modes

The directive supports a small set of string modes. The attribute value is read and normalized as:

Recognized modes:

Any other string:

Evaluation timing

*prevent-default is evaluated when Sercrod renders the host element and sets up its event handlers:

On re-render:

Execution model

Conceptually, when Sercrod processes an element that has *prevent-default or *prevent:

  1. Determine the mode:

    • Read *prevent-default or *prevent from the template node.
    • If it is empty or missing, use "enter"; otherwise, use the lowercase string value.
  2. Attach listeners on the rendered element:

    • For "enter" and "all":

      • Add keydown listener.
      • If e.key === "Enter", call e.preventDefault().
    • For "submit" and "all":

      • If the element is a FORM, add submit listener.
      • In that listener, always call e.preventDefault().
  3. The directive does not alter the element’s scope or its child rendering. It only adds these helper listeners.

Interaction with @event handlers:

The exact order of handler execution is an implementation detail, but you can rely on the fact that preventDefault() will be called for the configured modes, regardless of whether your handler calls it.

Scope and variables

*prevent-default:

Use inside event handlers:

Use with event handlers and modifiers

*prevent-default is designed to complement Sercrod’s event attributes:

Relationship to event modifiers:

Best practices

Additional examples

Prevent both Enter and submit on a login form:

<serc-rod id="login" data='{}'>
  <form *prevent-default="'all'" @submit="doLogin()">
    <label>
      Email
      <input type="email" name="email" />
    </label>
    <label>
      Password
      <input type="password" name="password" />
    </label>
    <button type="submit">Sign in</button>
  </form>
</serc-rod>

Use the short alias *prevent:

<serc-rod id="contact" data='{}'>
  <form *prevent="'submit'" @submit="sendMessage()">
    <textarea name="body"></textarea>
    <button type="submit">Send</button>
  </form>
</serc-rod>

In both examples:

Notes