Repository navigation
fix(Dialog, Drawer): a backdrop press dismisses only when it starts and ends on the backdrop - #9613
Conversation
…nd ends on the backdrop #8964. Dialog (purpose="info"), Drawer's scrim and CommandPalette (through Dialog) closed on any click whose target was the <dialog>. A press that starts inside and ends on the backdrop (a text selection or a slider drag released past the edge) also produces that click, because the browser sends it to the common ancestor, so the dialog closed and the input was lost. Specs' ruling, one shared path: Layer/useBackdropPress records whether the pointer-down landed on the backdrop and dismisses on the click only if it did; Dialog and Drawer both use it (CommandPalette inherits it). Record: family:overlay-dismissal FR10 states the rule, AV4 points to it, and its evidence row names the tests; Drawer FR6 says the press starts and ends there. Tests: useBackdropPress.test.tsx for all three members, mouse and touch (12 of 18 fail before), existing backdrop tests now press down first, and a Chromium spec with a real drag out of each member and a mouse click and finger tap on the backdrop.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
|
/approve-spec e9e455b |
PR Analysis Report🔎 Preview ·
|
There was a problem hiding this comment.
Changes requested: presses that start inside and end on the backdrop now leave the dialog open, but a press that starts on the backdrop and ends inside still closes Dialog, Drawer and CommandPalette, because useBackdropPress never checks where the press ended.
- Record the pointer-up target too, as
watchOutsidePressinLayer/outsidePress.tsdoes, clear it onpointercancel, and add a backdrop-to-inside case to the unit test, the Chromium spec and the FR10 evidence row. - FR10 and AV4 read as a rule for every member with backdrop dismissal, but BottomSheet, BottomSheetSwitcher, MobileNav and Lightbox keep their own handlers and MobileNav FR4 is unchanged; name the three members in FR10, or move the others onto the hook.
[Automated review]
| if ( | ||
| isEnabled && | ||
| startedOnBackdrop && | ||
| event.target === event.currentTarget | ||
| ) { | ||
| onBackdropPress(); | ||
| } |
There was a problem hiding this comment.
A press that starts on the backdrop and ends inside the dialog also produces a click on the <dialog> (the common ancestor), so this still dismisses. In Chromium on this head, pointer-down on the dialog, pointer-up on the field and click on the dialog closes Dialog, Drawer and CommandPalette. Record the pointer-up target as well and require both here, clearing both on click and on pointercancel, as watchOutsidePress in outsidePress.ts does.
| - **FR10 — A backdrop press starts and ends on the backdrop.** A member that | ||
| dismisses on a press on its modal backdrop (a Dialog with `purpose="info"`, | ||
| a Drawer's scrim, a CommandPalette through its Dialog) dismisses only when the | ||
| press both started and ended on the backdrop. A press that starts inside the | ||
| member and ends on the backdrop, such as a text selection or a slider drag | ||
| released past the edge, does not dismiss it. The rule is the same for a | ||
| mouse, a pen, and a finger, and the members share one implementation. |
There was a problem hiding this comment.
This reads as a rule for every member that dismisses on a backdrop press. BottomSheet, BottomSheetSwitcher, MobileNav and Lightbox do that with their own target === currentTarget handlers and are not changed, and component:MobileNav FR4 still says a click on the overlay requests close. Name the three members this pull request changes as the subject, or move the other four onto useBackdropPress and amend MobileNav FR4.
| // A press on the ::backdrop targets the <dialog> itself, so native browser | ||
| // popups (date pickers, selects, color pickers) that render outside the | ||
| // dialog's bounding rect. | ||
| const handleClick = (event: React.MouseEvent<HTMLDialogElement>) => { | ||
| if (event.target === event.currentTarget && allowBackdropClick) { | ||
| onOpenChange(false); | ||
| } | ||
| }; | ||
| // dialog's rect never count. The press must start and end there. |
There was a problem hiding this comment.
Non-blocking (INV29): the first sentence runs 26 words over two topics, and the last sentence is stronger than what the hook checks. Suggested: "A press on the ::backdrop targets the itself. A native popup outside the dialog's rect is not the backdrop."
…FR10 names its members CodeBunny on #9613: a press that started on the backdrop and ended inside still closed the members, because only the pointer-down was checked. useBackdropPress now records the pointer-up too (as Layer/outsidePress.ts does) and clears both on pointercancel; Dialog and Drawer wire the two new handlers. FR10 names Dialog (purpose="info"), Drawer's scrim and CommandPalette instead of reading as a rule for every member with a backdrop, and AV4 points to it for those members; Drawer FR6 checks the pointer-up. Tests gain backdrop-to-inside and cancelled-pointer cases (unit, mouse and touch) and a backdrop-to-inside Chromium drag.
|
/approve-spec 984947d |
There was a problem hiding this comment.
Changes requested: a press that crosses the edge either way now leaves Dialog, Drawer and CommandPalette open, and a press on the backdrop alone still closes them. One fix remains: Drawer now wraps a caller's onPointerDown, onPointerUp and onPointerCancel in composeEventHandlers, so they stop running whenever a descendant (a TextInput clear button, a NumberInput stepper) has called preventDefault(), which Drawer FR11's forwarding of unclaimed props does not allow. Chaining those three with chainEventHandlers, as Dialog already does, plus a Drawer test where a child prevents default on pointerdown, resolves it.
[Automated review]
| onPointerDown: composeEventHandlers( | ||
| scrimPress.onPointerDown, | ||
| safeProps.onPointerDown as PointerHandler | undefined, | ||
| ), | ||
| onPointerUp: composeEventHandlers( | ||
| scrimPress.onPointerUp, | ||
| safeProps.onPointerUp as PointerHandler | undefined, | ||
| ), | ||
| onPointerCancel: composeEventHandlers( | ||
| scrimPress.onPointerCancel, | ||
| safeProps.onPointerCancel as PointerHandler | undefined, | ||
| ), |
There was a problem hiding this comment.
composeEventHandlers returns once event.defaultPrevented is true, so a caller's onPointerDown, onPointerUp and onPointerCancel no longer run for a press on a child that prevented default (a TextInput clear button, a NumberInput stepper). Before this change they rode {...safeProps} into layerClickBoundary and always ran, as Drawer FR11 ("forwards remaining unclaimed props") requires. chainEventHandlers(scrimPress.onPointerDown, safeProps.onPointerDown) (and the same for up and cancel), as Dialog does, keeps the hook first and always forwards.
…ented default CodeBunny on #9613: composeEventHandlers stops at defaultPrevented, so a caller's onPointerDown/Up/Cancel no longer ran for a press on a child such as a clear button, which Drawer FR11's forwarding does not allow. Chain them with chainEventHandlers, as Dialog does; the backdrop press still records first.
|
/approve-spec 5d4f6c9 |
There was a problem hiding this comment.
Changes requested: a press that crosses the edge of a Dialog, Drawer or CommandPalette now leaves it open in Chromium, and a press on the backdrop alone still closes it, but two things block approval.
useBackdropPressrecords the press with bubble-phase handlers, which never see a press inside a nested layer such as a Popover open in the dialog, so a press from the backdrop into that Popover leaves its start recorded and the next press from the Popover to the backdrop closes the Dialog or Drawer; recording the press in the capture phase, clearing it on click, and adding a nested-Popover test resolves it.- The changeset says dragging a slider out of the dialog no longer closes it, but no test drags a slider, and in Chromium neither
Slidernor a native range input closed a Dialog on the base; drop that clause or narrow it to what a test reproduces.
The Drawer pointer-handler forwarding from the last round is fixed.
[Automated review]
| const onPointerDown = useCallback((event: PointerEvent<HTMLElement>) => { | ||
| startedOnBackdropRef.current = event.target === event.currentTarget; | ||
| endedOnBackdropRef.current = false; | ||
| }, []); | ||
|
|
||
| const onPointerUp = useCallback((event: PointerEvent<HTMLElement>) => { | ||
| endedOnBackdropRef.current = | ||
| startedOnBackdropRef.current && event.target === event.currentTarget; | ||
| startedOnBackdropRef.current = false; | ||
| }, []); |
There was a problem hiding this comment.
A press that starts inside a nested layer (a Popover, a menu, a Selector list) stops at that layer's root in the React tree, so these bubble-phase handlers never see it. After a press from the backdrop into an open Popover, startedOnBackdropRef stays true, and the next press from the Popover to the backdrop closes the dialog. In Chromium, a drag from a Popover field to the backdrop leaves a Dialog open, but closes it right after one drag from the backdrop into that field. Recording in the capture phase (onPointerDownCapture, onPointerUpCapture, onPointerCancelCapture), as watchOutsidePress does, and clearing both refs in onClick avoids it.
| // Where the press started is recorded first. The caller's handler | ||
| // always runs after it, also when a child prevented default. |
There was a problem hiding this comment.
Non-blocking (INV29): passive voice. Suggested: "The hook records where the press started first. The caller's handler then runs, even when a child prevented default."
# Conflicts: # packages/core/src/Drawer/Drawer.tsx
CodeBunny on #9613: a nested layer, such as a popover open in the dialog, stops its presses at its own root, so bubble-phase handlers missed a press into or out of it; a drag from the backdrop into the popover left its start recorded, and the next press from the popover to the backdrop closed the dialog. useBackdropPress now returns capture-phase pointer handlers, and a click clears both records. Dialog and Drawer set them as capture props and chain the caller's capture handlers after them. A nested-layer test covers Dialog and Drawer. The changeset drops the slider clause, which no test reproduces.
There was a problem hiding this comment.
This is ready apart from one spec decision for the current head; no code change is needed.
- The record changes (
family:overlay-dismissalFR10, its AV4 pointer and evidence row, and thecomponent:DrawerFR6 rewrite) add requirements the current specs do not state, and the latest commit cleared the earlier spec approval: an engineering owner records the decision by commenting/approve-spec <full head SHA>on this pull request, which the author, as an engineering owner, may do.
Both issues from the last round are fixed: the press is now recorded in the capture phase, so a press through a popover inside the dialog no longer carries over, and the changeset no longer claims a slider fix. One optional comment rewrite is suggested inline.
[Automated review]
| // The hook records where the press started first. The caller's capture | ||
| // handler then runs, also when a child prevented default. |
There was a problem hiding this comment.
Non-blocking: a capture handler on the root runs before any child's handler, so "also when a child prevented default" cannot happen here. Suggested: "The hook records the press before the caller's capture handler runs."
|
/approve-spec bd05bb3 |
There was a problem hiding this comment.
Ready to approve: a press that crosses the edge of a Dialog (purpose="info"), a Drawer's scrim or CommandPalette now leaves it open, a press on the backdrop alone still closes it, and the record changes are approved for this head.
Two optional comment rewrites from the earlier round remain open on Dialog.tsx and Drawer.tsx; neither blocks.
[Automated review]
MobilePowerSearch and MobileTokenizer press a confirmation Dialog's scrim with a bare click. A backdrop press now dismisses only when its pointer-down and pointer-up both land on the backdrop, so the tests send them first, as a real press does.
There was a problem hiding this comment.
Changes requested: the backdrop fix works and the record changes are approved for this head, but it leaves two existing tests failing.
packages/lab/src/MobilePowerSearch/MobilePowerSearch.test.tsx("returns to Filters unchanged when the clear confirmation scrim is pressed") andpackages/lab/src/MobileTokenizer/MobileTokenizer.test.tsx("guards dirty Escape, scrim, and swipe dismissal…") close apurpose="info"confirmation Dialog with a barefireEvent.click, which no longer dismisses under the new rule, so both fail on this head and pass onmain; firingpointerDownandpointerUpon the dialog before each click, asDialog.test.tsxandDrawer.test.tsxnow do, fixes them.
Two optional comment rewrites from the earlier round remain open on Dialog.tsx and Drawer.tsx.
[Automated review]
There was a problem hiding this comment.
Ready to approve: a press that crosses the edge of a Dialog (purpose="info"), a Drawer's scrim or CommandPalette now leaves it open, a press on the backdrop alone still closes it, and the record changes are approved for this head.
The two Lab tests from the last round now press down and up before the click and pass. Two optional comment rewrites remain open on Dialog.tsx and Drawer.tsx; neither blocks.
[Automated review]
|
/approve-spec a645f6a |
Fixes #8964.
User impact
Someone works in a light-dismissable
Dialog(purpose="info"), aDrawerwith its scrim, orCommandPalette. They select text in a field and release the mouse a little past the dialog's edge. The dialog closes, and what they were doing is lost. The same happens with a pen.Problem and solution fit
clickwhose target was the<dialog>itself, on the assumption that clicks on content always target the content. But when a press starts on one element and ends on another, the browser sendsclickto their nearest common ancestor, which here is the<dialog>.Expected behavior and authority
purpose="info"), Drawer's scrim and CommandPalette through one shared path. It's recorded here asfamily:overlay-dismissalFR10, withcomponent:DrawerFR6 updated to match. Dialog's record already gives backdrop dismissal to the family.Smallest restoration
Layer/useBackdropPress: records onpointerdownwhether the press started on the<dialog>itself, and onpointerupwhether it also ended there.pointercancelclears both. Onclick, it dismisses only for a press that started and ended on the backdrop, then clears the record (asLayer/outsidePress.tsdoes for light dismiss).onClickand pointer handlers still run, in each component's existing order, and a caller's capture handlers run after the record.family:overlay-dismissalFR10 names its three members, and AV4 points to it for them. BottomSheet, MobileNav and Lightbox keep their own backdrop handling.useBackdropPress.test.tsxjoins itsverified_by.Evidence
useBackdropPress.test.tsxcovers all three members, each with a mouse and a finger. Its crossing, cancelled and carry-over cases fail on main: a press that crosses the edge either way dismisses, and so does a cancelled one.formandrequiredpurposes still ignore it, and content clicks never dismiss.BackdropPress.a11y.chromium.spec.tson a new no-visual story checks each member in Chromium:Scope
Testing
pnpm vitest run packages/core/src/Dialog packages/core/src/Drawer packages/core/src/CommandPalette packages/core/src/Layer: 715 passed.pnpm check:knowledge: aligned.Writing rubric results
Run over
family:overlay-dismissalFR10, AV4 and its evidence row, and Drawer FR6 (docs/contributing/spec-writing.md):