-
Notifications
You must be signed in to change notification settings - Fork 954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get action
, cancel
, on_dismiss
and on_auto_close
working for rx.toast
#3216
Conversation
reflex/components/sonner/toast.py
Outdated
specs = [spec] | ||
events.extend(format.format_event(s) for s in specs) | ||
on_click = Var.create( | ||
f"() => {{queueEvents([{','.join(events)}], socket); processEvent(socket)}}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this could be exposed as a formatting method that user that wrap component can use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved to rx.utils.format.format_queue_events
i think this will be really useful for any rx.call_script
-based API that needs to pass callback functions that trigger reflex events, so good to formalize it.
and since i formalized it, it was also easy to add in on_dismiss
and on_auto_close
callbacks too 😎
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh nice, i realized i was also using a variant of this code inside call_script
itself, so i can clean that up too 😄
reflex/components/sonner/toast.py
Outdated
_var_is_local=False, | ||
) | ||
else: | ||
on_click = Var.create("() => null", _var_is_string=False, _var_is_local=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we can pass this kind of var here, it would probably be possible to allow it for built-in event triggers too right ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Little known feature; we actually do allow it if you set the _var_type
to EventChain
, it just gets rendered as is
reflex/reflex/components/component.py
Lines 497 to 499 in db47d39
elif isinstance(value, EventChain): | |
# Trust that the caller knows what they're doing passing an EventChain directly | |
return value |
Lines 572 to 581 in db47d39
if event_chain._var_type is not EventChain: | |
raise ValueError(f"Invalid event chain: {event_chain}") | |
return "".join( | |
[ | |
"(() => {", | |
format_var(event_chain), | |
f"; preventDefault({format_var(event_arg)})" if event_arg else "", | |
"})()", | |
] | |
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll have to remember that one 👍
@Lendemor idk if you want to take this into your branch (it has a refresh from |
I'm fine with both, let's just go with whatever is more convenient. |
4f71c26
to
4c75d06
Compare
Respect defaults set in ToastProvider toast_options when firing a toast with it's own ToastProps set.
Co-authored-by: Thomas Brandého <[email protected]>
Implement on_auto_close and on_dismiss callbacks inside ToastProps
4c75d06
to
0c8aad4
Compare
Replace duplicate logic in rx.call_script for handling the callback function.
0c8aad4
to
296952d
Compare
action
and cancel
working for rx.toastaction
, cancel
, on_dismiss
and on_auto_close
working for rx.toast
reflex/components/sonner/toast.py
Outdated
"onClick": format.format_queue_events(action.on_click), | ||
} | ||
|
||
|
||
class PropsBase(Base): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think PropsBase
should be exposed under _x
too.
I've been playing with some libs that make extensive use of hooks, and the feature from this would be needed for them too.
Maybe in its own file since the code for it is not specific to sonner/toast
This base class will be exposed via rx._x.PropsBase and can be shared by other wrapped components that need to pass a JS object full of extra props.
…rx.toast (reflex-dev#3216) * Get `action` and `cancel` working for rx.toast Respect defaults set in ToastProvider toast_options when firing a toast with it's own ToastProps set. * Update reflex/components/sonner/toast.py Co-authored-by: Thomas Brandého <[email protected]> * Move queueEvent formatting into rx.utils.format module Implement on_auto_close and on_dismiss callbacks inside ToastProps * Update rx.call_script to use new format.format_queue_events Replace duplicate logic in rx.call_script for handling the callback function. * Move PropsBase to reflex.components.props This base class will be exposed via rx._x.PropsBase and can be shared by other wrapped components that need to pass a JS object full of extra props. --------- Co-authored-by: Thomas Brandého <[email protected]>
Handle callbacks into reflex event handlers.
Respect defaults set in ToastProvider toast_options when firing a toast with it's own ToastProps set.
Create new
reflex.utils.format.format_queue_events
helper method that can be used to take arbitrary eventhandler/eventspec/lambda along with an args spec, and create a callback function for use withrx.call_script
-based APIs.