Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/bottom-sheet-flick-closes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[fix] BottomSheet: a touch release takes the iOS sheet's measured values, and a release moving back toward where the drag began cancels. A release faster than about 1,000 px/s is a flick: it goes at least to the next detent in its direction, so a short fast flick at the lowest detent closes the sheet instead of springing back. From a higher detent, a flick that would carry past the next detent goes on, so a hard flick closes the sheet and a softer one stops at the next detent. A slow or medium touch throw now travels less than before, and a slow touch drag closes the sheet only past halfway to closed. A sheet a touch drag closes leaves on its close spring, starting at the speed of the drag, and its scrim fades out with it at the theme's tempo. A mouse or pen drag is unchanged.

@vjeux
7 changes: 7 additions & 0 deletions .changeset/motion-settle-clock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[chore] Internal: the motion foundation's `resolveSettleMs` gives a spring role's settling time at the theme's tempo, so a CSS part that plays with the spring can start and land with it. Not exported from the package entry point.

@vjeux
1 change: 1 addition & 0 deletions docs/specs/AST-094-touch-presentations/motion-roles.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The owner's goal: "The main goals are consistent timings and well-coordinated pi
| `springs.menuOpen` | spring | response 0.32 s, damping ratio 0.8 | `spec:AST-094` FR4 |
| `springs.quickChange` | spring | duration 0.5 s, bounce 0.15 | `spec:AST-094` FR4 |
| `springs.settle` | spring | duration 0.5 s, bounce 0 | `spec:AST-094` FR4 |
| `springs.sheetDismissSwipe` | spring | response 0.3441442326 s, damping ratio 0.8 | `component:BottomSheet` |
| `springs.sliderDrop` | spring | response 0.5 s, damping ratio 0.7 | `component:Slider` |
| `springs.sliderLift` | spring | response 0.215 s, damping ratio 0.6 | `component:Slider` |
| `springs.sliderRelease` | spring | response 0.4 s, damping ratio 1 | `component:Slider` |
Expand Down
7 changes: 6 additions & 1 deletion docs/specs/AST-094-touch-presentations/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,12 @@ under a finger, and a mouse follows `module:DropdownMenu/useMenuPress` FR3 and
springs back. The decision MUST NOT change while the surface settles. The
same rule backs dismissing a BottomSheet, a Toast or a Lightbox, popping a
NavigationStack, and dismissing the keyboard; each use's commit point and
projection are measured values (FR1, FR2). A row swipe keeps
projection are measured values (FR1, FR2). A BottomSheet's touch release takes
UIKit's measured sheet values. Its projection is 0.1 s of the release
velocity, and its commit point is halfway from its lowest detent to closed. A
release past its measured flick speed lands on the first position at or past
its projection, at least its next one. So a short fast flick at its lowest
detent closes it (`component:BottomSheet` FR19). A row swipe keeps
`spec:AST-057`'s own release rule.
- **FR37 — Moving back cancels.** A release whose velocity points back
toward the start MUST cancel, whatever the travel so far.
Expand Down
47 changes: 45 additions & 2 deletions docs/specs/components/BottomSheet/BottomSheet.spec.md

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions packages/core/src/BottomSheet/BottomSheet.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,25 @@ function mockResizeObserverInstances(): ResizeObserverRecord[] {
return observers;
}

/**
* Lays a sheet out `height` px tall, as the browser's resize observer would.
* A touch release lands by the sheet's geometry, so a touch dismissal needs
* one.
*/
function layOutSheet(
observers: ResizeObserverRecord[],
sheet: HTMLElement,
height = 400,
) {
const observer = observers.find(instance => instance.observed.has(sheet));
act(() => {
observer?.callback(
[resizeEntry(height)],
observer as unknown as ResizeObserver,
);
});
}

