Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS for custom widgets is sometimes unintuitive #5481

Open
TomJGooding opened this issue Jan 17, 2025 · 1 comment
Open

CSS for custom widgets is sometimes unintuitive #5481

TomJGooding opened this issue Jan 17, 2025 · 1 comment

Comments

@TomJGooding
Copy link
Contributor

I've sometimes found updating the CSS for custom widgets to be unintuitive, especially with the introduction of themes.

Here's a simple example with a custom widget that extends the DataTable. I think you would expect the background to be red.

from textual.app import App, ComposeResult
from textual.widgets import DataTable


class MyDataTable(DataTable):
    DEFAULT_CSS = """
    MyDataTable {
        background: red;
    }
    """


class ExampleApp(App):
    def compose(self) -> ComposeResult:
        yield MyDataTable()

    def on_mount(self) -> None:
        table = self.query_one(MyDataTable)
        table.add_column("Column")
        table.add_rows([f"Row {i}"] for i in range(1, 10))


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

But you'd need to dig into the source code and understand that you also need to change this:

class MyDataTable(DataTable):
    DEFAULT_CSS = """
    MyDataTable {
        background: red;

        &:dark {
            background: red;
        }
    }
    """

I understand that widget CSS is sometimes necessarily complex, especially now with themes, but perhaps there's a way to make changing the CSS more intuitive?

Copy link

We found the following entry 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

TomJGooding added a commit to TomJGooding/textual that referenced this issue Jan 17, 2025
Remove the background CSS in the `:dark` pseudo selector, as this is
already the default background for the `DataTable`.

Currently this duplication makes styling a custom widget that extends
the `DataTable` unintuitive (see Textualize#5481).
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

No branches or pull requests

1 participant