Skip to content

Commit 421bfa0

Browse files
authored
Apply app theme color_mode/appearance as next-themes default (#2654)
* Apply app theme color_mode/appearance as next-themes default * compiler: blacken
1 parent 9534957 commit 421bfa0

File tree

7 files changed

+25
-12
lines changed

7 files changed

+25
-12
lines changed

reflex/.templates/jinja/web/pages/_app.js.jinja2

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import '/styles/styles.css'
55
{% endblock %}
66

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

1111

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

2929
export default function MyApp({ Component, pageProps }) {
3030
return (
31-
<ThemeProvider defaultTheme="light" storageKey="chakra-ui-color-mode" attribute="class">
31+
<ThemeProvider defaultTheme={ defaultColorMode } storageKey="chakra-ui-color-mode" attribute="class">
3232
<AppWrap>
3333
<StateProvider>
3434
<EventLoopProvider>

reflex/.templates/jinja/web/utils/context.js.jinja2

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const initialState = {{ initial_state|json_dumps }}
77
export const initialState = {}
88
{% endif %}
99

10+
export const defaultColorMode = "{{ default_color_mode }}"
1011
export const ColorModeContext = createContext(null);
1112
export const UploadFilesContext = createContext(null);
1213
export const DispatchContext = createContext(null);

reflex/.templates/web/components/reflex/radix_themes_color_mode_provider.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { useTheme } from "next-themes"
22
import { useEffect, useState } from "react"
3-
import { ColorModeContext } from "/utils/context.js"
3+
import { ColorModeContext, defaultColorMode } from "/utils/context.js"
44

55

66
export default function RadixThemesColorModeProvider({ children }) {
77
const {theme, setTheme} = useTheme()
8-
const [colorMode, setColorMode] = useState("light")
8+
const [colorMode, setColorMode] = useState(defaultColorMode)
99

10-
useEffect(() => setColorMode(theme), [theme])
10+
useEffect(() => {
11+
setColorMode(theme)
12+
}, [theme])
1113

1214
const toggleColorMode = () => {
1315
setTheme(theme === "light" ? "dark" : "light")

reflex/app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ def submit_work(fn, *args, **kwargs):
785785
submit_work(compiler.compile_theme, style=self.style)
786786

787787
# Compile the contexts.
788-
submit_work(compiler.compile_contexts, self.state)
788+
submit_work(compiler.compile_contexts, self.state, self.theme)
789789

790790
# Compile the Tailwind config.
791791
if config.tailwind is not None:

reflex/compiler/compiler.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
)
1717
from reflex.config import get_config
1818
from reflex.state import BaseState
19+
from reflex.style import LIGHT_COLOR_MODE
1920
from reflex.utils.imports import ImportVar
2021

2122

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

6970

70-
def _compile_contexts(state: Optional[Type[BaseState]]) -> str:
71+
def _compile_contexts(state: Optional[Type[BaseState]], theme: Component) -> str:
7172
"""Compile the initial state and contexts.
7273
7374
Args:
7475
state: The app state.
76+
theme: The top-level app theme.
7577
7678
Returns:
7779
The compiled context file.
@@ -82,9 +84,13 @@ def _compile_contexts(state: Optional[Type[BaseState]]) -> str:
8284
state_name=state.get_name(),
8385
client_storage=utils.compile_client_storage(state),
8486
is_dev_mode=_is_dev_mode(),
87+
default_color_mode=getattr(theme, "appearance", LIGHT_COLOR_MODE),
8588
)
8689
if state
87-
else templates.CONTEXT.render(is_dev_mode=_is_dev_mode())
90+
else templates.CONTEXT.render(
91+
is_dev_mode=_is_dev_mode(),
92+
default_color_mode=getattr(theme, "appearance", LIGHT_COLOR_MODE),
93+
)
8894
)
8995

9096

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

347353

348-
def compile_contexts(state: Optional[Type[BaseState]]) -> tuple[str, str]:
354+
def compile_contexts(
355+
state: Optional[Type[BaseState]],
356+
theme: Component,
357+
) -> tuple[str, str]:
349358
"""Compile the initial state / context.
350359
351360
Args:
352361
state: The app state.
362+
theme: The top-level app theme.
353363
354364
Returns:
355365
The path and code of the compiled context.
356366
"""
357367
# Get the path for the output file.
358368
output_path = utils.get_context_path()
359369

360-
return output_path, _compile_contexts(state)
370+
return output_path, _compile_contexts(state, theme)
361371

362372

363373
def compile_page(

reflex/components/radix/themes/color_mode.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def create(cls, *children, **props):
7171
"""
7272
return Switch.create(
7373
*children,
74-
is_checked=color_mode != LIGHT_COLOR_MODE,
74+
checked=color_mode != LIGHT_COLOR_MODE,
7575
on_change=toggle_color_mode,
7676
**props,
7777
)

reflex/constants/installer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class Commands(SimpleNamespace):
107107
"json5": "2.2.3",
108108
"next": "14.0.1",
109109
"next-sitemap": "4.1.8",
110-
"next-themes": "0.2.0",
110+
"next-themes": "0.2.1",
111111
"react": "18.2.0",
112112
"react-dom": "18.2.0",
113113
"socket.io-client": "4.6.1",

0 commit comments

Comments
 (0)