Skip to content

Commit dd6e04f

Browse files
committed
fix(core): a BottomSheet's dim follows the sheet the way an iOS sheet's does
Below the shortest detent the scrim is linear in the sheet's position and clears at the dismissed position (the sheet's top at the bottom of the screen), as UIKit's dim does; it used to clear halfway there. While a gesture moves it the host's backdrop easing is pinned off, so the dim follows the finger at once, and a release plays it on the dialog's ::backdrop through the motion foundation's animator, on the same clock as the sheet, each moment's value read from where the sheet is on its curve. A finger catches it where it is. A peek detent keeps its lighter scrim.
1 parent 33fd380 commit dd6e04f

8 files changed

Lines changed: 557 additions & 54 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[fix] A BottomSheet's scrim follows the sheet the way an iOS sheet's dim does. Dragged below its shortest detent, the dim thins in a straight line with the sheet's position and clears only when the sheet reaches the bottom of the screen; it used to clear halfway there. It follows the finger at once instead of easing 300 ms behind. When the sheet settles or is flung away, the dim rides the same spring frame for frame, on the compositor. A peek detent's lighter scrim is unchanged.
6+
7+
@vjeux

‎packages/core/src/BottomSheet/BottomSheet.test.tsx‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,34 @@ describe('BottomSheet', () => {
757757
expect(onOpenChange).toHaveBeenCalledWith(false);
758758
});
759759

