@milesroxas/design-hub 0.12.0 → 0.14.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/README.md +5 -5
- package/core/prototype.test.ts +12 -1
- package/core/prototype.ts +21 -2
- package/core/review/model.ts +71 -6
- package/core/review/service.test.ts +575 -39
- package/core/review/service.ts +452 -25
- package/core/review/store.ts +47 -6
- package/core/review/suite-kv.ts +17 -0
- package/dist/core/prototype.d.ts +20 -2
- package/dist/core/prototype.js +17 -2
- package/dist/core/review/model.d.ts +42 -3
- package/dist/core/review/model.js +52 -3
- package/dist/core/review/service.d.ts +81 -7
- package/dist/core/review/service.js +407 -23
- package/dist/core/review/store.d.ts +29 -2
- package/dist/core/review/store.js +6 -2
- package/dist/core/review/suite-kv.d.ts +7 -0
- package/dist/core/review/suite-kv.js +12 -0
- package/dist/scripts/client.d.ts +1 -1
- package/dist/scripts/client.js +4 -4
- package/dist/scripts/lib/cli.js +5 -0
- package/dist/scripts/lib/dns.d.ts +16 -2
- package/dist/scripts/lib/dns.js +66 -3
- package/dist/scripts/lib/fake-host.d.ts +17 -3
- package/dist/scripts/lib/fake-host.js +56 -10
- package/dist/scripts/lib/host.d.ts +15 -25
- package/dist/scripts/lib/host.js +15 -324
- package/dist/scripts/lib/resend.d.ts +36 -0
- package/dist/scripts/lib/resend.js +66 -0
- package/dist/scripts/lib/studio-deps.d.ts +23 -0
- package/dist/scripts/lib/studio-deps.js +46 -16
- package/dist/scripts/lib/studios.d.ts +16 -6
- package/dist/scripts/lib/studios.js +28 -10
- package/dist/scripts/lib/vercel-host.d.ts +17 -0
- package/dist/scripts/lib/vercel-host.js +416 -0
- package/dist/scripts/people.d.ts +16 -4
- package/dist/scripts/people.js +67 -17
- package/dist/scripts/share.js +3 -2
- package/dist/scripts/studio.d.ts +26 -4
- package/dist/scripts/studio.js +364 -19
- package/dist/server/api.d.ts +20 -7
- package/dist/server/api.js +83 -27
- package/dist/server/dev.js +21 -1
- package/dist/server/gate.d.ts +120 -0
- package/dist/server/gate.js +808 -0
- package/dist/server/mailer.d.ts +60 -0
- package/dist/server/mailer.js +104 -0
- package/dist/server/mailers/fake.d.ts +8 -0
- package/dist/server/mailers/fake.js +16 -0
- package/dist/server/mailers/resend.d.ts +21 -0
- package/dist/server/mailers/resend.js +49 -0
- package/dist/server/mark.d.ts +17 -0
- package/dist/server/mark.js +46 -1
- package/dist/server/session.d.ts +2 -1
- package/dist/server/session.js +6 -3
- package/dist/server/stores/backends.d.ts +22 -0
- package/dist/server/stores/backends.js +96 -0
- package/dist/server/{fake-upstash.js → stores/fake-upstash.js} +1 -1
- package/dist/server/stores/index.d.ts +41 -0
- package/dist/server/stores/index.js +63 -0
- package/dist/server/stores/pglite.d.ts +16 -0
- package/dist/server/stores/pglite.js +98 -0
- package/dist/server/stores/postgres.d.ts +63 -0
- package/dist/server/stores/postgres.js +153 -0
- package/dist/server/{upstash.d.ts → stores/upstash.d.ts} +1 -1
- package/dist/vercel/files.js +1 -1
- package/dist/vercel/proxy.d.ts +6 -82
- package/dist/vercel/proxy.js +24 -524
- package/dist/vite/plugin.js +4 -2
- package/kit/ui/Menu.tsx +29 -18
- package/kit/ui/layer.ts +41 -0
- package/package.json +3 -1
- package/shell/review/Bar.tsx +1 -0
- package/shell/review/Card.tsx +17 -7
- package/shell/review/People.tsx +908 -134
- package/shell/review/Review.tsx +2 -2
- package/shell/review/anchor.ts +6 -1
- package/shell/review/client.ts +41 -7
- package/shell/review/menu-place.test.ts +41 -0
- package/shell/review/place.test.ts +26 -1
- package/shell/review/place.ts +17 -0
- package/shell/review/review.css +97 -11
- package/shell/review/view.test.ts +115 -1
- package/shell/review/view.ts +92 -2
- package/shell/studio/StudioHub.tsx +1 -0
- package/shell/studio/hub.css +99 -13
- package/skills/miles-deploy/SKILL.md +26 -4
- package/templates/studio/.env.example +9 -0
- package/templates/studio/README.md +13 -0
- package/templates/studio/package.json +1 -1
- /package/dist/server/{fake-upstash.d.ts → stores/fake-upstash.d.ts} +0 -0
- /package/dist/server/{upstash.js → stores/upstash.js} +0 -0
package/dist/vercel/proxy.js
CHANGED
|
@@ -1,530 +1,30 @@
|
|
|
1
1
|
// Vercel Routing Middleware (a studio site's proxy.ts, named in vercel.json
|
|
2
|
-
// as proxy.entrypoint): the gate
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
// 503, never a sign out. A Client asking for a prototype that is not
|
|
20
|
-
// client-facing, its page or any file built from it under
|
|
21
|
-
// /<project>/_p/<slug>/, gets 404. Without a session a page gets the sign-in
|
|
22
|
-
// page (401), which says what to do and has no form, and anything else a
|
|
23
|
-
// plain-text 401. Without GATE_SECRET or the store the gate fails closed (503).
|
|
24
|
-
import { next, rewrite } from '@vercel/functions';
|
|
25
|
-
import { base64, sha256 } from '../core/bytes.js';
|
|
26
|
-
import { escapeHtml } from '../core/html.js';
|
|
27
|
-
import { GATE_FONT, handoffPath, hostName, isPrototypeSlug, PROOF_PATH, routeOf, STUDIO_PAGE, studioRouteOf, } from '../core/prototype.js';
|
|
28
|
-
import { STUDIO_SITE, } from '../core/review/model.js';
|
|
29
|
-
import { ReviewError } from '../core/review/service.js';
|
|
30
|
-
import { studioReview } from '../server/api.js';
|
|
31
|
-
import { hostCache, hostOf } from '../server/hosts.js';
|
|
32
|
-
import { clearedCookie, isFresh, issuePersonToken, PERSON_COOKIE, readCookie, readPersonToken, sameOrigin, sessionCookie, sessionSeconds, studioGate, } from '../server/session.js';
|
|
33
|
-
export const DEFAULT_SITE_NAME = 'Prototypes';
|
|
34
|
-
// What the gate pages load before sign-in: the hub's face (the Vite plugin
|
|
35
|
-
// emits it at this fixed path, at the host root) and the favicon. Nothing else.
|
|
36
|
-
export { GATE_FONT };
|
|
37
|
-
// The gate pages. The CSS is a constant so its CSP hash is computed once and
|
|
38
|
-
// the pages carry no 'unsafe-inline', and they run no script at all. The gate
|
|
39
|
-
// is hub chrome, so it wears the hub's neutral system look (shell/system.css)
|
|
40
|
-
// inlined, never the project's brand. Geist (latin, variable) is the one
|
|
41
|
-
// font, preloaded so the first paint is already in it.
|
|
42
|
-
const CSS = `
|
|
43
|
-
@font-face{font-family:Geist;src:url(${GATE_FONT}) format("woff2");font-weight:100 900;font-style:normal;font-display:swap}
|
|
44
|
-
:root{color-scheme:light;--bg:oklch(.985 0 0);--surface:oklch(1 0 0);--ink:oklch(.205 0 0);--ink-3:oklch(.46 0 0);--brand:oklch(.205 0 0);--brand-hover:oklch(.3 0 0);--on-brand:oklch(.985 0 0);--ring:0 0 0 2px oklch(1 0 0),0 0 0 4px oklch(.556 0 0);--shadow:0 1px 2px 0 oklch(0 0 0/.05);--raised:0 0 0 1px oklch(0 0 0/.06),0 1px 2px oklch(0 0 0/.05),0 12px 24px -8px oklch(0 0 0/.1);--ease-out:cubic-bezier(.23,1,.32,1);--ease-press:cubic-bezier(.33,1,.68,1);--ease-hover:cubic-bezier(.25,.46,.45,.94)}
|
|
45
|
-
@media(prefers-color-scheme:dark){:root{color-scheme:dark;--bg:oklch(.145 0 0);--surface:oklch(.175 0 0);--ink:oklch(.985 0 0);--ink-3:oklch(.72 0 0);--brand:oklch(.985 0 0);--brand-hover:oklch(.87 0 0);--on-brand:oklch(.205 0 0);--ring:0 0 0 2px oklch(.175 0 0),0 0 0 4px oklch(.708 0 0);--shadow:0 0 #0000;--raised:0 0 0 1px oklch(1 0 0/.09)}}
|
|
46
|
-
*{box-sizing:border-box}
|
|
47
|
-
html{height:100%;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}
|
|
48
|
-
body{margin:0;min-height:100%;display:grid;place-items:center;padding:max(24px,env(safe-area-inset-top)) max(16px,env(safe-area-inset-right)) max(24px,env(safe-area-inset-bottom)) max(16px,env(safe-area-inset-left));background:var(--bg);color:var(--ink);font:14px/20px Geist,ui-sans-serif,system-ui,-apple-system,"Segoe UI",sans-serif;font-optical-sizing:auto;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;text-rendering:optimizeLegibility}
|
|
49
|
-
.gate{width:100%;max-width:360px;padding:32px;background:var(--surface);border-radius:12px;box-shadow:var(--raised);transition:opacity 240ms var(--ease-out),transform 240ms var(--ease-out)}
|
|
50
|
-
@starting-style{.gate{opacity:0;transform:translateY(6px)}}
|
|
51
|
-
h1{margin:0 0 4px;font-size:20px;line-height:28px;font-weight:600;letter-spacing:-.02em;text-wrap:balance}
|
|
52
|
-
p{margin:0;color:var(--ink-3);text-wrap:pretty}
|
|
53
|
-
p+p{margin-top:8px}
|
|
54
|
-
strong{font-weight:500;color:var(--ink)}
|
|
55
|
-
form{margin:0}
|
|
56
|
-
.go{display:block;width:100%;height:44px;margin-top:24px;padding:0;border:0;border-radius:6px;background:var(--brand);color:var(--on-brand);font:inherit;font-weight:500;line-height:44px;text-align:center;text-decoration:none;cursor:pointer;touch-action:manipulation;-webkit-user-select:none;user-select:none;box-shadow:var(--shadow);transition:background-color 120ms var(--ease-hover),transform 160ms var(--ease-press)}
|
|
57
|
-
.go:focus-visible{outline:2px solid transparent;outline-offset:2px;box-shadow:var(--ring)}
|
|
58
|
-
.go:active{transform:scale(.97)}
|
|
59
|
-
@media(hover:hover) and (pointer:fine){.go:hover{background:var(--brand-hover)}}
|
|
60
|
-
@media(max-width:400px){.gate{padding:24px}}
|
|
61
|
-
@media(prefers-reduced-motion:reduce){.gate{transition:opacity 240ms var(--ease-out)}@starting-style{.gate{transform:none}}.go:active{transform:none}}
|
|
62
|
-
`;
|
|
63
|
-
function shell(siteName, page, body) {
|
|
64
|
-
return `<!doctype html>
|
|
65
|
-
<html lang="en">
|
|
66
|
-
<head>
|
|
67
|
-
<meta charset="utf-8">
|
|
68
|
-
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
|
69
|
-
<meta name="robots" content="noindex, nofollow">
|
|
70
|
-
<meta name="color-scheme" content="light dark">
|
|
71
|
-
<title>${escapeHtml(siteName)}</title>
|
|
72
|
-
<link rel="icon" type="image/svg+xml" href="/favicon.svg">
|
|
73
|
-
<link rel="preload" href="${GATE_FONT}" as="font" type="font/woff2" crossorigin>
|
|
74
|
-
<style>${CSS}</style>
|
|
75
|
-
</head>
|
|
76
|
-
<body>
|
|
77
|
-
<main class="gate" data-gate="${page}">
|
|
78
|
-
<h1>${escapeHtml(siteName)}</h1>
|
|
79
|
-
${body}
|
|
80
|
-
</main>
|
|
81
|
-
</body>
|
|
82
|
-
</html>
|
|
83
|
-
`;
|
|
84
|
-
}
|
|
85
|
-
const signInPage = (siteName) => shell(siteName, 'sign-in', `<p>To sign in, open the link the studio sent you. Each link signs in one device, then stops working.</p>
|
|
86
|
-
<p>For another device, or if your link has stopped working, ask the studio for a new one.</p>`);
|
|
87
|
-
// A GET shows who the link is for and a POST signs them in, so a mail
|
|
88
|
-
// scanner that opens every link never uses it up.
|
|
89
|
-
const linkPage = (siteName, person) => shell(siteName, 'link', `<p>This link signs in <strong>${escapeHtml(person.name)}</strong>, ${escapeHtml(person.team)}, on this device.</p>
|
|
90
|
-
<p>It works once. For another device, ask the studio for a new link.</p>
|
|
91
|
-
<form method="post"><button class="go" type="submit" autofocus>Continue</button></form>`);
|
|
92
|
-
// Someone who opens their link again on a device it already signed in gets
|
|
93
|
-
// here too, so the page offers the way in as well: the prototypes on a
|
|
94
|
-
// client's host, the list of clients on the studio's own.
|
|
95
|
-
const usedPage = (siteName, home = 'prototypes') => shell(siteName, 'used', `<p>This link has been used, has expired or was removed. Each link signs in one device, then stops working.</p>
|
|
96
|
-
<p>Ask the studio for a new one. If this device is already signed in, open the ${home}.</p>
|
|
97
|
-
<a class="go" href="/">Open the ${home}</a>`);
|
|
98
|
-
/**
|
|
99
|
-
* On the studio's own host, after Open: why the client did not open, and the
|
|
100
|
-
* way back to the clients. Only a Studio person signed in there meets it.
|
|
101
|
-
*/
|
|
102
|
-
const stoppedPage = (siteName, why) => shell(siteName, 'stopped', `<p>${escapeHtml(why)}</p>\n<a class="go" href="/">Back to the clients</a>`);
|
|
103
|
-
const SIGNED_OUT = 'Sign in with the link the studio sent you.';
|
|
104
|
-
const USED = 'This link has been used, has expired or was removed.';
|
|
105
|
-
const OPEN_IN_BROWSER = 'Open this link in a browser to sign in.';
|
|
106
|
-
const PREVIEWS = {
|
|
107
|
-
'sign-in': signInPage,
|
|
108
|
-
link: (siteName) => linkPage(siteName, { name: 'Rae Chen', team: 'Acme' }),
|
|
109
|
-
used: (siteName) => usedPage(siteName),
|
|
110
|
-
stopped: (siteName) => stoppedPage(siteName, NOT_OPENED),
|
|
111
|
-
};
|
|
112
|
-
// `design-hub dev` has no gate; the Vite plugin serves this at /_gate/ (?page=link or ?page=used) so the pages can be reviewed.
|
|
113
|
-
export async function gatePreview(page, siteName = DEFAULT_SITE_NAME) {
|
|
114
|
-
// hasOwn, so ?page=__proto__ or toString never reaches Object.prototype.
|
|
115
|
-
const render = page !== null && Object.hasOwn(PREVIEWS, page) ? PREVIEWS[page] : signInPage;
|
|
116
|
-
return { html: render(siteName), csp: await csp() };
|
|
117
|
-
}
|
|
118
|
-
let cspPromise;
|
|
119
|
-
function csp() {
|
|
120
|
-
cspPromise ??= sha256(CSS).then((css) => `default-src 'none'; style-src 'sha256-${base64(css)}'; script-src 'none'; font-src 'self'; img-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'`);
|
|
121
|
-
return cspPromise;
|
|
122
|
-
}
|
|
123
|
-
const NO_STORE = 'no-store, max-age=0';
|
|
124
|
-
function wantsHtml(request) {
|
|
125
|
-
const dest = request.headers.get('sec-fetch-dest');
|
|
126
|
-
if (dest)
|
|
127
|
-
return dest === 'document' || dest === 'iframe';
|
|
128
|
-
return (request.headers.get('accept') ?? '').includes('text/html');
|
|
129
|
-
}
|
|
130
|
-
export const isRead = (request) => request.method === 'GET' || request.method === 'HEAD';
|
|
131
|
-
/**
|
|
132
|
-
* Every answer the gate makes itself carries these: never cached, never
|
|
133
|
-
* indexed, no referrer. The link page alone sends a same-origin referrer, so
|
|
134
|
-
* its Continue POST carries a real Origin: with no-referrer a browser sends
|
|
135
|
-
* `Origin: null`, which sameOrigin trusts only beside Sec-Fetch-Site, and
|
|
136
|
-
* older Safari sends no Sec-Fetch-Site.
|
|
137
|
-
*/
|
|
138
|
-
function gateHeaders(type, { cookie, allow, referrer = 'no-referrer', retryAfter }) {
|
|
139
|
-
const headers = new Headers({
|
|
140
|
-
'cache-control': NO_STORE,
|
|
141
|
-
'content-type': type,
|
|
142
|
-
'x-robots-tag': 'noindex, nofollow',
|
|
143
|
-
'referrer-policy': referrer,
|
|
144
|
-
});
|
|
145
|
-
if (cookie)
|
|
146
|
-
headers.append('set-cookie', cookie);
|
|
147
|
-
if (allow)
|
|
148
|
-
headers.set('allow', allow);
|
|
149
|
-
if (retryAfter)
|
|
150
|
-
headers.set('retry-after', String(retryAfter));
|
|
151
|
-
return headers;
|
|
152
|
-
}
|
|
153
|
-
export const plain = (request, status, text, extra = {}) => new Response(request.method === 'HEAD' ? null : text, {
|
|
154
|
-
status,
|
|
155
|
-
headers: gateHeaders('text/plain; charset=utf-8', extra),
|
|
156
|
-
});
|
|
157
|
-
/** A gate page for a browser, or `text` for anything else (a fetch, an asset, a preview bot). */
|
|
158
|
-
async function page(request, status, html, text, extra = {}) {
|
|
159
|
-
if (!wantsHtml(request))
|
|
160
|
-
return plain(request, status, text, extra);
|
|
161
|
-
const headers = gateHeaders('text/html; charset=utf-8', extra);
|
|
162
|
-
headers.set('content-security-policy', await csp());
|
|
163
|
-
return new Response(request.method === 'HEAD' ? null : html, { status, headers });
|
|
164
|
-
}
|
|
165
|
-
const unavailable = (request, error) => plain(request, 503, error);
|
|
166
|
-
/** One answer for a prototype a Client may not have, whether it is internal or not there at all. */
|
|
167
|
-
export const notFound = (request) => plain(request, 404, NOT_FOUND);
|
|
168
|
-
const NOT_FOUND = 'Not found.';
|
|
169
|
-
/** The store did not answer: a 503 that passes, so it says when to try again. */
|
|
170
|
-
export const storeDown = (request) => plain(request, 503, STORE_DOWN, { retryAfter: 30 });
|
|
171
|
-
const STORE_DOWN = 'The review store did not answer. Try again in a minute.';
|
|
172
|
-
/**
|
|
173
|
-
* A store call that can fail without signing anyone out: `undefined` when the
|
|
174
|
-
* store did not answer, so the caller answers 503 and leaves the cookie alone.
|
|
175
|
-
*/
|
|
176
|
-
async function ask(run) {
|
|
177
|
-
try {
|
|
178
|
-
return await run();
|
|
2
|
+
// as proxy.entrypoint): the gate (server/gate.ts), with its decisions carried
|
|
3
|
+
// out the way Vercel's middleware does, `next()` to pass on and `rewrite()`
|
|
4
|
+
// to go on to a function. Everything the gate decides is in the gate; this
|
|
5
|
+
// file only says it in Vercel's words: work after the response goes to
|
|
6
|
+
// `waitUntil`, and the network address the cap counts is `x-real-ip`, which
|
|
7
|
+
// Vercel's edge sets from the connection and overwrites whatever the caller
|
|
8
|
+
// sent (read by `ipAddress`), so no caller chooses it.
|
|
9
|
+
import { ipAddress, next, rewrite, waitUntil } from '@vercel/functions';
|
|
10
|
+
import { createGate, GATE_FONT, gatePreview } from '../server/gate.js';
|
|
11
|
+
export { GATE_FONT, gatePreview };
|
|
12
|
+
/** A gate's decision as Vercel's middleware answers it. */
|
|
13
|
+
export function onVercel(decision) {
|
|
14
|
+
if (decision instanceof Response)
|
|
15
|
+
return decision;
|
|
16
|
+
if ('answer' in decision) {
|
|
17
|
+
waitUntil(decision.after);
|
|
18
|
+
return decision.answer;
|
|
179
19
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* What a link that signs nobody in here answers, on any host: the used page,
|
|
187
|
-
* 404, its way in the host's own `/`. On the studio's own host only Studio
|
|
188
|
-
* links are looked up, so a Client's link there reads as an unknown one.
|
|
189
|
-
*/
|
|
190
|
-
const noLink = ({ siteName, request }, at) => page(request, 404, usedPage(siteName, at === STUDIO_SITE ? 'clients' : 'prototypes'), USED);
|
|
191
|
-
async function showLink(ctx, review, at, token) {
|
|
192
|
-
const { siteName, request } = ctx;
|
|
193
|
-
const person = await ask(() => review.peekLink(at, token));
|
|
194
|
-
if (person === undefined)
|
|
195
|
-
return storeDown(request);
|
|
196
|
-
return person
|
|
197
|
-
? page(request, 200, linkPage(siteName, person), OPEN_IN_BROWSER, { referrer: 'same-origin' })
|
|
198
|
-
: noLink(ctx, at);
|
|
199
|
-
}
|
|
200
|
-
/** The cookie for someone the store just said yes to, lasting to their access end. */
|
|
201
|
-
async function sessionFor(gate, person, at) {
|
|
202
|
-
const claim = { id: person.id, epoch: person.epoch, kind: person.kind };
|
|
203
|
-
const token = await issuePersonToken(gate, claim, person.accessEndsAt, at);
|
|
204
|
-
return sessionCookie(PERSON_COOKIE, token, sessionSeconds(person.accessEndsAt, at));
|
|
205
|
-
}
|
|
206
|
-
/** A 303 to `location` on this host, setting `cookie`: where a link or a handoff sends the person it signed in. */
|
|
207
|
-
function signedIn(cookie, location) {
|
|
208
|
-
const headers = gateHeaders('text/plain; charset=utf-8', { cookie });
|
|
209
|
-
headers.set('location', location);
|
|
210
|
-
return new Response(null, { status: 303, headers });
|
|
211
|
-
}
|
|
212
|
-
async function signInWithLink(ctx, { review, gate }, at, token) {
|
|
213
|
-
const { request } = ctx;
|
|
214
|
-
const person = await ask(() => review.useLink(at, token));
|
|
215
|
-
if (person === undefined)
|
|
216
|
-
return storeDown(request);
|
|
217
|
-
if (!person)
|
|
218
|
-
return noLink(ctx, at);
|
|
219
|
-
return signedIn(await sessionFor(gate, person, ctx.at), '/');
|
|
220
|
-
}
|
|
221
|
-
/** A one-time link where `at` reaches: shown on a read, used on a same-origin POST. */
|
|
222
|
-
async function link(ctx, setup, at, token) {
|
|
223
|
-
const { request } = ctx;
|
|
224
|
-
if (request.method === 'POST' && !sameOrigin(request))
|
|
225
|
-
return plain(request, 403, 'Forbidden');
|
|
226
|
-
if (request.method !== 'POST' && !isRead(request))
|
|
227
|
-
return plain(request, 405, 'Method not allowed', { allow: 'GET, HEAD, POST' });
|
|
228
|
-
return request.method === 'POST'
|
|
229
|
-
? signInWithLink(ctx, setup, at, token)
|
|
230
|
-
: showLink(ctx, setup.review, at, token);
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* No session. A page gets the sign-in page, and anything else (an asset, a
|
|
234
|
-
* fetch, a POST to any path but a link) a plain 401: there is nothing to
|
|
235
|
-
* sign in with here. A cookie that was sent but does not hold is cleared.
|
|
236
|
-
*/
|
|
237
|
-
function signedOut({ siteName, request }, staleCookie) {
|
|
238
|
-
const extra = staleCookie ? { cookie: clearedCookie(PERSON_COOKIE) } : {};
|
|
239
|
-
if (!isRead(request))
|
|
240
|
-
return plain(request, 401, SIGNED_OUT, extra);
|
|
241
|
-
return page(request, 401, signInPage(siteName), SIGNED_OUT, extra);
|
|
242
|
-
}
|
|
243
|
-
const passHeaders = (cookie) => ({
|
|
244
|
-
'cache-control': 'private, no-store',
|
|
245
|
-
...(cookie ? { 'set-cookie': cookie } : {}),
|
|
246
|
-
});
|
|
247
|
-
/**
|
|
248
|
-
* Whether a prototype is open to a Client in `scope`: client-facing, asked of
|
|
249
|
-
* the store on every request, no cache. A name that is no prototype slug
|
|
250
|
-
* (`all.js`, or '' for a path that could mean more than one file) is never
|
|
251
|
-
* open, without asking. `undefined` when the store did not answer.
|
|
252
|
-
*/
|
|
253
|
-
async function prototypeOpen(review, scope, slug) {
|
|
254
|
-
if (!isPrototypeSlug(slug))
|
|
255
|
-
return false;
|
|
256
|
-
const audience = await ask(() => review.audienceOf(scope, slug));
|
|
257
|
-
return audience === undefined ? undefined : audience === 'client';
|
|
258
|
-
}
|
|
259
|
-
const NOT_THERE = { refused: 'not-found' };
|
|
260
|
-
const DOWN = { refused: 'store-down' };
|
|
261
|
-
/** What the request's host serves, or why nothing: a site not set up, a store that did not answer, or the plain 404. */
|
|
262
|
-
async function lookUp(request, hosts, at) {
|
|
263
|
-
const setup = studioReview(process.env, null, () => at);
|
|
264
|
-
if ('error' in setup)
|
|
265
|
-
return { refused: 'unavailable', error: setup.error };
|
|
266
|
-
const host = hostName(hostOf(request));
|
|
267
|
-
const site = host ? await ask(() => hosts(host, (h) => setup.review.siteOfHost(h))) : null;
|
|
268
|
-
if (site === undefined)
|
|
269
|
-
return DOWN;
|
|
270
|
-
return site ? { review: setup.review, keys: setup.keys, site } : NOT_THERE;
|
|
271
|
-
}
|
|
272
|
-
/** A client's host: its gate and scope, and where `pathname` goes on it. */
|
|
273
|
-
function placeOf({ review, keys }, client, request, pathname) {
|
|
274
|
-
const gate = { ...keys, client: client.slug, since: client.createdAt };
|
|
275
|
-
const place = { client, gate, scope: { client: client.slug } };
|
|
276
|
-
return { review, place, route: routeOf(hostOf(request), pathname) };
|
|
277
|
-
}
|
|
278
|
-
/**
|
|
279
|
-
* The client `pathname` is asked of on the request's host, or why there is
|
|
280
|
-
* none: an unknown host is the plain 404, and so is the studio's own host,
|
|
281
|
-
* which serves no client's anything.
|
|
282
|
-
*/
|
|
283
|
-
export async function findPlace(request, pathname, hosts, at) {
|
|
284
|
-
const looked = await lookUp(request, hosts, at);
|
|
285
|
-
if ('refused' in looked)
|
|
286
|
-
return looked;
|
|
287
|
-
if (looked.site === STUDIO_SITE)
|
|
288
|
-
return NOT_THERE;
|
|
289
|
-
return placeOf(looked, looked.site, request, pathname);
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* The project a route names, when it is the client's, and for a Client, only
|
|
293
|
-
* a client-facing prototype's files. Every other answer is the plain 404, so
|
|
294
|
-
* another client's project, a project with no build (its files and its
|
|
295
|
-
* review API alike) and an internal prototype read the same as a path that
|
|
296
|
-
* is not there.
|
|
297
|
-
*/
|
|
298
|
-
async function boundary({ review, place, route }, kind) {
|
|
299
|
-
if (route.kind === 'none' || route.kind === 'link' || route.kind === 'handoff')
|
|
300
|
-
return NOT_THERE;
|
|
301
|
-
const named = route.kind === 'file' || route.kind === 'api' ? route.project : null;
|
|
302
|
-
if (named === null)
|
|
303
|
-
return null;
|
|
304
|
-
const scope = { client: place.client.slug, project: named };
|
|
305
|
-
const project = await ask(() => review.projectOf(scope));
|
|
306
|
-
if (project === undefined)
|
|
307
|
-
return DOWN;
|
|
308
|
-
if (!project || project.build === null)
|
|
309
|
-
return NOT_THERE;
|
|
310
|
-
if (route.kind !== 'file' || route.prototype === null || kind === 'studio')
|
|
311
|
-
return project;
|
|
312
|
-
const open = await prototypeOpen(review, scope, route.prototype);
|
|
313
|
-
return open === undefined ? DOWN : open ? project : NOT_THERE;
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* The session a request's cookie carries where `at` is, signed to `gate`'s
|
|
317
|
-
* client, and asked of the store again once its stamp is stale. On the
|
|
318
|
-
* studio's own host only a Studio person's holds.
|
|
319
|
-
*/
|
|
320
|
-
async function session(review, gate, at, request, time) {
|
|
321
|
-
const cookie = readCookie(request, PERSON_COOKIE);
|
|
322
|
-
const claim = await readPersonToken(gate, cookie, time);
|
|
323
|
-
if (!claim || (at === STUDIO_SITE && claim.kind !== 'studio'))
|
|
324
|
-
return { refused: 'signed-out', stale: cookie !== undefined };
|
|
325
|
-
if (isFresh(claim, time))
|
|
326
|
-
return { claim, current: null };
|
|
327
|
-
const asked = await ask(() => review.sessionPerson(at, claim.id, claim.epoch));
|
|
328
|
-
if (asked === undefined)
|
|
329
|
-
return DOWN;
|
|
330
|
-
return asked ? { claim, current: asked } : { refused: 'signed-out', stale: true };
|
|
331
|
-
}
|
|
332
|
-
/**
|
|
333
|
-
* The checks every studio request passes, in the gate and again in the files
|
|
334
|
-
* function: a session signed to this host's client (asked of the store again
|
|
335
|
-
* once its stamp is stale), then the boundary.
|
|
336
|
-
*/
|
|
337
|
-
export async function admit(found, request, at) {
|
|
338
|
-
const held = await session(found.review, found.place.gate, found.place.scope, request, at);
|
|
339
|
-
if ('refused' in held)
|
|
340
|
-
return held;
|
|
341
|
-
const { claim, current } = held;
|
|
342
|
-
const kind = current ? current.kind : claim.kind;
|
|
343
|
-
const project = await boundary(found, kind);
|
|
344
|
-
return project && 'refused' in project ? project : { ...found, kind, current, project };
|
|
345
|
-
}
|
|
346
|
-
/** The answer for a refusal: the same from the gate and from the files function. */
|
|
347
|
-
export function refuse(ctx, refusal) {
|
|
348
|
-
switch (refusal.refused) {
|
|
349
|
-
case 'not-found':
|
|
350
|
-
return notFound(ctx.request);
|
|
351
|
-
case 'store-down':
|
|
352
|
-
return storeDown(ctx.request);
|
|
353
|
-
case 'unavailable':
|
|
354
|
-
return unavailable(ctx.request, refusal.error);
|
|
355
|
-
case 'signed-out':
|
|
356
|
-
return signedOut(ctx, refusal.stale);
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
/**
|
|
360
|
-
* Where an admitted request goes on to: a project's file to the files
|
|
361
|
-
* function and its review API to the review function, each with the project
|
|
362
|
-
* the path named, and anything else on to the site's own files.
|
|
363
|
-
*/
|
|
364
|
-
function onward(url, route, cookie) {
|
|
365
|
-
const headers = passHeaders(cookie);
|
|
366
|
-
if (route.kind === 'file')
|
|
367
|
-
return rewrite(new URL(`/api/files?path=${encodeURIComponent(url.pathname)}`, url), { headers });
|
|
368
|
-
if (route.kind !== 'api' || route.project === null)
|
|
369
|
-
return next({ headers });
|
|
370
|
-
const to = new URL(`/api/review${url.search}`, url);
|
|
371
|
-
to.searchParams.set('project', route.project);
|
|
372
|
-
return rewrite(to, { headers });
|
|
373
|
-
}
|
|
374
|
-
/**
|
|
375
|
-
* The host proof `client add` asks a new client's host for (PROOF_PATH): its
|
|
376
|
-
* answer, on a GET whose ask the store holds for this host's client. Any
|
|
377
|
-
* other ask, method or store failure goes on as the hub path it is, so the
|
|
378
|
-
* path tells a caller without the ask nothing, and a host no client has
|
|
379
|
-
* never gets here (findPlace's plain 404).
|
|
380
|
-
*/
|
|
381
|
-
async function proven({ review, place, route }, request, pathname) {
|
|
382
|
-
const given = route.kind === 'hub' && request.method === 'GET' ? pathname.match(PROOF_PATH)?.[1] : undefined;
|
|
383
|
-
const answer = given ? await ask(() => review.proofAnswer(place.scope, given)) : null;
|
|
384
|
-
return answer ? plain(request, 200, answer) : null;
|
|
385
|
-
}
|
|
386
|
-
/**
|
|
387
|
-
* A handoff's claim on a client's host (`/_gate/handoff/<token>`), a GET the
|
|
388
|
-
* studio's host redirected here: the store gives up the handoff once, only
|
|
389
|
-
* when it was made for this host's client and is not past its minute, and
|
|
390
|
-
* the gate sets this host's own cookie for the Studio person it names. It
|
|
391
|
-
* goes on to the client's hub, or to the project the handoff named, a path
|
|
392
|
-
* built here from the stored slug, never from the request. Anything else is
|
|
393
|
-
* a used link's answer; nothing is used up by a HEAD or a POST.
|
|
394
|
-
*/
|
|
395
|
-
async function openHandoff(ctx, { review, place }, token) {
|
|
396
|
-
const { request, siteName } = ctx;
|
|
397
|
-
if (request.method !== 'GET')
|
|
398
|
-
return plain(request, 405, 'Method not allowed', { allow: 'GET' });
|
|
399
|
-
const claimed = await ask(() => review.claimHandoff(place.scope, token));
|
|
400
|
-
if (claimed === undefined)
|
|
401
|
-
return storeDown(request);
|
|
402
|
-
if (!claimed)
|
|
403
|
-
return page(request, 404, usedPage(siteName), USED);
|
|
404
|
-
const to = claimed.project ? `/${claimed.project}/` : '/';
|
|
405
|
-
return signedIn(await sessionFor(place.gate, claimed.person, ctx.at), to);
|
|
406
|
-
}
|
|
407
|
-
const NOT_OPENED = 'This client or project cannot be opened now. It may still be waiting for its address, or it was archived or is not shared yet.';
|
|
408
|
-
const PICK_A_CLIENT = 'Open a client from the list of clients.';
|
|
409
|
-
/** A handoff that did not start, for a browser the stopped page, for anything else its text. */
|
|
410
|
-
const stopped = (ctx, status, why, extra = {}) => page(ctx.request, status, stoppedPage(ctx.siteName, why), why, extra);
|
|
411
|
-
/** Form bodies the studio hub sends: two slugs, never more than this. */
|
|
412
|
-
const MAX_FORM_BYTES = 1024;
|
|
413
|
-
/** The client and project a handoff is asked for, from a same-origin form POST; null for anything else. */
|
|
414
|
-
async function handoffAsked(request) {
|
|
415
|
-
const type = request.headers.get('content-type') ?? '';
|
|
416
|
-
if (!type.startsWith('application/x-www-form-urlencoded'))
|
|
417
|
-
return null;
|
|
418
|
-
const text = await request.text();
|
|
419
|
-
if (text.length > MAX_FORM_BYTES)
|
|
420
|
-
return null;
|
|
421
|
-
const form = new URLSearchParams(text);
|
|
422
|
-
return { client: form.get('client') ?? '', project: form.get('project') || null };
|
|
423
|
-
}
|
|
424
|
-
/**
|
|
425
|
-
* A handoff asked for on the studio's own host (POST /_gate/handoff): the
|
|
426
|
-
* Studio person is read from the store, whatever the cookie's stamp says,
|
|
427
|
-
* the service writes a one-time handoff for that one client, and the answer
|
|
428
|
-
* sends the browser to the client's host to claim it. The address is the
|
|
429
|
-
* client's stored host and the token, never text the request sent; a client
|
|
430
|
-
* still being added, archived or unknown, or a project it has not built, is
|
|
431
|
-
* the plain 404.
|
|
432
|
-
*/
|
|
433
|
-
async function startHandoff(ctx, review, { claim }) {
|
|
434
|
-
const { request } = ctx;
|
|
435
|
-
if (request.method !== 'POST')
|
|
436
|
-
return plain(request, 405, 'Method not allowed', { allow: 'POST' });
|
|
437
|
-
if (!sameOrigin(request))
|
|
438
|
-
return plain(request, 403, 'Forbidden');
|
|
439
|
-
const asked = await handoffAsked(request);
|
|
440
|
-
if (!asked)
|
|
441
|
-
return stopped(ctx, 400, PICK_A_CLIENT);
|
|
442
|
-
const person = await ask(() => review.sessionPerson(STUDIO_SITE, claim.id, claim.epoch));
|
|
443
|
-
if (person === undefined)
|
|
444
|
-
return stopped(ctx, 503, STORE_DOWN, { retryAfter: 30 });
|
|
445
|
-
if (!person)
|
|
446
|
-
return page(request, 401, signInPage(ctx.siteName), SIGNED_OUT, { cookie: clearedCookie(PERSON_COOKIE) });
|
|
447
|
-
let made;
|
|
448
|
-
try {
|
|
449
|
-
made = await review.startHandoff(person, asked);
|
|
450
|
-
}
|
|
451
|
-
catch (error) {
|
|
452
|
-
if (error instanceof ReviewError)
|
|
453
|
-
return stopped(ctx, 404, NOT_OPENED);
|
|
454
|
-
console.error('[gate]', error);
|
|
455
|
-
return stopped(ctx, 503, STORE_DOWN, { retryAfter: 30 });
|
|
456
|
-
}
|
|
457
|
-
const headers = gateHeaders('text/plain; charset=utf-8', {});
|
|
458
|
-
headers.set('location', `https://${made.host}${handoffPath(made.token)}`);
|
|
459
|
-
return new Response(null, { status: 303, headers });
|
|
460
|
-
}
|
|
461
|
-
/**
|
|
462
|
-
* The host proof `studio create` and `studio deploy` ask the studio's own
|
|
463
|
-
* host for: its answer, on a GET whose ask the store holds. Any other ask,
|
|
464
|
-
* method or store failure is the plain 404 every unknown path there gets.
|
|
465
|
-
*/
|
|
466
|
-
async function studioProof(ctx, review, given) {
|
|
467
|
-
const { request } = ctx;
|
|
468
|
-
const answer = request.method === 'GET' ? await ask(() => review.studioProofAnswer(given)) : null;
|
|
469
|
-
return answer ? plain(request, 200, answer) : notFound(request);
|
|
470
|
-
}
|
|
471
|
-
/**
|
|
472
|
-
* The studio's own host (`studio.<apex>`): Studio signs in here with a
|
|
473
|
-
* Studio person's link, and nobody else gets in. Its session is signed to
|
|
474
|
-
* `studio`, so a client host's cookie never holds here, and a Client's
|
|
475
|
-
* cookie answers the sign-in page a client host shows, word for word; a
|
|
476
|
-
* Client's link reads as an unknown one (the used page). Its host proof
|
|
477
|
-
* is answered before any session. Signed in, `/` is the studio hub and `/api/review` its data (the
|
|
478
|
-
* review function checks the session again), and a POST to /_gate/handoff
|
|
479
|
-
* opens a client. Everything else is the plain 404, every path a client's
|
|
480
|
-
* host answers included.
|
|
481
|
-
*/
|
|
482
|
-
async function studioHost(ctx, { review, keys }, url) {
|
|
483
|
-
const { request } = ctx;
|
|
484
|
-
const where = studioRouteOf(url.pathname);
|
|
485
|
-
const gate = studioGate(keys);
|
|
486
|
-
if (where.kind === 'none')
|
|
487
|
-
return notFound(request);
|
|
488
|
-
if (where.kind === 'link')
|
|
489
|
-
return link(ctx, { review, gate }, STUDIO_SITE, where.token);
|
|
490
|
-
if (where.kind === 'proof')
|
|
491
|
-
return studioProof(ctx, review, where.ask);
|
|
492
|
-
if (where.kind === 'asset' && isRead(request))
|
|
493
|
-
return next();
|
|
494
|
-
const held = await session(review, gate, STUDIO_SITE, request, ctx.at);
|
|
495
|
-
if ('refused' in held)
|
|
496
|
-
return refuse(ctx, held);
|
|
497
|
-
if (where.kind === 'handoff')
|
|
498
|
-
return startHandoff(ctx, review, held);
|
|
499
|
-
const headers = passHeaders(held.current ? await sessionFor(gate, held.current, ctx.at) : undefined);
|
|
500
|
-
return where.kind === 'page' ? rewrite(new URL(STUDIO_PAGE, url), { headers }) : next({ headers });
|
|
20
|
+
if (decision.pass === 'rewrite')
|
|
21
|
+
return rewrite(decision.to, { headers: decision.headers });
|
|
22
|
+
return decision.headers ? next({ headers: decision.headers }) : next();
|
|
501
23
|
}
|
|
502
|
-
/** The middleware a studio site's proxy.ts default-exports.
|
|
503
|
-
export function createStudioProxy(
|
|
504
|
-
const
|
|
24
|
+
/** The middleware a studio site's proxy.ts default-exports. */
|
|
25
|
+
export function createStudioProxy(options = {}) {
|
|
26
|
+
const gate = createGate(options);
|
|
505
27
|
return async function proxy(request) {
|
|
506
|
-
|
|
507
|
-
const url = new URL(request.url);
|
|
508
|
-
const looked = await lookUp(request, hosts, ctx.at);
|
|
509
|
-
if ('refused' in looked)
|
|
510
|
-
return refuse(ctx, looked);
|
|
511
|
-
if (looked.site === STUDIO_SITE)
|
|
512
|
-
return studioHost(ctx, looked, url);
|
|
513
|
-
const found = placeOf(looked, looked.site, request, url.pathname);
|
|
514
|
-
const proof = await proven(found, request, url.pathname);
|
|
515
|
-
if (proof)
|
|
516
|
-
return proof;
|
|
517
|
-
const { route, place } = found;
|
|
518
|
-
if (route.kind === 'link')
|
|
519
|
-
return link(ctx, { review: found.review, gate: place.gate }, place.scope, route.token);
|
|
520
|
-
if (route.kind === 'handoff')
|
|
521
|
-
return openHandoff(ctx, found, route.token);
|
|
522
|
-
if (route.kind === 'asset' && isRead(request))
|
|
523
|
-
return next();
|
|
524
|
-
const admitted = await admit(found, request, ctx.at);
|
|
525
|
-
if ('refused' in admitted)
|
|
526
|
-
return refuse(ctx, admitted);
|
|
527
|
-
const cookie = admitted.current ? await sessionFor(place.gate, admitted.current, ctx.at) : undefined;
|
|
528
|
-
return onward(url, route, cookie);
|
|
28
|
+
return onVercel(await gate(request, { address: ipAddress(request) ?? null }));
|
|
529
29
|
};
|
|
530
30
|
}
|
package/dist/vite/plugin.js
CHANGED
|
@@ -13,8 +13,8 @@ import { build, searchForWorkspaceRoot } from 'vite';
|
|
|
13
13
|
import { isProjectSlug, STUDIO_DIR, STUDIO_PAGE } from '../core/prototype.js';
|
|
14
14
|
import { devReview } from '../server/dev.js';
|
|
15
15
|
import { devPort, readEnv } from '../server/env.js';
|
|
16
|
+
import { GATE_FONT, gatePreview } from '../server/gate.js';
|
|
16
17
|
import { PACKAGE, PACKAGE_KIT, SHELL } from '../server/paths.js';
|
|
17
|
-
import { GATE_FONT, gatePreview } from '../vercel/proxy.js';
|
|
18
18
|
import { kitResolver } from './kit.js';
|
|
19
19
|
import { lapse } from './lapse.js';
|
|
20
20
|
import { markdownDocs } from './markdown.js';
|
|
@@ -23,7 +23,9 @@ const require = createRequire(import.meta.url);
|
|
|
23
23
|
// The gate pages load before sign-in, so they cannot reach the hashed
|
|
24
24
|
// bundle. Their font goes out at the one fixed path the gate lets through.
|
|
25
25
|
// `design-hub dev` has no gate, so there /_gate/ previews the sign-in page,
|
|
26
|
-
// ?page=link a link's page and ?page=used a used or expired link's
|
|
26
|
+
// ?page=link a link's page and ?page=used a used or expired link's; email
|
|
27
|
+
// sign-in's pages too: ?page=email, code, code-wrong, code-expired,
|
|
28
|
+
// code-tries, code-long and slow (gatePreview in server/gate.ts).
|
|
27
29
|
const gateFontFile = () => join(dirname(require.resolve('@fontsource-variable/geist')), 'files', 'geist-latin-wght-normal.woff2');
|
|
28
30
|
function gateFont() {
|
|
29
31
|
return {
|
package/kit/ui/Menu.tsx
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
} from 'react';
|
|
11
11
|
import { createPortal } from 'react-dom';
|
|
12
12
|
import { Icon, type IconName } from '../Icon';
|
|
13
|
-
import { afterTransition } from './layer';
|
|
13
|
+
import { afterTransition, placeMenu } from './layer';
|
|
14
14
|
import './ui.css';
|
|
15
15
|
|
|
16
16
|
export type MenuItem = {
|
|
@@ -40,6 +40,8 @@ export type MenuConfig = {
|
|
|
40
40
|
selected?: string;
|
|
41
41
|
checkable?: boolean;
|
|
42
42
|
role?: 'menu' | 'listbox';
|
|
43
|
+
/** A selector for the card or panel the trigger sits in: the menu opens on the side that stays inside it. */
|
|
44
|
+
within?: string;
|
|
43
45
|
/** Added to the popup, which portals to <body>: the way a layer with its own material and stacking dresses its menus. */
|
|
44
46
|
className?: string;
|
|
45
47
|
onSelect?: (value: string | undefined, item: MenuItem) => void;
|
|
@@ -162,7 +164,13 @@ function MenuPopup({
|
|
|
162
164
|
const menu = popupRef.current;
|
|
163
165
|
const trigger = triggerRef.current;
|
|
164
166
|
if (!menu || !trigger) return;
|
|
165
|
-
place(
|
|
167
|
+
place(
|
|
168
|
+
menu,
|
|
169
|
+
trigger,
|
|
170
|
+
typeof config.width === 'function' ? config.width(trigger) : config.width,
|
|
171
|
+
align,
|
|
172
|
+
config.within,
|
|
173
|
+
);
|
|
166
174
|
const frame = requestAnimationFrame(() => setShown(true));
|
|
167
175
|
const focus = setTimeout(() => {
|
|
168
176
|
const picked = selected != null ? menu.querySelector<HTMLElement>('.menu-item.is-selected') : null;
|
|
@@ -265,23 +273,26 @@ const STEP: Record<string, (i: number, last: number) => number> = {
|
|
|
265
273
|
End: (_, last) => last,
|
|
266
274
|
};
|
|
267
275
|
|
|
268
|
-
/**
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
276
|
+
/** Puts the menu where `placeMenu` says, for its trigger and the container it should stay inside. */
|
|
277
|
+
function place(
|
|
278
|
+
menu: HTMLElement,
|
|
279
|
+
trigger: HTMLElement,
|
|
280
|
+
width: number | undefined,
|
|
281
|
+
align: 'start' | 'end',
|
|
282
|
+
within: string | undefined,
|
|
283
|
+
) {
|
|
275
284
|
if (width) menu.style.width = `${width}px`;
|
|
276
|
-
const
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
menu.
|
|
284
|
-
menu.style.
|
|
285
|
+
const at = placeMenu(
|
|
286
|
+
trigger.getBoundingClientRect(),
|
|
287
|
+
{ width: width || menu.offsetWidth, height: menu.offsetHeight },
|
|
288
|
+
{ width: innerWidth, height: innerHeight },
|
|
289
|
+
align,
|
|
290
|
+
within ? trigger.closest(within)?.getBoundingClientRect() : null,
|
|
291
|
+
);
|
|
292
|
+
menu.dataset.side = at.side;
|
|
293
|
+
menu.style.top = `${at.top + scrollY}px`;
|
|
294
|
+
menu.style.left = `${at.left + scrollX}px`;
|
|
295
|
+
menu.style.transformOrigin = at.origin;
|
|
285
296
|
}
|
|
286
297
|
|
|
287
298
|
function MenuKeys({ keys }: { keys: NonNullable<MenuItem['keys']> }) {
|