Skip to content

Conversation

@sfc-gh-bnisco
Copy link
Collaborator

@sfc-gh-bnisco sfc-gh-bnisco commented Dec 8, 2025

Describe your changes

  • Improves the handling of keyboard shortcuts to prevent accidental triggering of global single-key shortcuts (like "c", "r", and "a") when the user is typing in editable elements, including those inside Shadow DOM.
  • Ensures that shortcuts are suppressed in appropriate contexts across both app-level and widget-level handlers.

GitHub Issue Link (if applicable)

Testing Plan

  • Adds unit tests

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@snyk-io
Copy link
Contributor

snyk-io bot commented Dec 8, 2025

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 8, 2025

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-13264/streamlit-1.52.1-py3-none-any.whl
📦 @streamlit/component-v2-lib Download from artifacts
🕹️ Preview app pr-13264.streamlit.app (☁️ Deploy here if not accessible)

Copy link
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@sfc-gh-bnisco sfc-gh-bnisco added security-assessment-completed Security assessment has been completed for PR change:bugfix PR contains bug fix implementation impact:users PR changes affect end users labels Dec 8, 2025
@sfc-gh-bnisco sfc-gh-bnisco requested a review from Copilot December 8, 2025 23:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes keyboard shortcut handling to prevent accidental triggering of global single-key shortcuts (like "c", "r", and "a") when users are typing in editable elements, including those inside Shadow DOM trees.

Key Changes:

  • Adds Shadow DOM-aware detection of editable elements via composedPath() inspection
  • Exports new helper function isKeyboardEventFromEditableTarget for app-level and widget-level shortcut suppression
  • Updates App and StatusWidget keyboard handlers to respect editable contexts

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
frontend/lib/src/hooks/useRegisterShortcut.ts Implements getEditableEventTarget to detect editable elements in Shadow DOM by inspecting composedPath(), and exports isKeyboardEventFromEditableTarget helper function. Updates filter and blocking logic to use the new Shadow DOM-aware detection.
frontend/lib/src/index.ts Exports the new isKeyboardEventFromEditableTarget helper for use in app components.
frontend/lib/src/hooks/useRegisterShortcut.test.tsx Adds comprehensive tests for Shadow DOM scenarios, verifying that shortcuts are correctly blocked when typing in inputs inside shadow roots.
frontend/app/src/App.tsx Updates handleKeyDown to accept optional KeyboardEvent parameter and suppress "c"/"r" shortcuts when user is typing in editable elements (including Shadow DOM).
frontend/app/src/components/StatusWidget/StatusWidget.tsx Updates handleKeyDown to accept optional KeyboardEvent parameter and suppress "a" shortcut when user is typing in editable elements (including Shadow DOM).

@github-actions
Copy link
Contributor

github-actions bot commented Dec 8, 2025

📈 Frontend coverage change detected

The frontend unit test (vitest) coverage has increased by 0.0900%

  • Current PR: 86.0200% (12454 lines, 1740 missed)
  • Latest develop: 85.9300% (12445 lines, 1751 missed)

🎉 Great job on improving test coverage!

📊 View detailed coverage comparison

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

@sfc-gh-bnisco sfc-gh-bnisco force-pushed the 12-08-_fix_ensure_key_down_targeting_works_for_elements_in_shadow_dom branch from fffb611 to b4e286d Compare December 9, 2025 17:50
@sfc-gh-bnisco sfc-gh-bnisco requested a review from Copilot December 9, 2025 17:50
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

@sfc-gh-bnisco sfc-gh-bnisco marked this pull request as ready for review December 9, 2025 18:35
@lukasmasuch
Copy link
Collaborator

@cursor review

@github-actions
Copy link
Contributor

Summary

This PR fixes an issue where keyboard shortcuts were incorrectly triggering (or failing to be suppressed) when the user was interacting with an editable element inside a Shadow DOM. It introduces a isKeyboardEventFromEditableTarget helper that correctly inspects event.composedPath() to identify the actual target of the event, ensuring that global shortcuts (like 'r' for rerun or 'c' for clear cache) are not fired while the user is typing in a Shadow DOM input.

Code Quality

The code is clean, well-structured, and follows the project's TypeScript conventions.

  • The logic in getEditableEventTarget correctly prioritizes composedPath() to handle Shadow DOM retargeting, with a fallback to target for environments without it.
  • The use of element?.tagName as a guard clause efficiently prevents runtime errors when iterating over non-Element targets in composedPath (like Window or Document), as getAttribute would otherwise fail.
  • The integration into App.tsx and StatusWidget.tsx is correct, ensuring the KeyboardEvent is passed down and checked.

Test Coverage

  • Unit Tests: The PR includes comprehensive unit tests in useRegisterShortcut.test.tsx. These tests explicitly mock composedPath to verify the behavior with Shadow DOM structures, covering various scenarios (input in shadow root, non-editable host, etc.). This provides high confidence in the fix.
  • E2E Tests: No new E2E tests were added. Given that reproducing this requires a custom component with Shadow DOM and the unit tests cover the event logic thoroughly, this is acceptable.

Backwards Compatibility

The changes are backwards compatible. The handleKeyDown signature change in App.tsx and StatusWidget.tsx makes the keyboardEvent parameter optional (though react-hot-keys provides it), and the logic defaults to safe behavior if the event is missing.

Security & Risk

  • Risk: Low. The change is specific to event target detection for shortcuts.
  • Security: No security implications found.

Recommendations

No changes requested. The implementation is solid.

Verdict

APPROVED: The PR correctly addresses the Shadow DOM event targeting issue with robust logic and excellent unit test coverage.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no bugs!


Copy link
Collaborator

@lukasmasuch lukasmasuch left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@sfc-gh-bnisco sfc-gh-bnisco merged commit 7666fad into develop Dec 10, 2025
50 of 51 checks passed
@sfc-gh-bnisco sfc-gh-bnisco deleted the 12-08-_fix_ensure_key_down_targeting_works_for_elements_in_shadow_dom branch December 10, 2025 19:25
github-actions bot pushed a commit that referenced this pull request Dec 15, 2025
)

## Describe your changes

- Improves the handling of keyboard shortcuts to prevent accidental
triggering of global single-key shortcuts (like "c", "r", and "a") when
the user is typing in editable elements, including those inside Shadow
DOM.
- Ensures that shortcuts are suppressed in appropriate contexts across
both app-level and widget-level handlers.

## GitHub Issue Link (if applicable)

## Testing Plan

- Adds unit tests

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:bugfix PR contains bug fix implementation impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants