Skip to content

Should widgets scroll to highlighted item when resized? #5580

Closed

Description

@TomJGooding

I've been hesitant to create an issue for this, but should widgets automatically scroll to the highlighted item after being resized? Or is this something that is expected to be handled by the developer?

The below example with an OptionList hopefully demonstrates what I mean. Obviously resizing the terminal could also cause the highlighted item to go out of view.

from textual.app import App, ComposeResult
from textual.containers import Horizontal
from textual.widgets import Footer, OptionList, Placeholder


class ExampleApp(App):
    BINDINGS = [
        ("t", "toggle_panel", "Toggle Panel"),
    ]

    CSS = """
    Placeholder {
        width: 2fr;
        display: none;
    }

    .show-panel {
        Placeholder {
            display: block;
        }
    }
    """

    def compose(self) -> ComposeResult:
        with Horizontal():
            yield OptionList(
                *[
                    f"This is option {i} which is long and might wrap when the widget is resized"
                    for i in range(100)
                ]
            )
            yield Placeholder()
        yield Footer()

    def on_mount(self) -> None:
        options = self.query_one(OptionList)
        options.highlighted = 50

    def action_toggle_panel(self) -> None:
        self.toggle_class("show-panel")


if __name__ == "__main__":
    app = ExampleApp()
    app.run()

Activity

  1. github-actions commented on Feb 25, 2025

    @github-actions

    We found the following entries in the FAQ which you may find helpful:

    Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

    This is an automated reply, generated by FAQtory

  2. willmcgugan commented on Feb 26, 2025

    @willmcgugan
    Member

    It probably should... I can't think of any reason why not scrolling would be more desirable. Should be a simple fix. Calling scroll_to_highlight on the resize event.

  3. darrenburns commented on Mar 3, 2025

    @darrenburns
    Contributor

    I could see reasons why I wouldn't want this. If I've got the highlighted item scrolled out of view, it might suggest I'm reading other items in the list. What is highlighted doesn't always correspond to where my attention currently is. If I resize in this case, I definitely would want to try and keep my current reading position in the list (in other words: don't scroll).

  4. TomJGooding commented on Mar 3, 2025

    @TomJGooding
    ContributorAuthor

    @darrenburns Fair point. I'm usually navigating with the keyboard, so hadn't considered if you'd scrolled away from the highlight using the mouse.

  5. github-actions commented on May 31, 2026

    @github-actions

    I hope I solved your issue!

    Consider sponsoring my work to continue to maintain and develop Textual.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions