diff --git a/src/textual/widgets/_toggle_button.py b/src/textual/widgets/_toggle_button.py index b649081fd7..dca89cdbd0 100644 --- a/src/textual/widgets/_toggle_button.py +++ b/src/textual/widgets/_toggle_button.py @@ -169,6 +169,7 @@ def _make_label(self, label: TextType) -> Text: label = label.split()[0] except IndexError: pass + return label @property @@ -216,7 +217,9 @@ def render(self) -> RenderResult: """ button = self._button label = self._label.copy() - label.stylize(self.get_component_rich_style("toggle--label", partial=True)) + label.stylize_before( + self.get_component_rich_style("toggle--label", partial=True) + ) spacer = " " if label else "" return Text.assemble( *( diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr index 1ecfe0e40d..fc13eb3a6d 100644 --- a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr +++ b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr @@ -48054,6 +48054,170 @@ ''' # --- +# name: test_toggle_style_order + ''' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CheckboxApp + + + + + + + + + + ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ + XThis is just some text. + ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ + This is just some text. + + + + + + + + + + + + + + + + + + + + + + + + + ''' +# --- # name: test_tooltips_in_compound_widgets ''' diff --git a/tests/snapshot_tests/snapshot_apps/toggle_style_order.py b/tests/snapshot_tests/snapshot_apps/toggle_style_order.py new file mode 100644 index 0000000000..a92e41a421 --- /dev/null +++ b/tests/snapshot_tests/snapshot_apps/toggle_style_order.py @@ -0,0 +1,19 @@ +from textual.app import App +from textual.widgets import Checkbox, Label + + +class CheckboxApp(App): + CSS = """ + Checkbox > .toggle--label, Label { + color: white; + text-opacity: 50%; + } + """ + + def compose(self): + yield Checkbox("[red bold]This is just[/] some text.") + yield Label("[red bold]This is just[/] some text.") + + +if __name__ == "__main__": + CheckboxApp().run() diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py index d63959c698..b74e44c316 100644 --- a/tests/snapshot_tests/test_snapshots.py +++ b/tests/snapshot_tests/test_snapshots.py @@ -1329,7 +1329,11 @@ async def run_before(pilot: Pilot) -> None: SNAPSHOT_APPS_DIR / "programmatic_disable_button.py", run_before=run_before ) +def test_toggle_style_order(snap_compare): + """Regression test for https://github.com/Textualize/textual/issues/3421""" + assert snap_compare(SNAPSHOT_APPS_DIR / "toggle_style_order.py") def test_component_text_opacity(snap_compare): """Regression test for https://github.com/Textualize/textual/issues/3413""" assert snap_compare(SNAPSHOT_APPS_DIR / "component_text_opacity.py") +