mirror of
https://github.com/bitbaum/accountkit.git
synced 2026-12-04 16:07:42 +01:00
* feat(menu): AccountControl, a panel that stays on screen, remote session read
A consumer gets, from the browser-safe "." entry:
- AccountControl: one header control for loading (an inert box the
trigger's size), signed out (one 44px "Sign in" link) and signed in
(AccountMenu, unchanged), plus accountSignInHtml({ href, label }), the
same signed-out markup byte for byte for a static page.
- fetchAccountSummary(url, { signal?, fetch?, timeoutMs? }): a
credentialed, no-store GET of a session held on another origin that
resolves null on any failure and never rejects, including an injected
fetch that returns something that is not a Response. timeoutMs (default
8000; 0, negative or Infinity turns it off) bounds the whole read, body
included, combined with the caller's signal (AbortSignal.any where
available, by hand otherwise), so a stalled server cannot leave a header
loading for ever and the header fails closed to "Sign in".
- returnPath, the same-origin path guard, no longer only behind the
server-only ./orangecat entry.
Fix: the open panel ran off the screen when the trigger was not last in its
row (-112px at 390 in Bitbaum's header). The menu now measures the open
panel and moves it inside only when it crosses an edge; a last trigger's
panel is unchanged. It is measured again while open on a window resize and,
without one, when the panel or its header row (nearest flex or grid
ancestor) or a control in that row changes size, or a control is added to
or removed from the row (ResizeObserver and MutationObserver, feature
detected, disconnected on close and unmount).
Checked by pnpm run verify: browser checks at 320 and 390, light and dark,
on four demo rows (a 44px control before or after the trigger, both
aligns), which failed on the old stylesheet; with the menu open, a 44px
control added after the trigger, and the existing one widened, without a
window resize (left -47px at 320 before); render tests comparing
AccountControl with accountSignInHtml and AccountMenu, escaping hostile
input; unit tests for fetchAccountSummary with an injected fetch (non-
Response values, a fetch and a body that never settle, the 8000ms default
under mock timers, the caller's abort with a timeout armed, the fallback
without AbortSignal.any, Infinity) and for returnPath from ".". The browser
check honours CHROMIUM_PATH. dist/orangecat.d.ts only shows a union
reordered by the compiler, not a source change.
Claude-Session: https://claude.ai/code/session_019MDtmsSWTTpVpe4wh3v1sb
* fix(menu): the panel is moved in layout, so nothing scrolls sideways
The pull request's own browser check failed in CI and passed locally:
with the panel open in the two `align="start"` demo rows the page
scrolled sideways by 186px and 242px, at 320 and 390 wide, while the
panel itself measured inside the screen. CI runs Chromium 156; the local
run was Chromium 153. The panel was moved back on screen with
`translate`, and the page still counted the place it had come from.
What a consumer gets: the panel is moved on the edge it is pinned by, in
layout (`inset-inline-start` or `inset-inline-end` take `--acct-shift`).
A box laid out inside the screen cannot make the page scroll in any
browser. The move follows the text direction: in a right-to-left page
the pinned edge is the other one, and the offset changes sign with it.
How it is checked: test/browser.mjs opens the four demo rows again with
the page set right to left, at 320 and 390px, light and dark, and asserts
the panel is inside the viewport and nothing scrolls sideways. The
existing left-to-right checks are unchanged. `pnpm run verify` passes on
Chromium 153; CI decides for 156.
Claude-Session: https://claude.ai/code/session_019MDtmsSWTTpVpe4wh3v1sb
---------
Co-authored-by: Claude <noreply@anthropic.com>
52 lines
2.3 KiB
JavaScript
52 lines
2.3 KiB
JavaScript
"use client";
|
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
import { AccountMenu, DEFAULT_LABELS, } from "./AccountMenu.js";
|
|
// React's own test for a script URL, so the static markup and the component
|
|
// refuse exactly the same hrefs.
|
|
const SCRIPT_URL =
|
|
// eslint-disable-next-line no-control-regex
|
|
/^[\u0000-\u001F ]*j[\r\n\t]*a[\r\n\t]*v[\r\n\t]*a[\r\n\t]*s[\r\n\t]*c[\r\n\t]*r[\r\n\t]*i[\r\n\t]*p[\r\n\t]*t[\r\n\t]*:/i;
|
|
/** A sign-in link never runs script: a `javascript:` href becomes "#". */
|
|
function signInHref(href) {
|
|
return SCRIPT_URL.test(href) ? "#" : href;
|
|
}
|
|
// The five characters React escapes in text and attribute values, escaped
|
|
// the way React does, so the two outputs are the same bytes.
|
|
const ESCAPES = {
|
|
"&": "&",
|
|
"<": "<",
|
|
">": ">",
|
|
'"': """,
|
|
"'": "'",
|
|
};
|
|
function escapeHtml(text) {
|
|
return text.replace(/[&<>"']/g, (c) => ESCAPES[c]);
|
|
}
|
|
/**
|
|
* The signed-out state as HTML, without React: byte for byte what
|
|
* `<AccountControl state={null} signIn={{ href, label }} />` renders. A static
|
|
* page prints it in the header slot, then mounts the control over it once the
|
|
* session is known. `href` and `label` are escaped.
|
|
*/
|
|
export function accountSignInHtml({ href, label }) {
|
|
return `<a class="acct-signin" href="${escapeHtml(signInHref(href))}">${escapeHtml(label ?? DEFAULT_LABELS.signIn)}</a>`;
|
|
}
|
|
/**
|
|
* The one account control for a header, in all three states, so no product
|
|
* writes its own signed-out corner:
|
|
* - `"loading"`: an empty box exactly the trigger's size, hidden from
|
|
* assistive technology and not focusable, so nothing moves when the answer
|
|
* arrives;
|
|
* - `null`: one "Sign in" link, the trigger's height, in the same slot;
|
|
* - a person: `AccountMenu`, unchanged.
|
|
*/
|
|
export function AccountControl({ state, signIn, ...menu }) {
|
|
if (state === "loading") {
|
|
return (_jsx("span", { className: ["acct-pending", menu.className].filter(Boolean).join(" "), "aria-hidden": "true" }));
|
|
}
|
|
if (state === null) {
|
|
return (_jsx("a", { className: ["acct-signin", menu.className].filter(Boolean).join(" "), href: signInHref(signIn.href), children: signIn.label ?? menu.labels?.signIn ?? DEFAULT_LABELS.signIn }));
|
|
}
|
|
return _jsx(AccountMenu, { user: state, ...menu });
|
|
}
|
|
//# sourceMappingURL=AccountControl.js.map
|