@milesroxas/design-hub 0.13.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 +1 -1
- 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 +536 -1
- package/core/review/service.ts +452 -25
- package/core/review/store.ts +45 -4
- 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 +4 -0
- package/dist/scripts/client.d.ts +1 -1
- package/dist/scripts/client.js +1 -1
- 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 +16 -1
- package/dist/scripts/lib/fake-host.js +36 -1
- package/dist/scripts/lib/host.d.ts +2 -0
- 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 +7 -0
- package/dist/scripts/lib/studio-deps.js +6 -1
- package/dist/scripts/lib/studios.d.ts +2 -2
- package/dist/scripts/lib/studios.js +5 -5
- package/dist/scripts/lib/vercel-host.js +6 -0
- package/dist/scripts/people.d.ts +16 -4
- package/dist/scripts/people.js +67 -17
- package/dist/scripts/studio.d.ts +19 -3
- package/dist/scripts/studio.js +327 -6
- package/dist/server/api.d.ts +14 -2
- package/dist/server/api.js +71 -17
- package/dist/server/dev.js +21 -1
- package/dist/server/gate.d.ts +26 -3
- package/dist/server/gate.js +323 -52
- 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/session.js +4 -1
- package/dist/vercel/proxy.js +10 -3
- package/dist/vite/plugin.js +3 -1
- package/kit/ui/Menu.tsx +29 -18
- package/kit/ui/layer.ts +41 -0
- package/package.json +1 -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 +24 -3
- package/templates/studio/.env.example +6 -0
- package/templates/studio/package.json +1 -1
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// The one port mail goes through (email.md, E12): a studio site sends when it
|
|
2
|
+
// has a mail key and a sender, and never otherwise. Resend is its first
|
|
3
|
+
// adapter (server/mailers/resend.ts). A send never throws and answers sent,
|
|
4
|
+
// refused or down; whoever sends records the answer for Studio (recordMail),
|
|
5
|
+
// and a person is never shown it (E7).
|
|
6
|
+
import { escapeHtml } from '../core/html.js';
|
|
7
|
+
import { CODE } from '../core/review/model.js';
|
|
8
|
+
import { resendMailer } from './mailers/resend.js';
|
|
9
|
+
/** The name in a sender (`Suits & Sandals <signin@mail.example.com>`), or its address when it has none. */
|
|
10
|
+
export function senderName(from) {
|
|
11
|
+
const named = from.match(/^\s*"?([^"<]*?)"?\s*<([^>]+)>\s*$/);
|
|
12
|
+
return (named?.[1] || named?.[2] || from).trim();
|
|
13
|
+
}
|
|
14
|
+
/** The studio's mailer: null unless both RESEND_API_KEY and MAIL_FROM are set, so a studio that wants only links sets up nothing. */
|
|
15
|
+
export function chooseMailer(env, fetcher) {
|
|
16
|
+
const key = env.RESEND_API_KEY?.trim();
|
|
17
|
+
const from = env.MAIL_FROM?.trim();
|
|
18
|
+
if (!key || !from)
|
|
19
|
+
return null;
|
|
20
|
+
return { name: senderName(from), ...resendMailer({ key, from, ...(fetcher ? { fetcher } : {}) }) };
|
|
21
|
+
}
|
|
22
|
+
/** A code as it is read aloud: two groups of four. */
|
|
23
|
+
const spacedCode = (code) => `${code.slice(0, 4)} ${code.slice(4)}`;
|
|
24
|
+
/**
|
|
25
|
+
* The code mail (email.md, section 3): the studio's name, the code in two
|
|
26
|
+
* groups of four, how long it lasts, and one line to ignore it. No link, no
|
|
27
|
+
* tracking, no image. The subject starts with the code, so it reads from a
|
|
28
|
+
* notification. `key` is the person and the code's hash, so each new code is
|
|
29
|
+
* a new mail and a second send of one code is not.
|
|
30
|
+
*/
|
|
31
|
+
export function codeMail(studio, to, code, key) {
|
|
32
|
+
const spaced = spacedCode(code);
|
|
33
|
+
const subject = `${spaced} is your sign-in code for ${studio}`;
|
|
34
|
+
const lasts = `It works once and lasts ${CODE.minutes} minutes.`;
|
|
35
|
+
const ignore = 'If you did not ask for it, ignore this email.';
|
|
36
|
+
const text = `Your sign-in code for ${studio}:\n\n${spaced}\n\n${lasts}\n\n${ignore}\n`;
|
|
37
|
+
const font = 'font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif';
|
|
38
|
+
const html = `<!doctype html>
|
|
39
|
+
<html lang="en">
|
|
40
|
+
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>${escapeHtml(subject)}</title></head>
|
|
41
|
+
<body style="margin:0;padding:32px 16px;background:#ffffff;color:#171717;${font};font-size:14px;line-height:20px">
|
|
42
|
+
<div style="max-width:360px;margin:0 auto">
|
|
43
|
+
<p style="margin:0 0 16px">Your sign-in code for ${escapeHtml(studio)}:</p>
|
|
44
|
+
<p style="margin:0 0 16px;font-size:28px;line-height:36px;font-weight:600;letter-spacing:.08em;font-variant-numeric:tabular-nums">${spaced}</p>
|
|
45
|
+
<p style="margin:0 0 8px;color:#585858">${lasts}</p>
|
|
46
|
+
<p style="margin:0;color:#585858">${ignore}</p>
|
|
47
|
+
</div>
|
|
48
|
+
</body>
|
|
49
|
+
</html>
|
|
50
|
+
`;
|
|
51
|
+
return { to, subject, text, html, key: `code-${key}` };
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The invite (email.md, E10): who gave them access, their one-time link as
|
|
55
|
+
* one button and as plain text, that it works once on one device, and the
|
|
56
|
+
* address to sign in at with this email from then on: their client's own
|
|
57
|
+
* host, or the studio's for a Studio person, never another's. Its words are
|
|
58
|
+
* the link page's ("Sign in as", "Next time, sign in with your email"). No
|
|
59
|
+
* tracking, no image. `key` is the person's id: one add, one mail.
|
|
60
|
+
*/
|
|
61
|
+
export function inviteMail(studio, person, link, address) {
|
|
62
|
+
const subject = `Your sign-in link for ${studio}`;
|
|
63
|
+
const title = `Sign in as ${person.name}`;
|
|
64
|
+
const lead = `${studio} gave you access. This link works once, on one device.`;
|
|
65
|
+
const home = `https://${address}`;
|
|
66
|
+
const text = `${title}\n\n${lead}\n\n${link}\n\nNext time, sign in with your email at ${home}\n`;
|
|
67
|
+
const font = 'font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif';
|
|
68
|
+
const muted = 'color:#585858';
|
|
69
|
+
const html = `<!doctype html>
|
|
70
|
+
<html lang="en">
|
|
71
|
+
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>${escapeHtml(subject)}</title></head>
|
|
72
|
+
<body style="margin:0;padding:32px 16px;background:#ffffff;color:#171717;${font};font-size:14px;line-height:20px">
|
|
73
|
+
<div style="max-width:360px;margin:0 auto">
|
|
74
|
+
<p style="margin:0 0 12px;${muted}">${escapeHtml(studio)}</p>
|
|
75
|
+
<p style="margin:0 0 8px;font-size:20px;line-height:28px;font-weight:600;overflow-wrap:anywhere">${escapeHtml(title)}</p>
|
|
76
|
+
<p style="margin:0 0 20px">${escapeHtml(lead)}</p>
|
|
77
|
+
<p style="margin:0 0 20px"><a href="${escapeHtml(link)}" style="display:inline-block;padding:12px 20px;line-height:20px;border-radius:8px;background:#171717;color:#ffffff;font-weight:600;text-decoration:none">Sign in</a></p>
|
|
78
|
+
<p style="margin:0 0 20px">Next time, sign in with your email at <a href="${escapeHtml(home)}" style="color:#171717">${escapeHtml(address)}</a>.</p>
|
|
79
|
+
<p style="margin:0 0 4px;font-size:12px;line-height:16px;${muted}">If the button does not open, paste this link into your browser:</p>
|
|
80
|
+
<p style="margin:0;font-size:12px;line-height:16px;${muted};word-break:break-all">${escapeHtml(link)}</p>
|
|
81
|
+
</div>
|
|
82
|
+
</body>
|
|
83
|
+
</html>
|
|
84
|
+
`;
|
|
85
|
+
return { to: person.email, subject, text, html, key: `invite-${person.id}` };
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Sends one mail and records the answer for Studio (recordMail, email.md,
|
|
89
|
+
* "When mail breaks"): the one way the studio's mail goes out, a code's or an
|
|
90
|
+
* invite's. Never rejects: a mailer that throws is down, and a record that
|
|
91
|
+
* fails is logged, so the answer stands whatever the store does.
|
|
92
|
+
*/
|
|
93
|
+
export async function sendAndRecord(mailer, review, mail) {
|
|
94
|
+
const answer = await mailer.send(mail).catch(() => 'down');
|
|
95
|
+
await review.recordMail(answer).catch((error) => console.error('[mail] not recorded:', error));
|
|
96
|
+
return answer;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Mails a new person their invite, with their link on `home`, the host they
|
|
100
|
+
* sign in on, and records the answer as a code's is (sendAndRecord). Waits
|
|
101
|
+
* for the send, since whoever adds is Studio (E7 is about the person signing
|
|
102
|
+
* in). Never throws: the person and their link stand whatever happens.
|
|
103
|
+
*/
|
|
104
|
+
export const sendInvite = (mailer, review, person, token, home) => sendAndRecord(mailer, review, inviteMail(mailer.name, person, `https://${home}/invite/${token}`, home));
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Mail, Mailer, Sent } from '../mailer.ts';
|
|
2
|
+
export type FakeMailer = Mailer & {
|
|
3
|
+
/** Every mail sent, in order. */
|
|
4
|
+
sent: Mail[];
|
|
5
|
+
/** What the next sends answer; `hang` never answers. */
|
|
6
|
+
answer: Sent | 'hang' | 'throw';
|
|
7
|
+
};
|
|
8
|
+
export declare function fakeMailer(name?: string): FakeMailer;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export function fakeMailer(name = 'Test Studio') {
|
|
2
|
+
const mailer = {
|
|
3
|
+
name,
|
|
4
|
+
sent: [],
|
|
5
|
+
answer: 'sent',
|
|
6
|
+
send(mail) {
|
|
7
|
+
mailer.sent.push(mail);
|
|
8
|
+
if (mailer.answer === 'hang')
|
|
9
|
+
return new Promise(() => { });
|
|
10
|
+
if (mailer.answer === 'throw')
|
|
11
|
+
return Promise.reject(new Error('fake mailer failed'));
|
|
12
|
+
return Promise.resolve(mailer.answer);
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
return mailer;
|
|
16
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Sent } from '../../core/review/service.ts';
|
|
2
|
+
type Mail = {
|
|
3
|
+
to: string;
|
|
4
|
+
subject: string;
|
|
5
|
+
text: string;
|
|
6
|
+
html: string;
|
|
7
|
+
key: string;
|
|
8
|
+
};
|
|
9
|
+
export type ResendOptions = {
|
|
10
|
+
/** The send-only key (RESEND_API_KEY). */
|
|
11
|
+
key: string;
|
|
12
|
+
/** The sender, `Name <address>` (MAIL_FROM). */
|
|
13
|
+
from: string;
|
|
14
|
+
fetcher?: typeof fetch;
|
|
15
|
+
/** How long a send may take before it is down; tests shorten it. */
|
|
16
|
+
timeoutMs?: number;
|
|
17
|
+
};
|
|
18
|
+
export declare function resendMailer({ key, from, fetcher, timeoutMs }: ResendOptions): {
|
|
19
|
+
send(mail: Mail): Promise<Sent>;
|
|
20
|
+
};
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const ENDPOINT = 'https://api.resend.com/emails';
|
|
2
|
+
const TIMEOUT_MS = 5_000;
|
|
3
|
+
/** A 200 whose body names the mail Resend made. */
|
|
4
|
+
async function made(response) {
|
|
5
|
+
const body = (await response.json().catch(() => null));
|
|
6
|
+
return typeof body?.id === 'string' && body.id !== '';
|
|
7
|
+
}
|
|
8
|
+
export function resendMailer({ key, from, fetcher = fetch, timeoutMs = TIMEOUT_MS }) {
|
|
9
|
+
async function post(mail, signal) {
|
|
10
|
+
const response = await fetcher(ENDPOINT, {
|
|
11
|
+
method: 'POST',
|
|
12
|
+
headers: {
|
|
13
|
+
authorization: `Bearer ${key}`,
|
|
14
|
+
'content-type': 'application/json',
|
|
15
|
+
// Held 24 hours by Resend: the same mail sent twice is one mail.
|
|
16
|
+
'idempotency-key': mail.key,
|
|
17
|
+
},
|
|
18
|
+
body: JSON.stringify({ from, to: [mail.to], subject: mail.subject, text: mail.text, html: mail.html }),
|
|
19
|
+
signal,
|
|
20
|
+
});
|
|
21
|
+
if (response.status === 200 && (await made(response)))
|
|
22
|
+
return 'sent';
|
|
23
|
+
console.error('[mail] Resend refused a send:', response.status);
|
|
24
|
+
return 'refused';
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
async send(mail) {
|
|
28
|
+
const abort = new AbortController();
|
|
29
|
+
let timer;
|
|
30
|
+
// A fetch that ignores its signal still ends here: the timer answers down.
|
|
31
|
+
const late = new Promise((resolve) => {
|
|
32
|
+
timer = setTimeout(() => {
|
|
33
|
+
abort.abort();
|
|
34
|
+
resolve('down');
|
|
35
|
+
}, timeoutMs);
|
|
36
|
+
});
|
|
37
|
+
try {
|
|
38
|
+
return await Promise.race([post(mail, abort.signal), late]);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
console.error('[mail] Resend did not answer.');
|
|
42
|
+
return 'down';
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
clearTimeout(timer);
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
package/dist/server/session.js
CHANGED
|
@@ -109,6 +109,9 @@ export const clearedCookie = (name) => `${name}=; Path=/; Max-Age=0; HttpOnly; S
|
|
|
109
109
|
// A request with neither header is not from a browser that could be tricked
|
|
110
110
|
// into it (every current browser sends Origin on a POST), so it passes: it
|
|
111
111
|
// holds whatever it sends, and a forged request needs a victim's browser.
|
|
112
|
+
// http only on this machine: localhost and its subdomains (acme.localhost),
|
|
113
|
+
// which every current browser resolves to loopback.
|
|
114
|
+
const isLocalhost = (hostname) => hostname === 'localhost' || hostname.endsWith('.localhost');
|
|
112
115
|
export function sameOrigin(request) {
|
|
113
116
|
const site = request.headers.get('sec-fetch-site');
|
|
114
117
|
if (site && site !== 'same-origin')
|
|
@@ -121,7 +124,7 @@ export function sameOrigin(request) {
|
|
|
121
124
|
const host = request.headers.get('x-forwarded-host') ?? request.headers.get('host') ?? new URL(request.url).host;
|
|
122
125
|
try {
|
|
123
126
|
const o = new URL(origin);
|
|
124
|
-
return (o.protocol === 'https:' || o.hostname
|
|
127
|
+
return (o.protocol === 'https:' || isLocalhost(o.hostname)) && o.host === host;
|
|
125
128
|
}
|
|
126
129
|
catch {
|
|
127
130
|
return false;
|
package/dist/vercel/proxy.js
CHANGED
|
@@ -2,14 +2,21 @@
|
|
|
2
2
|
// as proxy.entrypoint): the gate (server/gate.ts), with its decisions carried
|
|
3
3
|
// out the way Vercel's middleware does, `next()` to pass on and `rewrite()`
|
|
4
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
|
|
6
|
-
|
|
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';
|
|
7
10
|
import { createGate, GATE_FONT, gatePreview } from '../server/gate.js';
|
|
8
11
|
export { GATE_FONT, gatePreview };
|
|
9
12
|
/** A gate's decision as Vercel's middleware answers it. */
|
|
10
13
|
export function onVercel(decision) {
|
|
11
14
|
if (decision instanceof Response)
|
|
12
15
|
return decision;
|
|
16
|
+
if ('answer' in decision) {
|
|
17
|
+
waitUntil(decision.after);
|
|
18
|
+
return decision.answer;
|
|
19
|
+
}
|
|
13
20
|
if (decision.pass === 'rewrite')
|
|
14
21
|
return rewrite(decision.to, { headers: decision.headers });
|
|
15
22
|
return decision.headers ? next({ headers: decision.headers }) : next();
|
|
@@ -18,6 +25,6 @@ export function onVercel(decision) {
|
|
|
18
25
|
export function createStudioProxy(options = {}) {
|
|
19
26
|
const gate = createGate(options);
|
|
20
27
|
return async function proxy(request) {
|
|
21
|
-
return onVercel(await gate(request));
|
|
28
|
+
return onVercel(await gate(request, { address: ipAddress(request) ?? null }));
|
|
22
29
|
};
|
|
23
30
|
}
|
package/dist/vite/plugin.js
CHANGED
|
@@ -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']> }) {
|
package/kit/ui/layer.ts
CHANGED
|
@@ -49,3 +49,44 @@ export function installPressFeedback() {
|
|
|
49
49
|
document.addEventListener('pointercancel', lift);
|
|
50
50
|
document.addEventListener('pointerleave', lift, true);
|
|
51
51
|
}
|
|
52
|
+
|
|
53
|
+
type Box = { top: number; bottom: number; left: number; right: number };
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Where a menu goes for its trigger. Below when it fits, above when it does
|
|
57
|
+
* not, on the trigger's end or start edge and 8px inside the viewport. With
|
|
58
|
+
* `within` (the card or panel the trigger sits in), the side that stays
|
|
59
|
+
* inside it wins, and when neither does, the side that leaves it least. The
|
|
60
|
+
* origin is the trigger's own edge, in the menu's pixels, so the menu grows
|
|
61
|
+
* out of its button even when the viewport pushed it sideways.
|
|
62
|
+
*/
|
|
63
|
+
export function placeMenu(
|
|
64
|
+
trigger: Box,
|
|
65
|
+
menu: { width: number; height: number },
|
|
66
|
+
viewport: { width: number; height: number },
|
|
67
|
+
align: 'start' | 'end',
|
|
68
|
+
within?: Box | null,
|
|
69
|
+
) {
|
|
70
|
+
const GAP = 4;
|
|
71
|
+
const EDGE = 8;
|
|
72
|
+
const below = trigger.bottom + GAP + menu.height;
|
|
73
|
+
const above = trigger.top - GAP - menu.height;
|
|
74
|
+
const fitsBelow = below <= viewport.height - EDGE;
|
|
75
|
+
const fitsAbove = above >= EDGE;
|
|
76
|
+
let down = fitsBelow || !fitsAbove;
|
|
77
|
+
if (within && fitsBelow && fitsAbove) {
|
|
78
|
+
const overBelow = Math.max(0, below - within.bottom);
|
|
79
|
+
const overAbove = Math.max(0, within.top - above);
|
|
80
|
+
down = overBelow <= overAbove;
|
|
81
|
+
}
|
|
82
|
+
const anchor = align === 'end' ? trigger.right : trigger.left;
|
|
83
|
+
const wanted = align === 'end' ? trigger.right - menu.width : trigger.left;
|
|
84
|
+
const left = Math.max(EDGE, Math.min(wanted, viewport.width - menu.width - EDGE));
|
|
85
|
+
const x = Math.max(0, Math.min(anchor - left, menu.width));
|
|
86
|
+
return {
|
|
87
|
+
side: down ? ('below' as const) : ('above' as const),
|
|
88
|
+
top: down ? trigger.bottom + GAP : above,
|
|
89
|
+
left,
|
|
90
|
+
origin: `${x}px ${down ? 'top' : 'bottom'}`,
|
|
91
|
+
};
|
|
92
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milesroxas/design-hub",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "The Design Hub product: the hub that lists and serves prototypes, the review panel and its API, the sign-in gate, the kit, and the design-hub CLI. A project installs it and keeps only its prototypes, kit materials and docs.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
package/shell/review/Bar.tsx
CHANGED
package/shell/review/Card.tsx
CHANGED
|
@@ -3,14 +3,20 @@
|
|
|
3
3
|
// follows the page as it scrolls.
|
|
4
4
|
import { Button } from '@kit/ui';
|
|
5
5
|
import { type CSSProperties, type ReactNode, useEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
6
|
-
import { besideButton, besidePoint, type Placement } from './place';
|
|
6
|
+
import { besideButton, besidePoint, besideTrigger, type Placement } from './place';
|
|
7
7
|
import { useKeyboardInset, useNarrow, useRevealPin, useSheetDrag } from './sheet';
|
|
8
8
|
|
|
9
9
|
const toStyle = (p: Placement): CSSProperties => ({ left: p.left, top: p.top, transformOrigin: p.origin });
|
|
10
10
|
|
|
11
|
-
function place(at: { x: number; y: number } | null, h: number): CSSProperties {
|
|
11
|
+
function place(at: { x: number; y: number } | null, h: number, by?: string): CSSProperties {
|
|
12
12
|
const vp = { width: innerWidth, height: innerHeight };
|
|
13
13
|
if (at) return toStyle(besidePoint(at, h, vp));
|
|
14
|
+
const control = by ? document.querySelector(by) : null;
|
|
15
|
+
if (control) {
|
|
16
|
+
const c = control.getBoundingClientRect();
|
|
17
|
+
const bar = (control.closest('.rv-bar') ?? control).getBoundingClientRect();
|
|
18
|
+
return toStyle(besideTrigger({ left: c.left, right: c.right, top: bar.top, bottom: bar.bottom }, h, vp));
|
|
19
|
+
}
|
|
14
20
|
const b = document.querySelector('.rv-launch')?.getBoundingClientRect();
|
|
15
21
|
return b ? toStyle(besideButton(b, h, vp)) : { right: 16, bottom: 72 };
|
|
16
22
|
}
|
|
@@ -39,12 +45,15 @@ function useFollow() {
|
|
|
39
45
|
|
|
40
46
|
export function Card({
|
|
41
47
|
at,
|
|
48
|
+
by,
|
|
42
49
|
label,
|
|
43
50
|
resolved,
|
|
44
51
|
onDismiss,
|
|
45
52
|
children,
|
|
46
53
|
}: {
|
|
47
54
|
at: { x: number; y: number } | null;
|
|
55
|
+
/** A selector for the control that opened a card with no pin: the card sits by it, and by the Comments button when it is gone. */
|
|
56
|
+
by?: string;
|
|
48
57
|
label: string;
|
|
49
58
|
resolved?: boolean;
|
|
50
59
|
/** Dragging the sheet down on a phone calls this. */
|
|
@@ -65,17 +74,18 @@ export function Card({
|
|
|
65
74
|
ro.observe(el);
|
|
66
75
|
return () => ro.disconnect();
|
|
67
76
|
}, []);
|
|
68
|
-
// A card that closes with focus inside hands it to the button, never to <body>.
|
|
77
|
+
// A card that closes with focus inside hands it to the control that opened it, or the button, never to <body>.
|
|
69
78
|
useEffect(() => {
|
|
70
79
|
const el = ref.current;
|
|
71
80
|
return () => {
|
|
72
81
|
if (!el?.contains(document.activeElement)) return;
|
|
73
82
|
requestAnimationFrame(() => {
|
|
74
|
-
if (document.activeElement
|
|
75
|
-
|
|
83
|
+
if (document.activeElement !== document.body) return;
|
|
84
|
+
const opener = by ? document.querySelector<HTMLElement>(by) : null;
|
|
85
|
+
(opener ?? document.querySelector<HTMLElement>('.rv-launch'))?.focus();
|
|
76
86
|
});
|
|
77
87
|
};
|
|
78
|
-
}, []);
|
|
88
|
+
}, [by]);
|
|
79
89
|
return (
|
|
80
90
|
<section
|
|
81
91
|
ref={ref}
|
|
@@ -83,7 +93,7 @@ export function Card({
|
|
|
83
93
|
aria-label={label}
|
|
84
94
|
data-resolved={resolved ? '' : undefined}
|
|
85
95
|
data-sheet={narrow ? '' : undefined}
|
|
86
|
-
style={narrow ? undefined : place(at, h)}
|
|
96
|
+
style={narrow ? undefined : place(at, h, by)}
|
|
87
97
|
{...drag}
|
|
88
98
|
>
|
|
89
99
|
{narrow && <span className="rv-grab" aria-hidden="true" />}
|