Overview

onFileDrop(fn) attaches window-level dragover + drop handlers, prevents the browser from navigating to the dropped file, and passes the native DragEvent. Read files via e.dataTransfer.files.

onCopy, onCut, and onPaste receive ClipboardEvent. onSelectionChange receives the current Selection (or null). All listeners are removed on unmount.

Files & clipboard APIs

API
Event
Payload
onFileDrop(fn) window drop fn(DragEvent) — use e.dataTransfer.files.
onPaste(fn) paste fn(ClipboardEvent) — text or files via clipboardData.
onCopy / onCut(fn) copy / cut Can preventDefault and setData.
onSelectionChange(fn) selectionchange fn(Selection | null) from window.getSelection().

onFileDrop — window file drop

Drop files onto the preview. onFileDrop prevents default navigation and delivers the DragEvent so you can read dataTransfer.files.

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

const DropZone = () => {
const names = atom([]);
const count = atom(0);

// Window-level drop — prevents default navigation; receives DragEvent
onFileDrop((e) => {
const files = Array.from(e.dataTransfer?.files ?? []);
names.set(files.map(f => f.name));
count.set(files.length);
});

return () => html`
<div class="card card-body">
<p class="muted">Drop files onto this preview</p>
<span class="tag">${count()} file${count() === 1 ? '' : 's'}</span>
</div>
<div class="card card-body">
<span class="overline">Dropped</span>
${names().length === 0
? html`<div class="mono subtle">None yet…</div>`
: names().map(n => html`<div class="mono">${n}</div>`)
}
</div>
`;
};
renderApp(DropZone, document.getElementById('app'));

onPaste / onCopy / onSelectionChange

Select text, copy, and paste inside the preview to see clipboard and selection hooks update.

import { html, atom, onPaste, onCopy, onSelectionChange, renderApp } from 'mates';

const ClipboardDemo = () => {
const pasted = atom('—');
const selection = atom('');
const copies = atom(0);

onPaste((e) => {
const text = e.clipboardData?.getData('text/plain') ?? '';
pasted.set(text || '(empty)');
});

onCopy(() => {
copies.set(n => n + 1);
});

onSelectionChange((sel) => {
selection.set(sel?.toString() ?? '');
});

return () => html`
<div class="m-col m-gap">
<p class="muted">
Select text below, copy it, or paste into the preview.
</p>
<p>
Select this sentence, then press Ctrl/Cmd+C. Paste with Ctrl/Cmd+V.
</p>
<div class="m-grid m-grid-cols-3 m-gap">
<div class="card card-body center">
<span class="overline">Copies</span>
<span class="card-title">${copies()}</span>
</div>
<div class="card card-body center">
<span class="overline">Selection</span>
<span class="card-title hint">${selection() || '—'}</span>