lace:click and events

Call component methods or set properties in response to DOM events. lace:click is the common case, but the same directive binds any DOM event as lace:<event>.

PREVIEW

count = 0

.prevent on a link
class ClickDemo(Component):
    count: int = 0

    def increment(self):
        self.count += 1

    def add(self, n: int):                # the int hint coerces "5" from the template
        self.count += n

    def inline_template(self):
        return """
        <div class="...">
            <p>count = {{ count }}</p>
            <button lace:click="increment" class="...">call method</button>
            <button lace:click="add(5)" class="...">add(5) - with arg</button>
            <button lace:click="count = 0" class="...">count = 0 - set property</button>
            <a href="#" lace:click.prevent="increment" class="...">.prevent on a link</a>
        </div>
        """

Syntax

<button lace:click="method_name">Click</button>
<button lace:click="method_name('arg')">With an argument</button>
<button lace:click="property = value">Set a property</button>
<button lace:click="$refresh">Re-render only, no method</button>

Any DOM event

Bind any event as lace:<event>: click, dblclick, submit, keydown, keyup, change, input, focus, blur, mouseenter, mouseleave, mousedown, mouseup.

<form lace:submit="save">...</form>          {# submit auto-prevents the native page reload #}
<input lace:keydown.enter="send">
<select lace:change="pick">...</select>
<div lace:mouseenter="preview(3)">...</div>

Modifiers

Modifier Behavior
(none) call the method or set the property
.prevent event.preventDefault() (automatic for lace:submit)
.stop event.stopPropagation()
.self only when the event target is this element, not a descendant
.once run at most once
.enter .escape .tab .up .down ... on keydown/keyup, only fire for that key
.renderless run the method on the server but skip the re-render/morph (side-effect actions)
.preserve-scroll keep the window scroll position steady across the resulting update
<button lace:click.stop.self="pick">Pick</button>
<input lace:keydown.enter="send">
<button lace:click.renderless="track_view">Log</button>
<button lace:click.preserve-scroll="load_more">Load more</button>

See also lace:confirm to gate an action behind a confirmation dialog.

View this page as markdown - handy as context for your AI tools. Full index at /llms.txt.

Theme
Accent
Ink