Skip to content

Commit bcc34c7

Browse files
committed
fix(SegmentedControl): a finger in the gap between two segments counts for the nearer one
The selected face that follows a dragging finger jumped to the last segment while the finger crossed the 2 px gap between two segments: a point in no segment's box fell through to the end segment. A point in a gap now counts for the nearer segment, as UIKit's segments share an edge, and past the ends for the end segment.
1 parent 5c32f68 commit bcc34c7

3 files changed

Lines changed: 48 additions & 15 deletions

File tree

‎packages/core/src/SegmentedControl/__tests__/SegmentedControl.interaction.browser.spec.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,13 @@ test('during a lifted drag the selected face follows the finger at once, and the
14161416
table: unselected,
14171417
});
14181418
}
1419+
// Never a face ahead of the finger: Table's only once the finger is past
1420+
// the gap before it (the gap counts for the nearer segment).
1421+
const table = boxOf(boxes, 'table');
1422+
const gapMiddle = (list.x + list.width + table.x) / 2;
1423+
for (const s of seen.filter(s => s.finger < gapMiddle - 1)) {
1424+
expect(s.table, `finger at ${s.finger}`).toBe(unselected);
1425+
}
14191426
expect(seen.at(-1)).toMatchObject({
14201427
grid: unselected,
14211428
list: unselected,

‎packages/core/src/SegmentedControl/useSegmentedSelectionMotion.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,35 @@ describe('the titles under a finger (UIKit, F4 batch 64)', () => {
942942
expect(twinFades()).toHaveLength(0);
943943
});
944944

945+
it('a finger in the gap between two segments counts for the nearer one, never for a segment further on', () => {
946+
const ctx = setup('grid');
947+
// Gaps of 4 px between the segments, as the track's padding lays out.
948+
const gapped: Record<string, [number, number]> = {
949+
grid: [12, 96],
950+
list: [112, 96],
951+
table: [212, 102],
952+
};
953+
for (const [value, [left, width]] of Object.entries(gapped)) {
954+
ctx.segments[value].getBoundingClientRect = () =>
955+
rect(left, TRACK.top + 2, width, 27);
956+
}
957+
const weightOf = (value: string) => faceOf(ctx.segments[value]).weight;
958+
ctx.motion.press(ctx.track, 60);
959+
// Just past Grid, in the gap but nearer Grid: still Grid's.
960+
ctx.motion.drag(109);
961+
expect([weightOf('list'), weightOf('table')]).toEqual(['', '']);
962+
// Nearer List: List's.
963+
ctx.motion.drag(111);
964+
expect(weightOf('list')).toBe('600');
965+
expect(weightOf('table')).toBe('');
966+
// In the next gap, nearer List, then nearer Table.
967+
ctx.motion.drag(209);
968+
expect(weightOf('list')).toBe('600');
969+
ctx.motion.drag(211);
970+
expect(weightOf('table')).toBe('600');
971+
expect(weightOf('list')).toBe('');
972+
});
973+
945974
it('a lifted drag back onto the selected segment, or a cancel, puts the faces back', () => {
946975
const ctx = setup('grid');
947976
ctx.motion.press(ctx.track, clientCenterOf('grid'));

‎packages/core/src/SegmentedControl/useSegmentedSelectionMotion.ts‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -395,28 +395,25 @@ function isEnabledSegment(segment: HTMLElement): boolean {
395395
);
396396
}
397397

398-
/** The segment whose box holds `x` (the first or last one past the ends). */
398+
/**
399+
* The segment under `x`: the one whose box holds it, else the nearest box
400+
* (a gap between two segments splits at its middle, as UIKit's segments
401+
* share an edge; past the ends, the end segment).
402+
*/
399403
function segmentUnder(
400404
boxes: ReadonlyMap<HTMLElement, Box>,
401405
x: number,
402406
): HTMLElement | null {
403-
let first: [HTMLElement, Box] | null = null;
404-
let last: [HTMLElement, Box] | null = null;
407+
let best: HTMLElement | null = null;
408+
let distance = Infinity;
405409
for (const [segment, box] of boxes) {
406-
if (x >= box.x && x < box.x + box.width) {
407-
return segment;
408-
}
409-
if (first == null || box.x < first[1].x) {
410-
first = [segment, box];
411-
}
412-
if (last == null || box.x > last[1].x) {
413-
last = [segment, box];
410+
const d = Math.max(box.x - x, 0, x - (box.x + box.width));
411+
if (d < distance) {
412+
distance = d;
413+
best = segment;
414414
}
415415
}
416-
if (first != null && x < first[1].x) {
417-
return first[0];
418-
}
419-
return last?.[0] ?? null;
416+
return best;
420417
}
421418

422419
function titleOf(segment: HTMLElement): HTMLElement | null {

0 commit comments

Comments
 (0)