Skip to content

fix(modal): ESC closes only the top-most modal - #3352

Merged
SudoUserReal merged 1 commit into
DouyinFE:releasefrom
dvd233:fix/modal-esc-top-only
Sep 15, 2026
Merged

SudoUserReal merged 1 commit into
DouyinFE:releasefrom
dvd233:fix/modal-esc-top-only

Conversation

@dvd233

@dvd233 dvd233 commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #3351.

Every ModalContent registers its own keydown listener on document. Since stopPropagation() cannot stop sibling listeners on the same node, a single ESC closed all open dialogs at once instead of only the top-most one.

Changes:

  • packages/semi-foundation/modal/modalContentFoundation.ts: keep a module-level stack of the registered handleKeyDown handlers (pushed on listener mount, removed on unmount). handleKeyDown now only closes when the current modal's handler is the top of the stack, so ESC closes dialogs one at a time.
  • packages/semi-ui/modal/__test__/modal.test.js: regression test — with two open modals, one ESC cancels only the inner (most recently mounted) one; after it unmounts, the next ESC reaches the outer one.

How to verify

  • New unit test esc closes only the top-most modal when multiple are open covers the stacked-modal behavior (fails before this change, passes after).
  • Behavior of closeOnEsc: false is unchanged — such modals never enter the stack.

Related Issue

Fixes #3351

Checklist

  • I have read the contribution guide
  • I have added/updated unit tests for the change
  • The change is a bug fix, not a breaking change

This PR was prepared with AI coding assistance; the reproduction analysis, patch, and verification were reviewed and run locally by me. 本 PR 在 AI 编码助手辅助下完成,分析、改动与验证均由本人本地执行并审查。

Changelog

🇨🇳 Chinese

  • Fix: 修复多个 Modal 同时打开时,按一次 ESC 会关闭全部弹窗的问题(现在仅关闭最顶层弹窗)

🇺🇸 English

  • Fix: fix an issue where a single ESC key press closed all open Modals at once (now only the top-most Modal is closed)

@codesandbox-ci

codesandbox-ci Bot commented Sep 6, 2026 •

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 8db432d:

Sandbox Source
pr-story Configuration

Keep only the most recently mounted ModalContent keydown handler active so one ESC closes one dialog at a time. Add a controlled nested-modal regression test.

Fixes DouyinFE#3351
@dvd233
dvd233 force-pushed the fix/modal-esc-top-only branch from 5cbb623 to 8db432d Compare September 10, 2026 05:32
@SudoUserReal
SudoUserReal merged commit f777b9a into DouyinFE:release Sep 15, 2026
1 check passed
@cypress

cypress Bot commented Sep 15, 2026

Copy link
Copy Markdown

semi-design    Run #3778

Run Properties:  status check failed Failed #3778  •  git commit f777b9a831: Merge pull request #3352 from dvd233/fix/modal-esc-top-only
Project semi-design
Branch Review release
Run status status check failed Failed #3778
Run duration 08m 34s
Commit git commit f777b9a831: Merge pull request #3352 from dvd233/fix/modal-esc-top-only
Committer SudoUser
View all properties for this run ↗︎

Test results
Tests that failed  Failures 2
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 13
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 289
⚠️ You've recorded test results over your free plan limit.
Upgrade your plan to view test results.
View all changes introduced in this branch ↗︎

Tests for review

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.

2 participants