Open ecoue opened 3 weeks ago
The type in the CSS is the name of the class when it was defined. By styling Input
you are clashing with the builtin Input, and the usual specificity rules apply.
To create a custom widget, you will need to create a new name (don't call it Input again) and style that.
class MyInput(Input):
DEFAULT_CSS = """
MyWidget {
...
}
"""
"""
This will ensure that the rules you add in MyInput
take precedence over the base class Input
.
If you need further help, please include a fully working MRE as requested...
If you can, include a complete working example that demonstrates the bug. Check it can run without modifications.
Thank you very much for the quick reply. I had already suspected that the problem could be a naming conflict. Unfortunately, the problem exists independently of this. Rather, it is noticeable that there are no problems (even when named as Input
) if no nested CSS is used. This should not produce a different result in any case since it is just syntactic sugar. Furthermore, the problem only exists with the additional subclass InlineInput
; Input
, on the other hand, works.
I will need a fully working example to assist you further.
from textual.app import App
from textual.widgets import Input
class MyInputNested(Input):
DEFAULT_CSS = """
MyInputNested {
border: none;
padding: 1 2;
margin-bottom: 1;
&:focus {
border: none;
}
}
"""
class MyInputNestedSubclass(MyInputNested):
DEFAULT_CSS = """
MyInputNestedSubclass {
padding: 0 2;
height: auto;
}
"""
class MyInput(Input):
DEFAULT_CSS = """
MyInput {
border: none;
padding: 1 2;
margin-bottom: 1;
}
MyInput:focus {
border: none;
}
"""
class MyInputSubclass(MyInput):
DEFAULT_CSS = """
MyInputSubclass {
padding: 0 2;
height: auto;
}
"""
class BugDemo(App):
def compose(self):
yield MyInputNested(placeholder="MyInputNested")
yield MyInputNestedSubclass(placeholder="MyInputNestedSubclass")
yield MyInput(placeholder="MyInput")
yield MyInputSubclass(placeholder="MyInputSubclass")
if __name__ == "__main__":
app = BugDemo()
app.run()
Oddly the nested version only stops working if you add an instance of its subclass to the app.
You're running quite an old version of Textual, have you tried upgrading? Because I can't seem to reproduce this with the latest version.
You are absolutely right, should have checked that first. The bug seems to be fixed with the current version.
Report
When extending Textual's
Input
widget with custom CSS using nested selectors, child classes do not properly inherit all CSS rules, specifically those within pseudo-selectors and modifier classes (:hover
is working). The issue manifests when using the nested&
syntax for pseudo-selectors and modifiers. However, the same CSS rules work correctly when written with non-nested syntax.Reproduction
Expectation
InlineInput
widget should inherit all CSS rules from its parent classInlineInput
should have no border, as specified in the parent's&:focus
ruleBehavior
InlineInput
widget shows a border when focused&:hover
)Input:focus
instead of&:focus
)Diagnostics
Versions
Python
Operating System
Terminal
Rich Console options