function finishSheetExit() {
fireEvent.transitionEnd(getSheet(), {propertyName: 'transform'});
}
Expand Down Expand Up @@ -1112,6 +1131,7 @@ describe('BottomSheet', () => {
it("a sheet opened from inside another sheet's content drags to dismiss by its own handle, and the covered host neither drags nor takes the pointer (FR6, FR7)", () => {
const onInnerOpenChange = vi.fn();
const onOuterOpenChange = vi.fn();
const observers = mockResizeObserverInstances();
render(
<BottomSheet isOpen onOpenChange={onOuterOpenChange} label="Outer">
Outer content
Expand All @@ -1135,6 +1155,10 @@ describe('BottomSheet', () => {
const innerHandle = innerDialog.querySelector<HTMLElement>(
'.astryx-bottom-sheet > [aria-hidden="true"]',
)!;
layOutSheet(
observers,
innerDialog.querySelector<HTMLElement>('.astryx-bottom-sheet')!,
);
// `setPointerCapture` is a prototype stub here, so record who captures.
const captured: Element[] = [];
const capture = vi
Expand Down Expand Up @@ -4203,8 +4227,10 @@ describe('BottomSheet', () => {

it("a touch swipe dismissal under reduced motion fades out in place on a fine pointer (design:reduced-motion DR2, a touch drag's release)", () => {
stubReducedMotion(false);
const observers = mockResizeObserverInstances();
render(<Controlled />);
const dialog = screen.getByRole('dialog');
layOutSheet(observers, getSheet());
drag(getHandle(), [{y: 0}, {y: 40}, {y: 120}], 'touch');
// Exiting: a transform end does not close the dialog, the fade does.
fireEvent.transitionEnd(getSheet(), {propertyName: 'transform'});
Expand Down Expand Up @@ -4233,8 +4259,10 @@ describe('BottomSheet', () => {
</>
);
}
const observers = mockResizeObserverInstances();
render(<Reopenable />);
const dialog = screen.getByRole('dialog');
layOutSheet(observers, getSheet());
drag(getHandle(), [{y: 0}, {y: 40}, {y: 120}], 'touch');
fireEvent.transitionEnd(getSheet(), {propertyName: 'opacity'});
expect(dialog).not.toHaveAttribute('open');
Expand Down
54 changes: 54 additions & 0 deletions packages/core/src/BottomSheet/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,36 @@ function scrimOpacityForView(view: BottomSheetStackView): '0' | '1' {
return view.isStacked && !view.ownsScrim ? '0' : '1';
}

/**
* Writes the fade a touch drag's close paces on `dialog`'s scrim, or clears
* it when `ms` is null. `written` keeps the value the host wrote, so a clear
* removes only that value.
*/
function writeScrimFadeDuration(
dialog: HTMLDialogElement | null,
ms: number | null,
written: {current: string | null},
): void {
if (dialog == null) {
return;
}
const current = dialog.style.getPropertyValue('--_sheet-scrim-duration');
if (ms == null) {
// Only its own value: a stack's 0s pin in the meantime stays.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking (INV29 style): this is a fragment. Suggest:

// Remove only the value this host wrote. A stack's 0s pin written since then stays.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken at 5518789, as suggested.

if (written.current != null && current === written.current) {
dialog.style.removeProperty('--_sheet-scrim-duration');
}
written.current = null;
return;
}
// A stack's 0s pin hands the scrim over with no fade; it stays.
if (current === '0s') {
return;
}
written.current = `${ms}ms`;
dialog.style.setProperty('--_sheet-scrim-duration', written.current);
}

interface BottomSheetSharedProps extends BaseProps<HTMLDivElement> {
/** Ref forwarded to the visual sheet panel <div>. */
ref?: React.Ref<HTMLDivElement>;
Expand Down Expand Up @@ -544,6 +574,18 @@ function StandaloneBottomSheet({
String(opacity),
);
}, []);
// The fade duration a touch drag's close wrote, until that close ends.
const scrimFadeRef = useRef<string | null>(null);
const handleScrimFadeDuration = useCallback(
(panel: HTMLElement, ms: number | null) => {
const view = stackViewRef.current;
if (view.isStacked && !view.ownsScrim) {
return;
}
writeScrimFadeDuration(panel.closest('dialog'), ms, scrimFadeRef);
},
[],
);

// A sheet joins the registry for as long as it is presented. For a modal
// sheet the registry shows the dialog (in open order, repairing same-commit
Expand Down Expand Up @@ -946,6 +988,7 @@ function StandaloneBottomSheet({
xstyle={xstyle}
onDismiss={dismissOnLightInteraction}
onScrimOpacity={handleScrimOpacity}
onScrimFadeDuration={handleScrimFadeDuration}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only the standalone host passes this. SwitcherBottomSheetItem's panel (around line 1326) plays the same drag exit with no fade handler, so after a touch drag closes a switcher flow the scrim clears on the stylesheet's 300 ms while the dialog stays up about 730 ms; in Chromium a tap on the page 450 ms after the release is lost (spec:AST-108 FR4a). Passing the same handler for the scrim-owning step, or having the panel write the fade on its own dialog, would cover both hosts.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 7ab8a89. The switcher step passes the same fade handler, and both hosts share one writer. A drag-closed switcher flow's scrim now fades with its 710 ms close instead of 300 ms (a new drag-exit unit case and a Chromium case cover it).

onElementChange={handlePanelElementChange}
onMotionStart={handleMotionStart}
onMotionComplete={handleMotionComplete}>
Expand Down Expand Up @@ -1095,6 +1138,16 @@ function SwitcherBottomSheetItem({
},
[hasValidSheetId, onSheetScrimOpacityChange, sheetId],
);
// The fade a touch drag's close wrote on this step's dialog, until that
// close ends. The step a drag closes becomes the flow's retained step, so
// its backdrop is the flow's scrim.
const scrimFadeRef = useRef<string | null>(null);
const handleScrimFadeDuration = useCallback(
(panel: HTMLElement, ms: number | null) => {
writeScrimFadeDuration(panel.closest('dialog'), ms, scrimFadeRef);
},
[],
);

useLayoutEffect(() => {
if (!hasValidSheetId) {
Expand Down Expand Up @@ -1297,6 +1350,7 @@ function SwitcherBottomSheetItem({
xstyle={xstyle}
onDismiss={dismissOnSwipe}
onScrimOpacity={handleScrimOpacity}
onScrimFadeDuration={handleScrimFadeDuration}
onElementChange={handlePanelElementChange}
onMotionStart={handleMotionStart}
onMotionComplete={handleMotionComplete}>
Expand Down
Loading