expose toast
This commit is contained in:
parent
87f630e906
commit
3226556cd9
@ -75,6 +75,7 @@ from reflex.components.core.client_side_routing import (
|
|||||||
from reflex.components.core.sticky import sticky
|
from reflex.components.core.sticky import sticky
|
||||||
from reflex.components.core.upload import Upload, get_upload_dir
|
from reflex.components.core.upload import Upload, get_upload_dir
|
||||||
from reflex.components.radix import themes
|
from reflex.components.radix import themes
|
||||||
|
from reflex.components.sonner.toast import toast
|
||||||
from reflex.config import ExecutorType, environment, get_config
|
from reflex.config import ExecutorType, environment, get_config
|
||||||
from reflex.event import (
|
from reflex.event import (
|
||||||
_EVENT_FIELDS,
|
_EVENT_FIELDS,
|
||||||
@ -411,7 +412,7 @@ class App(MiddlewareMixin, LifespanMixin):
|
|||||||
] = default_backend_exception_handler
|
] = default_backend_exception_handler
|
||||||
|
|
||||||
# Put the toast provider in the app wrap.
|
# Put the toast provider in the app wrap.
|
||||||
bundle_toaster: bool = True
|
toaster: Component | None = dataclasses.field(default_factory=toast.provider)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def api(self) -> FastAPI | None:
|
def api(self) -> FastAPI | None:
|
||||||
@ -1186,15 +1187,12 @@ class App(MiddlewareMixin, LifespanMixin):
|
|||||||
# Add the custom components from the page to the set.
|
# Add the custom components from the page to the set.
|
||||||
custom_components |= component._get_all_custom_components()
|
custom_components |= component._get_all_custom_components()
|
||||||
|
|
||||||
if self.bundle_toaster:
|
if (toaster := self.toaster) is not None:
|
||||||
from reflex.components.component import memo
|
from reflex.components.component import memo
|
||||||
from reflex.components.sonner.toast import toast
|
|
||||||
|
|
||||||
internal_toast_provider = toast.provider()
|
|
||||||
|
|
||||||
@memo
|
@memo
|
||||||
def memoized_toast_provider():
|
def memoized_toast_provider():
|
||||||
return internal_toast_provider
|
return toaster
|
||||||
|
|
||||||
toast_provider = Fragment.create(memoized_toast_provider())
|
toast_provider = Fragment.create(memoized_toast_provider())
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ from reflex.components.radix.themes.components.dialog import (
|
|||||||
)
|
)
|
||||||
from reflex.components.radix.themes.layout.flex import Flex
|
from reflex.components.radix.themes.layout.flex import Flex
|
||||||
from reflex.components.radix.themes.typography.text import Text
|
from reflex.components.radix.themes.typography.text import Text
|
||||||
from reflex.components.sonner.toast import ToastProps
|
from reflex.components.sonner.toast import ToastProps, toast_ref
|
||||||
from reflex.config import environment
|
from reflex.config import environment
|
||||||
from reflex.constants import Dirs, Hooks, Imports
|
from reflex.constants import Dirs, Hooks, Imports
|
||||||
from reflex.constants.compiler import CompileVars
|
from reflex.constants.compiler import CompileVars
|
||||||
@ -114,11 +114,11 @@ class ConnectionToaster(Fragment):
|
|||||||
if environment.REFLEX_DOES_BACKEND_COLD_START.get():
|
if environment.REFLEX_DOES_BACKEND_COLD_START.get():
|
||||||
loading_message = Var.create("Backend is starting.")
|
loading_message = Var.create("Backend is starting.")
|
||||||
backend_is_loading_toast_var = Var(
|
backend_is_loading_toast_var = Var(
|
||||||
f"toast.loading({loading_message!s}, {{...toast_props, description: '', closeButton: false, onDismiss: () => setUserDismissed(true)}},)"
|
f"toast?.loading({loading_message!s}, {{...toast_props, description: '', closeButton: false, onDismiss: () => setUserDismissed(true)}},)"
|
||||||
)
|
)
|
||||||
backend_is_not_responding = Var.create("Backend is not responding.")
|
backend_is_not_responding = Var.create("Backend is not responding.")
|
||||||
backend_is_down_toast_var = Var(
|
backend_is_down_toast_var = Var(
|
||||||
f"toast.error({backend_is_not_responding!s}, {{...toast_props, description: '', onDismiss: () => setUserDismissed(true)}},)"
|
f"toast?.error({backend_is_not_responding!s}, {{...toast_props, description: '', onDismiss: () => setUserDismissed(true)}},)"
|
||||||
)
|
)
|
||||||
toast_var = Var(
|
toast_var = Var(
|
||||||
f"""
|
f"""
|
||||||
@ -139,10 +139,11 @@ setTimeout(() => {{
|
|||||||
f"Cannot connect to server: {connection_error}."
|
f"Cannot connect to server: {connection_error}."
|
||||||
)
|
)
|
||||||
toast_var = Var(
|
toast_var = Var(
|
||||||
f"toast.error({loading_message!s}, {{...toast_props, onDismiss: () => setUserDismissed(true)}},)"
|
f"toast?.error({loading_message!s}, {{...toast_props, onDismiss: () => setUserDismissed(true)}},)"
|
||||||
)
|
)
|
||||||
|
|
||||||
individual_hooks = [
|
individual_hooks = [
|
||||||
|
Var(f"const toast = {toast_ref};"),
|
||||||
f"const toast_props = {LiteralVar.create(props)!s};",
|
f"const toast_props = {LiteralVar.create(props)!s};",
|
||||||
"const [userDismissed, setUserDismissed] = useState(false);",
|
"const [userDismissed, setUserDismissed] = useState(false);",
|
||||||
"const [waitedForBackend, setWaitedForBackend] = useState(false);",
|
"const [waitedForBackend, setWaitedForBackend] = useState(false);",
|
||||||
@ -164,7 +165,7 @@ setTimeout(() => {{
|
|||||||
{toast_var!s}
|
{toast_var!s}
|
||||||
}}
|
}}
|
||||||
}} else {{
|
}} else {{
|
||||||
toast.dismiss("{toast_id}");
|
toast?.dismiss("{toast_id}");
|
||||||
setUserDismissed(false); // after reconnection reset dismissed state
|
setUserDismissed(false); // after reconnection reset dismissed state
|
||||||
}}
|
}}
|
||||||
}}
|
}}
|
||||||
|
@ -383,7 +383,6 @@ class Toaster(Component):
|
|||||||
Returns:
|
Returns:
|
||||||
The toaster component.
|
The toaster component.
|
||||||
"""
|
"""
|
||||||
cls.is_used = True
|
|
||||||
return super().create(*children, **props)
|
return super().create(*children, **props)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user