760+
it('pins the scrim easing while a drag moves the dim, and hands it back when the sheet lands', async () => {
761+
render(
762+
<BottomSheet isOpen onOpenChange={vi.fn()} label="Filters">
763+
Content
764+
</BottomSheet>,
765+
);
766+
measureTallSheet();
767+
const dialog = screen.getByRole('dialog');
768+
const handle = getHandle();
769+
fireTimedPointer(handle, 'pointerdown', {time: 0, y: 0});
770+
// The dim follows the finger with no easing of its own.
771+
expect(dialog.style.getPropertyValue('--_sheet-scrim-duration')).toBe(
772+
'0s',
773+
);
774+
fireTimedPointer(handle, 'pointermove', {time: 1000, y: 200});
775+
// Gone is 736px below open (the sheet's visible height): the dim is
776+
// linear in position to there.
777+
expect(
778+
Number(dialog.style.getPropertyValue('--_sheet-scrim-opacity')),
779+
).toBeCloseTo((736 - 200) / 736, 2);
780+
fireTimedPointer(handle, 'pointerup', {time: 2000, y: 200});
781+
await landSpring();
782+
// Back at rest the host's own easing is restored, for its open and
783+
// close fades, at the dim the sheet came to rest with.
784+
expect(dialog.style.getPropertyValue('--_sheet-scrim-duration')).toBe('');
785+
expect(dialog).toHaveStyle({'--_sheet-scrim-opacity': '1'});
786+
});
787+
760788
it('restores the sheet when a context menu interrupts an active drag', () => {
761789
const onOpenChange = vi.fn();
762790
render(

‎packages/core/src/BottomSheet/BottomSheet.tsx‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,24 @@ function StandaloneBottomSheet({
354354
String(opacity),
355355
);
356356
}, []);
357+
// While the sheet's gesture or its release moves the dim, the backdrop
358+
// follows it with no easing of its own (the release plays it in lockstep
359+
// with the sheet). The pin is lifted only after a style flush has applied
360+
// the last value, so that value does not ease in again.
361+
const handleScrimTracking = useCallback((tracking: boolean) => {
362+
const dialog = dialogRef.current;
363+
if (dialog == null) {
364+
return;
365+
}
366+
if (tracking) {
367+
dialog.style.setProperty('--_sheet-scrim-duration', '0s');
368+
return;
369+
}
370+
if (typeof getComputedStyle === 'function') {
371+
void getComputedStyle(dialog, '::backdrop').opacity;
372+
}
373+
dialog.style.removeProperty('--_sheet-scrim-duration');
374+
}, []);
357375

358376
useEffect(() => {
359377
const dialog = dialogRef.current;
@@ -495,6 +513,7 @@ function StandaloneBottomSheet({
495513
xstyle={xstyle}
496514
onDismiss={dismissOnLightInteraction}
497515
onScrimOpacity={handleScrimOpacity}
516+
onScrimTracking={hasScrim ? handleScrimTracking : undefined}
498517
onElementChange={handlePanelElementChange}
499518
onMotionComplete={handleMotionComplete}>
500519
<LayerDepthProvider>{children}</LayerDepthProvider>

‎packages/core/src/BottomSheet/BottomSheetPanel.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ interface BottomSheetPanelProps extends BaseProps<HTMLDivElement> {
313313
isPageScrollLocked?: boolean;
314314
onDismiss: () => void;
315315
onScrimOpacity: (opacity: number) => void;
316+
/** Given by a host whose scrim is the dialog's ::backdrop; see useSheetGestures. */
317+
onScrimTracking?: (tracking: boolean) => void;
316318
onElementChange?: (element: HTMLDivElement | null) => void;
317319
onMotionStart?: (motion: BottomSheetPanelMotion) => void;
318320
onMotionComplete?: (motion: BottomSheetPanelMotion) => void;
@@ -444,6 +446,7 @@ export function BottomSheetPanel({
444446
isPageScrollLocked = false,
445447
onDismiss,
446448
onScrimOpacity,
449+
onScrimTracking,
447450
onElementChange,
448451
onMotionStart,
449452
onMotionComplete,
@@ -533,6 +536,7 @@ export function BottomSheetPanel({
533536
onDismiss,
534537
snapHeights,
535538
onScrimOpacity,
539+
onScrimTracking,
536540
});
537541

538542
const {getViewportProps, getContentProps} = useScrollableArea({

‎packages/core/src/BottomSheet/__tests__/BottomSheetDetent.interaction.browser.spec.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
readLongTasks,
4545
releaseAnimations,
4646
releaseVelocity,
47+
seekAnimations,
4748
springStep,
4849
startScreencast,
4950
thawTimers,
@@ -315,6 +316,65 @@ test('pulled up past the tallest detent, the sheet holds', async ({page}) => {
315316
expect(await sheetTop(sheet)).toBeCloseTo(tallTop, 0);
316317
});
317318

319+
test('the dim follows the sheet: at once under the finger, in lockstep with the release', async ({
320+
page,
321+
}) => {
322+
const {driver, sheet, tallTop, halfTop, handle} = await openSheet(page);
323+
const dialog = page.getByRole('dialog', {name: 'Nearby places'});
324+
const viewportHeight = page.viewportSize()?.height ?? 0;
325+
// UIKit's dim: full at the shortest detent and above, linear in the sheet's
326+
// top below it, clear when the top reaches the bottom of the screen.
327+
const dimAt = (top: number) =>
328+
Math.min(
329+
1,
330+
Math.max(0, (viewportHeight - top) / (viewportHeight - halfTop)),
331+
);
332+
const backdropOpacity = async () =>
333+
dialog.evaluate(el => Number(getComputedStyle(el, '::backdrop').opacity));
334+
// Down to the half detent first.
335+
await driver.play(slowDragTo(handle, Math.round((halfTop - tallTop) * 0.8)));
336+
await waitForRest(sheet);
337+
expect(await sheetTop(sheet)).toBeCloseTo(halfTop, 0);
338+
expect(await backdropOpacity()).toBeCloseTo(1, 2);
339+
// 100 px below it, slowly: the dim follows every move at once.
340+
const fromHalf = {x: handle.x, y: halfTop + 12};
341+
const tracked: {top: number; dim: number}[] = [];
342+
await driver.play(
343+
drag(
344+
fromHalf,
345+
{x: fromHalf.x, y: fromHalf.y + 100},
346+
{durationMs: 400, hz: 60, restMs: 150},
347+
),
348+
{
349+
onEvent: async step => {
350+
if (step.type === 'touchMove') {
351+
await frames(page, 1);
352+
tracked.push({
353+
top: await sheetTop(sheet),
354+
dim: await backdropOpacity(),
355+
});
356+
}
357+
},
358+
},
359+
);
360+
expect(tracked.at(-1)?.top).toBeGreaterThan(halfTop + 90);
361+
for (const {top, dim} of tracked) {
362+
expect(dim).toBeCloseTo(dimAt(top), 2);
363+
}
364+
// Released at 250 px/s, short of halfway to gone: back to the half detent,
365+
// the dim riding the same spring, each moment's dim the one for where the
366+
// sheet is.
367+
await holdAnimations(page, dialog);
368+
for (const t of [0, 33, 83, 150, 250, 400]) {
369+
await seekAnimations(page, t);
370+
const top = await sheetTop(sheet);
371+
expect(await backdropOpacity(), `t=${t} top=${top}`).toBeCloseTo(
372+
dimAt(top),
373+
2,
374+
);
375+
}
376+
});
377+
318378
test('a downward flick lands on the next detent, and from there dismisses', async ({
319379
page,
320380
}) => {

‎packages/core/src/BottomSheet/snapOffsets.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,9 @@ export function peekOffsetFor(
188188
* The scrim is full down to the shortest stop that is still a working surface,
189189
* and only thins past it — onto a peek, where it holds at
190190
* `MIN_PEEK_SCRIM_OPACITY` (a glance state, and the sheet is still modal), or
191-
* out through the dismiss overshoot toward `dismissOffset`, where it clears
192-
* completely because the sheet is leaving.
191+
* out toward `dismissOffset`, linearly, clearing completely there. Pass the
192+
* dismissed position (the sheet's top at the bottom of the screen): UIKit's
193+
* dim is linear in the sheet's position from its shortest detent to there.
193194
*
194195
* So a sheet whose stops are all working heights keeps a full scrim at every
195196
* one of them; only the sliver of a peek, or a sheet on its way out, dims less.

0 commit comments

Comments
 (0)