Skip to content

fix(modal/drawer): prevent modal/drawer from closing on press instead…#5623

Closed
anshumandev2025 wants to merge 4 commits into
heroui-inc:canaryfrom
anshumandev2025:fix/modal-drawer-close-on-press
Closed

fix(modal/drawer): prevent modal/drawer from closing on press instead…#5623
anshumandev2025 wants to merge 4 commits into
heroui-inc:canaryfrom
anshumandev2025:fix/modal-drawer-close-on-press

Conversation

@anshumandev2025
Copy link
Copy Markdown

@anshumandev2025 anshumandev2025 commented Aug 23, 2025

Closes #5616

📝 Description

This PR fixes a bug where the Modal/Drawer was closing on press instead of on press release, resulting in an unintended and abrupt user experience.

Root Cause

The issue was caused by the onHide function being triggered inside the onInteractOutsideStart event within useAriaOverlay. This event fires as soon as the user presses outside the modal, rather than waiting for the full press interaction (press + release), which led to the modal closing prematurely.

Solution

To align with expected behaviour—where a modal or drawer should only close after the complete interaction (i.e. press and release)—the onHide call has been removed from onInteractOutsideStart.

Summary by CodeRabbit

  • Bug Fixes

    • Overlays (modals, drawers) no longer close on outside press start; they now dismiss only after the outside interaction completes, avoiding premature closures. Event behavior on interaction end remains unchanged. No public API changes.
  • Tests

    • Added tests verifying overlays close only on release (not on press start) for modal and drawer.
  • Chores

    • Patch release entry added for the overlay fix.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Aug 23, 2025

🦋 Changeset detected

Latest commit: fe85fd1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
@heroui/use-aria-overlay Patch
@heroui/popover Patch
@heroui/tooltip Patch
@heroui/use-aria-modal-overlay Patch
@heroui/autocomplete Patch
@heroui/date-picker Patch
@heroui/dropdown Patch
@heroui/select Patch
@heroui/react Patch
@heroui/drawer Patch
@heroui/slider Patch
@heroui/snippet Patch
@heroui/modal Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link
Copy Markdown

vercel Bot commented Aug 23, 2025

@anshumandev2025 is attempting to deploy a commit to the HeroUI Inc Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 23, 2025

Walkthrough

Removed immediate closure on interaction start for overlays: onHide() is no longer invoked during onInteractOutsideStart for modal/drawer elements; overlays still call onHide() on onInteractOutside (release). No public API changes.

Changes

Cohort / File(s) Summary
Overlay interaction handling
packages/hooks/use-aria-overlay/src/index.ts
Added internal helper to infer overlay type from a ref and guard the onHide() call in onInteractOutsideStart so modals/drawers do not close on press-start; onInteractOutside still triggers onHide(). No public API changes.
Tests: modal & drawer
packages/components/modal/__tests__/modal.test.tsx, packages/components/drawer/__tests__/drawer.test.tsx
Added tests asserting overlays do not close on press-start but do close on release (simulate pointer down + up).
Changelog / release
.changeset/shy-garlics-peel.md
Added a patch changeset describing the fix for press-start closing behavior.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User as Pointer/Touch
  participant Overlay as Overlay component
  participant Hook as use-aria-overlay handlers

  User->>Overlay: Press down outside
  Overlay->>Hook: onInteractOutsideStart(event)
  Note right of Hook #d3f9d8: onHide() skipped for modal/drawer on start
  Hook-->>Overlay: (may stopPropagation/preventDefault)

  User-->>Overlay: Release outside
  Overlay->>Hook: onInteractOutside(event)
  Note right of Hook #cfe8ff: onHide() invoked on release (close)
  Hook->>Overlay: onHide()
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Assessment against linked issues

