1515from reflex .utils .serializers import serializer
1616from reflex .vars import VarData
1717from reflex .vars .base import LiteralVar , Var
18+ from reflex .vars .function import FunctionVar
19+ from reflex .vars .object import ObjectVar
1820
1921LiteralPosition = Literal [
2022 "top-left" ,
@@ -232,7 +234,9 @@ def add_hooks(self) -> list[Var | str]:
232234 return [hook ]
233235
234236 @staticmethod
235- def send_toast (message : str = "" , level : str | None = None , ** props ) -> EventSpec :
237+ def send_toast (
238+ message : str | Var = "" , level : str | None = None , ** props
239+ ) -> EventSpec :
236240 """Send a toast message.
237241
238242 Args:
@@ -250,20 +254,27 @@ def send_toast(message: str = "", level: str | None = None, **props) -> EventSpe
250254 raise ValueError (
251255 "Toaster component must be created before sending a toast. (use `rx.toast.provider()`)"
252256 )
253- toast_command = f"{ toast_ref } .{ level } " if level is not None else toast_ref
254- if message == "" and ("title" not in props or "description" not in props ):
257+
258+ toast_command = (
259+ ObjectVar .__getattr__ (toast_ref .to (dict ), level ) if level else toast_ref
260+ ).to (FunctionVar )
261+
262+ if isinstance (message , Var ):
263+ props .setdefault ("title" , message )
264+ message = ""
265+ elif message == "" and "title" not in props and "description" not in props :
255266 raise ValueError ("Toast message or title or description must be provided." )
267+
256268 if props :
257- args = LiteralVar .create (ToastProps (component_name = "rx.toast" , ** props )) # type : ignore
258- toast = f" { toast_command } (` { message } `, { str ( args )} )"
269+ args = LiteralVar .create (ToastProps (component_name = "rx.toast" , ** props )) # pyright : ignore [reportCallIssue, reportGeneralTypeIssues]
270+ toast = toast_command . call ( message , args )
259271 else :
260- toast = f" { toast_command } (` { message } `)"
272+ toast = toast_command . call ( message )
261273
262- toast_action = Var (_js_expr = toast )
263- return run_script (toast_action )
274+ return run_script (toast )
264275
265276 @staticmethod
266- def toast_info (message : str = "" , ** kwargs ):
277+ def toast_info (message : str | Var = "" , ** kwargs ):
267278 """Display an info toast message.
268279
269280 Args:
@@ -276,7 +287,7 @@ def toast_info(message: str = "", **kwargs):
276287 return Toaster .send_toast (message , level = "info" , ** kwargs )
277288
278289 @staticmethod
279- def toast_warning (message : str = "" , ** kwargs ):
290+ def toast_warning (message : str | Var = "" , ** kwargs ):
280291 """Display a warning toast message.
281292
282293 Args:
@@ -289,7 +300,7 @@ def toast_warning(message: str = "", **kwargs):
289300 return Toaster .send_toast (message , level = "warning" , ** kwargs )
290301
291302 @staticmethod
292- def toast_error (message : str = "" , ** kwargs ):
303+ def toast_error (message : str | Var = "" , ** kwargs ):
293304 """Display an error toast message.
294305
295306 Args:
@@ -302,7 +313,7 @@ def toast_error(message: str = "", **kwargs):
302313 return Toaster .send_toast (message , level = "error" , ** kwargs )
303314
304315 @staticmethod
305- def toast_success (message : str = "" , ** kwargs ):
316+ def toast_success (message : str | Var = "" , ** kwargs ):
306317 """Display a success toast message.
307318
308319 Args:
0 commit comments