Skip to content

Commit 733e506

Browse files
authored
inherit text-style (#2862)
1 parent 3bc4fb7 commit 733e506

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/textual/css/_style_properties.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -959,10 +959,10 @@ def __set__(self, obj: StylesBase, style_flags: Style | str | None):
959959
_rich_traceback_omit = True
960960
if style_flags is None:
961961
if obj.clear_rule(self.name):
962-
obj.refresh()
962+
obj.refresh(children=True)
963963
elif isinstance(style_flags, Style):
964964
if obj.set_rule(self.name, style_flags):
965-
obj.refresh()
965+
obj.refresh(children=True)
966966
else:
967967
words = [word.strip() for word in style_flags.split(" ")]
968968
valid_word = VALID_STYLE_FLAGS.__contains__
@@ -986,7 +986,7 @@ def __set__(self, obj: StylesBase, style_flags: Style | str | None):
986986
) from None
987987
raise error from None
988988
if obj.set_rule(self.name, style):
989-
obj.refresh()
989+
obj.refresh(children=True)
990990

991991

992992
class TransitionsProperty:

tests/test_style_inheritance.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from rich.style import Style
2+
3+
from textual.app import App, ComposeResult
4+
from textual.widgets import Button, Static
5+
6+
7+
async def test_text_style_inheritance():
8+
"""Check that changes to text style are inherited in children."""
9+
10+
class FocusableThing(Static, can_focus=True):
11+
DEFAULT_CSS = """
12+
FocusableThing {
13+
text-style: bold;
14+
}
15+
16+
FocusableThing:focus {
17+
text-style: bold reverse;
18+
}
19+
"""
20+
21+
def compose(self) -> ComposeResult:
22+
yield Static("test", id="child-of-focusable-thing")
23+
24+
class InheritanceApp(App):
25+
def compose(self) -> ComposeResult:
26+
yield Button("button1")
27+
yield FocusableThing()
28+
yield Button("button2")
29+
30+
app = InheritanceApp()
31+
async with app.run_test() as pilot:
32+
await pilot.pause()
33+
child = app.query_one("#child-of-focusable-thing")
34+
assert child.rich_style.bold
35+
assert not child.rich_style.reverse
36+
await pilot.press("tab")
37+
await pilot.pause()
38+
assert child.rich_style.bold
39+
assert child.rich_style.reverse

0 commit comments

Comments
 (0)