From 896c0982d1b72bb034c49f052bd7144b3b27380b Mon Sep 17 00:00:00 2001 From: Nayan Date: Fri, 31 Jul 2026 17:56:00 +0530 Subject: [PATCH] feat(overlay): package shell and the css-in-ts style system Styles are authored in TypeScript and emitted into the overlay's own shadow root. The overlay is injected into a page it does not control, so it can neither leak styles into that page nor inherit them from it. check-css.mjs is the guard: it fails the build on a raw value where a token exists, which is the drift that would otherwise accumulate one hex code at a time. --- packages/overlay/package.json | 43 + packages/overlay/scripts/check-css.mjs | 145 +++ packages/overlay/src/styles/base.css.ts | 100 ++ packages/overlay/src/styles/canvas.css.ts | 282 ++++++ packages/overlay/src/styles/chat.css.ts | 495 ++++++++++ packages/overlay/src/styles/chrome.css.ts | 287 ++++++ packages/overlay/src/styles/const.ts | 42 + packages/overlay/src/styles/controls.css.ts | 907 +++++++++++++++++++ packages/overlay/src/styles/css-pane.css.ts | 129 +++ packages/overlay/src/styles/docks.css.ts | 220 +++++ packages/overlay/src/styles/empty.css.ts | 54 ++ packages/overlay/src/styles/index.test.ts | 114 +++ packages/overlay/src/styles/index.ts | 113 +++ packages/overlay/src/styles/inspector.css.ts | 418 +++++++++ packages/overlay/src/styles/motion.css.ts | 33 + packages/overlay/src/styles/pop.css.ts | 252 ++++++ packages/overlay/src/styles/portable.css.ts | 118 +++ packages/overlay/src/styles/toast.css.ts | 59 ++ packages/overlay/tsconfig.json | 9 + packages/overlay/tsup.config.ts | 24 + packages/overlay/vitest.config.ts | 25 + 21 files changed, 3869 insertions(+) create mode 100644 packages/overlay/package.json create mode 100644 packages/overlay/scripts/check-css.mjs create mode 100644 packages/overlay/src/styles/base.css.ts create mode 100644 packages/overlay/src/styles/canvas.css.ts create mode 100644 packages/overlay/src/styles/chat.css.ts create mode 100644 packages/overlay/src/styles/chrome.css.ts create mode 100644 packages/overlay/src/styles/const.ts create mode 100644 packages/overlay/src/styles/controls.css.ts create mode 100644 packages/overlay/src/styles/css-pane.css.ts create mode 100644 packages/overlay/src/styles/docks.css.ts create mode 100644 packages/overlay/src/styles/empty.css.ts create mode 100644 packages/overlay/src/styles/index.test.ts create mode 100644 packages/overlay/src/styles/index.ts create mode 100644 packages/overlay/src/styles/inspector.css.ts create mode 100644 packages/overlay/src/styles/motion.css.ts create mode 100644 packages/overlay/src/styles/pop.css.ts create mode 100644 packages/overlay/src/styles/portable.css.ts create mode 100644 packages/overlay/src/styles/toast.css.ts create mode 100644 packages/overlay/tsconfig.json create mode 100644 packages/overlay/tsup.config.ts create mode 100644 packages/overlay/vitest.config.ts diff --git a/packages/overlay/package.json b/packages/overlay/package.json new file mode 100644 index 0000000..a593f7a --- /dev/null +++ b/packages/overlay/package.json @@ -0,0 +1,43 @@ +{ + "name": "@airship/overlay", + "version": "0.0.0", + "private": true, + "type": "module", + "exports": { + "./bundle": "./dist/overlay.global.js", + "./hook": "./dist/hook.global.js" + }, + "scripts": { + "build": "node scripts/check-css.mjs && tsup", + "build-storybook": "storybook build", + "check:css": "node scripts/check-css.mjs", + "clean": "rm -rf dist .turbo storybook-static", + "dev": "tsup --watch", + "storybook": "storybook dev -p 6006 --no-open", + "test": "vitest run", + "test:browser": "vitest run --config vitest.browser.config.ts", + "typecheck": "tsc --noEmit" + }, + "dependencies": { + "@airship/editor-icons": "workspace:*", + "@airship/editor-tokens": "workspace:*", + "@airship/protocol": "workspace:*", + "@airship/source": "workspace:*", + "@dnd-kit/abstract": "0.5.0", + "@dnd-kit/dom": "0.5.0", + "@dnd-kit/geometry": "0.5.0", + "bippy": "^0.5.32" + }, + "devDependencies": { + "@storybook/addon-a11y": "10.5.7", + "@storybook/addon-vitest": "10.5.7", + "@storybook/html-vite": "10.5.7", + "@vitest/browser": "^4.1.10", + "@vitest/browser-playwright": "^4.1.10", + "happy-dom": "^20.11.1", + "playwright": "^1.62.1", + "storybook": "10.5.7", + "vite": "^7.3.6", + "vitest": "^4.1.10" + } +} diff --git a/packages/overlay/scripts/check-css.mjs b/packages/overlay/scripts/check-css.mjs new file mode 100644 index 0000000..d03950c --- /dev/null +++ b/packages/overlay/scripts/check-css.mjs @@ -0,0 +1,145 @@ +/* + * Guard the CSS template literals in `src/styles/*.css.ts`. + * + * Those files are one big backtick string each, so an unescaped backtick inside + * a CSS comment — the natural way to write `position: relative` in prose — + * silently terminates the literal and turns the rest of the stylesheet into + * TypeScript. `tsc` does catch it, but it reports a parse error thirty rules + * downstream of the cause and reads as a compiler bug. This names it. + * + * The scan starts at `export const css = \`` and walks the literal properly, + * stepping over `${ ... }` interpolations (which may themselves contain nested + * template literals) so only backticks in *CSS* text are reported. + * + * It also enforces one design rule that cannot be expressed in the type system: + * canvas chrome does not animate. See `NO_TRANSITION`. + */ +import { readdirSync, readFileSync } from "node:fs"; +import { join } from "node:path"; + +const DIR = new URL("../src/styles/", import.meta.url).pathname; +const START = /export const css\s*=\s*`/; +const problems = []; + +/** + * Stylesheets that may not declare a `transition`, and why. + * + * The hover and selection outlines are re-positioned on every pointer move, by + * writing a measured rect straight into `left`/`top`/`width`/`height`. A + * transition on those properties interpolates between one element's box and the + * next, so the outline visibly trails the cursor and lags behind whatever it is + * supposed to be tracking. It does not read as easing; it reads as the overlay + * being slow. + * + * This is not obvious from the rule that causes it — the offending declaration + * was a plausible-looking `transition: all .05s ease` that survived a long time + * — and it is exactly the sort of thing a well-meaning motion pass would put + * back. Hence a build check rather than a comment. + * + * Chrome that is *shown and hidden* rather than moved may still fade; list it in + * `allow` with the reason, and keep that list short. + */ +const NO_TRANSITION = [{ allow: ["snap-label"], file: "chrome.css.ts" }]; + +/** A `transition` / `transition-property` declaration starting a value. */ +const TRANSITION_DECL = /(^|[;\s])transition(-[a-z]+)?\s*:/; + +/** Walk one template literal from just after its opening backtick. */ +function scan(source, from, file) { + let depth = 0; // `${ ... }` nesting + for (let i = from; i < source.length; i += 1) { + const c = source[i]; + if (c === "\\") { + i += 1; + continue; + } + if (c === "$" && source[i + 1] === "{") { + depth += 1; + i += 1; + continue; + } + if (c === "}" && depth > 0) { + depth -= 1; + continue; + } + if (c === "`") { + if (depth > 0) { + // A nested literal inside an interpolation — skip to its end. + i = scan(source, i + 1, file).end; + continue; + } + return { closed: true, end: i }; + } + } + problems.push(`${file}: template literal is never closed`); + return { closed: false, end: source.length }; +} + +for (const file of readdirSync(DIR).filter((f) => f.endsWith(".css.ts"))) { + const source = readFileSync(join(DIR, file), "utf8"); + const start = START.exec(source); + if (!start) { + continue; + } + const open = start.index + start[0].length; + const { end } = scan(source, open, file); + + // Anything after the literal should be whitespace and a semicolon. If the + // literal closed early, real CSS is sitting out here as broken TypeScript. + const tail = source.slice(end + 1).trim(); + if (tail && tail !== ";") { + const line = source.slice(0, end).split("\n").length; + problems.push( + `${file}: the CSS literal closes at line ${line}, but the file continues.\n` + + " Almost always an unescaped backtick in a comment — escape it (\\`) " + + "or drop the quotes." + ); + } + + const body = source.slice(open, end); + const opens = (body.match(/{/g) ?? []).length; + const closes = (body.match(/}/g) ?? []).length; + // `${...}` contributes one of each, so they cancel. + if (opens !== closes) { + problems.push( + `${file}: unbalanced braces (${opens} open, ${closes} close)` + ); + } + + checkNoTransition(file, body); +} + +/** Report `transition` declarations in a stylesheet that may not have them. */ +function checkNoTransition(file, body) { + const rule = NO_TRANSITION.find((r) => r.file === file); + if (!rule) { + return; + } + // Split on `}` so each chunk holds one rule's selector and its declarations, + // which is enough to tell an allowed selector from a forbidden one. + for (const chunk of body.split("}")) { + if (!TRANSITION_DECL.test(chunk)) { + continue; + } + if (rule.allow.some((name) => chunk.includes(name))) { + continue; + } + const selector = chunk.split("{")[0].trim().split("\n").pop().trim(); + problems.push( + `${file}: \`transition\` on \`${selector}\`.\n` + + " Canvas chrome is re-positioned on every pointer move, so a " + + "transition makes the outline trail the cursor.\n" + + " If this element is faded rather than moved, add its class to " + + "NO_TRANSITION's `allow` list with a reason." + ); + } +} + +if (problems.length) { + console.error(`check-css: ${problems.length} problem(s)\n`); + for (const p of problems) { + console.error(` ${p}`); + } + process.exit(1); +} +console.log("check-css: ok"); diff --git a/packages/overlay/src/styles/base.css.ts b/packages/overlay/src/styles/base.css.ts new file mode 100644 index 0000000..fac2e50 --- /dev/null +++ b/packages/overlay/src/styles/base.css.ts @@ -0,0 +1,100 @@ +import { PREFIX } from "../dom"; +import { ROOT, Z } from "./const"; + +/** Root reset, icon colour roles, edit/drag cursors, the drop sentinel. */ +export const css = ` +${ROOT}, ${ROOT} * { box-sizing: border-box; } +${ROOT} { + position: fixed; inset: 0; z-index: ${Z}; pointer-events: none; + font-family: var(--ap-font-sans); color: var(--ap-text-primary); + -webkit-font-smoothing: antialiased; +} + +/* Icons sit one step below text, the way a design tool's do — an icon that matches the + text colour reads as loud as a label, which is what made the old chrome feel + busy. The --ap-icon-* ramp exists for exactly this and was previously unused, + so every glyph inherited --ap-text-primary (#FFF). + + The tone comes through a custom property rather than being set flat, because + this selector is \`#id .class\` and it matches the glyph's own span. A rule + that matches an element beats any \`color\` that element would have inherited, + whatever the parent's specificity — so a button could not tint its own glyph. + Send was the visible casualty: \`.action.primary\` sets white, and the caret + painted #BEBEBE on the blue anyway, which is most of why it read as badly + set rather than merely small. A custom property inherits, so any ancestor can + set --ic-tone and the var() picks it up here with no specificity contest. */ +${ROOT} .${PREFIX}-ic, .${PREFIX}-chrome-layer .${PREFIX}-ic { + display: inline-flex; + color: var(--${PREFIX}-ic-tone, var(--ap-icon-secondary)); +} +${ROOT} .${PREFIX}-ic svg, .${PREFIX}-chrome-layer .${PREFIX}-ic svg { display: block; } +/* State tones set the same property, so there is one way to tint a glyph. These + land on the \`.ic\` element itself and a container's --ic-tone only reaches it + by inheritance, which is what keeps hover and disabled winning over it. */ +${ROOT} button:hover .${PREFIX}-ic, +${ROOT} .${PREFIX}-ctl-seg-on .${PREFIX}-ic, +${ROOT} .${PREFIX}-insp-tab-on .${PREFIX}-ic { --${PREFIX}-ic-tone: var(--ap-icon-primary); } +${ROOT} button:disabled .${PREFIX}-ic, +${ROOT} [disabled] .${PREFIX}-ic { --${PREFIX}-ic-tone: var(--ap-icon-disabled); } + +.${PREFIX}-hidden { display: none !important; } + +/* Edit mode: the page is a canvas, not an app. Element-level cursors (a link's + pointer, an input's text caret) beat \`document.body.style.cursor\`, so the + arrow has to be forced from here — and text selection has to be off, or + dragging across the page selects paragraphs instead of moving elements. + + The cursor is a plain arrow and not a crosshair, which is what this was for a + long time. A crosshair is what a design tool shows when you are about to *draw* + something — it means "click and drag defines a new rect". Edit mode does the + opposite: it points at things that already exist. Forcing a crosshair over + every element in the page turned a direct-manipulation surface into something + that read like a screenshot tool, and it fought the resize and grab cursors + the chrome sets deliberately. + The \`:not([data-…-drag])\` guard hands the cursor over to the drag rule below + for the duration of a gesture, which would otherwise lose on specificity. The + \`:not([data-…-text-edit])\` guard on the descendant does the same job for the + node being edited in place: this selector carries two ID-weight \`:not()\` + arguments and declares \`!important\`, so the rules in \`portable.css.ts\` cannot + beat it on merit, and without the exemption you get a crosshair and + \`user-select: none\` sitting on top of the text you are typing into. Inline + only — which is exactly why it is the sort of thing that gets found late. + + Scoped to \`:not([data-…-shell])\` because it only applies to the inline + overlay. On the canvas this document contains no app to make inert — the + frames handle that with \`pointer-events: none\` — and blanket-crosshairing + the shell would put a crosshair on the chat composer. */ +html[data-${PREFIX}-mode="edit"]:not([data-${PREFIX}-shell]):not([data-${PREFIX}-drag]), +html[data-${PREFIX}-mode="edit"]:not([data-${PREFIX}-shell]):not([data-${PREFIX}-drag]) body, +html[data-${PREFIX}-mode="edit"]:not([data-${PREFIX}-shell]):not([data-${PREFIX}-drag]) body *:not(${ROOT}):not(${ROOT} *):not([data-${PREFIX}-text-edit]) { + cursor: default !important; + user-select: none !important; +} +/* A text cursor over anything a double-click would open for editing, so the + gesture reads as available rather than as something you have to already know + about. Driven from a root attribute for the reason the block above is: + \`document.body.style.cursor\` loses to any element-level \`cursor: pointer\`, so + a link you can edit the label of would keep showing a hand. Written by + \`SelectionController.applyHover\`, and only when the value changes. + + Placed after the edit-mode block and before the drag block deliberately: it + has to beat the blanket \`cursor: default\`, and lose to a live drag. */ +html[data-${PREFIX}-mode="edit"][data-${PREFIX}-cursor="text"]:not([data-${PREFIX}-shell]):not([data-${PREFIX}-drag]) body *:not(${ROOT}):not(${ROOT} *) { + cursor: text !important; +} +/* While a drag is live the gesture owns the cursor, overlay chrome included. */ +html[data-${PREFIX}-drag], html[data-${PREFIX}-drag] body, html[data-${PREFIX}-drag] body * { + cursor: var(--${PREFIX}-drag-cursor, grabbing) !important; + user-select: none !important; +} +html[data-${PREFIX}-drag="col-resize"] { --${PREFIX}-drag-cursor: col-resize; } +html[data-${PREFIX}-drag="ns-resize"] { --${PREFIX}-drag-cursor: ns-resize; } +html[data-${PREFIX}-drag="ew-resize"] { --${PREFIX}-drag-cursor: ew-resize; } +html[data-${PREFIX}-drag="nwse-resize"] { --${PREFIX}-drag-cursor: nwse-resize; } +html[data-${PREFIX}-drag="nesw-resize"] { --${PREFIX}-drag-cursor: nesw-resize; } + +/* Viewport-sized stand-in that gives the canvas droppable a shape. It is never + hit-tested — the collision detector reads the pointer against the live DOM. */ +.${PREFIX}-drop-sentinel { + position: fixed; inset: 0; pointer-events: none; opacity: 0; +}`; diff --git a/packages/overlay/src/styles/canvas.css.ts b/packages/overlay/src/styles/canvas.css.ts new file mode 100644 index 0000000..38823e0 --- /dev/null +++ b/packages/overlay/src/styles/canvas.css.ts @@ -0,0 +1,282 @@ +import { PREFIX } from "../dom"; + +/** The pan/zoom surface, the frames on it, and their per-frame chrome. */ +export const css = ` +/* The clipping viewport — full-bleed, the way a design tool's canvas is. The docks + float on top of it rather than pushing it in, so opening one no longer + resizes the surface underneath: the frames stay exactly where they were. + What stops a fit from parking frames behind a dock is the safe-area inset + (\`setSafeInset\` in app.ts → \`safeRect\` in viewport.ts), not this box. + Sits *below* the overlay root in z-order: it is the substrate, not chrome. */ +.${PREFIX}-canvas-viewport { + position: fixed; inset: 0; + overflow: hidden; z-index: 0; touch-action: none; + background: var(--ap-surface-canvas); + /* A faint dot grid, so panning and zooming read as motion over a surface. */ + background-image: radial-gradient(rgba(255,255,255,var(--ap-opacity-08)) 1px, transparent 1px); + background-size: 24px 24px; + font-family: var(--ap-font-sans); +} +.${PREFIX}-canvas-world { + position: absolute; top: 0; left: 0; width: 0; height: 0; + transform-origin: 0 0; + /* One composited layer for the whole canvas: panning past several live app + instances moves an existing layer instead of re-rasterising them. */ + will-change: transform; +} +/* Space held down, or the Hand tool armed: the canvas is ready to be grabbed. + Both put the same class on the viewport, so there is one cursor rule for the + pair — see \`CanvasViewport.setHandTool\` for why the tool exists on top of the + modifier. Nothing here reaches inside a frame: an iframe renders its own + cursor, which is exactly the boundary the Hand respects. */ +.${PREFIX}-canvas-pannable { cursor: grab; } +.${PREFIX}-canvas-panning { cursor: grabbing; } +.${PREFIX}-canvas-panning .${PREFIX}-canvas-world, +/* Mid-wheel-gesture, stop hit-testing the world: every frame of a pan would + otherwise drive an elementFromPoint into a moving iframe. */ +.${PREFIX}-canvas-gesture .${PREFIX}-canvas-world { pointer-events: none; } + +/* A frame: a device-sized window onto the app, positioned in world space. The + #fff background and drop shadow are the *app preview's* defaults, not editor + chrome theming — the frame renders the user's own app (which defaults to a + white page) as a card floating on the canvas, so it intentionally does not + reference the editor surface/shadow tokens. */ +.${PREFIX}-frame { + position: absolute; overflow: hidden; + background: #fff; border-radius: 2px; + box-shadow: 0 1px 3px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.28); +} +.${PREFIX}-frame-doc { + display: block; width: 100%; height: 100%; border: 0; background: #fff; + color-scheme: normal; +} +/* The capture plane. In edit mode it takes every pointer event so all input is + handled in the shell's document and hit-tested into the frame — which is what + lets one dnd-kit manager see every gesture. Hidden in view mode, when the app + below is meant to be used. + + A plain arrow, matching the inline overlay's edit-mode rule in \`base.css.ts\` + — and changing one without the other leaves the same tool showing two + different cursors depending on which stage it is running on. See that rule for + why a crosshair is the wrong cursor for pointing at things. */ +.${PREFIX}-frame-plane { + position: absolute; inset: 0; cursor: default; +} +/* The canvas half of the editable-text affordance. The plane is the shell + element the pointer is actually over, so it is the one that has to say so — + the node it is hovering lives a realm down and behind it. Paired with the + \`[data-…-cursor="text"]\` rule in \`base.css.ts\`, which does the same job + inline; both are driven from the same attribute write. */ +html[data-${PREFIX}-cursor="text"] .${PREFIX}-frame-plane { cursor: text; } +html[data-${PREFIX}-drag] .${PREFIX}-frame-plane { cursor: inherit; } + +/* Per-frame furniture, drawn in screen space at 1x — see frame-chrome.ts. */ +.${PREFIX}-fchrome-root { position: absolute; inset: 0; pointer-events: none; } +.${PREFIX}-fc { position: absolute; pointer-events: none; } +.${PREFIX}-fc-label { + position: absolute; left: 0; bottom: 100%; margin-bottom: 6px; + display: inline-flex; align-items: center; gap: 6px; max-width: 100%; + pointer-events: auto; cursor: grab; user-select: none; white-space: nowrap; + font-size: var(--ap-font-size-body); line-height: 1.6; color: var(--ap-text-tertiary); +} +.${PREFIX}-fc-label:active { cursor: grabbing; } +/* The name is the drag handle — the badge beside it is a real