-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtoast.py
More file actions
416 lines (319 loc) ยท 12.5 KB
/
Copy pathtoast.py
File metadata and controls
416 lines (319 loc) ยท 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
"""Sonner toast component."""
from __future__ import annotations
import dataclasses
from typing import Any, Literal
from reflex_base.components.component import Component, ComponentNamespace, field
from reflex_base.components.props import NoExtrasAllowedProps
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.imports import ImportVar
from reflex_base.utils.serializers import serializer
from reflex_base.vars import VarData
from reflex_base.vars.base import LiteralVar, Var
from reflex_base.vars.function import FunctionVar
from reflex_base.vars.number import ternary_operation
from reflex_base.vars.object import ObjectVar
LiteralPosition = Literal[
"top-left",
"top-center",
"top-right",
"bottom-left",
"bottom-center",
"bottom-right",
]
toast_ref = Var(
_js_expr="refs['__toast']",
_var_data=VarData(imports={f"$/{Dirs.STATE_PATH}": [ImportVar(tag="refs")]}),
)
@dataclasses.dataclass
class ToastAction:
"""A toast action that render a button in the toast."""
label: str
on_click: Any
@serializer
def serialize_action(action: ToastAction) -> dict:
"""Serialize a toast action.
Args:
action: The toast action to serialize.
Returns:
The serialized toast action with on_click formatted to queue the given event.
"""
return {
"label": action.label,
"onClick": format.format_queue_events(action.on_click),
}
def _toast_callback_signature(toast: Var) -> list[Var]:
"""The signature for the toast callback, stripping out unserializable keys.
Args:
toast: The toast variable.
Returns:
A function call stripping non-serializable members of the toast object.
"""
return [
Var(
_js_expr=f"(() => {{let {{action, cancel, onDismiss, onAutoClose, ...rest}} = {toast!s}; return rest}})()"
)
]
class ToastProps(NoExtrasAllowedProps):
"""Props for the toast component."""
# Toast's title, renders above the description.
title: str | Var | None
# Toast's description, renders underneath the title.
description: str | Var | None
# Whether to show the close button.
close_button: bool | None
# Dark toast in light mode and vice versa.
invert: bool | None
# Control the sensitivity of the toast for screen readers
important: bool | None
# Time in milliseconds that should elapse before automatically closing the toast.
duration: int | None
# Position of the toast.
position: LiteralPosition | None
# If false, it'll prevent the user from dismissing the toast.
dismissible: bool | None
# TODO: fix serialization of icons for toast? (might not be possible yet)
# Icon displayed in front of toast's text, aligned vertically.
# icon: Icon | None = None # noqa: ERA001
# TODO: fix implementation for action / cancel buttons
# Renders a primary button, clicking it will close the toast.
action: ToastAction | None
# Renders a secondary button, clicking it will close the toast.
cancel: ToastAction | None
# Custom id for the toast.
id: str | Var | None
# Removes the default styling, which allows for easier customization.
unstyled: bool | None
# Custom style for the toast.
style: Style | None
# Class name for the toast.
class_name: str | None
# XXX: These still do not seem to work Custom style for the toast primary button.
action_button_styles: Style | None
# Custom style for the toast secondary button.
cancel_button_styles: Style | None
# The function gets called when either the close button is clicked, or the toast is swiped.
on_dismiss: Any | None
# Function that gets called when the toast disappears automatically after it's timeout (duration` prop).
on_auto_close: Any | None
def dict(self, *args: Any, **kwargs: Any) -> dict[str, Any]:
"""Convert the object to a dictionary.
Args:
*args: The arguments to pass to the base class.
**kwargs: The keyword arguments to pass to the base
Returns:
The object as a dictionary with ToastAction fields intact.
"""
d = super().dict(*args, **kwargs)
# Keep these fields as ToastAction so they can be serialized specially
if "action" in d:
d["action"] = self.action
if isinstance(self.action, dict):
d["action"] = ToastAction(**self.action)
if "cancel" in d:
d["cancel"] = self.cancel
if isinstance(self.cancel, dict):
d["cancel"] = ToastAction(**self.cancel)
if "onDismiss" in d:
d["onDismiss"] = format.format_queue_events(
self.on_dismiss, _toast_callback_signature
)
if "onAutoClose" in d:
d["onAutoClose"] = format.format_queue_events(
self.on_auto_close, _toast_callback_signature
)
return d
class Toaster(Component):
"""A Toaster Component for displaying toast notifications."""
library: str | None = "sonner@2.0.7"
tag = "Toaster"
theme: Var[str] = field(default=resolved_color_mode, doc="the theme of the toast")
rich_colors: Var[bool] = field(
default=LiteralVar.create(True), doc="whether to show rich colors"
)
expand: Var[bool] = field(
default=LiteralVar.create(True), doc="whether to expand the toast"
)
visible_toasts: Var[int] = field(
doc="the number of toasts that are currently visible"
)
position: Var[LiteralPosition] = field(
default=LiteralVar.create("bottom-right"), doc="the position of the toast"
)
close_button: Var[bool] = field(
default=LiteralVar.create(False), doc="whether to show the close button"
)
offset: Var[str] = field(doc="offset of the toast")
dir: Var[str] = field(doc="directionality of the toast (default: ltr)")
hotkey: Var[str] = field(
doc="Keyboard shortcut that will move focus to the toaster area."
)
invert: Var[bool] = field(doc="Dark toasts in light mode and vice versa.")
toast_options: Var[ToastProps] = field(
doc="These will act as default options for all toasts. See toast() for all available options."
)
gap: Var[int] = field(doc="Gap between toasts when expanded")
loading_icon: Var[Component] = field(doc="Changes the default loading icon")
pause_when_page_is_hidden: Var[bool] = field(
doc="Pauses toast timers when the page is hidden, e.g., when the tab is backgrounded, the browser is minimized, or the OS is locked."
)
def add_hooks(self) -> list[Var | str]:
"""Add hooks for the toaster component.
Returns:
The hooks for the toaster component.
"""
if self.library is None:
return []
hook = Var(
_js_expr=f"{toast_ref} = toast",
_var_data=VarData(
imports={
"$/utils/state": [ImportVar(tag="refs")],
self.library: [ImportVar(tag="toast", install=False)],
}
),
)
return [hook]
@staticmethod
def send_toast(
message: str | Var[str] = "",
level: str | None = None,
fallback_to_alert: bool = False,
**props,
) -> EventSpec:
"""Send a toast message.
Args:
message: The message to display.
level: The level of the toast.
fallback_to_alert: Whether to fallback to an alert if the toaster is not created.
**props: The options for the toast.
Returns:
The toast event.
Raises:
ValueError: If the Toaster component is not created.
"""
toast_command = (
ObjectVar.__getattr__(toast_ref.to(dict), level) if level else toast_ref
).to(FunctionVar)
if isinstance(message, Var):
props.setdefault("title", message)
message = ""
elif message == "" and "title" not in props and "description" not in props:
msg = "Toast message or title or description must be provided."
raise ValueError(msg)
if props:
args = LiteralVar.create(ToastProps(component_name="rx.toast", **props)) # pyright: ignore [reportCallIssue]
toast = toast_command.call(message, args)
else:
toast = toast_command.call(message)
if fallback_to_alert:
toast = ternary_operation(
toast_ref.bool(),
toast,
FunctionVar("window.alert").call(
Var
.create(
message
if isinstance(message, str) and message
else props.get("title", props.get("description", ""))
)
.to(str)
.replace("<br/>", "\n")
),
)
return run_script(toast)
@staticmethod
def toast_info(message: str | Var[str] = "", **kwargs: Any) -> EventSpec:
"""Display an info toast message.
Args:
message: The message to display.
**kwargs: Additional toast props.
Returns:
The toast event.
"""
return Toaster.send_toast(message, level="info", **kwargs)
@staticmethod
def toast_warning(message: str | Var[str] = "", **kwargs: Any) -> EventSpec:
"""Display a warning toast message.
Args:
message: The message to display.
**kwargs: Additional toast props.
Returns:
The toast event.
"""
return Toaster.send_toast(message, level="warning", **kwargs)
@staticmethod
def toast_error(message: str | Var[str] = "", **kwargs: Any) -> EventSpec:
"""Display an error toast message.
Args:
message: The message to display.
**kwargs: Additional toast props.
Returns:
The toast event.
"""
return Toaster.send_toast(message, level="error", **kwargs)
@staticmethod
def toast_success(message: str | Var[str] = "", **kwargs: Any) -> EventSpec:
"""Display a success toast message.
Args:
message: The message to display.
**kwargs: Additional toast props.
Returns:
The toast event.
"""
return Toaster.send_toast(message, level="success", **kwargs)
@staticmethod
def toast_loading(message: str | Var[str] = "", **kwargs: Any) -> EventSpec:
"""Display a loading toast message.
Args:
message: The message to display.
**kwargs: Additional toast props.
Returns:
The toast event.
"""
return Toaster.send_toast(message, level="loading", **kwargs)
@staticmethod
def toast_dismiss(id: Var[str] | str | None = None) -> EventSpec:
"""Dismiss a toast.
Args:
id: The id of the toast to dismiss.
Returns:
The toast dismiss event.
"""
dismiss_var_data = None
if isinstance(id, Var):
dismiss = f"{toast_ref}.dismiss({id!s})"
dismiss_var_data = id._get_all_var_data()
elif isinstance(id, str):
dismiss = f"{toast_ref}.dismiss('{id}')"
else:
dismiss = f"{toast_ref}.dismiss()"
dismiss_action = Var(
_js_expr=dismiss, _var_data=VarData.merge(dismiss_var_data)
)
return run_script(dismiss_action)
@classmethod
def create(cls, *children: Any, **props: Any) -> Component:
"""Create a toaster component.
Args:
*children: The children of the toaster.
**props: The properties of the toaster.
Returns:
The toaster component.
"""
return super().create(*children, **props)
# TODO: figure out why loading toast stay open forever when using level="loading" in toast()
class ToastNamespace(ComponentNamespace):
"""Namespace for toast components."""
provider = staticmethod(Toaster.create)
options = staticmethod(ToastProps)
info = staticmethod(Toaster.toast_info)
warning = staticmethod(Toaster.toast_warning)
error = staticmethod(Toaster.toast_error)
success = staticmethod(Toaster.toast_success)
loading = staticmethod(Toaster.toast_loading)
dismiss = staticmethod(Toaster.toast_dismiss)
__call__ = staticmethod(Toaster.send_toast)
toast = ToastNamespace()