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

Abstract color_mode related Var creation #3533

Merged
merged 4 commits into from
Jun 20, 2024
Merged

Conversation

masenf
Copy link
Collaborator

@masenf masenf commented Jun 20, 2024

  • Each color_mode-related Var only destructs the name it needs to use from ColorModeContext.
    • Create the BaseVar via helper function.
  • Allow set_color_mode to take a parameter at compile time as a CallableVar.
    • Replace similar logic in color_mode.py
  • Update type hinting of Var._replace to indicate that it returns BaseVar.
  • Fix dependency array creation for useCallback when the hook uses object destructuring (only worked with array before)

Sample Code

import reflex as rx


class State(rx.State):
    """The app state."""

    to_mode: str = "system"


def index() -> rx.Component:
    return rx.container(
        rx.color_mode.button(position="top-right"),
        rx.color_mode.button(position="top-left", allow_system=True),
        rx.vstack(
            # Uncomment the following line and _always_ get a hydration error.
            # rx.text("resolvedColorMode: ", rx.style.resolved_color_mode),
            rx.text("colorMode: ", rx.style.color_mode),
            rx.button("To System", on_click=rx.style.set_color_mode(rx.style.SYSTEM_COLOR_MODE)),
            rx.button("To {State.to_mode}", on_click=rx.style.set_color_mode(State.to_mode)),
            rx.button("Poor Man's Color Toggle", on_click=rx.style.set_color_mode(
                rx.color_mode_cond(
                    light=rx.style.DARK_COLOR_MODE,
                    dark=rx.style.LIGHT_COLOR_MODE,
                )
            )),
            spacing="5",
            justify="center",
            min_height="85vh",
        ),
    )


app = rx.App()
app.add_page(index)

(If you hit hydration errors, simply click out of the error, reset the color mode to System, and refresh. The errors should go away)

Ensure the useCallback dependency array for event handlers also handled
destructuring objects and the `...` rest parameter.

Avoid hooks that are not variable declarations.
* Allow `set_color_mode` to take a parameter at compile time
* Update type hinting of `Var._replace` to indicate that it returns BaseVar
`set_color_mode` is now a CallableVar and uses very similar logic internally,
so this bit of code can be replaced.
Lendemor
Lendemor previously approved these changes Jun 20, 2024
@masenf masenf merged commit 958c4fa into main Jun 20, 2024
47 checks passed
@masenf masenf deleted the masenf/color-mode-vars branch June 20, 2024 23:46
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

3 participants