@milesroxas/design-hub 0.5.0 → 0.6.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/kit/prefs.ts +32 -6
- package/kit/ui/Menu.tsx +28 -4
- package/kit/ui/index.ts +9 -1
- package/package.json +2 -2
- package/shell/review/Bar.tsx +156 -0
- package/shell/review/Card.tsx +106 -0
- package/shell/review/Field.tsx +99 -0
- package/shell/review/Launcher.tsx +180 -0
- package/shell/review/People.tsx +168 -0
- package/shell/review/PickLayer.tsx +130 -0
- package/shell/review/Pins.tsx +97 -0
- package/shell/review/Review.tsx +282 -902
- package/shell/review/ThreadCard.tsx +498 -0
- package/shell/review/anchor.ts +2 -1
- package/shell/review/client.ts +3 -0
- package/shell/review/place.test.ts +72 -0
- package/shell/review/place.ts +78 -0
- package/shell/review/review.css +504 -205
- package/shell/review/sheet.ts +107 -0
- package/shell/review/snap.test.ts +44 -0
- package/shell/review/snap.ts +59 -0
- package/shell/review/view.test.ts +155 -0
- package/shell/review/view.ts +101 -0
package/kit/prefs.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { useSyncExternalStore } from 'react';
|
|
|
2
2
|
|
|
3
3
|
// Theme, reduced motion and speed for the page, mirrored onto <html>
|
|
4
4
|
// (.dark-mode, [data-motion="reduce"]) so CSS and JS read one source.
|
|
5
|
+
// Also whether the comments button and pins are hidden: a choice about the
|
|
6
|
+
// page a person is looking at, kept the same way as their theme.
|
|
5
7
|
|
|
6
8
|
export type Appearance = 'system' | 'light' | 'dark';
|
|
7
9
|
|
|
@@ -15,7 +17,10 @@ const params = new URLSearchParams(location.search);
|
|
|
15
17
|
const urlReduce = params.get('motion') === 'reduce';
|
|
16
18
|
const listeners = new Set<() => void>();
|
|
17
19
|
|
|
20
|
+
const COMMENTS_KEY = 'proto:comments';
|
|
21
|
+
|
|
18
22
|
let appearance: Appearance = readAppearance();
|
|
23
|
+
let commentsHidden = readStored(COMMENTS_KEY) === 'hidden';
|
|
19
24
|
let forceReduce = false;
|
|
20
25
|
let speed = 1;
|
|
21
26
|
let lapse: LapseHandle | null = null;
|
|
@@ -23,11 +28,24 @@ let lapse: LapseHandle | null = null;
|
|
|
23
28
|
function readAppearance(): Appearance {
|
|
24
29
|
const fromUrl = params.get('theme');
|
|
25
30
|
if (fromUrl === 'light' || fromUrl === 'dark' || fromUrl === 'system') return fromUrl;
|
|
31
|
+
const saved = readStored('proto:appearance');
|
|
32
|
+
if (saved === 'light' || saved === 'dark' || saved === 'system') return saved;
|
|
33
|
+
return 'light';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Storage throws in a private window or with site data blocked: the page then runs on its defaults.
|
|
37
|
+
function readStored(key: string): string | null {
|
|
38
|
+
try {
|
|
39
|
+
return localStorage.getItem(key);
|
|
40
|
+
} catch {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function store(key: string, value: string) {
|
|
26
46
|
try {
|
|
27
|
-
|
|
28
|
-
if (saved === 'light' || saved === 'dark' || saved === 'system') return saved;
|
|
47
|
+
localStorage.setItem(key, value);
|
|
29
48
|
} catch {}
|
|
30
|
-
return 'light';
|
|
31
49
|
}
|
|
32
50
|
|
|
33
51
|
function emit() {
|
|
@@ -58,9 +76,7 @@ function applyMotion() {
|
|
|
58
76
|
export function setAppearance(next: Appearance) {
|
|
59
77
|
if (next === appearance) return;
|
|
60
78
|
appearance = next;
|
|
61
|
-
|
|
62
|
-
localStorage.setItem('proto:appearance', next);
|
|
63
|
-
} catch {}
|
|
79
|
+
store('proto:appearance', next);
|
|
64
80
|
applyTheme();
|
|
65
81
|
emit();
|
|
66
82
|
}
|
|
@@ -71,6 +87,16 @@ export function setForceReduce(on: boolean) {
|
|
|
71
87
|
emit();
|
|
72
88
|
}
|
|
73
89
|
|
|
90
|
+
/** Whether the comments button and pins are off the page. Remembered per browser. */
|
|
91
|
+
export const areCommentsHidden = () => commentsHidden;
|
|
92
|
+
|
|
93
|
+
export function setCommentsHidden(hidden: boolean) {
|
|
94
|
+
if (hidden === commentsHidden) return;
|
|
95
|
+
commentsHidden = hidden;
|
|
96
|
+
store(COMMENTS_KEY, hidden ? 'hidden' : 'shown');
|
|
97
|
+
emit();
|
|
98
|
+
}
|
|
99
|
+
|
|
74
100
|
export const isDark = () => root.classList.contains('dark-mode');
|
|
75
101
|
export const prefersReducedMotion = () => root.dataset.motion === 'reduce';
|
|
76
102
|
|
package/kit/ui/Menu.tsx
CHANGED
|
@@ -21,10 +21,15 @@ export type MenuItem = {
|
|
|
21
21
|
keys?: (string | { icon: IconName; label: string })[];
|
|
22
22
|
danger?: boolean;
|
|
23
23
|
disabled?: boolean;
|
|
24
|
+
/** One of a set of choices: drawn with a check when true. Several groups can share a menu, unlike `selected`. */
|
|
25
|
+
checked?: boolean;
|
|
24
26
|
onClick?: () => void;
|
|
25
27
|
};
|
|
26
28
|
|
|
27
|
-
|
|
29
|
+
/** A label over the items that follow it, for a menu that holds more than one group. */
|
|
30
|
+
export type MenuHeading = { heading: string };
|
|
31
|
+
|
|
32
|
+
export type MenuEntry = MenuItem | MenuHeading | '-';
|
|
28
33
|
|
|
29
34
|
export type MenuConfig = {
|
|
30
35
|
items: MenuEntry[];
|
|
@@ -35,6 +40,8 @@ export type MenuConfig = {
|
|
|
35
40
|
selected?: string;
|
|
36
41
|
checkable?: boolean;
|
|
37
42
|
role?: 'menu' | 'listbox';
|
|
43
|
+
/** Added to the popup, which portals to <body>: the way a layer with its own material and stacking dresses its menus. */
|
|
44
|
+
className?: string;
|
|
38
45
|
onSelect?: (value: string | undefined, item: MenuItem) => void;
|
|
39
46
|
};
|
|
40
47
|
|
|
@@ -202,7 +209,12 @@ function MenuPopup({
|
|
|
202
209
|
onSelect?.(item.value, item);
|
|
203
210
|
};
|
|
204
211
|
|
|
205
|
-
const cls = [
|
|
212
|
+
const cls = [
|
|
213
|
+
'menu',
|
|
214
|
+
config.className,
|
|
215
|
+
shown && phase === 'open' ? 'is-open' : '',
|
|
216
|
+
phase === 'closing' ? 'is-closing' : '',
|
|
217
|
+
]
|
|
206
218
|
.filter(Boolean)
|
|
207
219
|
.join(' ');
|
|
208
220
|
|
|
@@ -226,12 +238,18 @@ function MenuPopup({
|
|
|
226
238
|
// biome-ignore lint/a11y/useSemanticElements: an <hr> brings borders the menu rule does not want
|
|
227
239
|
// biome-ignore lint/suspicious/noArrayIndexKey: separators have no identity; the list is fixed while open
|
|
228
240
|
if (item === '-') return <div key={`sep-${n}`} className="menu-sep" role="separator" />;
|
|
241
|
+
if ('heading' in item)
|
|
242
|
+
return (
|
|
243
|
+
<div key={`head-${item.heading}`} className="menu-header" role="presentation">
|
|
244
|
+
{item.heading}
|
|
245
|
+
</div>
|
|
246
|
+
);
|
|
229
247
|
return (
|
|
230
248
|
<MenuRow
|
|
231
249
|
key={`${item.label}-${item.value ?? n}`}
|
|
232
250
|
item={item}
|
|
233
|
-
role={role
|
|
234
|
-
selected={selected != null && selected === item.value}
|
|
251
|
+
role={rowRole(role, checkable || item.checked !== undefined)}
|
|
252
|
+
selected={item.checked ?? (selected != null && selected === item.value)}
|
|
235
253
|
onPick={pick}
|
|
236
254
|
/>
|
|
237
255
|
);
|
|
@@ -278,6 +296,9 @@ function MenuKeys({ keys }: { keys: NonNullable<MenuItem['keys']> }) {
|
|
|
278
296
|
);
|
|
279
297
|
}
|
|
280
298
|
|
|
299
|
+
const rowRole = (role: 'menu' | 'listbox', checkable: boolean | undefined) =>
|
|
300
|
+
role === 'listbox' ? 'option' : checkable ? 'menuitemradio' : 'menuitem';
|
|
301
|
+
|
|
281
302
|
const rowClass = (item: MenuItem, selected: boolean) =>
|
|
282
303
|
['menu-item', item.danger && 'is-danger', item.disabled && 'is-disabled', selected && 'is-selected']
|
|
283
304
|
.filter(Boolean)
|
|
@@ -334,6 +355,7 @@ export function Select({
|
|
|
334
355
|
onChange,
|
|
335
356
|
width,
|
|
336
357
|
className,
|
|
358
|
+
menuClassName,
|
|
337
359
|
placeholder = 'Select',
|
|
338
360
|
labelId,
|
|
339
361
|
'aria-label': ariaLabel,
|
|
@@ -343,6 +365,7 @@ export function Select({
|
|
|
343
365
|
onChange: (value: string) => void;
|
|
344
366
|
width?: number;
|
|
345
367
|
className?: string;
|
|
368
|
+
menuClassName?: string;
|
|
346
369
|
placeholder?: string;
|
|
347
370
|
labelId?: string;
|
|
348
371
|
'aria-label'?: string;
|
|
@@ -355,6 +378,7 @@ export function Select({
|
|
|
355
378
|
items: options.map((o) => ({ label: o.label, value: o.value })),
|
|
356
379
|
selected: value ?? undefined,
|
|
357
380
|
label: ariaLabel,
|
|
381
|
+
className: menuClassName,
|
|
358
382
|
width: (trigger) => Math.max(trigger.offsetWidth, width || 0),
|
|
359
383
|
onSelect: (v) => {
|
|
360
384
|
if (v != null && v !== value) onChange(v);
|
package/kit/ui/index.ts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
export { Badge, Button, Checkbox, Chevron, Segmented } from './Button';
|
|
2
2
|
export { installPressFeedback } from './layer';
|
|
3
|
-
export {
|
|
3
|
+
export {
|
|
4
|
+
type MenuConfig,
|
|
5
|
+
type MenuEntry,
|
|
6
|
+
type MenuHeading,
|
|
7
|
+
type MenuItem,
|
|
8
|
+
Select,
|
|
9
|
+
type SelectOption,
|
|
10
|
+
useMenu,
|
|
11
|
+
} from './Menu';
|
|
4
12
|
export {
|
|
5
13
|
confirmDialog,
|
|
6
14
|
OverlayHost,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milesroxas/design-hub",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "The Design Hub product: the hub that lists and serves prototypes, the review panel and its API, the password 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": {
|
|
@@ -81,7 +81,7 @@
|
|
|
81
81
|
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
82
82
|
"check": "tsc --noEmit && biome check .",
|
|
83
83
|
"fix": "biome check --write .",
|
|
84
|
-
"test": "node --test \"core/**/*.test.ts\" \"server/**/*.test.ts\" \"scripts/**/*.test.ts\" \"vite/**/*.test.ts\" \"vercel/**/*.test.ts\"",
|
|
84
|
+
"test": "node --test \"core/**/*.test.ts\" \"shell/**/*.test.ts\" \"server/**/*.test.ts\" \"scripts/**/*.test.ts\" \"vite/**/*.test.ts\" \"vercel/**/*.test.ts\"",
|
|
85
85
|
"quality": "rm -rf .coverage && NODE_V8_COVERAGE=.coverage pnpm run --silent test > /dev/null && npx -y fallow@3.38.0 --coverage .coverage"
|
|
86
86
|
}
|
|
87
87
|
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// Comment mode's bar: one quiet pill in three groups. What a new comment is
|
|
2
|
+
// about (Pin or Page), the threads to step through, and the way out.
|
|
3
|
+
// Everything rarer sits behind ⋯. Only the chosen half of the toggle is
|
|
4
|
+
// filled: the page under the bar is the work.
|
|
5
|
+
import { Icon } from '@kit/Icon';
|
|
6
|
+
import { Button, type MenuEntry, useMenu } from '@kit/ui';
|
|
7
|
+
import type { Scope } from './view';
|
|
8
|
+
|
|
9
|
+
const VERB = matchMedia('(pointer: coarse)').matches ? 'Tap' : 'Click';
|
|
10
|
+
|
|
11
|
+
type Props = {
|
|
12
|
+
count: number;
|
|
13
|
+
/** Where the open thread sits in the round, from 0; -1 with none open. */
|
|
14
|
+
index: number;
|
|
15
|
+
scope: Scope;
|
|
16
|
+
onScope: (scope: Scope) => void;
|
|
17
|
+
resolved: boolean;
|
|
18
|
+
resolvedCount: number;
|
|
19
|
+
editor: boolean;
|
|
20
|
+
error: string | null;
|
|
21
|
+
onStep: (by: number) => void;
|
|
22
|
+
onResolved: () => void;
|
|
23
|
+
onPeople: () => void;
|
|
24
|
+
onHide: () => void;
|
|
25
|
+
onDone: () => void;
|
|
26
|
+
onDismissError: () => void;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function moreItems(resolved: boolean, resolvedCount: number, editor: boolean): MenuEntry[] {
|
|
30
|
+
return [
|
|
31
|
+
{
|
|
32
|
+
label: resolved ? 'Hide resolved' : 'Show resolved',
|
|
33
|
+
desc: resolvedCount ? `${resolvedCount} on this prototype` : 'Nothing resolved yet',
|
|
34
|
+
value: 'resolved',
|
|
35
|
+
icon: 'check-circle',
|
|
36
|
+
disabled: !resolvedCount && !resolved,
|
|
37
|
+
},
|
|
38
|
+
...(editor
|
|
39
|
+
? [{ label: 'People', desc: 'Who can comment', value: 'people', icon: 'users-01' as const }]
|
|
40
|
+
: []),
|
|
41
|
+
{ label: 'Hide comments', desc: 'Tuck the button into the edge', value: 'hide', icon: 'chevron-right' },
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function Alert({ error, onDismiss }: { error: string; onDismiss: () => void }) {
|
|
46
|
+
return (
|
|
47
|
+
<p className="rv-alert" role="alert">
|
|
48
|
+
<span>{error}</span>
|
|
49
|
+
<Button kind="tertiary" size={null} className="rv-act" label="Dismiss" onClick={onDismiss} />
|
|
50
|
+
</p>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const SCOPES: { value: Scope; label: string; icon: 'target-04' | 'file-06'; title: string }[] = [
|
|
55
|
+
{ value: 'pin', label: 'Pin', icon: 'target-04', title: `${VERB} anything to comment on it` },
|
|
56
|
+
{ value: 'page', label: 'Page', icon: 'file-06', title: 'Comment on the whole page' },
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
/** Pin a comment to part of the page, or leave one on the page as a whole. */
|
|
60
|
+
function Toggle({ scope, onScope }: Pick<Props, 'scope' | 'onScope'>) {
|
|
61
|
+
return (
|
|
62
|
+
// biome-ignore lint/a11y/useSemanticElements: a fieldset brings a border and a legend the bar does not want
|
|
63
|
+
<div className="rv-toggle" role="group" aria-label="Comment on">
|
|
64
|
+
{SCOPES.map((s) => (
|
|
65
|
+
<button
|
|
66
|
+
key={s.value}
|
|
67
|
+
type="button"
|
|
68
|
+
aria-pressed={scope === s.value}
|
|
69
|
+
title={s.title}
|
|
70
|
+
onClick={() => onScope(s.value)}
|
|
71
|
+
>
|
|
72
|
+
<Icon name={s.icon} size={16} />
|
|
73
|
+
<span className="rv-toggle-label">{s.label}</span>
|
|
74
|
+
</button>
|
|
75
|
+
))}
|
|
76
|
+
</div>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** ‹ 2 open › with nothing open, ‹ 1 of 2 › with one: the arrows step straight to a thread. */
|
|
81
|
+
function Stepper({ count, index, resolved, onStep }: Pick<Props, 'count' | 'index' | 'resolved' | 'onStep'>) {
|
|
82
|
+
const label = index < 0 ? `${count} ${resolved ? 'in all' : 'open'}` : `${index + 1} of ${count}`;
|
|
83
|
+
return (
|
|
84
|
+
// biome-ignore lint/a11y/useSemanticElements: a fieldset brings a border and a legend the bar does not want
|
|
85
|
+
<div className="rv-bar-group rv-stepper" role="group" aria-label="Comments on this prototype">
|
|
86
|
+
<Button
|
|
87
|
+
kind="tertiary"
|
|
88
|
+
size={null}
|
|
89
|
+
icon="chevron-left"
|
|
90
|
+
iconSize={16}
|
|
91
|
+
aria-label="Previous comment"
|
|
92
|
+
disabled={!count}
|
|
93
|
+
onClick={() => onStep(-1)}
|
|
94
|
+
/>
|
|
95
|
+
<span className="rv-stepper-count tabular" aria-live="polite">
|
|
96
|
+
{label}
|
|
97
|
+
</span>
|
|
98
|
+
<Button
|
|
99
|
+
kind="tertiary"
|
|
100
|
+
size={null}
|
|
101
|
+
icon="chevron-right"
|
|
102
|
+
iconSize={16}
|
|
103
|
+
aria-label="Next comment"
|
|
104
|
+
disabled={!count}
|
|
105
|
+
onClick={() => onStep(1)}
|
|
106
|
+
/>
|
|
107
|
+
</div>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function Bar(p: Props) {
|
|
112
|
+
const actions: Record<string, () => void> = {
|
|
113
|
+
resolved: p.onResolved,
|
|
114
|
+
people: p.onPeople,
|
|
115
|
+
hide: p.onHide,
|
|
116
|
+
};
|
|
117
|
+
const more = useMenu({
|
|
118
|
+
className: 'rv-menu',
|
|
119
|
+
label: 'Comment options',
|
|
120
|
+
width: 264,
|
|
121
|
+
align: 'end',
|
|
122
|
+
items: moreItems(p.resolved, p.resolvedCount, p.editor),
|
|
123
|
+
onSelect: (v) => actions[v ?? '']?.(),
|
|
124
|
+
});
|
|
125
|
+
return (
|
|
126
|
+
<div className="rv-bar-stack">
|
|
127
|
+
{p.error && <Alert error={p.error} onDismiss={p.onDismissError} />}
|
|
128
|
+
<section className="rv-bar" aria-label="Comment mode">
|
|
129
|
+
<Toggle scope={p.scope} onScope={p.onScope} />
|
|
130
|
+
<span className="rv-bar-rule" aria-hidden="true" />
|
|
131
|
+
<Stepper count={p.count} index={p.index} resolved={p.resolved} onStep={p.onStep} />
|
|
132
|
+
<span className="rv-bar-rule" aria-hidden="true" />
|
|
133
|
+
<div className="rv-bar-group">
|
|
134
|
+
<Button
|
|
135
|
+
kind="tertiary"
|
|
136
|
+
size={null}
|
|
137
|
+
icon="dots-horizontal"
|
|
138
|
+
iconSize={16}
|
|
139
|
+
aria-label="Comment options"
|
|
140
|
+
{...more.triggerProps}
|
|
141
|
+
/>
|
|
142
|
+
{more.menu}
|
|
143
|
+
<Button
|
|
144
|
+
kind="tertiary"
|
|
145
|
+
size={null}
|
|
146
|
+
icon="x-close"
|
|
147
|
+
iconSize={16}
|
|
148
|
+
aria-label="Stop commenting"
|
|
149
|
+
title="Stop commenting (Esc)"
|
|
150
|
+
onClick={p.onDone}
|
|
151
|
+
/>
|
|
152
|
+
</div>
|
|
153
|
+
</section>
|
|
154
|
+
</div>
|
|
155
|
+
);
|
|
156
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// The one card on screen: beside its pin when it has one, beside the
|
|
2
|
+
// Comments button when it does not. It flips and clamps to stay in view and
|
|
3
|
+
// follows the page as it scrolls.
|
|
4
|
+
import { Button } from '@kit/ui';
|
|
5
|
+
import { type CSSProperties, type ReactNode, useEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
6
|
+
import { besideButton, besidePoint, type Placement } from './place';
|
|
7
|
+
import { useKeyboardInset, useNarrow, useRevealPin, useSheetDrag } from './sheet';
|
|
8
|
+
|
|
9
|
+
const toStyle = (p: Placement): CSSProperties => ({ left: p.left, top: p.top, transformOrigin: p.origin });
|
|
10
|
+
|
|
11
|
+
function place(at: { x: number; y: number } | null, h: number): CSSProperties {
|
|
12
|
+
const vp = { width: innerWidth, height: innerHeight };
|
|
13
|
+
if (at) return toStyle(besidePoint(at, h, vp));
|
|
14
|
+
const b = document.querySelector('.rv-launch')?.getBoundingClientRect();
|
|
15
|
+
return b ? toStyle(besideButton(b, h, vp)) : { right: 16, bottom: 72 };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Re-renders once a frame on scroll and resize, so a card beside a pin moves with it. */
|
|
19
|
+
function useFollow() {
|
|
20
|
+
const [, tick] = useState(0);
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
let frame = 0;
|
|
23
|
+
const on = () => {
|
|
24
|
+
if (frame) return;
|
|
25
|
+
frame = requestAnimationFrame(() => {
|
|
26
|
+
frame = 0;
|
|
27
|
+
tick((n) => n + 1);
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
addEventListener('scroll', on, { capture: true, passive: true });
|
|
31
|
+
addEventListener('resize', on);
|
|
32
|
+
return () => {
|
|
33
|
+
cancelAnimationFrame(frame);
|
|
34
|
+
removeEventListener('scroll', on, { capture: true });
|
|
35
|
+
removeEventListener('resize', on);
|
|
36
|
+
};
|
|
37
|
+
}, []);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function Card({
|
|
41
|
+
at,
|
|
42
|
+
label,
|
|
43
|
+
resolved,
|
|
44
|
+
onDismiss,
|
|
45
|
+
children,
|
|
46
|
+
}: {
|
|
47
|
+
at: { x: number; y: number } | null;
|
|
48
|
+
label: string;
|
|
49
|
+
resolved?: boolean;
|
|
50
|
+
/** Dragging the sheet down on a phone calls this. */
|
|
51
|
+
onDismiss?: () => void;
|
|
52
|
+
children: ReactNode;
|
|
53
|
+
}) {
|
|
54
|
+
useFollow();
|
|
55
|
+
const ref = useRef<HTMLElement>(null);
|
|
56
|
+
const narrow = useNarrow();
|
|
57
|
+
const drag = useSheetDrag(ref, narrow, onDismiss);
|
|
58
|
+
useKeyboardInset(ref, narrow);
|
|
59
|
+
useRevealPin(ref, at, narrow);
|
|
60
|
+
const [h, setH] = useState(0);
|
|
61
|
+
useLayoutEffect(() => {
|
|
62
|
+
const el = ref.current;
|
|
63
|
+
if (!el) return;
|
|
64
|
+
const ro = new ResizeObserver(() => setH(el.offsetHeight));
|
|
65
|
+
ro.observe(el);
|
|
66
|
+
return () => ro.disconnect();
|
|
67
|
+
}, []);
|
|
68
|
+
// A card that closes with focus inside hands it to the button, never to <body>.
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
const el = ref.current;
|
|
71
|
+
return () => {
|
|
72
|
+
if (!el?.contains(document.activeElement)) return;
|
|
73
|
+
requestAnimationFrame(() => {
|
|
74
|
+
if (document.activeElement === document.body)
|
|
75
|
+
document.querySelector<HTMLElement>('.rv-launch')?.focus();
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
}, []);
|
|
79
|
+
return (
|
|
80
|
+
<section
|
|
81
|
+
ref={ref}
|
|
82
|
+
className="rv-card"
|
|
83
|
+
aria-label={label}
|
|
84
|
+
data-resolved={resolved ? '' : undefined}
|
|
85
|
+
data-sheet={narrow ? '' : undefined}
|
|
86
|
+
style={narrow ? undefined : place(at, h)}
|
|
87
|
+
{...drag}
|
|
88
|
+
>
|
|
89
|
+
{narrow && <span className="rv-grab" aria-hidden="true" />}
|
|
90
|
+
{children}
|
|
91
|
+
</section>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** A quiet text action in a row of them: Edit, Keep, Delete. */
|
|
96
|
+
export function Action({ label, danger, onClick }: { label: string; danger?: boolean; onClick: () => void }) {
|
|
97
|
+
return (
|
|
98
|
+
<Button
|
|
99
|
+
kind="tertiary"
|
|
100
|
+
size={null}
|
|
101
|
+
className={danger ? 'rv-act rv-act-danger' : 'rv-act'}
|
|
102
|
+
label={label}
|
|
103
|
+
onClick={onClick}
|
|
104
|
+
/>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// The one text box: a new comment, a reply, an edit. It grows with its
|
|
2
|
+
// words; Enter sends, Shift Enter starts a new line, Esc in an empty box backs out.
|
|
3
|
+
import { Icon } from '@kit/Icon';
|
|
4
|
+
import { type KeyboardEvent, useEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
5
|
+
import { LIMITS } from '../../core/review/model';
|
|
6
|
+
|
|
7
|
+
const MAX_HEIGHT = 160;
|
|
8
|
+
|
|
9
|
+
/** A text box's words, its busy flag, and a textarea that grows to fit them up to `max` px. */
|
|
10
|
+
export function useTextBox(initial: string, max: number) {
|
|
11
|
+
const [body, setBody] = useState(initial);
|
|
12
|
+
const [busy, setBusy] = useState(false);
|
|
13
|
+
const area = useRef<HTMLTextAreaElement>(null);
|
|
14
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: grows to fit whenever the words change
|
|
15
|
+
useLayoutEffect(() => {
|
|
16
|
+
const a = area.current;
|
|
17
|
+
if (!a) return;
|
|
18
|
+
a.style.height = 'auto';
|
|
19
|
+
a.style.height = `${Math.min(a.scrollHeight, max)}px`;
|
|
20
|
+
}, [body, max]);
|
|
21
|
+
return { body, setBody, busy, setBusy, area };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function Field({
|
|
25
|
+
label,
|
|
26
|
+
placeholder,
|
|
27
|
+
send,
|
|
28
|
+
initial = '',
|
|
29
|
+
autoFocus,
|
|
30
|
+
onSend,
|
|
31
|
+
onCancel,
|
|
32
|
+
}: {
|
|
33
|
+
label: string;
|
|
34
|
+
placeholder: string;
|
|
35
|
+
send: string;
|
|
36
|
+
initial?: string;
|
|
37
|
+
autoFocus?: boolean;
|
|
38
|
+
onSend: (body: string) => Promise<unknown>;
|
|
39
|
+
onCancel?: () => void;
|
|
40
|
+
}) {
|
|
41
|
+
const { body, setBody, busy, setBusy, area } = useTextBox(initial, MAX_HEIGHT);
|
|
42
|
+
// Never on a touch screen: focusing there throws the keyboard over the page.
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
if (autoFocus && matchMedia('(pointer: fine)').matches) area.current?.focus();
|
|
45
|
+
}, [autoFocus, area]);
|
|
46
|
+
|
|
47
|
+
const submit = async () => {
|
|
48
|
+
if (!body.trim() || busy) return;
|
|
49
|
+
setBusy(true);
|
|
50
|
+
try {
|
|
51
|
+
await onSend(body.trim());
|
|
52
|
+
setBody('');
|
|
53
|
+
} catch {
|
|
54
|
+
// The store shows the error; the words stay so nothing typed is lost.
|
|
55
|
+
} finally {
|
|
56
|
+
setBusy(false);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
const onKey = (e: KeyboardEvent<HTMLTextAreaElement>) => {
|
|
60
|
+
if (e.key === 'Enter' && !e.shiftKey) {
|
|
61
|
+
e.preventDefault();
|
|
62
|
+
void submit();
|
|
63
|
+
} else if (e.key === 'Escape' && onCancel && !body) {
|
|
64
|
+
e.preventDefault();
|
|
65
|
+
onCancel();
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
return (
|
|
69
|
+
<form
|
|
70
|
+
className="rv-field"
|
|
71
|
+
aria-busy={busy}
|
|
72
|
+
onSubmit={(e) => {
|
|
73
|
+
e.preventDefault();
|
|
74
|
+
void submit();
|
|
75
|
+
}}
|
|
76
|
+
>
|
|
77
|
+
<textarea
|
|
78
|
+
ref={area}
|
|
79
|
+
rows={1}
|
|
80
|
+
value={body}
|
|
81
|
+
maxLength={LIMITS.body}
|
|
82
|
+
aria-label={label}
|
|
83
|
+
placeholder={placeholder}
|
|
84
|
+
enterKeyHint="send"
|
|
85
|
+
onChange={(e) => setBody(e.target.value)}
|
|
86
|
+
onKeyDown={onKey}
|
|
87
|
+
/>
|
|
88
|
+
<button
|
|
89
|
+
type="submit"
|
|
90
|
+
className="rv-send"
|
|
91
|
+
aria-label={send}
|
|
92
|
+
title={`${send} (Enter). Shift Enter for a new line.`}
|
|
93
|
+
disabled={!body.trim() || busy}
|
|
94
|
+
>
|
|
95
|
+
<Icon name="arrow-right" size={16} />
|
|
96
|
+
</button>
|
|
97
|
+
</form>
|
|
98
|
+
);
|
|
99
|
+
}
|