Skip to content
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

add toast component #3186

Merged
merged 14 commits into from
May 3, 2024
Merged

add toast component #3186

merged 14 commits into from
May 3, 2024

Conversation

Lendemor
Copy link
Collaborator

class ToastState(rx.State):
    def trigger_toast(self):
        return rx.toast("Hello World 2")

@rx.page(route="/toast", title="Toast")
def index():
    return rx.center(
        rx.color_mode.button(),
        rx.toast.provider(),
        rx.vstack(
            rx.button("Toast", on_click=rx.toast("Hello World 1")),
            rx.grid(
                rx.icon_button(
                    "arrow-up-left", on_click=rx.toast("Hello World", position="top-left"),
                ),
                rx.icon_button(
                    "arrow-up-right", on_click=rx.toast("Hello World", position="top-right"),
                ),
                rx.icon_button(
                    "arrow-down-left", on_click=rx.toast("Hello World", position="bottom-left"),
                ),
                rx.icon_button(
                    "arrow-down-right", on_click=rx.toast("Hello World", position="bottom-right"),
                ),
                columns="2",
            ),
            rx.button("Toast", on_click=ToastState.trigger_toast),
            rx.button("Toast info", on_click=rx.toast.info("Hello World")),
            rx.button("Toast success", on_click=rx.toast.success("Hello World")),
            rx.button("Toast warning", on_click=rx.toast.warning("Hello World")),
            rx.button("Toast error", on_click=rx.toast.error("Hello World")),
         ),
         width="100vw",
         height="100vh",
     )

@Lendemor Lendemor changed the title Lendemor/add toast component add toast component Apr 29, 2024
@Alek99
Copy link
Contributor

Alek99 commented Apr 30, 2024

Since we plan on having an implicit toast on every page via the new connection banner, can we get rid of needed to add rx.toast.provider()

@Lendemor
Copy link
Collaborator Author

Since we plan on having an implicit toast on every page via the new connection banner, can we get rid of needed to add rx.toast.provider()

I'm not too sure about that, maybe we can have the new connection banner include it by default, but we should still leave the option for people to use their own toast system too I think.

reflex/components/sonner/toast.py Show resolved Hide resolved
reflex/components/sonner/toast.py Show resolved Hide resolved
reflex/components/sonner/toast.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@masenf masenf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a good way to style the toasts?

i tried passing styles to the toast.provider:

rx.toast.provider(
        style={
            "[data-sonner-toast][data-type=warning]": {
                "background_color": f"{rx.color('amber', 7)} !important",
                "_hover": {
                    "background_color": f"{rx.color('amber', 9)} !important",
                },
            },
        }
    )

But this is a bit messy and requires the use of !important, which is not great, and if we bundle a toast provider in the future for the connection banner, then i don't think this will be exposed outside of global styles or a custom stylesheet. Not sure if it will come up often, but worth thinking about.

reflex/utils/pyi_generator.py Outdated Show resolved Hide resolved
reflex/components/sonner/toast.py Outdated Show resolved Hide resolved
duration: int = 4000
position: LiteralPosition = "bottom-right"
dismissible: bool = True
icon: Optional[Icon] = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if i pass a lucide icon here, it gives a frontend error and crashes

Unhandled Runtime Error
Error: Objects are not valid as a React child (found: object with keys {children, library, lib_dependencies, transpile_packages, tag, style, event_triggers, alias, is_default, key, id, class_name, special_props, autofocus, custom_attrs, State, size, color}). If you meant to render a collection of children, use an array instead.

reflex/components/sonner/toast.py Show resolved Hide resolved
@Lendemor Lendemor mentioned this pull request May 2, 2024
@Lendemor
Copy link
Collaborator Author

Lendemor commented May 2, 2024

is there a good way to style the toasts?

i tried passing styles to the toast.provider:

rx.toast.provider(
        style={
            "[data-sonner-toast][data-type=warning]": {
                "background_color": f"{rx.color('amber', 7)} !important",
                "_hover": {
                    "background_color": f"{rx.color('amber', 9)} !important",
                },
            },
        }
    )

But this is a bit messy and requires the use of !important, which is not great, and if we bundle a toast provider in the future for the connection banner, then i don't think this will be exposed outside of global styles or a custom stylesheet. Not sure if it will come up often, but worth thinking about.

toast will support style props so it'll be simpler

rx.button(
    "Toast",
    on_click=rx.toast(
        "Hello World 1",
        style={"background": "red", "color": "white"},
    ),
),

@picklelo
Copy link
Contributor

picklelo commented May 2, 2024

Can we allow passing in the style props as kwargs here as well:

rx.toast(
    "Hello World 1",
    color="red",
)

@Lendemor
Copy link
Collaborator Author

Lendemor commented May 2, 2024

Can we allow passing in the style props as kwargs here as well:

rx.toast(
    "Hello World 1",
    color="red",
)

Can we add in a follow up? I don't think this change is trivial.

Copy link
Collaborator

@masenf masenf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Posted #3216 to get the action and cancel working, which i think are kind of essential for using toasts effectively

It also fixes some of the issues mentioned below

position: Var[LiteralPosition] = Var.create_safe("bottom-right")

# whether to show the close button
close_button: Var[bool] = Var.create_safe(False)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When i set this to True, the toasts still don't have a close button

invert: Var[bool]

# These will act as default options for all toasts. See toast() for all available options.
toast_options: Var[ToastProps]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When i set defaults here, they do not seem to be respected.

I tried setting it like

rx.toast.provider(toast_options=rx.toast.options(close_button=True, duration=300), close_button=True)

And the toasts still use the default 4000ms duration and do not have close buttons, unless i pass no options at all.

For example, i'm trying to toast like this

                    yield rx.toast.warning(
                        f"Removed {item.text} from {list_name}",
                        description="Whoops, this was irreversible"
                    )

It seems that any options passed myself overwrite all of the toaster options with the original defaults.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so i updated ToastProps to make all of the fields optional, which seemed to help; but now i'm finding that some of the fields in ToastProps don't take effect when passed to the toast_options prop.

  • description
  • position
  • close_button
    (maybe others, didn't try them all)

@picklelo picklelo merged commit 1817c30 into main May 3, 2024
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants