Skip to content

Commit ceeb4c3

Browse files
committed
Fixed to remove flashes on load and on change of dark/light
- Updated load based on direction in Installation guide - Found that adding initialisation in HTML helps - updated button to reduce flashing on both onLoad and reload
1 parent a8474b2 commit ceeb4c3

3 files changed

Lines changed: 54 additions & 22 deletions

File tree

dev/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>SciReactUI Dev</title>
7+
<!-- Initialise the colour scheme before React loads -->
8+
<script>
9+
(function () {
10+
var mode = localStorage.getItem("mui-mode") || "system";
11+
document.documentElement.setAttribute("data-mode", mode);
12+
})();
13+
</script>
714
</head>
815
<body>
916
<div id="root"></div>

dev/src/main.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import * as React from "react";
22
import * as ReactDOM from "react-dom/client";
3+
4+
import { InitColorSchemeScript } from "@mui/material";
5+
36
import App from "./App";
47

58
ReactDOM.createRoot(document.getElementById("root")!).render(
69
<React.StrictMode>
10+
<InitColorSchemeScript attribute="[data-mode='%s']" />
711
<App />
812
</React.StrictMode>,
913
);
Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IconButton, IconButtonProps } from "@mui/material";
1+
import { GlobalStyles, IconButton, IconButtonProps } from "@mui/material";
22
import { useColorScheme } from "@mui/material/styles";
33
import LightModeIcon from "@mui/icons-material/LightMode";
44
import BedtimeIcon from "@mui/icons-material/Bedtime";
@@ -7,30 +7,51 @@ export const ColourSchemeButton = (props: IconButtonProps) => {
77
const { mode, systemMode, setMode } = useColorScheme();
88

99
const resolvedMode = mode === "system" ? systemMode : mode;
10+
const isReady = resolvedMode === "light" || resolvedMode === "dark";
1011
const isDark = resolvedMode === "dark";
1112

1213
return (
13-
<IconButton
14-
aria-label={`Colour scheme switcher: ${resolvedMode ?? "unknown"}`}
15-
{...props}
16-
onClick={(event) => {
17-
setMode(isDark ? "light" : "dark");
18-
props.onClick?.(event);
19-
}}
20-
sx={(theme) => ({
21-
ml: 1,
22-
width: 32,
23-
height: 32,
24-
borderRadius: 1,
25-
backgroundColor: theme.palette.surface.strong,
26-
color: theme.palette.text.primary,
27-
"&:hover": {
14+
<>
15+
<GlobalStyles
16+
styles={`
17+
html.ds-mode-changing *,
18+
html.ds-mode-changing *::before,
19+
html.ds-mode-changing *::after {
20+
transition: none !important;
21+
animation: none !important;
22+
}
23+
`}
24+
/>
25+
26+
<IconButton
27+
aria-label={`Colour scheme switcher: ${resolvedMode ?? "unknown"}`}
28+
{...props}
29+
onClick={(event) => {
30+
document.documentElement.classList.add("ds-mode-changing");
31+
32+
setMode(isDark ? "light" : "dark");
33+
34+
window.setTimeout(() => {
35+
document.documentElement.classList.remove("ds-mode-changing");
36+
}, 250);
37+
38+
props.onClick?.(event);
39+
}}
40+
sx={(theme) => ({
41+
ml: 1,
42+
width: 32,
43+
height: 32,
44+
borderRadius: 1,
2845
backgroundColor: theme.palette.surface.strong,
29-
borderColor: theme.palette.border.subtle,
30-
},
31-
})}
32-
>
33-
{isDark ? <LightModeIcon /> : <BedtimeIcon />}
34-
</IconButton>
46+
color: theme.palette.text.primary,
47+
"&:hover": {
48+
backgroundColor: theme.palette.surface.strong,
49+
borderColor: theme.palette.border.subtle,
50+
},
51+
})}
52+
>
53+
{isReady ? isDark ? <LightModeIcon /> : <BedtimeIcon /> : null}
54+
</IconButton>
55+
</>
3556
);
3657
};

0 commit comments

Comments
 (0)