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

Apply app theme color_mode/appearance as next-themes default #2654

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions reflex/.templates/jinja/web/pages/_app.js.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import '/styles/styles.css'
{% endblock %}

{% block declaration %}
import { EventLoopProvider, StateProvider } from "/utils/context.js";
import { EventLoopProvider, StateProvider, defaultColorMode } from "/utils/context.js";
import { ThemeProvider } from 'next-themes'


Expand All @@ -28,7 +28,7 @@ function AppWrap({children}) {

export default function MyApp({ Component, pageProps }) {
return (
<ThemeProvider defaultTheme="light" storageKey="chakra-ui-color-mode" attribute="class">
<ThemeProvider defaultTheme={ defaultColorMode } storageKey="chakra-ui-color-mode" attribute="class">
<AppWrap>
<StateProvider>
<EventLoopProvider>
Expand Down
1 change: 1 addition & 0 deletions reflex/.templates/jinja/web/utils/context.js.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const initialState = {{ initial_state|json_dumps }}
export const initialState = {}
{% endif %}

export const defaultColorMode = "{{ default_color_mode }}"
export const ColorModeContext = createContext(null);
export const UploadFilesContext = createContext(null);
export const DispatchContext = createContext(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { useTheme } from "next-themes"
import { useEffect, useState } from "react"
import { ColorModeContext } from "/utils/context.js"
import { ColorModeContext, defaultColorMode } from "/utils/context.js"


export default function RadixThemesColorModeProvider({ children }) {
const {theme, setTheme} = useTheme()
const [colorMode, setColorMode] = useState("light")
const [colorMode, setColorMode] = useState(defaultColorMode)

useEffect(() => setColorMode(theme), [theme])
useEffect(() => {
setColorMode(theme)
}, [theme])

const toggleColorMode = () => {
setTheme(theme === "light" ? "dark" : "light")
Expand Down
2 changes: 1 addition & 1 deletion reflex/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ def submit_work(fn, *args, **kwargs):
submit_work(compiler.compile_theme, style=self.style)

# Compile the contexts.
submit_work(compiler.compile_contexts, self.state)
submit_work(compiler.compile_contexts, self.state, self.theme)

# Compile the Tailwind config.
if config.tailwind is not None:
Expand Down
18 changes: 14 additions & 4 deletions reflex/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from reflex.config import get_config
from reflex.state import BaseState
from reflex.style import LIGHT_COLOR_MODE
from reflex.utils.imports import ImportVar


Expand Down Expand Up @@ -67,11 +68,12 @@ def _is_dev_mode() -> bool:
return os.environ.get("REFLEX_ENV_MODE", "dev") == "dev"


def _compile_contexts(state: Optional[Type[BaseState]]) -> str:
def _compile_contexts(state: Optional[Type[BaseState]], theme: Component) -> str:
"""Compile the initial state and contexts.

Args:
state: The app state.
theme: The top-level app theme.

Returns:
The compiled context file.
Expand All @@ -82,9 +84,13 @@ def _compile_contexts(state: Optional[Type[BaseState]]) -> str:
state_name=state.get_name(),
client_storage=utils.compile_client_storage(state),
is_dev_mode=_is_dev_mode(),
default_color_mode=getattr(theme, "appearance", LIGHT_COLOR_MODE),
)
if state
else templates.CONTEXT.render(is_dev_mode=_is_dev_mode())
else templates.CONTEXT.render(
is_dev_mode=_is_dev_mode(),
default_color_mode=getattr(theme, "appearance", LIGHT_COLOR_MODE),
)
)


Expand Down Expand Up @@ -345,19 +351,23 @@ def compile_theme(style: ComponentStyle) -> tuple[str, str]:
return output_path, code


def compile_contexts(state: Optional[Type[BaseState]]) -> tuple[str, str]:
def compile_contexts(
state: Optional[Type[BaseState]],
theme: Component,
) -> tuple[str, str]:
"""Compile the initial state / context.

Args:
state: The app state.
theme: The top-level app theme.

Returns:
The path and code of the compiled context.
"""
# Get the path for the output file.
output_path = utils.get_context_path()

return output_path, _compile_contexts(state)
return output_path, _compile_contexts(state, theme)


def compile_page(
Expand Down
2 changes: 1 addition & 1 deletion reflex/components/radix/themes/color_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def create(cls, *children, **props):
"""
return Switch.create(
*children,
is_checked=color_mode != LIGHT_COLOR_MODE,
checked=color_mode != LIGHT_COLOR_MODE,
on_change=toggle_color_mode,
**props,
)
Expand Down
2 changes: 1 addition & 1 deletion reflex/constants/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class Commands(SimpleNamespace):
"json5": "2.2.3",
"next": "14.0.1",
"next-sitemap": "4.1.8",
"next-themes": "0.2.0",
"next-themes": "0.2.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"socket.io-client": "4.6.1",
Expand Down
Loading