Skip to content

trap focus#6202

Merged
willmcgugan merged 6 commits into
mainfrom
trap-focus
Oct 31, 2025
Merged

trap focus#6202
willmcgugan merged 6 commits into
mainfrom
trap-focus

Conversation

@willmcgugan

@willmcgugan willmcgugan commented Oct 31, 2025

Copy link
Copy Markdown
Member

Adds ability to "trap" the focus. So that tabbing through controls is restricted to the given container. This can be a requirement for something like a modal dialog, which doesn't use a modal screen.

@willmcgugan willmcgugan requested a review from Copilot October 31, 2025 10:47

Copilot AI left a comment

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.

Pull Request Overview

This PR adds a trap_focus() method to DOMNode that allows restricting focus navigation (via Tab key) to a specific container and its descendants. When focus trapping is enabled on a container that contains the currently focused widget, the focus chain is limited to only that container's children.

  • Added DOMNode.trap_focus() method to enable/disable focus trapping
  • Modified Screen.focus_chain property to respect focus trapping by finding the nearest ancestor with _trap_focus=True
  • Added comprehensive test coverage for the trap focus functionality

Reviewed Changes

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

File Description
src/textual/dom.py Added _trap_focus instance variable and trap_focus() method to DOMNode
src/textual/screen.py Modified focus_chain property to limit focus traversal to trapped container
tests/test_focus.py Added test case for trap focus functionality with multiple scenarios
CHANGELOG.md Documented the addition of the new feature

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_focus.py Outdated
Comment thread src/textual/dom.py
willmcgugan and others added 3 commits October 31, 2025 10:55
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@willmcgugan willmcgugan merged commit ea8ae47 into main Oct 31, 2025
23 checks passed
@willmcgugan willmcgugan deleted the trap-focus branch October 31, 2025 13:24
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