Files
allmende/web/src/dom.ts
T
nilsandClaude Sonnet 5.5 71e6dc8658 Send messages with Ctrl+Enter
Ctrl+Enter (Cmd+Enter on a Mac) submits the buyer's and the owner's message
boxes through the normal form, so the usual checks apply. Plain Enter still
adds a line. The empty box hints at the shortcut. Not added to the publish
description or the address-release note, where it could submit by accident.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 20:55:30 +01:00

46 lines
1.6 KiB
TypeScript

// Tiny DOM builder. Listing content comes from strangers, so it is only ever
// inserted as text nodes or attribute values, never as HTML.
export type Child = Node | string | number | null | undefined | false;
export type Props = Record<string, string | number | boolean | ((e: Event) => void) | undefined>;
export function h<K extends keyof HTMLElementTagNameMap>(
tag: K,
props: Props | null = null,
...children: Child[]
): HTMLElementTagNameMap[K] {
const el = document.createElement(tag);
for (const [key, value] of Object.entries(props ?? {})) {
if (value === undefined || value === false) continue;
if (typeof value === "function") {
el.addEventListener(key.replace(/^on/, "").toLowerCase(), value);
} else if (value === true) {
el.setAttribute(key, "");
} else {
el.setAttribute(key, String(value));
}
}
append(el, children);
return el;
}
export function append(parent: Node, children: Child[]): void {
for (const c of children) {
if (c === null || c === undefined || c === false) continue;
parent.appendChild(typeof c === "object" ? c : document.createTextNode(String(c)));
}
}
export function clear(el: Element): void {
while (el.firstChild) el.removeChild(el.firstChild);
}
/** Lets Ctrl+Enter (Cmd+Enter on a Mac) send the form a text box belongs to, like its Send button. */
export function sendOnCtrlEnter(area: HTMLTextAreaElement): void {
area.addEventListener("keydown", (ev) => {
if (ev.key !== "Enter" || !(ev.ctrlKey || ev.metaKey) || ev.isComposing || area.disabled) return;
ev.preventDefault();
area.form?.requestSubmit();
});
}