Overview

onClickAway(fn) fires on mousedown outside the component host — the standard pattern for dismissing dropdowns, popovers, and menus.

onFocus / onBlur listen on window for the browser window gaining or losing focus (not individual inputs). Use template @focus / @blur for element-level focus.

Focus & dismiss

API
Target
Notes
onClickAway(fn) document mousedown Fires when click target is outside the host element.
onFocus(fn) window focus Browser/tab regained focus — not per-element.
onBlur(fn) window blur Browser/tab lost focus.

Dropdown with onClickAway

onClickAway registers a document-level listener that fires whenever the user clicks outside the component's root element. The listener is removed automatically on unmount — no manual cleanup needed.

import { html, atom, nothing, onClickAway, renderApp } from 'mates';

const App = () => {
const open = atom(false);
const selected = atom('Choose…');

// Closes the dropdown when the user clicks anywhere outside this component
onClickAway(() => open.set(false));

const options = ['Apple', 'Banana', 'Cherry', 'Mango'];

return () => html`
<div class="m-col m-gap m-items-center">
<div>
<button class=${open() ? 'btn-primary' : ''} @click=${() => open.set(v => !v)}>
${selected()}
</button>
${open() ? html`
<div class="card card-body">
${options.map(opt => html`
<button class="btn-ghost btn-block"
@click=${() => { selected.set(opt); open.set(false); }}>
${opt}
</button>
`)}
</div>
` : nothing}
<p class="label m-t-10">Click outside to close</p>
</div>
</div>
`;
};
renderApp(App, document.getElementById('app'));