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

[REF-2588]Clear color mode local storage for dev mode #3548

Merged
merged 9 commits into from
Jun 28, 2024
2 changes: 2 additions & 0 deletions integration/test_client_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ def set_sub_sub(var: str, value: str):
assert not driver.get_cookies()
local_storage_items = local_storage.items()
local_storage_items.pop("chakra-ui-color-mode", None)
local_storage_items.pop("last_compiled_time", None)
assert not local_storage_items

# set some cookies and local storage values
Expand Down Expand Up @@ -426,6 +427,7 @@ def set_sub_sub(var: str, value: str):

local_storage_items = local_storage.items()
local_storage_items.pop("chakra-ui-color-mode", None)
local_storage_items.pop("last_compiled_time", None)
assert (
local_storage_items.pop("state.client_side_state.client_side_sub_state.l1")
== "l1 value"
Expand Down
1 change: 0 additions & 1 deletion reflex/.templates/jinja/web/pages/_app.js.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import '/styles/styles.css'
import { EventLoopProvider, StateProvider, defaultColorMode } from "/utils/context.js";
import { ThemeProvider } from 'next-themes'


{% for custom_code in custom_codes %}
{{custom_code}}
{% endfor %}
Expand Down
2 changes: 2 additions & 0 deletions reflex/.templates/jinja/web/utils/context.js.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export const initialEvents = () => []

export const isDevMode = {{ is_dev_mode|json_dumps }}

export const lastCompiledTimeStamp = {{ last_compiled_time|json_dumps }}

export function UploadFilesProvider({ children }) {
const [filesById, setFilesById] = useState({})
refs["__clear_selected_files"] = (id) => setFilesById(filesById => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import { useTheme } from "next-themes";
import { useEffect, useState } from "react";
import { ColorModeContext, defaultColorMode } from "/utils/context.js";
import {
ColorModeContext,
defaultColorMode,
isDevMode,
lastCompiledTimeStamp
} from "/utils/context.js";

export default function RadixThemesColorModeProvider({ children }) {
const { theme, resolvedTheme, setTheme } = useTheme();
const [rawColorMode, setRawColorMode] = useState(defaultColorMode);
const [resolvedColorMode, setResolvedColorMode] = useState("dark");

useEffect(() => {
if (isDevMode) {
const lastCompiledTimeInLocalStorage =
localStorage.getItem("last_compiled_time");
if (
lastCompiledTimeInLocalStorage &&
lastCompiledTimeInLocalStorage !== lastCompiledTimeStamp
) {
// on app startup, make sure the application color mode is persisted correctly.
setTheme(defaultColorMode);
localStorage.setItem("last_compiled_time", lastCompiledTimeStamp);
return;
}
}
setRawColorMode(theme);
setResolvedColorMode(resolvedTheme);
}, [theme, resolvedTheme]);
Expand All @@ -19,7 +37,7 @@ export default function RadixThemesColorModeProvider({ children }) {
const allowedModes = ["light", "dark", "system"];
if (!allowedModes.includes(mode)) {
console.error(
`Invalid color mode "${mode}". Defaulting to "${defaultColorMode}".`
`Invalid color mode "${mode}". Defaulting to "${defaultColorMode}".`,
);
mode = defaultColorMode;
}
Expand Down
7 changes: 6 additions & 1 deletion reflex/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import os
from datetime import datetime
from pathlib import Path
from typing import Dict, Iterable, Optional, Type, Union

Expand Down Expand Up @@ -79,20 +80,24 @@ def _compile_contexts(state: Optional[Type[BaseState]], theme: Component | None)
The compiled context file.
"""
appearance = getattr(theme, "appearance", None)
if appearance is None:
if appearance is None or Var.create_safe(appearance)._var_name == "inherit":
appearance = SYSTEM_COLOR_MODE

last_compiled_time = str(datetime.now())
return (
templates.CONTEXT.render(
initial_state=utils.compile_state(state),
state_name=state.get_name(),
client_storage=utils.compile_client_storage(state),
is_dev_mode=not is_prod_mode(),
last_compiled_time=last_compiled_time,
default_color_mode=appearance,
)
if state
else templates.CONTEXT.render(
is_dev_mode=not is_prod_mode(),
default_color_mode=appearance,
last_compiled_time=last_compiled_time,
)
)

Expand Down
Loading