@milesroxas/design-hub 0.4.3 → 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/dist/scripts/cli.js +2 -0
- package/dist/scripts/commands.js +2 -0
- package/dist/scripts/hook.d.ts +1 -1
- package/dist/scripts/hook.js +4 -1
- package/dist/scripts/lib/update.d.ts +71 -0
- package/dist/scripts/lib/update.js +83 -0
- package/dist/scripts/skills.d.ts +7 -2
- package/dist/scripts/skills.js +19 -3
- package/dist/scripts/upgrade.d.ts +25 -0
- package/dist/scripts/upgrade.js +166 -0
- 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/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
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// The way into the review layer: one round button that turns comment mode on
|
|
2
|
+
// and off. Drag it anywhere and it glides to the nearest of six spots on the
|
|
3
|
+
// screen's edge (snap.ts); Alt and an arrow move it by keyboard. Hidden, it
|
|
4
|
+
// tucks into its edge and leaves a tab that brings it back.
|
|
5
|
+
import { Icon } from '@kit/Icon';
|
|
6
|
+
import { type KeyboardEvent, type PointerEvent, useEffect, useLayoutEffect, useRef } from 'react';
|
|
7
|
+
import { nearestSpot, type Spot, stepSpot } from './snap';
|
|
8
|
+
|
|
9
|
+
const SIZE = 40;
|
|
10
|
+
const GAP = 16;
|
|
11
|
+
/** Pointer travel before a press becomes a drag, so a slightly moving click still clicks. */
|
|
12
|
+
const SLOP = 6;
|
|
13
|
+
/** A drag shorter than this that lands back on its own spot was a click with a wobble: it still toggles. */
|
|
14
|
+
const WOBBLE = 24;
|
|
15
|
+
|
|
16
|
+
type Sample = { x: number; y: number; t: number };
|
|
17
|
+
|
|
18
|
+
type Props = {
|
|
19
|
+
hidden: boolean;
|
|
20
|
+
commenting: boolean;
|
|
21
|
+
openCount: number;
|
|
22
|
+
spot: Spot;
|
|
23
|
+
onSpot: (spot: Spot) => void;
|
|
24
|
+
onToggle: () => void;
|
|
25
|
+
onShow: () => void;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** Plays the move from where the button was to where it now rests: the new spot is already laid out, the transform runs it back. */
|
|
29
|
+
function glide(el: HTMLElement, from: DOMRect) {
|
|
30
|
+
el.style.transition = 'none';
|
|
31
|
+
el.style.transform = '';
|
|
32
|
+
const to = el.getBoundingClientRect();
|
|
33
|
+
el.style.transform = `translate(${from.left - to.left}px, ${from.top - to.top}px)`;
|
|
34
|
+
el.getBoundingClientRect();
|
|
35
|
+
el.style.transition = '';
|
|
36
|
+
el.style.transform = '';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function velocity(samples: Sample[]) {
|
|
40
|
+
const a = samples[0];
|
|
41
|
+
const b = samples.at(-1) ?? a;
|
|
42
|
+
const dt = Math.max(1, b.t - a.t);
|
|
43
|
+
return { x: (b.x - a.x) / dt, y: (b.y - a.y) / dt };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function useDrag(spot: Spot, onSpot: (s: Spot) => void) {
|
|
47
|
+
const ref = useRef<HTMLButtonElement>(null);
|
|
48
|
+
const press = useRef<{ id: number; x: number; y: number; moved: boolean; samples: Sample[] } | null>(null);
|
|
49
|
+
const dragged = useRef(false);
|
|
50
|
+
const from = useRef<DOMRect | null>(null);
|
|
51
|
+
|
|
52
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: runs when the spot changes, to play the move into it
|
|
53
|
+
useLayoutEffect(() => {
|
|
54
|
+
if (ref.current && from.current) glide(ref.current, from.current);
|
|
55
|
+
from.current = null;
|
|
56
|
+
}, [spot]);
|
|
57
|
+
|
|
58
|
+
const settle = (next: Spot) => {
|
|
59
|
+
const el = ref.current;
|
|
60
|
+
if (!el) return;
|
|
61
|
+
if (next === spot) glide(el, el.getBoundingClientRect());
|
|
62
|
+
else {
|
|
63
|
+
from.current = el.getBoundingClientRect();
|
|
64
|
+
onSpot(next);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const handlers = {
|
|
69
|
+
onPointerDown(e: PointerEvent<HTMLButtonElement>) {
|
|
70
|
+
if (e.button !== 0 || !e.isPrimary) return;
|
|
71
|
+
dragged.current = false;
|
|
72
|
+
press.current = { id: e.pointerId, x: e.clientX, y: e.clientY, moved: false, samples: [] };
|
|
73
|
+
// Captured from the press, so a quick first move that leaves the 40px button is still this drag.
|
|
74
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
75
|
+
},
|
|
76
|
+
onPointerMove(e: PointerEvent<HTMLButtonElement>) {
|
|
77
|
+
const p = press.current;
|
|
78
|
+
const el = ref.current;
|
|
79
|
+
if (!p || !el || e.pointerId !== p.id) return;
|
|
80
|
+
const dx = e.clientX - p.x;
|
|
81
|
+
const dy = e.clientY - p.y;
|
|
82
|
+
if (!p.moved) {
|
|
83
|
+
if (Math.hypot(dx, dy) < SLOP) return;
|
|
84
|
+
p.moved = true;
|
|
85
|
+
el.dataset.dragging = '';
|
|
86
|
+
}
|
|
87
|
+
el.style.transform = `translate(${dx}px, ${dy}px)`;
|
|
88
|
+
p.samples = [...p.samples, { x: e.clientX, y: e.clientY, t: e.timeStamp }].filter(
|
|
89
|
+
(s) => e.timeStamp - s.t < 100,
|
|
90
|
+
);
|
|
91
|
+
},
|
|
92
|
+
onPointerUp(e: PointerEvent<HTMLButtonElement>) {
|
|
93
|
+
const p = press.current;
|
|
94
|
+
const el = ref.current;
|
|
95
|
+
press.current = null;
|
|
96
|
+
if (!p?.moved || !el) return;
|
|
97
|
+
delete el.dataset.dragging;
|
|
98
|
+
const r = el.getBoundingClientRect();
|
|
99
|
+
const centre = { x: r.left + r.width / 2, y: r.top + r.height / 2 };
|
|
100
|
+
const next = nearestSpot(
|
|
101
|
+
centre,
|
|
102
|
+
velocity(p.samples),
|
|
103
|
+
{ width: innerWidth, height: innerHeight },
|
|
104
|
+
SIZE,
|
|
105
|
+
GAP,
|
|
106
|
+
);
|
|
107
|
+
dragged.current = next !== spot || Math.hypot(e.clientX - p.x, e.clientY - p.y) >= WOBBLE;
|
|
108
|
+
settle(next);
|
|
109
|
+
},
|
|
110
|
+
onPointerCancel() {
|
|
111
|
+
press.current = null;
|
|
112
|
+
if (!ref.current) return;
|
|
113
|
+
delete ref.current.dataset.dragging;
|
|
114
|
+
glide(ref.current, ref.current.getBoundingClientRect());
|
|
115
|
+
},
|
|
116
|
+
onKeyDown(e: KeyboardEvent<HTMLButtonElement>) {
|
|
117
|
+
if (!e.altKey || !e.key.startsWith('Arrow')) return;
|
|
118
|
+
e.preventDefault();
|
|
119
|
+
settle(stepSpot(spot, e.key));
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
// A drag ends in a click on the same button; it moved the button, so it must not also toggle comment mode.
|
|
123
|
+
const swallowClick = () => {
|
|
124
|
+
const was = dragged.current;
|
|
125
|
+
dragged.current = false;
|
|
126
|
+
return was;
|
|
127
|
+
};
|
|
128
|
+
return { ref, handlers, swallowClick };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function Launcher({ hidden, commenting, openCount, spot, onSpot, onToggle, onShow }: Props) {
|
|
132
|
+
const { ref, handlers, swallowClick } = useDrag(spot, onSpot);
|
|
133
|
+
const tab = useRef<HTMLButtonElement>(null);
|
|
134
|
+
const was = useRef(hidden);
|
|
135
|
+
// Hiding and showing hand focus across, so a keyboard never loses its place.
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
if (was.current !== hidden) (hidden ? tab : ref).current?.focus();
|
|
138
|
+
was.current = hidden;
|
|
139
|
+
}, [hidden, ref]);
|
|
140
|
+
|
|
141
|
+
const label = commenting ? 'Stop commenting' : 'Comment on this page';
|
|
142
|
+
return (
|
|
143
|
+
<>
|
|
144
|
+
<button
|
|
145
|
+
ref={ref}
|
|
146
|
+
type="button"
|
|
147
|
+
className="rv-launch"
|
|
148
|
+
data-spot={spot}
|
|
149
|
+
data-away={hidden ? '' : undefined}
|
|
150
|
+
inert={hidden}
|
|
151
|
+
aria-label={openCount > 0 ? `${label}, ${openCount} open` : label}
|
|
152
|
+
aria-pressed={commenting}
|
|
153
|
+
aria-keyshortcuts="Alt+ArrowLeft Alt+ArrowRight Alt+ArrowUp Alt+ArrowDown"
|
|
154
|
+
title={`${label}. Drag to move.`}
|
|
155
|
+
onClick={() => swallowClick() || onToggle()}
|
|
156
|
+
{...handlers}
|
|
157
|
+
>
|
|
158
|
+
<Icon name="message-text-square-01" size={18} />
|
|
159
|
+
{openCount > 0 && (
|
|
160
|
+
<span className="rv-count" aria-hidden="true">
|
|
161
|
+
{openCount > 99 ? '99+' : openCount}
|
|
162
|
+
</span>
|
|
163
|
+
)}
|
|
164
|
+
</button>
|
|
165
|
+
<button
|
|
166
|
+
ref={tab}
|
|
167
|
+
type="button"
|
|
168
|
+
className="rv-tab"
|
|
169
|
+
data-spot={spot}
|
|
170
|
+
data-away={hidden ? undefined : ''}
|
|
171
|
+
inert={!hidden}
|
|
172
|
+
aria-label="Show comments"
|
|
173
|
+
title="Show comments"
|
|
174
|
+
onClick={onShow}
|
|
175
|
+
>
|
|
176
|
+
<Icon name="message-text-square-01" size={16} />
|
|
177
|
+
</button>
|
|
178
|
+
</>
|
|
179
|
+
);
|
|
180
|
+
}
|