From 79907b65dbd66f4770dd0472ae70f9bc89bfa1ae Mon Sep 17 00:00:00 2001 From: zeyadrabie <81588100+Corencas@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:04:35 -0600 Subject: [PATCH 1/3] Rename the toast button style props to Sonner's singular names Add action_button_style and cancel_button_style to ToastProps and deprecate action_button_styles and cancel_button_styles. The plural names still work until 1.0: each logs a deprecation warning and is moved to the singular field. Passing both names for the same button raises InvalidPropValueError. The serialize-time rename added in #7786 is no longer needed and is removed. Closes #7984 --- docs/library/overlay/toast.md | 2 + .../news/+toast-button-style.deprecation.md | 9 ++ .../src/reflex_components_sonner/toast.py | 50 ++++++++-- pyi_hashes.json | 2 +- .../reflex_components_sonner/test_toast.py | 95 +++++++++++++++++-- 5 files changed, 140 insertions(+), 18 deletions(-) create mode 100644 packages/reflex-components-sonner/news/+toast-button-style.deprecation.md diff --git a/docs/library/overlay/toast.md b/docs/library/overlay/toast.md index 5991f5b585b..b993315659b 100644 --- a/docs/library/overlay/toast.md +++ b/docs/library/overlay/toast.md @@ -104,6 +104,8 @@ The following props are available for customization: - `id`: `str | Var`: Custom id for the toast. - `unstyled`: `bool`: Removes the default styling, which allows for easier customization. - `style`: `Style`: Custom style for the toast. +- `action_button_style`: `Style`: Custom style for the button that `action` renders. +- `cancel_button_style`: `Style`: Custom style for the button that `cancel` renders. - `on_dismiss`: `Any`: The function gets called when either the close button is clicked, or the toast is swiped. - `on_auto_close`: `Any`: Function that gets called when the toast disappears automatically after it's timeout (`duration` prop). diff --git a/packages/reflex-components-sonner/news/+toast-button-style.deprecation.md b/packages/reflex-components-sonner/news/+toast-button-style.deprecation.md new file mode 100644 index 00000000000..6a03663be31 --- /dev/null +++ b/packages/reflex-components-sonner/news/+toast-button-style.deprecation.md @@ -0,0 +1,9 @@ +Deprecate the `action_button_styles` and `cancel_button_styles` toast props in favor of `action_button_style` and `cancel_button_style`, which match Sonner's option names. The plural names keep working with a deprecation warning until Reflex 1.0, and passing both names for the same button raises an error. + +```python +# deprecated +rx.toast("Saved", action=..., action_button_styles={"background_color": "red"}) + +# supported +rx.toast("Saved", action=..., action_button_style={"background_color": "red"}) +``` diff --git a/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py b/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py index ff6bece4b13..ccd7c658b36 100644 --- a/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py +++ b/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py @@ -11,7 +11,8 @@ from reflex_base.constants.base import Dirs from reflex_base.event import EventSpec, run_script from reflex_base.style import Style, resolved_color_mode -from reflex_base.utils import format +from reflex_base.utils import console, format +from reflex_base.utils.exceptions import InvalidPropValueError from reflex_base.utils.imports import ImportVar from reflex_base.utils.serializers import serializer from reflex_base.vars import VarData @@ -35,9 +36,10 @@ _var_data=VarData(imports={f"$/{Dirs.STATE_PATH}": [ImportVar(tag="refs")]}), ) -_TOAST_STYLE_PROP_ALIASES = ( - ("actionButtonStyles", "actionButtonStyle"), - ("cancelButtonStyles", "cancelButtonStyle"), +# Deprecated button style prop names and their replacements. Remove in 1.0. +_DEPRECATED_BUTTON_STYLE_PROPS = ( + ("action_button_styles", "action_button_style"), + ("cancel_button_styles", "cancel_button_style"), ) @@ -130,9 +132,16 @@ class ToastProps(NoExtrasAllowedProps): # Class name for the toast. class_name: str | None - action_button_styles: Style | None + # Custom style for the toast primary button. + action_button_style: Style | None # Custom style for the toast secondary button. + cancel_button_style: Style | None + + # Deprecated, use `action_button_style`. + action_button_styles: Style | None + + # Deprecated, use `cancel_button_style`. cancel_button_styles: Style | None # The function gets called when either the close button is clicked, or the toast is swiped. @@ -141,6 +150,34 @@ class ToastProps(NoExtrasAllowedProps): # Function that gets called when the toast disappears automatically after it's timeout (duration` prop). on_auto_close: Any | None + def __init__(self, component_name: str | None = None, **kwargs: Any): + """Initialize the toast props, moving deprecated prop names to their replacements. + + Args: + component_name: The custom name of the component. + **kwargs: Kwargs to initialize the props. + + Raises: + InvalidPropValueError: If a deprecated prop name is passed together with its replacement. + """ + for deprecated_name, name in _DEPRECATED_BUTTON_STYLE_PROPS: + if deprecated_name not in kwargs: + continue + if name in kwargs: + msg = ( + f"Pass only one of {name!r} and the deprecated {deprecated_name!r} " + f"to {component_name or type(self).__name__!r}." + ) + raise InvalidPropValueError(msg) + console.deprecate( + feature_name=f"The `{deprecated_name}` toast prop", + reason=f"Use `{name}` instead, which matches Sonner's option name", + deprecation_version="0.11.1", + removal_version="1.0", + ) + kwargs[name] = kwargs.pop(deprecated_name) + super().__init__(component_name=component_name, **kwargs) + def dict(self, *args: Any, **kwargs: Any) -> builtins.dict[str, Any]: """Convert the object to a dictionary. @@ -152,9 +189,6 @@ def dict(self, *args: Any, **kwargs: Any) -> builtins.dict[str, Any]: The object as a dictionary with ToastAction fields intact. """ d = super().dict(*args, **kwargs) - for reflex_name, sonner_name in _TOAST_STYLE_PROP_ALIASES: - if reflex_name in d: - d[sonner_name] = d.pop(reflex_name) # Keep these fields as ToastAction so they can be serialized specially if "action" in d: d["action"] = self.action diff --git a/pyi_hashes.json b/pyi_hashes.json index fec0b8f2474..6c999ec9391 100644 --- a/pyi_hashes.json +++ b/pyi_hashes.json @@ -117,7 +117,7 @@ "packages/reflex-components-recharts/src/reflex_components_recharts/general.pyi": "fe22b3cf69e9ae3d425cc88c3511525b", "packages/reflex-components-recharts/src/reflex_components_recharts/polar.pyi": "adc4db1a3ca7cd292b099ec4d97a4346", "packages/reflex-components-recharts/src/reflex_components_recharts/recharts.pyi": "f2a9cf6db58d169289fbc76de6813300", - "packages/reflex-components-sonner/src/reflex_components_sonner/toast.pyi": "b195e3845260389ced026d2f5c4adb93", + "packages/reflex-components-sonner/src/reflex_components_sonner/toast.pyi": "113231e807bb4fb013962fa8b1e1aa0b", "reflex/__init__.pyi": "067a60e22fb825072f2f0b690662da5a", "reflex/components/__init__.pyi": "9facd05a776d0641432696bbf8e34388", "reflex/experimental/memo.pyi": "0fc8eae24cd0def5ea9d8d66ce87206e" diff --git a/tests/units/reflex_components_sonner/test_toast.py b/tests/units/reflex_components_sonner/test_toast.py index bca21a536ed..1bd219aa0e5 100644 --- a/tests/units/reflex_components_sonner/test_toast.py +++ b/tests/units/reflex_components_sonner/test_toast.py @@ -2,7 +2,10 @@ import json +import pytest +from pytest_mock import MockerFixture from reflex_base.style import Style +from reflex_base.utils.exceptions import InvalidPropValueError from reflex_components_sonner.toast import Toaster, ToastProps @@ -12,16 +15,90 @@ def test_toaster_hotkey_accepts_key_list(): assert 'hotkey:["altKey", "KeyT"]' in str(toaster) -def test_toast_button_styles_use_sonner_prop_names(): - """Serialize toast button styles with Sonner's singular option names.""" +def test_toast_button_styles_use_sonner_prop_names(mocker: MockerFixture): + """The button style props serialize to Sonner's option names as they are.""" + deprecate = mocker.patch("reflex_base.utils.console.deprecate") props = ToastProps( - action_button_styles=Style({"background_color": "red"}), - cancel_button_styles=Style({"color": "white"}), + action_button_style=Style({"background_color": "red"}), + cancel_button_style=Style({"color": "white"}), ) # pyright: ignore[reportCallIssue] - serialized = json.loads(props.json()) + deprecate.assert_not_called() + assert json.loads(props.json()) == { + "actionButtonStyle": {"backgroundColor": "red"}, + "cancelButtonStyle": {"color": "white"}, + } - assert serialized["actionButtonStyle"]["backgroundColor"] == "red" - assert serialized["cancelButtonStyle"]["color"] == "white" - assert "actionButtonStyles" not in serialized - assert "cancelButtonStyles" not in serialized + +def test_toast_deprecated_button_styles(mocker: MockerFixture): + """The plural names still work, warn, and serialize like the singular ones.""" + deprecate = mocker.patch("reflex_base.utils.console.deprecate") + action_style = Style({"background_color": "red"}) + cancel_style = Style({"color": "white"}) + props = ToastProps( + action_button_styles=action_style, + cancel_button_styles=cancel_style, + ) # pyright: ignore[reportCallIssue] + + assert deprecate.call_args_list == [ + mocker.call( + feature_name="The `action_button_styles` toast prop", + reason="Use `action_button_style` instead, which matches Sonner's option name", + deprecation_version="0.11.1", + removal_version="1.0", + ), + mocker.call( + feature_name="The `cancel_button_styles` toast prop", + reason="Use `cancel_button_style` instead, which matches Sonner's option name", + deprecation_version="0.11.1", + removal_version="1.0", + ), + ] + assert props.action_button_style is action_style + assert props.cancel_button_style is cancel_style + assert props.action_button_styles is None + assert props.cancel_button_styles is None + assert json.loads(props.json()) == { + "actionButtonStyle": {"backgroundColor": "red"}, + "cancelButtonStyle": {"color": "white"}, + } + + +def test_toast_button_style_and_deprecated_name_together(): + """Passing a button style under both names is ambiguous, so it raises.""" + with pytest.raises(InvalidPropValueError) as action_error: + Toaster.send_toast( + "Saved", + action_button_style=Style({"color": "red"}), + action_button_styles=Style({"color": "blue"}), + ) + with pytest.raises(InvalidPropValueError) as cancel_error: + Toaster.send_toast( + "Saved", + cancel_button_style=Style({"color": "red"}), + cancel_button_styles=Style({"color": "blue"}), + ) + + assert str(action_error.value) == ( + "Pass only one of 'action_button_style' and the deprecated " + "'action_button_styles' to 'rx.toast'." + ) + assert str(cancel_error.value) == ( + "Pass only one of 'cancel_button_style' and the deprecated " + "'cancel_button_styles' to 'rx.toast'." + ) + + +def test_toaster_toast_options_button_styles(): + """Default toast options passed to the provider carry Sonner's option names.""" + toaster = Toaster.create( + toast_options=ToastProps( + action_button_style=Style({"background_color": "red"}), + cancel_button_style=Style({"color": "white"}), + ) # pyright: ignore[reportCallIssue] + ) + + assert str(toaster.toast_options) == ( # pyright: ignore[reportAttributeAccessIssue] + '({ ["actionButtonStyle"] : ({ ["backgroundColor"] : "red" }), ' + '["cancelButtonStyle"] : ({ ["color"] : "white" }) })' + ) From 92a511594e75c2fd70ff45614b210cfedaee0e48 Mon Sep 17 00:00:00 2001 From: zeyadrabie <81588100+Corencas@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:49:29 -0600 Subject: [PATCH 2/3] Say which calls still accept the plural toast style names --- .../news/+toast-button-style.deprecation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/reflex-components-sonner/news/+toast-button-style.deprecation.md b/packages/reflex-components-sonner/news/+toast-button-style.deprecation.md index 6a03663be31..79388598097 100644 --- a/packages/reflex-components-sonner/news/+toast-button-style.deprecation.md +++ b/packages/reflex-components-sonner/news/+toast-button-style.deprecation.md @@ -1,4 +1,4 @@ -Deprecate the `action_button_styles` and `cancel_button_styles` toast props in favor of `action_button_style` and `cancel_button_style`, which match Sonner's option names. The plural names keep working with a deprecation warning until Reflex 1.0, and passing both names for the same button raises an error. +Deprecate the `action_button_styles` and `cancel_button_styles` toast props in favor of `action_button_style` and `cancel_button_style`, which match Sonner's option names. Passing the plural names to `rx.toast(...)` or `rx.toast.options(...)` keeps working with a deprecation warning until Reflex 1.0, and passing both names for the same button raises an error. ```python # deprecated From e1c38b8af294a6c1d591da1761e2bce275cb0d02 Mon Sep 17 00:00:00 2001 From: zeyadrabie <81588100+Corencas@users.noreply.github.com> Date: Thu, 26 Nov 2026 20:08:00 -0600 Subject: [PATCH 3/3] Use the singular toast style when both names are passed Passing a deprecated plural toast style name together with its singular replacement no longer raises. The plural name still logs the deprecation warning, and the singular value is used. --- .../news/+toast-button-style.deprecation.md | 2 +- .../src/reflex_components_sonner/toast.py | 14 +--- .../reflex_components_sonner/test_toast.py | 77 +++++++++---------- 3 files changed, 42 insertions(+), 51 deletions(-) diff --git a/packages/reflex-components-sonner/news/+toast-button-style.deprecation.md b/packages/reflex-components-sonner/news/+toast-button-style.deprecation.md index 79388598097..622b0c513d5 100644 --- a/packages/reflex-components-sonner/news/+toast-button-style.deprecation.md +++ b/packages/reflex-components-sonner/news/+toast-button-style.deprecation.md @@ -1,4 +1,4 @@ -Deprecate the `action_button_styles` and `cancel_button_styles` toast props in favor of `action_button_style` and `cancel_button_style`, which match Sonner's option names. Passing the plural names to `rx.toast(...)` or `rx.toast.options(...)` keeps working with a deprecation warning until Reflex 1.0, and passing both names for the same button raises an error. +Deprecate the `action_button_styles` and `cancel_button_styles` toast props in favor of `action_button_style` and `cancel_button_style`, which match Sonner's option names. Passing the plural names to `rx.toast(...)` or `rx.toast.options(...)` keeps working with a deprecation warning until Reflex 1.0. If both names are passed for the same button, the singular one is used. ```python # deprecated diff --git a/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py b/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py index ccd7c658b36..334f2260570 100644 --- a/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py +++ b/packages/reflex-components-sonner/src/reflex_components_sonner/toast.py @@ -12,7 +12,6 @@ from reflex_base.event import EventSpec, run_script from reflex_base.style import Style, resolved_color_mode from reflex_base.utils import console, format -from reflex_base.utils.exceptions import InvalidPropValueError from reflex_base.utils.imports import ImportVar from reflex_base.utils.serializers import serializer from reflex_base.vars import VarData @@ -153,29 +152,22 @@ class ToastProps(NoExtrasAllowedProps): def __init__(self, component_name: str | None = None, **kwargs: Any): """Initialize the toast props, moving deprecated prop names to their replacements. + If a deprecated name and its replacement are both passed, the replacement is used. + Args: component_name: The custom name of the component. **kwargs: Kwargs to initialize the props. - - Raises: - InvalidPropValueError: If a deprecated prop name is passed together with its replacement. """ for deprecated_name, name in _DEPRECATED_BUTTON_STYLE_PROPS: if deprecated_name not in kwargs: continue - if name in kwargs: - msg = ( - f"Pass only one of {name!r} and the deprecated {deprecated_name!r} " - f"to {component_name or type(self).__name__!r}." - ) - raise InvalidPropValueError(msg) console.deprecate( feature_name=f"The `{deprecated_name}` toast prop", reason=f"Use `{name}` instead, which matches Sonner's option name", deprecation_version="0.11.1", removal_version="1.0", ) - kwargs[name] = kwargs.pop(deprecated_name) + kwargs.setdefault(name, kwargs.pop(deprecated_name)) super().__init__(component_name=component_name, **kwargs) def dict(self, *args: Any, **kwargs: Any) -> builtins.dict[str, Any]: diff --git a/tests/units/reflex_components_sonner/test_toast.py b/tests/units/reflex_components_sonner/test_toast.py index 1bd219aa0e5..827b36d1150 100644 --- a/tests/units/reflex_components_sonner/test_toast.py +++ b/tests/units/reflex_components_sonner/test_toast.py @@ -1,13 +1,27 @@ """Sonner toast component tests.""" import json +from unittest.mock import call -import pytest from pytest_mock import MockerFixture from reflex_base.style import Style -from reflex_base.utils.exceptions import InvalidPropValueError from reflex_components_sonner.toast import Toaster, ToastProps +_DEPRECATION_CALLS = [ + call( + feature_name="The `action_button_styles` toast prop", + reason="Use `action_button_style` instead, which matches Sonner's option name", + deprecation_version="0.11.1", + removal_version="1.0", + ), + call( + feature_name="The `cancel_button_styles` toast prop", + reason="Use `cancel_button_style` instead, which matches Sonner's option name", + deprecation_version="0.11.1", + removal_version="1.0", + ), +] + def test_toaster_hotkey_accepts_key_list(): """Sonner reads `hotkey` as an array of KeyboardEvent keys and codes.""" @@ -40,20 +54,7 @@ def test_toast_deprecated_button_styles(mocker: MockerFixture): cancel_button_styles=cancel_style, ) # pyright: ignore[reportCallIssue] - assert deprecate.call_args_list == [ - mocker.call( - feature_name="The `action_button_styles` toast prop", - reason="Use `action_button_style` instead, which matches Sonner's option name", - deprecation_version="0.11.1", - removal_version="1.0", - ), - mocker.call( - feature_name="The `cancel_button_styles` toast prop", - reason="Use `cancel_button_style` instead, which matches Sonner's option name", - deprecation_version="0.11.1", - removal_version="1.0", - ), - ] + assert deprecate.call_args_list == _DEPRECATION_CALLS assert props.action_button_style is action_style assert props.cancel_button_style is cancel_style assert props.action_button_styles is None @@ -64,29 +65,27 @@ def test_toast_deprecated_button_styles(mocker: MockerFixture): } -def test_toast_button_style_and_deprecated_name_together(): - """Passing a button style under both names is ambiguous, so it raises.""" - with pytest.raises(InvalidPropValueError) as action_error: - Toaster.send_toast( - "Saved", - action_button_style=Style({"color": "red"}), - action_button_styles=Style({"color": "blue"}), - ) - with pytest.raises(InvalidPropValueError) as cancel_error: - Toaster.send_toast( - "Saved", - cancel_button_style=Style({"color": "red"}), - cancel_button_styles=Style({"color": "blue"}), - ) - - assert str(action_error.value) == ( - "Pass only one of 'action_button_style' and the deprecated " - "'action_button_styles' to 'rx.toast'." - ) - assert str(cancel_error.value) == ( - "Pass only one of 'cancel_button_style' and the deprecated " - "'cancel_button_styles' to 'rx.toast'." - ) +def test_toast_button_style_wins_over_deprecated_name(mocker: MockerFixture): + """With both names passed, the singular one is used and the plural one only warns.""" + deprecate = mocker.patch("reflex_base.utils.console.deprecate") + action_style = Style({"color": "red"}) + cancel_style = Style({"color": "white"}) + props = ToastProps( + action_button_style=action_style, + action_button_styles=Style({"color": "blue"}), + cancel_button_style=cancel_style, + cancel_button_styles=Style({"color": "black"}), + ) # pyright: ignore[reportCallIssue] + + assert deprecate.call_args_list == _DEPRECATION_CALLS + assert props.action_button_style is action_style + assert props.cancel_button_style is cancel_style + assert props.action_button_styles is None + assert props.cancel_button_styles is None + assert json.loads(props.json()) == { + "actionButtonStyle": {"color": "red"}, + "cancelButtonStyle": {"color": "white"}, + } def test_toaster_toast_options_button_styles():