Skip to content

Commit

Permalink
Auto-focus in confirmation dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed Aug 14, 2024
1 parent c0aaf3d commit cb53370
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/posting/widgets/collection/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def deletion_callback(confirmed: bool | None) -> None:
)

await self.app.push_screen(
ConfirmationModal(confirmation_message),
ConfirmationModal(confirmation_message, auto_focus="confirm"),
callback=deletion_callback,
)

Expand Down
5 changes: 5 additions & 0 deletions src/posting/widgets/confirmation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""A modal screen for confirming a destructive action."""

from typing import Literal
from textual.app import ComposeResult
from textual.containers import Horizontal, Vertical
from textual.screen import ModalScreen
Expand Down Expand Up @@ -31,6 +32,7 @@ def __init__(
confirm_binding: str = "y",
cancel_text: str = "No \[n]",
cancel_binding: str = "n",
auto_focus: Literal["confirm", "cancel"] | None = "confirm",
name: str | None = None,
id: str | None = None,
classes: str | None = None,
Expand All @@ -41,11 +43,14 @@ def __init__(
self.confirm_binding = confirm_binding
self.cancel_text = cancel_text
self.cancel_binding = cancel_binding
self.auto_focus = auto_focus

def on_mount(self) -> None:
self._bindings.bind(self.confirm_binding, "screen.dismiss(True)")
self._bindings.bind(self.cancel_binding, "screen.dismiss(False)")
self._bindings.bind("escape", "screen.dismiss(False)")
if self.auto_focus is not None:
self.query_one(f"#{self.auto_focus}-button").focus()

def compose(self) -> ComposeResult:
with Vertical(id="confirmation-screen", classes="modal-body") as container:
Expand Down

0 comments on commit cb53370

Please sign in to comment.