cursedauth 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +87 -0
- package/dist/account/AccountMenu.d.ts +153 -0
- package/dist/account/AccountMenu.d.ts.map +1 -0
- package/dist/account/AccountMenu.js +388 -0
- package/dist/account/AccountMenu.js.map +1 -0
- package/dist/account/AccountNotice.d.ts +35 -0
- package/dist/account/AccountNotice.d.ts.map +1 -0
- package/dist/account/AccountNotice.js +133 -0
- package/dist/account/AccountNotice.js.map +1 -0
- package/dist/account/accountNotice.state.d.ts +20 -0
- package/dist/account/accountNotice.state.d.ts.map +1 -0
- package/dist/account/accountNotice.state.js +55 -0
- package/dist/account/accountNotice.state.js.map +1 -0
- package/dist/account/avatar.d.ts +37 -0
- package/dist/account/avatar.d.ts.map +1 -0
- package/dist/account/avatar.js +47 -0
- package/dist/account/avatar.js.map +1 -0
- package/dist/accountSlot.client.d.ts +113 -0
- package/dist/accountSlot.client.d.ts.map +1 -0
- package/dist/accountSlot.client.js +330 -0
- package/dist/accountSlot.client.js.map +1 -0
- package/dist/accountSlots.d.ts +192 -0
- package/dist/accountSlots.d.ts.map +1 -0
- package/dist/accountSlots.js +274 -0
- package/dist/accountSlots.js.map +1 -0
- package/dist/auth.d.ts +167 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +310 -0
- package/dist/auth.js.map +1 -0
- package/dist/auth.server.d.ts +35 -0
- package/dist/auth.server.d.ts.map +1 -0
- package/dist/auth.server.js +67 -0
- package/dist/auth.server.js.map +1 -0
- package/dist/clientDescription.d.ts +186 -0
- package/dist/clientDescription.d.ts.map +1 -0
- package/dist/clientDescription.js +278 -0
- package/dist/clientDescription.js.map +1 -0
- package/dist/constantTime.d.ts +35 -0
- package/dist/constantTime.d.ts.map +1 -0
- package/dist/constantTime.js +46 -0
- package/dist/constantTime.js.map +1 -0
- package/dist/contentCensus.d.ts +59 -0
- package/dist/contentCensus.d.ts.map +1 -0
- package/dist/contentCensus.js +55 -0
- package/dist/contentCensus.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/mountGatedApp.d.ts +128 -0
- package/dist/mountGatedApp.d.ts.map +1 -0
- package/dist/mountGatedApp.js +107 -0
- package/dist/mountGatedApp.js.map +1 -0
- package/dist/runtimeMode.d.ts +28 -0
- package/dist/runtimeMode.d.ts.map +1 -0
- package/dist/runtimeMode.js +37 -0
- package/dist/runtimeMode.js.map +1 -0
- package/dist/secureRequest.d.ts +22 -0
- package/dist/secureRequest.d.ts.map +1 -0
- package/dist/secureRequest.js +27 -0
- package/dist/secureRequest.js.map +1 -0
- package/dist/sso.d.ts +373 -0
- package/dist/sso.d.ts.map +1 -0
- package/dist/sso.js +420 -0
- package/dist/sso.js.map +1 -0
- package/dist/ssoAutoSignIn.d.ts +272 -0
- package/dist/ssoAutoSignIn.d.ts.map +1 -0
- package/dist/ssoAutoSignIn.js +431 -0
- package/dist/ssoAutoSignIn.js.map +1 -0
- package/dist/ssoConsumer.server.d.ts +295 -0
- package/dist/ssoConsumer.server.d.ts.map +1 -0
- package/dist/ssoConsumer.server.js +972 -0
- package/dist/ssoConsumer.server.js.map +1 -0
- package/dist/stageIdentity.d.ts +72 -0
- package/dist/stageIdentity.d.ts.map +1 -0
- package/dist/stageIdentity.js +74 -0
- package/dist/stageIdentity.js.map +1 -0
- package/dist/ui/popover.d.ts +190 -0
- package/dist/ui/popover.d.ts.map +1 -0
- package/dist/ui/popover.js +351 -0
- package/dist/ui/popover.js.map +1 -0
- package/dist/userSessions.d.ts +205 -0
- package/dist/userSessions.d.ts.map +1 -0
- package/dist/userSessions.js +419 -0
- package/dist/userSessions.js.map +1 -0
- package/package.json +115 -0
- package/src/account/AccountMenu.tsx +729 -0
- package/src/account/AccountNotice.tsx +223 -0
- package/src/account/accountNotice.state.ts +66 -0
- package/src/account/avatar.ts +50 -0
- package/src/accountSlot.client.ts +362 -0
- package/src/accountSlots.ts +305 -0
- package/src/auth.server.ts +89 -0
- package/src/auth.ts +515 -0
- package/src/clientDescription.ts +366 -0
- package/src/constantTime.ts +45 -0
- package/src/contentCensus.ts +106 -0
- package/src/index.ts +58 -0
- package/src/mountGatedApp.ts +240 -0
- package/src/runtimeMode.ts +37 -0
- package/src/secureRequest.ts +25 -0
- package/src/sso.ts +767 -0
- package/src/ssoAutoSignIn.ts +544 -0
- package/src/ssoConsumer.server.ts +1260 -0
- package/src/stageIdentity.ts +76 -0
- package/src/ui/popover.ts +390 -0
- package/src/userSessions.ts +678 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THE LOCAL STAGE'S IDENTITY — the accounts door a staged artifact uses, and the marker
|
|
3
|
+
* that says it is one.
|
|
4
|
+
*
|
|
5
|
+
* ## 🔴 Why this is its own module, with ZERO imports
|
|
6
|
+
*
|
|
7
|
+
* Four callers need these three values and they do not run in the same runtime:
|
|
8
|
+
*
|
|
9
|
+
* · `appConfig.ts` — the app's server, under **Bun**
|
|
10
|
+
* · `ssoConsumer.server.ts` — the app's server, under **Bun**
|
|
11
|
+
* · `smoke/previewTarget.ts` — a Playwright **config**, loaded under **NODE**
|
|
12
|
+
* · `smoke/stageSignIn.ts` — a Playwright **spec**, under **NODE**
|
|
13
|
+
*
|
|
14
|
+
* The two halves have to agree about all three values or the stage does not work — the
|
|
15
|
+
* artifact would expect a `<app>-local` grant while the spec minted a production one.
|
|
16
|
+
* But `appConfig.ts` reaches `cwip/env` and `node:path` and is the entry to the whole
|
|
17
|
+
* config reader, and a Playwright config that imports it drags that graph into Node at
|
|
18
|
+
* config-load time — where the failure is `ERR_MODULE_NOT_FOUND` or a `bun:` specifier,
|
|
19
|
+
* thrown before a single spec runs and pointing at the config rather than at the cause.
|
|
20
|
+
* `smoke/previewTarget.ts` already carries a scar of exactly this shape in its own
|
|
21
|
+
* `import.meta.dir` note.
|
|
22
|
+
*
|
|
23
|
+
* So the shared values live at a LEAF with no dependencies at all, and everything else
|
|
24
|
+
* imports them. `appConfig.ts` re-exports them so no existing caller has to learn that
|
|
25
|
+
* this file exists.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* THE FLEET'S LOCAL ACCOUNTS DOOR — the `auth-local` launchd daemon on this Mac.
|
|
30
|
+
*
|
|
31
|
+
* The same daemon `appConfig`'s `DEV_AUTH_URL` names, in the `127.0.0.1` spelling. Both
|
|
32
|
+
* spellings exist because they are not interchangeable to an exact-match redirect
|
|
33
|
+
* allowlist and never have been: `localhost` is what a dev shell types and what
|
|
34
|
+
* `apps/auth` has registered for the dev ports, `127.0.0.1` is what the always-on
|
|
35
|
+
* consumers (`orch-local`, `station-local`) were registered with. Aliasing one to the
|
|
36
|
+
* other is a one-line change that answers 400 `unregistered redirect_uri`.
|
|
37
|
+
*
|
|
38
|
+
* 🔴 It carries its OWN `users.sqlite`, entirely separate from production's, which is the
|
|
39
|
+
* whole reason the owner's answer to *"we need three test accounts"* is this door rather
|
|
40
|
+
* than a boundary change: a stage account costs the live registry nothing, because it is
|
|
41
|
+
* never in it.
|
|
42
|
+
*/
|
|
43
|
+
export const LOCAL_AUTH_URL = "http://127.0.0.1:3309";
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* THE STAGE IDENTITY MARKER — "this artifact's accounts are the LOCAL ones".
|
|
47
|
+
*
|
|
48
|
+
* Set by exactly one thing: `smoke/localStage.ts`, the single seam that turns a locally
|
|
49
|
+
* booted artifact into a stage, and mirrored onto the test RUNNER by
|
|
50
|
+
* `smoke/previewTarget.ts` so both halves of a run read one decision. Nothing else writes
|
|
51
|
+
* it, no deploy secrets file contains it, and the deploy engine does not know it exists.
|
|
52
|
+
*
|
|
53
|
+
* ## Why this one IS an env var when `deployed` and `isStageOrigin` deliberately are not
|
|
54
|
+
*
|
|
55
|
+
* The rule `appConfig.ts` states twice is that a boolean meaning *"it is fine to let
|
|
56
|
+
* someone else in"* must be derived, never configured — because a typo makes it true in
|
|
57
|
+
* production and nothing about the value looks wrong sitting in a secrets file.
|
|
58
|
+
*
|
|
59
|
+
* 🔴 This marker points the OTHER WAY, and that is the whole reason it is allowed to be a
|
|
60
|
+
* variable. It does not widen a door; it takes production's accounts away from the
|
|
61
|
+
* artifact and hands it a loopback registry instead. A stray `APP_STAGE_IDENTITY=local` on
|
|
62
|
+
* a real release does not admit anybody — it makes that release ask a daemon on
|
|
63
|
+
* `127.0.0.1` that is not there, and refuse every sign-in loudly. Failure in the safe
|
|
64
|
+
* direction is what buys the simplicity, the same trade `SMOKE_PROD_READ_ONLY` makes.
|
|
65
|
+
*
|
|
66
|
+
* The alternative was deriving it from `live && !deployed`, which is ALSO true of a plain
|
|
67
|
+
* `bun run preview:prod` — so a developer previewing the production build on their own
|
|
68
|
+
* machine would silently stop being able to sign in. A stage is a deliberate act and it
|
|
69
|
+
* says so.
|
|
70
|
+
*/
|
|
71
|
+
export const STAGE_IDENTITY_ENV = "APP_STAGE_IDENTITY";
|
|
72
|
+
|
|
73
|
+
/** True when this process is a staged artifact whose accounts are the local ones. */
|
|
74
|
+
export function stageIdentityIsLocal(env: NodeJS.ProcessEnv = process.env): boolean {
|
|
75
|
+
return env[STAGE_IDENTITY_ENV]?.trim().toLowerCase() === "local";
|
|
76
|
+
}
|
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The header-popover machinery the account widgets share — style
|
|
3
|
+
* injection, host dark mode, viewport anchoring, dismissal.
|
|
4
|
+
*
|
|
5
|
+
* ── Why these four live together, and why here ──────────────────────────────
|
|
6
|
+
* `FleetDirectory` grew all four first, for a reason that applies to any UI
|
|
7
|
+
* this package renders INTO an app rather than beside it: the satellites split
|
|
8
|
+
* between barrel-bundling and barrel-excluding Vite configs and keep per-app
|
|
9
|
+
* Tailwind `@source` lists, so a widget styled with utility classes renders
|
|
10
|
+
* correctly in whichever apps remembered to add a line and silently unstyled in
|
|
11
|
+
* the rest. "Unstyled" is not a visible failure to the person who shipped it.
|
|
12
|
+
* One scoped stylesheet, injected once, keyed on its own variables, renders
|
|
13
|
+
* identically everywhere and costs no per-app wiring.
|
|
14
|
+
*
|
|
15
|
+
* The moment a SECOND widget needed the same four (`AccountMenu`, 2026-08-10),
|
|
16
|
+
* copying them would have meant two dark-mode readers that could disagree about
|
|
17
|
+
* what dark means while sitting 8px apart in the same header. So they moved
|
|
18
|
+
* here, and `FleetDirectory` imports them like anybody else.
|
|
19
|
+
*/
|
|
20
|
+
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Inject a scoped stylesheet once per document, keyed on `id`.
|
|
24
|
+
*
|
|
25
|
+
* Idempotent by the id check, which is what makes it safe to call from every
|
|
26
|
+
* instance of a widget — a header renders one launcher, an account page may
|
|
27
|
+
* render another, and neither should care whether the other already ran.
|
|
28
|
+
*/
|
|
29
|
+
export function useInjectedStyles(id: string, css: string): void {
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (typeof document === "undefined" || document.getElementById(id)) return;
|
|
32
|
+
const el = document.createElement("style");
|
|
33
|
+
el.id = id;
|
|
34
|
+
el.textContent = css;
|
|
35
|
+
document.head.appendChild(el);
|
|
36
|
+
}, [id, css]);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Follow the host app's dark mode without owning a theme of our own.
|
|
41
|
+
*
|
|
42
|
+
* ── 🔴 The rung that was missing, and the bug it caused ─────────────────────────────────
|
|
43
|
+
* A flix user, 2026-08-25: *"the square button looks like it's in dark mode even though he
|
|
44
|
+
* was in light mode, and so did the pill with user initials and user name."*
|
|
45
|
+
*
|
|
46
|
+
* The order below was: explicit attribute, then the `dark` CLASS, then the OS preference.
|
|
47
|
+
* cursedbelt's theme store expresses LIGHT by REMOVING the class (`classList.toggle('dark',
|
|
48
|
+
* scheme === 'dark')` — `theme.css`'s contract, and right for `dark:` utilities), and it
|
|
49
|
+
* sets no attribute. So a host in light mode looked exactly like a host that had never
|
|
50
|
+
* chosen: nothing matched, and the fall-through asked the OPERATING SYSTEM — which for
|
|
51
|
+
* this reader was dark. Every popover in the header therefore rendered dark over a light
|
|
52
|
+
* app. The class is a positive signal for dark and carries no signal at all for light,
|
|
53
|
+
* which is the whole defect.
|
|
54
|
+
*
|
|
55
|
+
* The missing rung is `color-scheme` on the root, which the same applier sets to the
|
|
56
|
+
* RESOLVED value in both directions. It is a web-platform property rather than a
|
|
57
|
+
* cursedbelt invention — it is what tells native scrollbars and form controls which way to
|
|
58
|
+
* paint — so reading it also covers any host that themes itself properly without using
|
|
59
|
+
* this design system at all. `prefers-color-scheme` stays as the last resort, for a host
|
|
60
|
+
* that has genuinely expressed nothing.
|
|
61
|
+
*
|
|
62
|
+
* 🔴 `style` is in the observed attribute list for the same reason: the applier writes
|
|
63
|
+
* `documentElement.style.colorScheme`, so leaving it out would read the value once and
|
|
64
|
+
* then never notice the theme being toggled.
|
|
65
|
+
*/
|
|
66
|
+
export function useHostDark(): boolean {
|
|
67
|
+
const [dark, setDark] = useState(false);
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
if (typeof document === "undefined") return;
|
|
70
|
+
const read = () => {
|
|
71
|
+
const root = document.documentElement;
|
|
72
|
+
const attr = root.getAttribute("data-theme") ?? root.getAttribute("data-color-scheme");
|
|
73
|
+
if (attr === "dark") return true;
|
|
74
|
+
if (attr === "light") return false;
|
|
75
|
+
if (root.classList.contains("dark")) return true;
|
|
76
|
+
// The resolved scheme, in BOTH directions — see the header.
|
|
77
|
+
const scheme = root.style.colorScheme || getComputedStyle(root).colorScheme;
|
|
78
|
+
if (scheme === "dark") return true;
|
|
79
|
+
if (scheme === "light") return false;
|
|
80
|
+
return window.matchMedia?.("(prefers-color-scheme: dark)").matches ?? false;
|
|
81
|
+
};
|
|
82
|
+
setDark(read());
|
|
83
|
+
const observer = new MutationObserver(() => setDark(read()));
|
|
84
|
+
observer.observe(document.documentElement, {
|
|
85
|
+
attributes: true,
|
|
86
|
+
attributeFilter: ["class", "data-theme", "data-color-scheme", "style"],
|
|
87
|
+
});
|
|
88
|
+
return () => observer.disconnect();
|
|
89
|
+
}, []);
|
|
90
|
+
return dark;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Breathing room kept between a popover and the edge of the screen. Matches the
|
|
95
|
+
* `calc(100vw - 32px)` cap in each widget's stylesheet — 16px a side.
|
|
96
|
+
*
|
|
97
|
+
* 🔴 **16, not 12** (2026-08-22, unit 34-life). The owner's designer asked for it by
|
|
98
|
+
* number — *"a minimum padding safe-zone of 16px relative to the screen edge"* — and it
|
|
99
|
+
* is the same figure every Radix surface in cursedbelt already passes as
|
|
100
|
+
* `collisionPadding` since unit 11. Two floating-surface systems in one header disagreeing
|
|
101
|
+
* by 4px is exactly the drift this queue exists to end, so they now agree.
|
|
102
|
+
*/
|
|
103
|
+
const VIEWPORT_MARGIN = 16;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Where a popover's left edge goes, in coordinates relative to its launcher.
|
|
107
|
+
*
|
|
108
|
+
* 🔴 **This exists because `right: 0` is wrong on a phone.** The panel is
|
|
109
|
+
* positioned against a ~30px launcher button, so right-aligning it puts its LEFT
|
|
110
|
+
* edge at `button.right - panelWidth`. On a 393px-wide iPhone with the button two
|
|
111
|
+
* thirds across the header, that is roughly **-90px**: the panel hangs off the
|
|
112
|
+
* side of the screen and its entire content is unreadable. (Reported from a real
|
|
113
|
+
* iPhone against flix, 2026-08-09.) A `max-width` cannot fix it; the width was
|
|
114
|
+
* already clamped, and it was the OFFSET that was off-screen.
|
|
115
|
+
*
|
|
116
|
+
* So the edge is clamped into the viewport instead: right-aligned to the button
|
|
117
|
+
* when that fits, pushed in from whichever side it would otherwise overhang when
|
|
118
|
+
* it does not.
|
|
119
|
+
*
|
|
120
|
+
* 🔴 **Returned relative to the launcher, and that is now the LEGACY axis.** The
|
|
121
|
+
* comment here used to explain that a `position: fixed` panel "is not available,
|
|
122
|
+
* since a sticky app header with `backdrop-filter` becomes the containing block
|
|
123
|
+
* for fixed descendants". That is true of a panel rendered INSIDE the launcher —
|
|
124
|
+
* and it is exactly why {@link anchoredLeftInViewport} plus a portal to `body`
|
|
125
|
+
* replaced it (2026-09-09, E22). Once the panel is a child of `body` there is no
|
|
126
|
+
* transformed ancestor to resolve against, so viewport coordinates are simply
|
|
127
|
+
* correct. This relative form is kept because it is pure, tested, and is the
|
|
128
|
+
* arithmetic the viewport form is built out of.
|
|
129
|
+
*
|
|
130
|
+
* Pure and exported so the arithmetic can be tested without a browser.
|
|
131
|
+
*/
|
|
132
|
+
export function anchoredLeft(
|
|
133
|
+
anchor: { left: number; right: number },
|
|
134
|
+
viewportWidth: number,
|
|
135
|
+
panelWidth: number,
|
|
136
|
+
margin = VIEWPORT_MARGIN,
|
|
137
|
+
): number {
|
|
138
|
+
return anchoredLeftInViewport(anchor, viewportWidth, panelWidth, margin) - anchor.left;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The same clamp, in VIEWPORT coordinates — what a portaled `position: fixed`
|
|
143
|
+
* panel needs. {@link anchoredLeft} is this minus `anchor.left`.
|
|
144
|
+
*/
|
|
145
|
+
export function anchoredLeftInViewport(
|
|
146
|
+
anchor: { left: number; right: number },
|
|
147
|
+
viewportWidth: number,
|
|
148
|
+
panelWidth: number,
|
|
149
|
+
margin = VIEWPORT_MARGIN,
|
|
150
|
+
): number {
|
|
151
|
+
const width = Math.min(panelWidth, viewportWidth - margin * 2);
|
|
152
|
+
const rightAligned = anchor.right - width;
|
|
153
|
+
// `Math.max` LAST so that a viewport narrower than the panel still starts at
|
|
154
|
+
// the left margin rather than at a negative "keep the right margin" answer.
|
|
155
|
+
return Math.max(margin, Math.min(rightAligned, viewportWidth - margin - width));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The gap between a launcher and its panel. Was `calc(100% + 6px)` in each
|
|
160
|
+
* widget's stylesheet until the panel stopped being positioned by CSS at all.
|
|
161
|
+
*/
|
|
162
|
+
export const POPOVER_GAP = 6;
|
|
163
|
+
|
|
164
|
+
/** Which side of the launcher a panel ended up on. */
|
|
165
|
+
export type PopoverPlacement = "below" | "above";
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Where a popover's TOP edge goes, in viewport coordinates, and which side it
|
|
169
|
+
* chose.
|
|
170
|
+
*
|
|
171
|
+
* 🔴 **This exists because the fleet launcher is at the BOTTOM-LEFT corner.**
|
|
172
|
+
* The owner, 2026-09-09: *"The icon is on the bottom left corner and the popup it
|
|
173
|
+
* opens is going under it outside of view plus behind the sidebar. Its position
|
|
174
|
+
* calculation makes it completely unusable."*
|
|
175
|
+
*
|
|
176
|
+
* There was no vertical calculation at all. `anchoredLeft` solved the horizontal
|
|
177
|
+
* axis in August (a phone pushed the panel off the side); the vertical axis was
|
|
178
|
+
* a single CSS line, `top: calc(100% + 6px)` — *always* below the launcher. That
|
|
179
|
+
* is right for a header at the top of the page and unusable for a launcher at the
|
|
180
|
+
* bottom, where "below" is past the fold. A `max-height` cannot fix it, for the
|
|
181
|
+
* same reason a `max-width` could not fix the phone bug: the panel was the right
|
|
182
|
+
* size and in the wrong PLACE.
|
|
183
|
+
*
|
|
184
|
+
* So: below when it fits, flipped above when it does not, and when neither side
|
|
185
|
+
* fits (a panel taller than the viewport) the roomier side wins and the result is
|
|
186
|
+
* clamped to the margins so the top of the panel is always reachable.
|
|
187
|
+
*
|
|
188
|
+
* Pure and exported so the arithmetic can be tested without a browser.
|
|
189
|
+
*/
|
|
190
|
+
export function anchoredTop(
|
|
191
|
+
anchor: { top: number; bottom: number },
|
|
192
|
+
viewportHeight: number,
|
|
193
|
+
panelHeight: number,
|
|
194
|
+
gap = POPOVER_GAP,
|
|
195
|
+
margin = VIEWPORT_MARGIN,
|
|
196
|
+
): { top: number; placement: PopoverPlacement } {
|
|
197
|
+
const below = anchor.bottom + gap;
|
|
198
|
+
const above = anchor.top - gap - panelHeight;
|
|
199
|
+
if (below + panelHeight <= viewportHeight - margin) return { top: below, placement: "below" };
|
|
200
|
+
if (above >= margin) return { top: above, placement: "above" };
|
|
201
|
+
// Neither side fits. Prefer the side with more room, then clamp — a panel
|
|
202
|
+
// taller than the viewport scrolls internally (`max-height` in the widget's
|
|
203
|
+
// stylesheet), so what matters is that its TOP is on screen.
|
|
204
|
+
const roomBelow = viewportHeight - margin - below;
|
|
205
|
+
const roomAbove = anchor.top - gap - margin;
|
|
206
|
+
const lowest = Math.max(margin, viewportHeight - margin - panelHeight);
|
|
207
|
+
return roomAbove > roomBelow
|
|
208
|
+
? { top: margin, placement: "above" }
|
|
209
|
+
: { top: Math.min(below, lowest), placement: "below" };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* The full position of a portaled panel, in viewport coordinates.
|
|
214
|
+
*
|
|
215
|
+
* `null` until the first measurement — a caller paints nothing rather than
|
|
216
|
+
* painting in the wrong place and then jumping. Unlike the old `left`-only hook
|
|
217
|
+
* there is no "paint right-aligned meanwhile" fallback available, because the
|
|
218
|
+
* panel is no longer inside the launcher and has no edge to align against.
|
|
219
|
+
*/
|
|
220
|
+
export interface PopoverPosition {
|
|
221
|
+
left: number;
|
|
222
|
+
top: number;
|
|
223
|
+
placement: PopoverPlacement;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Measure launcher and panel, and place the panel on screen.
|
|
228
|
+
*
|
|
229
|
+
* 🔴 **A scroll listener is REQUIRED here**, where the old left-only hook
|
|
230
|
+
* correctly did without one. That hook positioned the panel absolutely inside the
|
|
231
|
+
* launcher, so it travelled with it and only a resize could move it. A portaled
|
|
232
|
+
* `position: fixed` panel does not travel with anything: scroll the page and the
|
|
233
|
+
* launcher moves out from under a panel that stayed put. `capture: true` so a
|
|
234
|
+
* scrolling CONTAINER (a sidebar's own overflow, which is where this bug was
|
|
235
|
+
* reported) is heard too — those events do not bubble.
|
|
236
|
+
*
|
|
237
|
+
* The panel is measured, not assumed: whether it fits below depends on its real
|
|
238
|
+
* rendered height, and the entry list is variable-length.
|
|
239
|
+
*/
|
|
240
|
+
export function usePopoverPosition(
|
|
241
|
+
open: boolean,
|
|
242
|
+
wrapRef: React.RefObject<HTMLElement | null>,
|
|
243
|
+
panelRef: React.RefObject<HTMLElement | null>,
|
|
244
|
+
panelWidth: number,
|
|
245
|
+
): PopoverPosition | null {
|
|
246
|
+
const [pos, setPos] = useState<PopoverPosition | null>(null);
|
|
247
|
+
// Layout, not effect: an ordinary effect measures AFTER the browser has
|
|
248
|
+
// painted, so the panel would be seen at the wrong place for one frame and
|
|
249
|
+
// then jump. This runs before that paint.
|
|
250
|
+
useLayoutEffect(() => {
|
|
251
|
+
if (!open || typeof window === "undefined") {
|
|
252
|
+
setPos(null);
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
const measure = () => {
|
|
256
|
+
const anchor = wrapRef.current?.getBoundingClientRect();
|
|
257
|
+
if (!anchor) return;
|
|
258
|
+
// Before the first paint the panel has no box; fall back to its CSS cap
|
|
259
|
+
// so the FIRST placement is already right rather than briefly below.
|
|
260
|
+
const height = panelRef.current?.offsetHeight || 0;
|
|
261
|
+
const { top, placement } = anchoredTop(anchor, window.innerHeight, height);
|
|
262
|
+
setPos({
|
|
263
|
+
left: anchoredLeftInViewport(anchor, window.innerWidth, panelWidth),
|
|
264
|
+
top,
|
|
265
|
+
placement,
|
|
266
|
+
});
|
|
267
|
+
};
|
|
268
|
+
measure();
|
|
269
|
+
// A second pass once the panel has a real height — the first ran before it
|
|
270
|
+
// was laid out, so `offsetHeight` was 0 and "fits below" was answered for a
|
|
271
|
+
// zero-height panel.
|
|
272
|
+
const raf = requestAnimationFrame(measure);
|
|
273
|
+
window.addEventListener("resize", measure);
|
|
274
|
+
window.addEventListener("orientationchange", measure);
|
|
275
|
+
window.addEventListener("scroll", measure, true);
|
|
276
|
+
return () => {
|
|
277
|
+
cancelAnimationFrame(raf);
|
|
278
|
+
window.removeEventListener("resize", measure);
|
|
279
|
+
window.removeEventListener("orientationchange", measure);
|
|
280
|
+
window.removeEventListener("scroll", measure, true);
|
|
281
|
+
};
|
|
282
|
+
}, [open, wrapRef, panelRef, panelWidth]);
|
|
283
|
+
return pos;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Close on a click outside the wrapper or on Escape — the two dismissals a
|
|
288
|
+
* person expects from anything that opened over the page.
|
|
289
|
+
*
|
|
290
|
+
* `mousedown`, not `click`: a menu item that navigates must not be able to fire
|
|
291
|
+
* its own dismissal after the navigation has already begun.
|
|
292
|
+
*
|
|
293
|
+
* 🔴 `panelRef` is not optional decoration. Once the panel is PORTALED to `body`
|
|
294
|
+
* it is no longer a descendant of the wrapper, so a wrapper-only containment test
|
|
295
|
+
* calls every click inside the panel an outside click — the menu would close on
|
|
296
|
+
* the way to any item it contains.
|
|
297
|
+
*/
|
|
298
|
+
export function useDismissOnOutside(
|
|
299
|
+
open: boolean,
|
|
300
|
+
wrapRef: React.RefObject<HTMLElement | null>,
|
|
301
|
+
close: () => void,
|
|
302
|
+
panelRef?: React.RefObject<HTMLElement | null>,
|
|
303
|
+
): void {
|
|
304
|
+
useEffect(() => {
|
|
305
|
+
if (!open) return;
|
|
306
|
+
const onDown = (e: MouseEvent) => {
|
|
307
|
+
const target = e.target as Node;
|
|
308
|
+
if (wrapRef.current?.contains(target)) return;
|
|
309
|
+
if (panelRef?.current?.contains(target)) return;
|
|
310
|
+
close();
|
|
311
|
+
};
|
|
312
|
+
const onKey = (e: KeyboardEvent) => {
|
|
313
|
+
if (e.key === "Escape") close();
|
|
314
|
+
};
|
|
315
|
+
document.addEventListener("mousedown", onDown);
|
|
316
|
+
document.addEventListener("keydown", onKey);
|
|
317
|
+
return () => {
|
|
318
|
+
document.removeEventListener("mousedown", onDown);
|
|
319
|
+
document.removeEventListener("keydown", onKey);
|
|
320
|
+
};
|
|
321
|
+
}, [open, wrapRef, close, panelRef]);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const SCRIM_ID = "sk-popover-scrim";
|
|
325
|
+
const SCRIM_STYLE_ID = "sk-popover-scrim-style";
|
|
326
|
+
/** One below the panels' `z-index: 2147482000`, so it dims the page and nothing else. */
|
|
327
|
+
const SCRIM_CSS = `
|
|
328
|
+
#${SCRIM_ID}{position:fixed;inset:0;z-index:2147481999;background:rgba(16,18,22,.28);
|
|
329
|
+
animation:sk-scrim-in .12s ease-out}
|
|
330
|
+
#${SCRIM_ID}[data-dark="true"]{background:rgba(0,0,0,.46)}
|
|
331
|
+
@keyframes sk-scrim-in{from{opacity:0}to{opacity:1}}
|
|
332
|
+
@media (prefers-reduced-motion:reduce){#${SCRIM_ID}{animation:none}}
|
|
333
|
+
`;
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* The dimmed backdrop behind an open header popover.
|
|
337
|
+
*
|
|
338
|
+
* 🔴 Why it exists (owner's designer, 2026-08-19, on the "Your Apps" menu):
|
|
339
|
+
* *"The secondary app dropdown menu completely masks the main layout content underneath
|
|
340
|
+
* without dimmed backdrops or absolute separation … foreground elements fight background
|
|
341
|
+
* data for active user focus."* Elevation alone did not settle it — the page behind is
|
|
342
|
+
* flat high-contrast text at the same weight as the menu's own.
|
|
343
|
+
*
|
|
344
|
+
* 🔴 Built imperatively on `document.body` rather than as a React node, and that is the
|
|
345
|
+
* point: `position: fixed` inside the launcher resolves against the nearest ancestor with
|
|
346
|
+
* a `backdrop-filter` — which a sticky app header usually has — so a JSX scrim would dim
|
|
347
|
+
* the header and leave the page it was meant to dim untouched. `body` has no such
|
|
348
|
+
* ancestor. (A portal would work too and would cost this package a `react-dom` dependency
|
|
349
|
+
* it does not otherwise have.)
|
|
350
|
+
*
|
|
351
|
+
* It is inert to the keyboard and hidden from assistive tech: Escape and the existing
|
|
352
|
+
* outside-click dismissal are the real doors, and this is decoration plus a wide target.
|
|
353
|
+
*/
|
|
354
|
+
export function useScrim(open: boolean, close: () => void, dark: boolean): void {
|
|
355
|
+
useInjectedStyles(SCRIM_STYLE_ID, SCRIM_CSS);
|
|
356
|
+
useEffect(() => {
|
|
357
|
+
if (!open || typeof document === "undefined") return;
|
|
358
|
+
const el = document.createElement("div");
|
|
359
|
+
el.id = SCRIM_ID;
|
|
360
|
+
el.setAttribute("aria-hidden", "true");
|
|
361
|
+
el.dataset.dark = dark ? "true" : "false";
|
|
362
|
+
// `mousedown` for the same reason `useDismissOnOutside` uses it: a click that
|
|
363
|
+
// began on a menu item must not be able to dismiss the menu mid-navigation.
|
|
364
|
+
el.addEventListener("mousedown", close);
|
|
365
|
+
document.body.appendChild(el);
|
|
366
|
+
return () => {
|
|
367
|
+
el.removeEventListener("mousedown", close);
|
|
368
|
+
el.remove();
|
|
369
|
+
};
|
|
370
|
+
}, [open, close, dark]);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** The open/close state, both refs, both dismissals and the panel's placement —
|
|
374
|
+
* everything a launcher needs but the panel's contents. Saves each widget
|
|
375
|
+
* re-deriving the same lines and, more usefully, keeps them placing and
|
|
376
|
+
* dismissing identically.
|
|
377
|
+
*
|
|
378
|
+
* 🔴 `panelRef` must be attached to the portaled panel. It is what lets the
|
|
379
|
+
* placement read the panel's real height, and what stops a click inside the
|
|
380
|
+
* panel reading as a click outside the launcher. */
|
|
381
|
+
export function usePopover<T extends HTMLElement>(panelWidth: number) {
|
|
382
|
+
const [open, setOpen] = useState(false);
|
|
383
|
+
const wrapRef = useRef<T | null>(null);
|
|
384
|
+
const panelRef = useRef<HTMLDivElement | null>(null);
|
|
385
|
+
const close = useCallback(() => setOpen(false), []);
|
|
386
|
+
const toggle = useCallback(() => setOpen((v) => !v), []);
|
|
387
|
+
useDismissOnOutside(open, wrapRef, close, panelRef);
|
|
388
|
+
const position = usePopoverPosition(open, wrapRef, panelRef, panelWidth);
|
|
389
|
+
return { open, close, toggle, wrapRef, panelRef, position };
|
|
390
|
+
}
|