Skip to content

Commit f182ff2

Browse files
committed
feat(web): open a passage's comment from its underline when narrow
With no room for cards beside the page, the margin stays empty and a press on an underlined passage opens its card under the line, or above it where the view ends first. A hidden button per passage does the same from the keyboard; the card then takes focus, so Escape hands it back. Scrolling closes the card.
1 parent f2a6aea commit f182ff2

3 files changed

Lines changed: 189 additions & 9 deletions

File tree

‎apps/web/src/config.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,9 @@ export const wording = {
10421042
percent === 100 ? "Zoom 100%" : `Zoom ${String(percent)}%, back to 100%`,
10431043
zoomIn: "Zoom in",
10441044
zoomOut: "Zoom out",
1045+
/** Opens what is said about a passage when the margin is too narrow to show it beside. */
1046+
passageNote: (quote: string) =>
1047+
`Comment on “${quote.length > 40 ? `${quote.slice(0, 40).trimEnd()}…` : quote}”`,
10451048
},
10461049
printDocument: "Save as PDF or print",
10471050
removeAttachment: {

‎apps/web/src/pages/workspace/document-page.module.css‎

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
/* On dark the backdrop is pure black and so is the theme's page colour, so the sheet, the bar, the
2121
contents' open section and the margin's cards would all sink into it. They take the theme's first
22-
raised step instead, and the backdrop stays black around them. */
23-
:global(.dark) .reader {
22+
raised step instead, and the backdrop stays black around them. A card opened from its passage
23+
is drawn outside the reader, so it takes the same step itself. */
24+
:global(.dark) .reader,
25+
:global(.dark) .card {
2426
--wb-pane: var(--muted);
2527
--wb-background: var(--muted);
2628
}
@@ -59,6 +61,7 @@
5961
}
6062

6163
.column {
64+
position: relative;
6265
grid-column: 2;
6366
display: flex;
6467
flex-direction: column;
@@ -206,6 +209,42 @@
206209
width: var(--card-width);
207210
}
208211

212+
/* With no room for cards beside the page, a passage's card opens from its underline. The mark's
213+
button is there for the keyboard alone: the underline already shows the passage to the eye. */
214+
.sheet[data-pointing] {
215+
cursor: pointer;
216+
}
217+
218+
.mark {
219+
position: absolute;
220+
right: 4px;
221+
width: 24px;
222+
height: 24px;
223+
color: var(--wb-primary);
224+
opacity: 0;
225+
pointer-events: none;
226+
}
227+
228+
.mark:focus-visible {
229+
opacity: 1;
230+
outline-offset: 0;
231+
}
232+
233+
.card {
234+
position: fixed;
235+
z-index: 60;
236+
width: min(var(--card-width), calc(100vw - 16px));
237+
animation: global(fade-in) 150ms ease-out;
238+
}
239+
240+
.card[data-keys] {
241+
animation: none;
242+
}
243+
244+
.card:focus {
245+
outline: none;
246+
}
247+
209248
/* Every passage something is said about is underlined in the attention orange; the one whose
210249
comment is being written is tinted too. A passage being changed fades to a grey dashed line, and
211250
a changed one is underlined in the done green. */

‎apps/web/src/pages/workspace/document-page.tsx‎

Lines changed: 145 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@
44
* being read, the zoom and the download; the document's sections sit to the left; the right margin
55
* holds whatever is placed beside a passage, level with it.
66
*/
7-
import { Minus, Plus } from "lucide-react";
7+
import { Button, useDismissablePopover } from "@corbits/react-ui";
8+
import { MessageSquare, Minus, Plus } from "lucide-react";
89
import {
910
useCallback,
1011
useEffect,
12+
useId,
1113
useLayoutEffect,
1214
useRef,
1315
useState,
1416
type CSSProperties,
17+
type MouseEvent,
1518
type ReactNode,
1619
} from "react";
20+
import { createPortal } from "react-dom";
1721
import { Fault } from "../../components.tsx";
1822
import { wording } from "../../config.ts";
1923
import {
@@ -49,11 +53,13 @@ const READING_LINE = 0.4;
4953
// A margin item starts a little above its passage, so its first line sits level with it.
5054
const MARGIN_LEAD = 15;
5155
const MARGIN_GAP = 8;
52-
// A margin item is a card this wide; with less room than that beside the page, the margin stays
53-
// empty and the underlines alone mark the passages. The stylesheet takes the width from here.
56+
// A margin item is a card at most this wide; with less room than that beside the page, the cards
57+
// leave the margin and open from their passages instead. The stylesheet takes the width from here.
5458
const CARD_WIDTH = 280;
5559
// Cards sit --space-8 clear of the page and want --space-6 clear of the pane's edge.
5660
const MARGIN_INSET = 32 + 24;
61+
// The underline sits below the line's box, so a press just under the words still reaches it.
62+
const UNDERLINE_REACH = 8;
5763
// Each state is its own highlight, named as the stylesheet paints it.
5864
const HIGHLIGHTS: Readonly<Record<PassageState | "commented", string>> = {
5965
commented: "document-passage",
@@ -180,6 +186,21 @@ export function DocumentPage({
180186
const [page, setPage] = useState({ current: 1, total: 1 });
181187
const [places, setPlaces] = useState<readonly PassagePlace[]>([]);
182188
const [roomy, setRoomy] = useState(true);
189+
const ranges = useRef(new Map<string, Range>());
190+
const cardId = useId();
191+
const [card, setCard] = useState<{
192+
place: PassagePlace;
193+
line: DOMRect;
194+
} | null>(null);
195+
const { rootRef: cardRef, triggerRef: cardTrigger } = useDismissablePopover<
196+
HTMLDivElement,
197+
HTMLButtonElement
198+
>({
199+
open: card !== null,
200+
onOpenChange: (open) => {
201+
if (!open) setCard(null);
202+
},
203+
});
183204

184205
const follow = useCallback(() => {
185206
const view = scroller.current;
@@ -239,6 +260,7 @@ export function DocumentPage({
239260
range: hit.range,
240261
})),
241262
);
263+
ranges.current = new Map(found.map((hit) => [hit.place.id, hit.range]));
242264
setPlaces((was) =>
243265
same(
244266
was,
@@ -284,7 +306,26 @@ export function DocumentPage({
284306
};
285307
}, [measure]);
286308

287-
const inMargin = margin !== undefined && roomy;
309+
// A card opened from its passage sits under the line, or above it when the view ends first.
310+
useLayoutEffect(() => {
311+
const box = cardRef.current;
312+
if (card === null || box === null) return;
313+
const below = card.line.bottom + MARGIN_GAP;
314+
const fitsBelow =
315+
below + box.offsetHeight <= window.innerHeight - MARGIN_GAP;
316+
box.style.top = `${String(fitsBelow ? below : card.line.top - MARGIN_GAP - box.offsetHeight)}px`;
317+
box.style.left = `${String(Math.max(MARGIN_GAP, Math.min(card.line.left, window.innerWidth - box.offsetWidth - MARGIN_GAP)))}px`;
318+
// Opened from the keyboard, the card takes focus so Escape can hand it back, and appears at
319+
// once: a key press is answered without a flourish.
320+
if (cardTrigger.current === null) delete box.dataset.keys;
321+
else {
322+
box.dataset.keys = "";
323+
box.focus({ preventScroll: true });
324+
}
325+
}, [card, cardRef, cardTrigger]);
326+
327+
const narrow = margin !== undefined && !roomy;
328+
const inMargin = margin !== undefined && !narrow;
288329

289330
// Items too close to fit level with their passages stack below one another, in page order.
290331
// Every height is read before any top is written, so placing them costs one layout. An item
@@ -309,6 +350,48 @@ export function DocumentPage({
309350
return () => resized.disconnect();
310351
}, [places, inMargin]);
311352

353+
/** The passage under a point on the page, by the line of it that was pressed. */
354+
const passageAt = (x: number, y: number) => {
355+
for (const place of places) {
356+
const lines = ranges.current.get(place.id)?.getClientRects() ?? [];
357+
const line = [...lines].find(
358+
(rect) =>
359+
x >= rect.left &&
360+
x <= rect.right &&
361+
y >= rect.top &&
362+
y <= rect.bottom + UNDERLINE_REACH,
363+
);
364+
if (line !== undefined) return { place, line };
365+
}
366+
return null;
367+
};
368+
369+
// With the margin too narrow for its cards, a passage's underline opens its card instead.
370+
const openFromMark = (event: MouseEvent) => {
371+
if (!narrow || window.getSelection()?.isCollapsed === false) return;
372+
const hit = passageAt(event.clientX, event.clientY);
373+
if (hit === null) return;
374+
// The card takes this press; quoting the passage into a message is not offered as well.
375+
event.stopPropagation();
376+
cardTrigger.current = null;
377+
setCard(hit);
378+
};
379+
380+
const pointAtMark = (event: MouseEvent<HTMLElement>) => {
381+
if (!narrow) return;
382+
event.currentTarget.toggleAttribute(
383+
"data-pointing",
384+
passageAt(event.clientX, event.clientY) !== null,
385+
);
386+
};
387+
388+
const openFromKeys = (place: PassagePlace, button: HTMLButtonElement) => {
389+
const line = ranges.current.get(place.id)?.getClientRects()[0];
390+
if (line === undefined) return;
391+
cardTrigger.current = button;
392+
setCard({ place, line });
393+
};
394+
312395
// The reader's scroll padding keeps a section clear of the bar; its scroll behaviour is smooth
313396
// unless the person asks for less motion.
314397
const open = (id: string | null) => {
@@ -376,7 +459,10 @@ export function DocumentPage({
376459
} as CSSProperties
377460
}
378461
data-slot="document-page"
379-
onScroll={follow}
462+
onScroll={() => {
463+
follow();
464+
setCard(null);
465+
}}
380466
>
381467
<PaneBar
382468
label={wording.documentBar}
@@ -395,7 +481,13 @@ export function DocumentPage({
395481
{notes === undefined || notes === null ? null : (
396482
<div className={styles.notes}>{notes}</div>
397483
)}
398-
<article ref={sheet} className={styles.sheet} data-writing={writing}>
484+
<article
485+
ref={sheet}
486+
className={styles.sheet}
487+
data-writing={writing}
488+
onClickCapture={openFromMark}
489+
onPointerMove={pointAtMark}
490+
>
399491
<header className={styles.head}>
400492
<span>{project}</span>
401493
<span>{meta}</span>
@@ -406,9 +498,29 @@ export function DocumentPage({
406498
<Fault onRetry={onResume}>{words.stopped}</Fault>
407499
) : null}
408500
</article>
501+
{narrow
502+
? places.map((place) => (
503+
<Button
504+
key={place.id}
505+
variant="ghost"
506+
size="icon"
507+
className={styles.mark}
508+
style={{ top: place.top }}
509+
aria-label={words.passageNote(place.quote)}
510+
aria-haspopup="dialog"
511+
aria-expanded={card?.place.id === place.id}
512+
aria-controls={
513+
card?.place.id === place.id ? cardId : undefined
514+
}
515+
onClick={(event) => openFromKeys(place, event.currentTarget)}
516+
>
517+
<MessageSquare aria-hidden="true" />
518+
</Button>
519+
))
520+
: null}
409521
</div>
410522
<div ref={marginColumn} className={styles.margin}>
411-
{margin === undefined || !roomy
523+
{margin === undefined || narrow
412524
? null
413525
: places.map((place, index) => (
414526
<div
@@ -424,6 +536,32 @@ export function DocumentPage({
424536
))}
425537
</div>
426538
</div>
539+
{card === null || margin === undefined
540+
? null
541+
: createPortal(
542+
<div
543+
ref={cardRef}
544+
className={styles.card}
545+
style={
546+
{ "--card-width": `${String(CARD_WIDTH)}px` } as CSSProperties
547+
}
548+
id={cardId}
549+
data-slot="document-margin-card"
550+
role="dialog"
551+
aria-label={words.passageNote(card.place.quote)}
552+
tabIndex={-1}
553+
onBlur={(event) => {
554+
if (
555+
event.relatedTarget !== null &&
556+
!event.currentTarget.contains(event.relatedTarget)
557+
)
558+
setCard(null);
559+
}}
560+
>
561+
{margin(card.place)}
562+
</div>,
563+
document.body,
564+
)}
427565
</div>
428566
);
429567
}

0 commit comments

Comments
 (0)