Objective Addressed Explanation
Modals/Drawer close only on press release, not on press start (#5616)

Possibly related PRs

Suggested reviewers

  • wingkwong
  • jrgarciadev

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between fe85fd1 and ed5be13.

📒 Files selected for processing (1)
  • packages/hooks/use-aria-overlay/src/index.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/hooks/use-aria-overlay/src/index.ts
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Member

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

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

  1. please add changeset
  2. please add test case

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Aug 23, 2025

Open in StackBlitz

@heroui/accordion

npm i https://pkg.pr.new/@heroui/accordion@5623

@heroui/alert

npm i https://pkg.pr.new/@heroui/alert@5623

@heroui/autocomplete

npm i https://pkg.pr.new/@heroui/autocomplete@5623

@heroui/avatar

npm i https://pkg.pr.new/@heroui/avatar@5623

@heroui/badge

npm i https://pkg.pr.new/@heroui/badge@5623

@heroui/breadcrumbs

npm i https://pkg.pr.new/@heroui/breadcrumbs@5623

@heroui/button

npm i https://pkg.pr.new/@heroui/button@5623

@heroui/calendar

npm i https://pkg.pr.new/@heroui/calendar@5623

@heroui/card

npm i https://pkg.pr.new/@heroui/card@5623

@heroui/checkbox

npm i https://pkg.pr.new/@heroui/checkbox@5623

@heroui/chip

npm i https://pkg.pr.new/@heroui/chip@5623

@heroui/code

npm i https://pkg.pr.new/@heroui/code@5623

@heroui/date-input

npm i https://pkg.pr.new/@heroui/date-input@5623

@heroui/date-picker

npm i https://pkg.pr.new/@heroui/date-picker@5623

@heroui/divider

npm i https://pkg.pr.new/@heroui/divider@5623

@heroui/drawer

npm i https://pkg.pr.new/@heroui/drawer@5623

@heroui/dropdown

npm i https://pkg.pr.new/@heroui/dropdown@5623

@heroui/form

npm i https://pkg.pr.new/@heroui/form@5623

@heroui/image

npm i https://pkg.pr.new/@heroui/image@5623

@heroui/input

npm i https://pkg.pr.new/@heroui/input@5623

@heroui/input-otp

npm i https://pkg.pr.new/@heroui/input-otp@5623

@heroui/kbd

npm i https://pkg.pr.new/@heroui/kbd@5623

@heroui/link

npm i https://pkg.pr.new/@heroui/link@5623

@heroui/listbox

npm i https://pkg.pr.new/@heroui/listbox@5623

@heroui/menu

npm i https://pkg.pr.new/@heroui/menu@5623

@heroui/modal

npm i https://pkg.pr.new/@heroui/modal@5623

@heroui/navbar

npm i https://pkg.pr.new/@heroui/navbar@5623

@heroui/number-input

npm i https://pkg.pr.new/@heroui/number-input@5623

@heroui/pagination

npm i https://pkg.pr.new/@heroui/pagination@5623

@heroui/popover

npm i https://pkg.pr.new/@heroui/popover@5623

@heroui/progress

npm i https://pkg.pr.new/@heroui/progress@5623

@heroui/radio

npm i https://pkg.pr.new/@heroui/radio@5623

@heroui/ripple

npm i https://pkg.pr.new/@heroui/ripple@5623

@heroui/scroll-shadow

npm i https://pkg.pr.new/@heroui/scroll-shadow@5623

@heroui/select

npm i https://pkg.pr.new/@heroui/select@5623

@heroui/skeleton

npm i https://pkg.pr.new/@heroui/skeleton@5623

@heroui/slider

npm i https://pkg.pr.new/@heroui/slider@5623

@heroui/snippet

npm i https://pkg.pr.new/@heroui/snippet@5623

@heroui/spacer

npm i https://pkg.pr.new/@heroui/spacer@5623

@heroui/spinner

npm i https://pkg.pr.new/@heroui/spinner@5623

@heroui/switch

npm i https://pkg.pr.new/@heroui/switch@5623

@heroui/table

npm i https://pkg.pr.new/@heroui/table@5623

@heroui/tabs

npm i https://pkg.pr.new/@heroui/tabs@5623

@heroui/toast

npm i https://pkg.pr.new/@heroui/toast@5623

@heroui/tooltip

npm i https://pkg.pr.new/@heroui/tooltip@5623

@heroui/user

npm i https://pkg.pr.new/@heroui/user@5623

@heroui/react

npm i https://pkg.pr.new/@heroui/react@5623

@heroui/system

npm i https://pkg.pr.new/@heroui/system@5623

@heroui/system-rsc

npm i https://pkg.pr.new/@heroui/system-rsc@5623

@heroui/theme

npm i https://pkg.pr.new/@heroui/theme@5623

@heroui/use-aria-accordion

npm i https://pkg.pr.new/@heroui/use-aria-accordion@5623

@heroui/use-aria-accordion-item

npm i https://pkg.pr.new/@heroui/use-aria-accordion-item@5623

@heroui/use-aria-button

npm i https://pkg.pr.new/@heroui/use-aria-button@5623

@heroui/use-aria-link

npm i https://pkg.pr.new/@heroui/use-aria-link@5623

@heroui/use-aria-modal-overlay

npm i https://pkg.pr.new/@heroui/use-aria-modal-overlay@5623

@heroui/use-aria-multiselect

npm i https://pkg.pr.new/@heroui/use-aria-multiselect@5623

@heroui/use-aria-overlay

npm i https://pkg.pr.new/@heroui/use-aria-overlay@5623

@heroui/use-callback-ref

npm i https://pkg.pr.new/@heroui/use-callback-ref@5623

@heroui/use-clipboard

npm i https://pkg.pr.new/@heroui/use-clipboard@5623

@heroui/use-data-scroll-overflow

npm i https://pkg.pr.new/@heroui/use-data-scroll-overflow@5623

@heroui/use-disclosure

npm i https://pkg.pr.new/@heroui/use-disclosure@5623

@heroui/use-draggable

npm i https://pkg.pr.new/@heroui/use-draggable@5623

@heroui/use-form-reset

npm i https://pkg.pr.new/@heroui/use-form-reset@5623

@heroui/use-image

npm i https://pkg.pr.new/@heroui/use-image@5623

@heroui/use-infinite-scroll

npm i https://pkg.pr.new/@heroui/use-infinite-scroll@5623

@heroui/use-intersection-observer

npm i https://pkg.pr.new/@heroui/use-intersection-observer@5623

@heroui/use-is-mobile

npm i https://pkg.pr.new/@heroui/use-is-mobile@5623

@heroui/use-is-mounted

npm i https://pkg.pr.new/@heroui/use-is-mounted@5623

@heroui/use-measure

npm i https://pkg.pr.new/@heroui/use-measure@5623

@heroui/use-pagination

npm i https://pkg.pr.new/@heroui/use-pagination@5623

@heroui/use-real-shape

npm i https://pkg.pr.new/@heroui/use-real-shape@5623

@heroui/use-ref-state

npm i https://pkg.pr.new/@heroui/use-ref-state@5623

@heroui/use-resize

npm i https://pkg.pr.new/@heroui/use-resize@5623

@heroui/use-safe-layout-effect

npm i https://pkg.pr.new/@heroui/use-safe-layout-effect@5623

@heroui/use-scroll-position

npm i https://pkg.pr.new/@heroui/use-scroll-position@5623

@heroui/use-ssr

npm i https://pkg.pr.new/@heroui/use-ssr@5623

@heroui/use-theme

npm i https://pkg.pr.new/@heroui/use-theme@5623

@heroui/use-update-effect

npm i https://pkg.pr.new/@heroui/use-update-effect@5623

@heroui/use-viewport-size

npm i https://pkg.pr.new/@heroui/use-viewport-size@5623

@heroui/aria-utils

npm i https://pkg.pr.new/@heroui/aria-utils@5623

@heroui/dom-animation

npm i https://pkg.pr.new/@heroui/dom-animation@5623

@heroui/framer-utils

npm i https://pkg.pr.new/@heroui/framer-utils@5623

@heroui/react-rsc-utils

npm i https://pkg.pr.new/@heroui/react-rsc-utils@5623

@heroui/react-utils

npm i https://pkg.pr.new/@heroui/react-utils@5623

@heroui/shared-icons

npm i https://pkg.pr.new/@heroui/shared-icons@5623

@heroui/shared-utils

npm i https://pkg.pr.new/@heroui/shared-utils@5623

@heroui/stories-utils

npm i https://pkg.pr.new/@heroui/stories-utils@5623

@heroui/test-utils

npm i https://pkg.pr.new/@heroui/test-utils@5623

commit: fe85fd1

@anshumandev2025
Copy link
Copy Markdown
Author

Thanks for you suggestion @wingkwong i added the changeset and testcases.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (3)
.changeset/shy-garlics-peel.md (1)

5-7: Tighten phrasing and clarify interaction type.

Minor copy edit to be more explicit and formal; also prefer “press + release” or “pointerdown/pointerup” over “press start”.

Apply this diff:

-fix: prevent premature closing of modal/drawer on press start
+fix: prevent premature closing of modal/drawer on pointer down
@@
-Removes `onHide()` from `onInteractOutsideStart` to fix an issue where modals and drawers closed as soon as the user pressed outside them, instead of after a full press and release.
+Removes calling `onHide()` in `onInteractOutsideStart`, resolving an issue where modals and drawers closed on initial outside press instead of after the full interaction (press + release).
packages/components/modal/__tests__/modal.test.tsx (1)

240-241: Optionally guard for async scheduling with waitFor.

If the close handler is scheduled (microtask/raf), wrap the final assertion in waitFor.

Within this test:

-    expect(onClose).toHaveBeenCalledTimes(1);
+    await waitFor(() => expect(onClose).toHaveBeenCalledTimes(1));

And add the import at the top of the file:

import {render, fireEvent, waitFor} from "@testing-library/react";
packages/components/drawer/__tests__/drawer.test.tsx (1)

135-136: Optionally use waitFor for stability.

Mirror the optional async guard from the Modal test.

-    expect(onClose).toHaveBeenCalledTimes(1);
+    await waitFor(() => expect(onClose).toHaveBeenCalledTimes(1));

Top-level import:

import {render, fireEvent, waitFor} from "@testing-library/react";
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 1d7e6e5 and fe85fd1.

📒 Files selected for processing (3)
  • .changeset/shy-garlics-peel.md (1 hunks)
  • packages/components/drawer/__tests__/drawer.test.tsx (1 hunks)
  • packages/components/modal/__tests__/modal.test.tsx (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/components/modal/__tests__/modal.test.tsx (1)
packages/components/modal/src/index.ts (5)
  • Modal (24-24)
  • ModalContent (24-24)
  • ModalHeader (24-24)
  • ModalBody (24-24)
  • ModalFooter (24-24)
🪛 LanguageTool
.changeset/shy-garlics-peel.md

[style] ~7-~7: Consider using a different verb for a more formal wording.
Context: ...ide()fromonInteractOutsideStart` to fix an issue where modals and drawers close...

(FIX_RESOLVE)

🔇 Additional comments (1)
.changeset/shy-garlics-peel.md (1)

1-7: No action needed: Changesets will patch all downstream packages

The .changeset/config.json is configured with

"updateInternalDependencies": "patch"

which ensures that when @heroui/use-aria-overlay is bumped, all internal dependents receive a patch version bump. In this repo, direct dependents include:

  • @heroui/popover
  • @heroui/tooltip
  • @heroui/use-aria-modal-overlay

Because changesets cascades these updates, @heroui/modal (which depends on use-aria-modal-overlay) and then @heroui/drawer (which depends on modal) will also be bumped automatically. No manual version bumps are required.

Comment on lines +114 to +136
test("should not close drawer on press start outside, only on release", async () => {
const onClose = jest.fn();
const user = userEvent.setup();

render(
<>
<div data-testid="outside">Outside</div>
<Drawer isOpen onClose={onClose}>
<DrawerContent>
<DrawerHeader>Drawer header</DrawerHeader>
<DrawerBody>Drawer body</DrawerBody>
<DrawerFooter>Drawer footer</DrawerFooter>
</DrawerContent>
</Drawer>
</>,
);

// Simulate full click outside (press + release)
await user.pointer({keys: "[MouseLeft>]", target: document.body});
await user.pointer({keys: "[/MouseLeft]", target: document.body});

expect(onClose).toHaveBeenCalledTimes(1);
});
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.

⚠️ Potential issue

Same gap as Modal test: assert zero calls on press start and click an explicit outside node.

The test’s final “called once” assertion doesn’t guard against a premature call on press start. Add an assertion after the pointer down, and target the outside div instead of document.body to make intent unambiguous.

Apply this diff:

-    render(
+    const {getByTestId} = render(
       <>
         <div data-testid="outside">Outside</div>
         <Drawer isOpen onClose={onClose}>
           <DrawerContent>
             <DrawerHeader>Drawer header</DrawerHeader>
             <DrawerBody>Drawer body</DrawerBody>
             <DrawerFooter>Drawer footer</DrawerFooter>
           </DrawerContent>
         </Drawer>
       </>,
     );
 
-    // Simulate full click outside (press + release)
-    await user.pointer({keys: "[MouseLeft>]", target: document.body});
+    const outside = getByTestId("outside");
+    // Simulate full click outside (press + release)
+    await user.pointer({keys: "[MouseLeft>]", target: outside});
+    // Must not close on press start
+    expect(onClose).toHaveBeenCalledTimes(0);
-    await user.pointer({keys: "[/MouseLeft]", target: document.body});
+    await user.pointer({keys: "[/MouseLeft]", target: outside});
 
     expect(onClose).toHaveBeenCalledTimes(1);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
test("should not close drawer on press start outside, only on release", async () => {
const onClose = jest.fn();
const user = userEvent.setup();
render(
<>
<div data-testid="outside">Outside</div>
<Drawer isOpen onClose={onClose}>
<DrawerContent>
<DrawerHeader>Drawer header</DrawerHeader>
<DrawerBody>Drawer body</DrawerBody>
<DrawerFooter>Drawer footer</DrawerFooter>
</DrawerContent>
</Drawer>
</>,
);
// Simulate full click outside (press + release)
await user.pointer({keys: "[MouseLeft>]", target: document.body});
await user.pointer({keys: "[/MouseLeft]", target: document.body});
expect(onClose).toHaveBeenCalledTimes(1);
});
test("should not close drawer on press start outside, only on release", async () => {
const onClose = jest.fn();
const user = userEvent.setup();
const { getByTestId } = render(
<>
<div data-testid="outside">Outside</div>
<Drawer isOpen onClose={onClose}>
<DrawerContent>
<DrawerHeader>Drawer header</DrawerHeader>
<DrawerBody>Drawer body</DrawerBody>
<DrawerFooter>Drawer footer</DrawerFooter>
</DrawerContent>
</Drawer>
</>,
);
const outside = getByTestId("outside");
// Simulate full click outside (press + release)
await user.pointer({ keys: "[MouseLeft>]", target: outside });
// Must not close on press start
expect(onClose).toHaveBeenCalledTimes(0);
await user.pointer({ keys: "[/MouseLeft]", target: outside });
expect(onClose).toHaveBeenCalledTimes(1);
});
🤖 Prompt for AI Agents
In packages/components/drawer/__tests__/drawer.test.tsx around lines 114 to 136,
the test currently only asserts onClose was called once after a full click on
document.body, which doesn't ensure the handler wasn't invoked on the initial
pointer down and uses an ambiguous target; update the test to (1) use the
explicit outside div as the pointer target, (2) after the pointer down event
assert onClose has not been called, and (3) then issue the pointer up and assert
onClose was called exactly once.

Comment on lines +219 to +241
test("should not close modal on press start outside, only on release", async () => {
const onClose = jest.fn();
const user = userEvent.setup();

render(
<>
<div data-testid="outside">Outside</div>
<Modal isOpen onClose={onClose}>
<ModalContent>
<ModalHeader>Modal header</ModalHeader>
<ModalBody>Modal body</ModalBody>
<ModalFooter>Modal footer</ModalFooter>
</ModalContent>
</Modal>
</>,
);

// Simulate full click outside (press + release)
await user.pointer({keys: "[MouseLeft>]", target: document.body});
await user.pointer({keys: "[/MouseLeft]", target: document.body});

expect(onClose).toHaveBeenCalledTimes(1);
});
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.

⚠️ Potential issue

Test doesn’t prove “no close on press start” — it would pass even if close triggers on press.

Right now you only assert a single call after release. If onClose fires on press start and not on release, the test still passes (1 call total). Add an assertion immediately after pointer down to ensure zero calls, and target the explicit outside element for clarity (instead of document.body).

Apply this diff to strengthen the test:

-    render(
+    const {getByTestId} = render(
       <>
         <div data-testid="outside">Outside</div>
         <Modal isOpen onClose={onClose}>
           <ModalContent>
             <ModalHeader>Modal header</ModalHeader>
             <ModalBody>Modal body</ModalBody>
             <ModalFooter>Modal footer</ModalFooter>
           </ModalContent>
         </Modal>
       </>,
     );
 
-    // Simulate full click outside (press + release)
-    await user.pointer({keys: "[MouseLeft>]", target: document.body});
+    const outside = getByTestId("outside");
+    // Simulate full click outside (press + release)
+    await user.pointer({keys: "[MouseLeft>]", target: outside});
+    // Must not close on press start
+    expect(onClose).toHaveBeenCalledTimes(0);
-    await user.pointer({keys: "[/MouseLeft]", target: document.body});
+    await user.pointer({keys: "[/MouseLeft]", target: outside});
 
     expect(onClose).toHaveBeenCalledTimes(1);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
test("should not close modal on press start outside, only on release", async () => {
const onClose = jest.fn();
const user = userEvent.setup();
render(
<>
<div data-testid="outside">Outside</div>
<Modal isOpen onClose={onClose}>
<ModalContent>
<ModalHeader>Modal header</ModalHeader>
<ModalBody>Modal body</ModalBody>
<ModalFooter>Modal footer</ModalFooter>
</ModalContent>
</Modal>
</>,
);
// Simulate full click outside (press + release)
await user.pointer({keys: "[MouseLeft>]", target: document.body});
await user.pointer({keys: "[/MouseLeft]", target: document.body});
expect(onClose).toHaveBeenCalledTimes(1);
});
test("should not close modal on press start outside, only on release", async () => {
const onClose = jest.fn();
const user = userEvent.setup();
const { getByTestId } = render(
<>
<div data-testid="outside">Outside</div>
<Modal isOpen onClose={onClose}>
<ModalContent>
<ModalHeader>Modal header</ModalHeader>
<ModalBody>Modal body</ModalBody>
<ModalFooter>Modal footer</ModalFooter>
</ModalContent>
</Modal>
</>,
);
const outside = getByTestId("outside");
// Simulate full click outside (press + release)
await user.pointer({ keys: "[MouseLeft>]", target: outside });
// Must not close on press start
expect(onClose).toHaveBeenCalledTimes(0);
await user.pointer({ keys: "[/MouseLeft]", target: outside });
expect(onClose).toHaveBeenCalledTimes(1);
});
🤖 Prompt for AI Agents
In packages/components/modal/__tests__/modal.test.tsx around lines 219 to 241,
the test only asserts onClose after the full click so it would still pass if
onClose fired on press; change the test to (1) target the explicit outside
element (query by data-testid "outside") instead of document.body, (2) perform
the pointer down via user.pointer({keys: "[MouseLeft>]", target:
outsideElement}), then immediately assert
expect(onClose).not.toHaveBeenCalled(), and (3) perform the pointer up via
user.pointer({keys: "[/MouseLeft]", target: outsideElement}) and assert
expect(onClose).toHaveBeenCalledTimes(1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] - Modals/Drawer are closing 'on press' instead of 'on press release'

2 participants