How it works
Delegation is nothing more than the bubble phase plus a lookup. Dispatch builds the path from the target up to Window, and a non-capture listener on the container runs when the walk reaches it, with target still pointing at the original node. closest() walks from that node up through ancestors and returns the first that matches the selector, which may be the node itself or something above the container. Hence the contains() guard.
Because the lookup runs at event time, the container doesn't care when its children were created. That is the whole case for delegation in lists that re-render: no listener bookkeeping, and nothing to leak when rows are removed.
Edge cases
Non-bubbling events. In Chrome 156 and Firefox 159 a capture listener on document saw bubbles: false for focus, blur, mouseenter, mouseleave, pointerenter, element scroll, invalid, and an image's load and error; focusin, focusout, mouseover and pointerover were true. To emulate a per-item pointerenter, delegate pointerover and filter out moves inside the same item:
// <ul id="list"><li id="one">One <b>bold</b></li><li id="two">Two</li></ul>
const list = document.getElementById('list');
const log = [];
list.addEventListener('pointerover', (event) => {
const item = event.target.closest('li');
if (!item || item.contains(event.relatedTarget)) return; // moved within the item
log.push(`enter ${item.id}`);
});
list.addEventListener('mouseenter', (event) => log.push(`mouseenter ${event.target.id}`));
// real mouse: over <b>, then the rest of #one, <b> again, then #two
console.log(log.join(', '));
// → enter one, mouseenter list, enter two
The mouse was moved with Puppeteer's page.mouse.move() (trusted input) in Chrome 156 and Firefox 159; both printed the same. mouseenter fired once, for the list itself, never for an item.
Shadow DOM. By the time a click inside a shadow root reaches a delegate outside it, target is the host. composedPath() still lists the inner nodes when the shadow root is open:
// <div id="menu"><my-button id="host"></my-button></div>
const menu = document.getElementById('menu');
const host = document.getElementById('host');
host.attachShadow({ mode: 'open' }).innerHTML = '<button data-cmd="paste">Paste</button>';
menu.addEventListener('click', (event) => {
const viaClosest = event.target.closest('[data-cmd]');
const viaPath = event.composedPath().find((node) => node.dataset?.cmd);
console.log(`${event.target.nodeName} ${viaClosest} ${viaPath.dataset.cmd}`);
});
host.shadowRoot.querySelector('button').click();
// → MY-BUTTON null paste
Chrome 156 and Firefox 159 printed the same. A closed shadow root's internals are left out of composedPath() for outside listeners, so a component that wants to be delegated to should dispatch its own composed event instead.
Disabled controls. The HTML Standard requires a disabled form control to suppress user click events. Browsers go further: with a trusted Puppeteer click on a disabled <button>, and on a <span> inside one, Chrome 156 and Firefox 159 dispatched pointerdown to the delegate but no mousedown, mouseup or click. A delegate that must react (to show "why is this disabled?") needs pointerdown, or the button should use aria-disabled instead of disabled.
Real-world scenario
A data table with 5,000 rows and edit/delete buttons per row: one click listener on <tbody>, closest('button[data-action]'), and closest('tr').dataset.id for the row. Re-rendering the body costs no listener work, and the handler can't leak references to removed rows. Keyboard support comes free if the targets are real buttons, because Enter and Space on a focused <button> fire click.
Trade-offs
Delegation moves work from setup time to event time: every event in the container runs the lookup, including ones that match nothing. For click and input that's negligible; for pointermove or scroll it isn't, and a direct listener on the few elements that care is cheaper. It also gives up per-element control: one delegate can't easily be removed for one item, and its order relative to item-level handlers is fixed by the bubble phase.