Overview

eleHook(mountFn) is the primitive for building element directives. mountFn receives a $ (DollarChain) bound to the host element, plus any user args you define on the returned factory.

Return { onUpdate, onCleanup } to sync on re-render and tear down resources. Many Mates directives (attr, style, onIntersect, onParent) are built on eleHook.

eleHook vs htmlHook vs onConnect

API
Target
Notes
eleHook Host element Imperative access to $.el; must be an element expression.
htmlHook Child part Renders into a template part (used by timerTemplate, masonryGrid, etc.).
onConnect One-shot connect Lightweight callback when the element connects — no update loop.

Canvas drawing with eleHook

eleHook receives a $ (DollarChain) bound to the element. Access the raw DOM node via $.el. The returned onUpdate hook re-runs on every re-render so the canvas redraws when the reactive color atom changes.

import { html, atom, eleHook, renderApp } from 'mates';

const App = () => {
const color = atom('#00b3a8');
return () => html`
<div class="m-col m-gap m-items-center">
<canvas
width="300"
height="150"
${eleHook($ => {
const canvas = $.el;
const ctx = canvas.getContext('2d');
const draw = (c) => {
ctx.clearRect(0, 0, 300, 150);
ctx.fillStyle = c;
ctx.beginPath();
ctx.arc(150, 75, 60, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = 'rgba(255,255,255,0.2)';
ctx.beginPath();
ctx.arc(130, 55, 20, 0, Math.PI * 2);
ctx.fill();
};
draw(color.val);
return {
onUpdate: () => draw(color.val),
};
})}
></canvas>
<input
type="color"
.value=${color()}
@input=${e => color.set(e.target.value)}
/>
<p class="label">eleHook gives direct DOM access — pick a color to redraw</p>

Focus on mount

eleHook can run one-shot mount logic — focus an input without onConnect.

import { html, eleHook, renderApp } from 'mates';

const App = () => () => html`
<div class="m-col m-gap">
<input
placeholder="Focused via eleHook"
${eleHook(($) => { $.el.focus(); })}
/>
</div>
`;
renderApp(App, document.getElementById('app'));