@stapel/listings-react 0.30.2 → 0.30.4

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.
@@ -76,33 +76,34 @@ import {
76
76
  import type { ListingCardOpenProps } from "./ListingCard.js";
77
77
  import { ListingPhoto } from "./ListingPhoto.js";
78
78
  import { ListingPrice } from "./ListingPrice.js";
79
+ import {
80
+ TITLE_CLAMP_CLASS,
81
+ TITLE_CLAMP_STYLE_HREF,
82
+ titleClampCss,
83
+ } from "./titleClamp.js";
79
84
  import type { ThemeModeProp } from "./types.js";
80
85
 
81
- /** Lines of title a tile draws before it clips. See the file header. */
82
- const TITLE_LINES = 2;
83
-
84
- /** The class the clamped title carries, for {@link feedCardCss}. */
85
- export const FEED_TITLE_CLASS = "stapel-listing-feed-title";
86
-
87
- /** The `href` the hoisted feed stylesheet is deduplicated by. */
88
- export const FEED_CARD_STYLE_HREF = "stapel-listings-feed-card";
89
-
90
86
  /**
91
- * The clamp, as a real CSS rule rather than an inline style.
87
+ * The title clamp is `titleClamp.ts`'s, not this file's.
88
+ *
89
+ * It lived here, and the grid card (`ListingCard`) answered the same question
90
+ * with antd's one-line `ellipsis` — which is how the busiest surface on the
91
+ * storefront ended up cutting job titles mid-word while the tile beside it
92
+ * wrapped them over two lines. One answer, one module, both cards.
92
93
  *
93
- * `-webkit-line-clamp` and `-webkit-box-orient` are the two declarations that
94
- * do not survive the trip through an inline style object: React's serializer
95
- * and every DOM implementation that is not a browser drop them silently, so a
96
- * tile written that way clamps in Chrome, does not clamp in a test, and
97
- * nothing anywhere says which. A hoisted sheet keeps one copy for the document
98
- * and makes the rule something a test can read.
94
+ * These three names stay as aliases because this file's tests and a reader
95
+ * looking for "the feed card's clamp" both expect them here.
96
+ *
97
+ * They are re-export bindings rather than `const` copies: under
98
+ * `--isolatedDeclarations` the declaration emitter types each export from that
99
+ * export alone, and `const FEED_TITLE_CLASS = TITLE_CLAMP_CLASS` gives it
100
+ * nothing to go on. An aliased re-export carries the original's type with it.
99
101
  */
100
- export function feedCardCss(): string {
101
- return (
102
- `.${FEED_TITLE_CLASS}{display:-webkit-box;-webkit-box-orient:vertical;` +
103
- `-webkit-line-clamp:${String(TITLE_LINES)};overflow:hidden}`
104
- );
105
- }
102
+ export {
103
+ TITLE_CLAMP_CLASS as FEED_TITLE_CLASS,
104
+ TITLE_CLAMP_STYLE_HREF as FEED_CARD_STYLE_HREF,
105
+ titleClampCss as feedCardCss,
106
+ } from "./titleClamp.js";
106
107
 
107
108
  /** The tile. `position: relative` is what the heart and the badge overlay are
108
109
  * pinned to; `minWidth: 0` keeps a long word inside its grid track. */
@@ -172,8 +173,8 @@ export function ListingFeedCard(props: ListingFeedCardProps): ReactElement {
172
173
  <style href={CARD_TARGET_STYLE_HREF} precedence="default">
173
174
  {cardTargetCss()}
174
175
  </style>
175
- <style href={FEED_CARD_STYLE_HREF} precedence="default">
176
- {feedCardCss()}
176
+ <style href={TITLE_CLAMP_STYLE_HREF} precedence="default">
177
+ {titleClampCss()}
177
178
  </style>
178
179
  <div
179
180
  style={TILE}
@@ -206,7 +207,7 @@ export function ListingFeedCard(props: ListingFeedCardProps): ReactElement {
206
207
  {/* Title before price: on a feed a person is browsing, not
207
208
  comparing — the ref's order, and the reverse of the SERP's. */}
208
209
  <Typography.Text
209
- className={FEED_TITLE_CLASS}
210
+ className={TITLE_CLAMP_CLASS}
210
211
  data-testid="listings-feed-title"
211
212
  >
212
213
  {title}
@@ -34,6 +34,31 @@
34
34
  * {@link SWIPE_AXIS_RATIO}. A diagonal thumb scrolling the feed changes no
35
35
  * photograph.
36
36
  *
37
+ * ── HOW FAR IS FAR ENOUGH, and how many photographs that buys ────────────
38
+ *
39
+ * Intent is not a commit. A drag that has declared itself horizontal still
40
+ * has to EARN the photograph, and the price is a fraction of the photograph
41
+ * itself — {@link SWIPE_COMMIT_FRACTION} of the SLIDE's own measured width,
42
+ * never a pixel constant. The constant was the defect, and measurably so: a
43
+ * one-column search card on a 390px phone is a ~351px slide (the well less
44
+ * the carousel's 8% peek), so a fixed 32px priced a photograph at 9% of
45
+ * itself; the same 32px on the two-column home feed is 19% of a ~170px slide.
46
+ * One number, two different gestures, and the cheap one is the one the owner
47
+ * met — "it feels like 10%", and the middle photograph flying past.
48
+ *
49
+ * Two further rules, and both are about a gesture being ONE decision:
50
+ *
51
+ * - A flick is a choice too. Under the distance, a drag thrown faster than
52
+ * {@link SWIPE_FLICK_VELOCITY} still advances — the speed says the intent
53
+ * the distance did not.
54
+ * - ONE photograph per gesture, and one only. The old handler moved its own
55
+ * origin on each commit, so a drag across three slide widths advanced
56
+ * three photographs (and a fling across the card advanced ten). A gesture
57
+ * now commits at most once, and a second photograph costs a second press.
58
+ *
59
+ * Below the threshold the strip goes back to the picture it was showing —
60
+ * a refused gesture must leave the strip where it found it, not halfway.
61
+ *
37
62
  * ── What neither gesture touches ─────────────────────────────────────────
38
63
  *
39
64
  * The keyboard, and the card's single link target. The strip underneath is
@@ -59,14 +84,56 @@ export const CARD_GALLERY_STYLE_HREF = "stapel-listings-card-gallery";
59
84
  export const SCRUB_MEDIA = "(hover: hover) and (pointer: fine)";
60
85
 
61
86
  /**
62
- * How far a finger travels before it has said "photo", in CSS pixels.
87
+ * How far a finger travels before it is a GESTURE at all, in CSS pixels.
63
88
  *
64
89
  * Under this, a drag is a tap that wobbled — and a tap on a card is a
65
90
  * navigation, so a low threshold does not change a photograph, it changes one
66
91
  * and then leaves the listing.
92
+ *
93
+ * This is the floor, not the price: what actually buys a photograph is
94
+ * {@link SWIPE_COMMIT_FRACTION} of the slide, or a flick above
95
+ * {@link SWIPE_FLICK_VELOCITY}. A pixel count cannot be the price, because
96
+ * the same 32px is a third of a card in a four-column desktop grid and a
97
+ * twelfth of one on a phone.
67
98
  */
68
99
  export const SWIPE_MIN_PX = 32;
69
100
 
101
+ /**
102
+ * How much of ONE PHOTOGRAPH a slow drag must cover to turn it, as a fraction
103
+ * of that photograph's own measured width.
104
+ *
105
+ * The owner's ruling, third pass: "maybe not 75, maybe 30 — right now it
106
+ * feels like 10%". 10% is what the old fixed 32px came to on a phone slide of
107
+ * ~350px, and it is why a middle photograph flew past on a gesture aimed at
108
+ * the feed. 0.3 is far enough that the drag is unmistakably a drag and short
109
+ * enough that a thumb can make it without crossing the whole card.
110
+ */
111
+ export const SWIPE_COMMIT_FRACTION = 0.3;
112
+
113
+ /**
114
+ * The speed, in CSS pixels per millisecond, at which a SHORT drag still turns
115
+ * the photograph.
116
+ *
117
+ * 0.5 px/ms — 500 px/s — is the owner's number and sits in the empty band
118
+ * between the two gestures it has to tell apart: a deliberate slow drag runs
119
+ * at 100–300 px/s (a thumb crossing a 350px card in one to three seconds),
120
+ * and a flick people expect to throw the strip runs at 800–2000 px/s. Nothing
121
+ * a person means as a careful drag arrives here, and nothing they mean as a
122
+ * flick falls under it.
123
+ */
124
+ export const SWIPE_FLICK_VELOCITY = 0.5;
125
+
126
+ /**
127
+ * The window the speed is measured over, in milliseconds.
128
+ *
129
+ * A flick is what the finger was doing AS IT LEFT, not the average of the
130
+ * whole gesture: a drag that crawls for a second and is then thrown would
131
+ * average out to a crawl, and the throw is the part the person meant. One
132
+ * moving window of roughly six frames is long enough to survive a single
133
+ * jittery sample and short enough to still be about the end of the gesture.
134
+ */
135
+ export const SWIPE_VELOCITY_WINDOW_MS = 100;
136
+
70
137
  /**
71
138
  * How much more horizontal than vertical a drag must be to count.
72
139
  *
@@ -90,18 +157,60 @@ export function segmentIndex(offsetX: number, width: number, count: number): num
90
157
 
91
158
  /**
92
159
  * A drag → the number of photographs it asks for: `1` forward, `-1` back, `0`
93
- * for a drag that has not declared horizontal intent.
160
+ * for a drag that has not earned one.
94
161
  *
95
162
  * Dragging LEFT advances, the direction the content moves under the finger —
96
- * the same mapping the native scroller has.
163
+ * the same mapping the native scroller has. The answer is never outside
164
+ * `-1…1`: a gesture is worth one photograph however far it travelled, which
165
+ * is what stops a fling crossing the whole strip.
166
+ *
167
+ * @param dx Horizontal travel from the press, signed, in CSS pixels.
168
+ * @param dy Vertical travel from the press, signed.
169
+ * @param slideWidth The measured width of ONE slide. `0` means it could not
170
+ * be measured (an unlaid-out strip, a server render) — and an unknown width
171
+ * has no fraction, so the decision falls back to {@link SWIPE_MIN_PX}
172
+ * alone rather than to an invented number.
173
+ * @param velocity The finger's recent speed in CSS pixels per millisecond.
97
174
  */
98
- export function swipeStep(dx: number, dy: number): -1 | 0 | 1 {
175
+ export function swipeStep(
176
+ dx: number,
177
+ dy: number,
178
+ slideWidth = 0,
179
+ velocity = 0
180
+ ): -1 | 0 | 1 {
99
181
  const across = Math.abs(dx);
182
+ // Is this a gesture at all — past the tap wobble, and across rather than
183
+ // down the page.
100
184
  if (across < SWIPE_MIN_PX) return 0;
101
185
  if (across <= Math.abs(dy) * SWIPE_AXIS_RATIO) return 0;
186
+ // Has it earned a photograph: the distance rule, measured against THIS
187
+ // strip's slide, or the speed rule when the distance is short.
188
+ const far = slideWidth > 0 ? across >= slideWidth * SWIPE_COMMIT_FRACTION : true;
189
+ if (!far && velocity < SWIPE_FLICK_VELOCITY) return 0;
102
190
  return dx < 0 ? 1 : -1;
103
191
  }
104
192
 
193
+ /**
194
+ * The width of ONE slide inside this gallery's strip, in CSS pixels.
195
+ *
196
+ * Read off the SLIDE, which is the house rule (§83: geometry from the
197
+ * element's width, never the viewport's) and here also the only correct
198
+ * answer: a slide is the well less the carousel's peek, so the well's width
199
+ * is 8% too generous and a viewport-derived number is not about this card at
200
+ * all — the same card is full-bleed on a phone and a quarter of a row on a
201
+ * desktop grid.
202
+ *
203
+ * `0` when nothing can be measured. That is a refusal to guess, not a
204
+ * measurement: {@link swipeStep} falls back to the pixel floor for it.
205
+ */
206
+ export function measureSlideWidth(box: HTMLElement): number {
207
+ const strip = box.querySelector<HTMLElement>("[data-stapel-carousel-strip]");
208
+ if (strip === null) return 0;
209
+ const slide = strip.children.item(0);
210
+ const width = slide === null ? 0 : slide.getBoundingClientRect().width;
211
+ return width > 0 ? width : 0;
212
+ }
213
+
105
214
  /** Does this environment have a real pointer? `false` where there is no
106
215
  * `matchMedia` to ask (a server render, an old jsdom), which is the safe
107
216
  * side: a scrub that does not happen costs a hover, a scrub on a phone is a
@@ -198,6 +307,63 @@ function showSlide(box: HTMLElement, index: number, instant: boolean): void {
198
307
  }
199
308
  }
200
309
 
310
+ /** One position of the finger, in time — the raw material of a flick. */
311
+ interface Sample {
312
+ readonly x: number;
313
+ readonly t: number;
314
+ }
315
+
316
+ /** The drag in progress. Everything a release has to decide on. */
317
+ interface Drag {
318
+ /** Where the finger went down. */
319
+ readonly x: number;
320
+ readonly y: number;
321
+ /** One slide's width, measured ONCE at the press: a strip whose geometry
322
+ * changed mid-gesture would move the goalposts under the finger. */
323
+ readonly width: number;
324
+ /** The recent trail, newest last — see {@link SWIPE_VELOCITY_WINDOW_MS}. */
325
+ samples: Sample[];
326
+ /** Has this gesture already spent its one photograph. */
327
+ committed: boolean;
328
+ /** Has it declared horizontal intent — the only kind of refused gesture
329
+ * that is worth snapping back from. */
330
+ crossed: boolean;
331
+ }
332
+
333
+ /** When this event happened, on whatever clock the environment has. A UA
334
+ * stamps every input event; the fallback is for a synthesised one. */
335
+ function stampOf(event: { timeStamp: number }): number {
336
+ const stamp = event.timeStamp;
337
+ if (Number.isFinite(stamp) && stamp > 0) return stamp;
338
+ return typeof performance === "object" && typeof performance.now === "function"
339
+ ? performance.now()
340
+ : Date.now();
341
+ }
342
+
343
+ /** Record a position, and forget the ones the speed window has passed. */
344
+ function sample(drag: Drag, x: number, t: number): void {
345
+ drag.samples.push({ x, t });
346
+ // Drop the oldest only while the NEXT one is still inside the window, so
347
+ // the trail always spans at least one interval to divide by.
348
+ while (drag.samples.length > 2) {
349
+ const second = drag.samples[1];
350
+ if (second === undefined || t - second.t <= SWIPE_VELOCITY_WINDOW_MS) break;
351
+ drag.samples.shift();
352
+ }
353
+ }
354
+
355
+ /** The finger's speed over the trail, in CSS pixels per millisecond. `0`
356
+ * where the environment gave no usable clock — an unknown speed is not a
357
+ * flick. */
358
+ function velocityOf(drag: Drag): number {
359
+ const first = drag.samples[0];
360
+ const last = drag.samples[drag.samples.length - 1];
361
+ if (first === undefined || last === undefined) return 0;
362
+ const elapsed = last.t - first.t;
363
+ if (elapsed <= 0) return 0;
364
+ return Math.abs(last.x - first.x) / elapsed;
365
+ }
366
+
201
367
  /**
202
368
  * The gallery gestures for a media well holding `count` photographs.
203
369
  *
@@ -208,9 +374,9 @@ export function useCardGallery(count: number): CardGallery {
208
374
  const ref = useRef<HTMLDivElement | null>(null);
209
375
  const [active, setActive] = useState(0);
210
376
  const [scrubbing, setScrubbing] = useState(false);
211
- // The origin of the drag in progress, or `null`. A ref rather than state:
212
- // it changes on every move and no render depends on it.
213
- const origin = useRef<{ x: number; y: number } | null>(null);
377
+ // The drag in progress, or `null`. A ref rather than state: it changes on
378
+ // every move and no render depends on it.
379
+ const drag = useRef<Drag | null>(null);
214
380
  /**
215
381
  * THE INDEX THIS HOOK ASKED FOR, and the whole of the fix.
216
382
  *
@@ -276,15 +442,23 @@ export function useCardGallery(count: number): CardGallery {
276
442
  request(segmentIndex(event.clientX - rect.left, rect.width, count));
277
443
  return;
278
444
  }
279
- const from = origin.current;
280
- if (from === null) return;
281
- const step = swipeStep(event.clientX - from.x, event.clientY - from.y);
445
+ const current = drag.current;
446
+ if (current === null) return;
447
+ sample(current, event.clientX, stampOf(event));
448
+ // ONE PHOTOGRAPH PER GESTURE. The trail keeps being recorded — a
449
+ // release still wants to know how the finger was moving — but the
450
+ // decision below is made once and the rest of the drag is scenery.
451
+ if (current.committed) return;
452
+ const dx = event.clientX - current.x;
453
+ const dy = event.clientY - current.y;
454
+ if (Math.abs(dx) >= SWIPE_MIN_PX && Math.abs(dx) > Math.abs(dy) * SWIPE_AXIS_RATIO) {
455
+ current.crossed = true;
456
+ }
457
+ const step = swipeStep(dx, dy, current.width, velocityOf(current));
282
458
  if (step === 0) return;
283
- // The origin moves with the commit, so a long drag walks the strip one
284
- // photograph per threshold rather than one per gesture.
285
- origin.current = { x: event.clientX, y: event.clientY };
459
+ current.committed = true;
286
460
  setScrubbing(false);
287
- request((current) => Math.min(count - 1, Math.max(0, current + step)));
461
+ request((at) => Math.min(count - 1, Math.max(0, at + step)));
288
462
  },
289
463
  [count, fine, many, request]
290
464
  );
@@ -292,18 +466,48 @@ export function useCardGallery(count: number): CardGallery {
292
466
  const onPointerDown = useCallback(
293
467
  (event: ReactPointerEvent<HTMLDivElement>): void => {
294
468
  if (!many || event.pointerType === "mouse") return;
295
- origin.current = { x: event.clientX, y: event.clientY };
469
+ const box = ref.current;
470
+ drag.current = {
471
+ x: event.clientX,
472
+ y: event.clientY,
473
+ // Measured here, from the slide itself, and held for the gesture.
474
+ width: box === null ? 0 : measureSlideWidth(box),
475
+ samples: [{ x: event.clientX, t: stampOf(event) }],
476
+ committed: false,
477
+ crossed: false,
478
+ };
296
479
  },
297
480
  [many]
298
481
  );
299
482
 
483
+ /**
484
+ * The finger leaves.
485
+ *
486
+ * A gesture that earned its photograph already took it, mid-drag, the
487
+ * moment it crossed the threshold. What is left here is the REFUSED one: a
488
+ * drag that went sideways far enough to be read as a swipe and then stopped
489
+ * short. The strip goes back to the photograph it was showing, because a
490
+ * refused gesture that leaves the strip halfway is the defect wearing the
491
+ * other face.
492
+ *
493
+ * A release with no drag behind it touches nothing at all — that is the
494
+ * guard probe p23 bought: `pointerup` also arrives after a NATIVE scroll
495
+ * this hook never started, and scrolling "back" there means undoing the
496
+ * person's own swipe.
497
+ */
300
498
  const endDrag = useCallback((): void => {
301
- origin.current = null;
302
- }, []);
499
+ const current = drag.current;
500
+ drag.current = null;
501
+ if (current === null || !many) return;
502
+ if (current.committed || !current.crossed) return;
503
+ const box = ref.current;
504
+ if (box === null) return;
505
+ showSlide(box, active, false);
506
+ }, [active, many]);
303
507
 
304
508
  const onPointerLeave = useCallback(
305
509
  (event: ReactPointerEvent<HTMLDivElement>): void => {
306
- origin.current = null;
510
+ drag.current = null;
307
511
  if (!many) return;
308
512
  // THE REWIND IS A HOVER RULE, AND ONLY A HOVER RULE.
309
513
  //
@@ -215,6 +215,15 @@ export function detailGalleryCss(): string {
215
215
  .${LISTINGS_GALLERY_CLASS}[data-gallery-layout="strip"] > * {
216
216
  flex: 0 0 ${LISTINGS_GALLERY_STRIP_BASIS};
217
217
  scroll-snap-align: start;
218
+ /* EVERY PHOTOGRAPH IS A DESTINATION, not a waypoint. A mandatory snap type
219
+ alone only promises the strip will COME TO REST on a snap point — a fling
220
+ with any momentum behind it still crosses two or three photographs on its
221
+ way there, which is the owner's "the middle photo flies past" in its
222
+ native form. "always" makes the scroller stop at the first snap point it
223
+ reaches, so one gesture is one photograph here for the same reason the
224
+ card's gesture layer clamps its own step to one. The same declaration
225
+ SkinCarousel already carries on its slides. */
226
+ scroll-snap-stop: always;
218
227
  }
219
228
  .${LISTINGS_GALLERY_FRAME_CLASS} {
220
229
  position: relative;
@@ -176,9 +176,13 @@ export type { CardBadgesProps } from "./CardBadges.js";
176
176
  // ── the card gallery's two gestures ─────────────────────────────────────────
177
177
  export {
178
178
  SWIPE_AXIS_RATIO,
179
+ SWIPE_COMMIT_FRACTION,
180
+ SWIPE_FLICK_VELOCITY,
179
181
  SWIPE_MIN_PX,
182
+ SWIPE_VELOCITY_WINDOW_MS,
180
183
  cardGalleryCss,
181
184
  hasFinePointer,
185
+ measureSlideWidth,
182
186
  segmentIndex,
183
187
  swipeStep,
184
188
  useCardGallery,
@@ -0,0 +1,56 @@
1
+ /**
2
+ * `titleClamp` — how many lines of a listing title a card shows, and where the
3
+ * cut goes when there are more.
4
+ *
5
+ * ── the defect ────────────────────────────────────────────────────────────
6
+ *
7
+ * The tile card (`ListingFeedCard`) clamped its title to two lines. The grid
8
+ * card (`ListingCard`) used antd's `<Typography.Text ellipsis>`, which is ONE
9
+ * line, and one line of a job or a part title lands in the middle of a word:
10
+ * the live storefront's home grid drew titles cut as "Electrician - construc…"
11
+ * and "Administrator - restaur…". The reference wraps the same titles over two
12
+ * lines and cuts, if it has to, at the end of the second.
13
+ *
14
+ * Two cards, two different answers to the same question, and the wrong one was
15
+ * on the busiest surface. So the answer lives here once and both cards read it.
16
+ *
17
+ * ── the rule ──────────────────────────────────────────────────────────────
18
+ *
19
+ * display: -webkit-box + -webkit-box-orient: vertical + -webkit-line-clamp
20
+ * two lines, with the browser's own ellipsis at the end of the second.
21
+ * overflow-wrap: normal
22
+ * a long word moves to the next line whole rather than being broken across
23
+ * the wrap. Without it a narrow grid track will split a word to fill a
24
+ * line, which is the same defect one level down.
25
+ *
26
+ * ── why a hoisted stylesheet and not an inline style ──────────────────────
27
+ *
28
+ * `-webkit-line-clamp` and `-webkit-box-orient` do not survive the trip
29
+ * through a React style object: the serializer and every DOM implementation
30
+ * that is not a browser drop them silently, so a card written that way clamps
31
+ * in Chrome, does not clamp in a test, and nothing anywhere says which. A
32
+ * hoisted sheet keeps one copy for the document and makes the rule something a
33
+ * test can read.
34
+ *
35
+ * Both cards emit the sheet under the SAME `href`, which is how React 19
36
+ * deduplicates it — a second href carrying the same rule would hoist a second
37
+ * copy for every page showing both card shapes.
38
+ */
39
+
40
+ /** Lines of title a card draws before it clips. */
41
+ export const TITLE_CLAMP_LINES = 2;
42
+
43
+ /** The class a clamped title carries. */
44
+ export const TITLE_CLAMP_CLASS = "stapel-listing-title-clamp";
45
+
46
+ /** The `href` the hoisted stylesheet is deduplicated by. */
47
+ export const TITLE_CLAMP_STYLE_HREF = "stapel-listings-title-clamp";
48
+
49
+ /** The rule itself, as text, so a test can assert it. */
50
+ export function titleClampCss(): string {
51
+ return (
52
+ `.${TITLE_CLAMP_CLASS}{display:-webkit-box;-webkit-box-orient:vertical;` +
53
+ `-webkit-line-clamp:${String(TITLE_CLAMP_LINES)};overflow:hidden;` +
54
+ `overflow-wrap:normal}`
55
+ );
56
+ }