An event handler is a function or method that runs when an event occurs. A button-click handler can open a page; a change handler can react when a user selects a different radio button. Handlers let a component respond to its own controls, while listeners on the component let the surrounding app respond to events it dispatches. section 5.5 section 11.3
Handle a button click
In Lit, `<button @click=${this.handleClick}>Google It</button>` connects the click event to a method. That method calls `window.open(...)`. Stencil expresses the same connection as `onClick={this.handleClick}`; FAST uses `@click=${x => x.handleClick()}`. The syntax differs, but each connects a rendered button to code that runs when it is clicked. section 5.5 section 6.6 section 7.8
Handlers can also change component state. In a sortable table, each heading button calls `updateSort(property)`. That method updates the sort property and direction, then dispatches a `sort` event whose `detail` contains the new criteria. An app can register a listener on the `<sortable-table>` element to receive them. When an event must reach ancestors outside shadow DOM, dispatch it with `bubbles: true` and `composed: true`. section 5.9 section 4.2.14
Listing 4.16 Listening for "sort" events
const sortableTable = document.querySelector('sortable-table');
sortableTable?.addEventListener("sort", (event) => {
const { property, descending } = event.detail;
console.log(
`The table is now sorted by ${property}`,
`${descending ? "descending" : "ascending"}.`,
);
});
See how an app attaches a `sort` listener to the component and reads the event’s `detail`. section 4.2.14
Attach a listener after its target exists. For example, when server-rendered radio-group HTML is inserted later, an insertion event handler finds the new component and then attaches its `change` listener. Inside the radio group, a separate handler listens for input changes and dispatches a change event for the host app. section 11.3




























