Files
loki/widget/dom.ts
CatoandClaude 7c5c8e081b fix(widget): the edge tab says "Feedback" and is 44px to hit (#1707)
OrangeCat's UX audit (chrome#13, 2026-11-12) found the widget's edge tab a
24px-wide dark sliver marked only with the spiral: the hardest control on a
near-black page to see, and half the 44px touch minimum.

- The tab button is 44px wide; its inner 12px is transparent padding and
  the visible handle is the outer 32px.
- The handle is the surface ringed in the text colour, so in either mode
  one of the two clears 3:1 on near-white and near-black host pages.
- A visitor's tab shows a speech bubble and a vertical "Feedback" label,
  named "Send feedback"; the owner's tab keeps its live dot and mark and
  now shows "Watching" / "Paused".
- Still an edge tab, never the round launcher it replaced.

The browser test now asserts the 44px width, the name and the label, and
measures the visible handle for Minimize's "smaller than the button".


Claude-Session: https://claude.ai/code/session_019MDtmvT2nQyPBK4EfWf3Rn

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-13 12:28:57 +01:00

99 lines
4.4 KiB
TypeScript

import { MARK_D, MARK_VIEWBOX } from "./brand-mark.generated";
/**
* The widget's tiny DOM toolkit: one element helper, the inline icons, and the
* client-side image downscale. Zero dependencies — this bundle mounts on any
* stranger's site.
*/
export function h<K extends keyof HTMLElementTagNameMap>(
tag: K,
cls?: string,
text?: string,
): HTMLElementTagNameMap[K] {
const el = document.createElement(tag);
if (cls) el.className = cls;
if (text) el.textContent = text;
return el;
}
export const PENCIL_SVG =
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 3a2.85 2.83 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5Z"/></svg>';
const MIC_SVG =
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 2a3 3 0 0 0-3 3v7a3 3 0 0 0 6 0V5a3 3 0 0 0-3-3Z"/><path d="M19 10v2a7 7 0 0 1-14 0v-2"/><path d="M12 19v3"/></svg>';
/** The edge tab's glyph: a speech bubble says "feedback" on any site; the
* spiral is Loki's mark and means nothing to a visitor on someone else's. */
export const CHAT_SVG =
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>';
/** "Show Loki this": a target. */
export const TARGET_SVG =
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="9"/><circle cx="12" cy="12" r="4"/><path d="M12 1v3M12 20v3M1 12h3M20 12h3"/></svg>';
export const CAMERA_SVG =
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14.5 4h-5L7 7H4a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2h-3l-2.5-3z"/><circle cx="12" cy="13" r="3"/></svg>';
/**
* Loki's mark — the same disc as the logo (generated from the brand SSOT) —
* used as the widget's status light instead of a generic dot. The CSS turns it
* slowly while Loki watches, fast while it thinks, and stops it when paused,
* so the one shape that says "Loki" also says what Loki is doing.
*/
export function spiralMark(cls = ""): HTMLSpanElement {
const span = h("span", `dot spiral${cls ? ` ${cls}` : ""}`);
span.setAttribute("aria-hidden", "true");
span.innerHTML = `<svg viewBox="0 0 ${MARK_VIEWBOX} ${MARK_VIEWBOX}"><path d="${MARK_D}" fill="currentColor" fill-rule="evenodd"/></svg>`;
return span;
}
/** A fresh <span> each call — one node cannot sit in two places, and the label
* is rebuilt on every state change. */
export function micIcon(): HTMLSpanElement {
const span = h("span");
span.innerHTML = MIC_SVG;
return span;
}
/** Client-side downscale so a phone photo never ships megabytes: longest edge
* ≤1280px, JPEG, quality stepped down until it fits the ingest cap. */
const MAX_SHOT_CHARS = 590_000; // ingest caps the data URL at 600k chars
export function downscaleImage(file: Blob): Promise<string | null> {
return new Promise((resolve) => {
const url = URL.createObjectURL(file);
const img = new Image();
img.onload = () => {
URL.revokeObjectURL(url);
const scale = Math.min(1, 1280 / Math.max(img.width, img.height));
const canvas = document.createElement("canvas");
canvas.width = Math.max(1, Math.round(img.width * scale));
canvas.height = Math.max(1, Math.round(img.height * scale));
const ctx = canvas.getContext("2d");
if (!ctx) return resolve(null);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
for (const quality of [0.8, 0.6, 0.4]) {
const out = canvas.toDataURL("image/jpeg", quality);
if (out.length <= MAX_SHOT_CHARS) return resolve(out);
}
resolve(null);
};
img.onerror = () => {
URL.revokeObjectURL(url);
resolve(null);
};
img.src = url;
});
}
/**
* On a phone the keyboard covers the bottom of the sheet, and the box being
* typed into sat half behind it (2026-10-09). Once the keyboard has settled,
* bring the box back into view.
*/
export function keepInViewOnFocus(el: HTMLElement, settleMs = 350): void {
el.addEventListener("focus", () => {
window.setTimeout(() => el.scrollIntoView({ block: "nearest" }), settleMs);
});
}