Events

Components communicate through events. Emit events from one component that others can receive, or dispatch browser events that JavaScript can listen to.

Broadcasting events

Use emit(event_name, payload) to broadcast an event to every component on the page. The sender doesn't know or care who is listening, any component with a matching method handles it. That loose coupling is ideal when one action, a checkout, a finished background job, a saved form, needs unrelated parts of the UI to react.

Buy the license below. The Purchase component emits notification_created, and the bell in the navbar, a completely separate Notification component, picks up the broadcast: its unread count ticks up, and the dropdown shows the message.

PREVIEW
Acme Store

Notifications

Nothing yet

Pro License

$29 one-time

class Notification(Component):
    notes: list = []
    unread: int = 0

    # Method name == event name, so this fires on every broadcast.
    def notification_created(self, payload):
        self.notes.insert(0, payload["message"])
        self.unread += 1

    def mark_read(self):
        self.unread = 0

    def inline_template(self):
        return """
        <div class="relative" x-data="{ open: false }">
            <button @click="open = !open" lace:click="mark_read">
                🔔 {% if unread %}<span class="badge">{{ unread }}</span>{% endif %}
            </button>
            <div x-show="open" x-cloak @click.outside="open = false">
                {% for note in notes %}
                    <p>{{ note }}</p>
                {% empty %}
                    <p>Nothing yet</p>
                {% endfor %}
            </div>
        </div>
        """
class Purchase(Component):
    owned: bool = False

    def buy(self):
        self.owned = True
        # Broadcast: the sender doesn't know who is listening.
        self.emit("notification_created", {"message": "Your download is ready"})

    def inline_template(self):
        return """
        <div class="...">
            <p>Pro License</p>
            <button lace:click="buy">Buy now</button>
        </div>
        """

Targeted events

Use emit_to(component_name, event_name, payload) to send events to a specific component. Only components matching the name will receive the event.

Send targeted events. Only the specified component receives each event.

PREVIEW

Alpha

No messages yet

Beta

No messages yet

class EventsEmitTo(Component):
    def send_to_alpha(self):
        self.emit_to("EventsTargetAlpha", "message_received", {"text": "Hello Alpha"})

    def send_to_beta(self):
        self.emit_to("EventsTargetBeta", "message_received", {"text": "Hello Beta"})

    def inline_template(self):
        return """
        <div class="...">
            <button lace:click="send_to_alpha" class="...">Send to Alpha</button>
            <button lace:click="send_to_beta" class="...">Send to Beta</button>
        </div>
        """
class EventsTargetAlpha(Component):
    message: str = ""

    def message_received(self, payload):
        self.message = payload["text"]

    def inline_template(self):
        return """
        <div class="...">
            <p class="...">Alpha</p>
            <p class="...">{{ message|default:"No messages yet" }}</p>
        </div>
        """

Browser events

Use dispatch_browser_event(event_name, payload) to dispatch a CustomEvent on the DOM. Any JavaScript can listen, including Alpine.js, vanilla JS, or other frameworks.

Interlace dispatches a browser event that Alpine.js receives.

PREVIEW

class EventsBrowser(Component):
    click_count: int = 0

    def trigger_event(self):
        self.click_count += 1
        # Dispatch a DOM CustomEvent any JS can listen to
        self.dispatch_browser_event("user-action", {"count": self.click_count})

    def inline_template(self):
        return """
        <div class="...">
            <button lace:click="trigger_event" class="...">Dispatch browser event</button>
            <!-- Any JS can listen; here Alpine.js does -->
            <div x-data="{ count: 0 }"
                @user-action.window="count = $event.detail.count">
                <p x-text="count ? 'Alpine received event #' + count : 'Waiting…'"></p>
            </div>
        </div>
        """

Browser events use window.dispatchEvent() with CustomEvent. Access payload via $event.detail in Alpine.js or event.detail in vanilla JS.

Event methods

Method Description
emit(event_name, payload) Broadcast to all components
emit_to(component_name, event_name, payload) Send to specific component by name
dispatch_browser_event(event_name, payload) Dispatch DOM CustomEvent for JavaScript

How to listen

Define a method with the same name as the event:

class MyComponent(Component):
    message: str = ""

    def notification_sent(self, payload):
        # Method name = event name
        self.message = payload['text']

When notification_sent is emitted, Interlace calls this method automatically with the payload.

Component Communication

Events enable loosely coupled components. A form can emit "user_saved" without knowing which components care. Multiple listeners can respond independently.

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

Theme
Accent
Ink