Skip to content
Draft
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
7 changes: 7 additions & 0 deletions dev/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>SciReactUI Dev</title>
<!-- Initialise the colour scheme before React loads -->
<script>
(function () {
var mode = localStorage.getItem("mui-mode") || "system";
document.documentElement.setAttribute("data-mode", mode);
})();
</script>
</head>
<body>
<div id="root"></div>
Expand Down
4 changes: 4 additions & 0 deletions dev/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import * as React from "react";
import * as ReactDOM from "react-dom/client";

import { InitColorSchemeScript } from "@mui/material";

import App from "./App";

ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<InitColorSchemeScript attribute="[data-mode='%s']" />
<App />
</React.StrictMode>,
);
65 changes: 43 additions & 22 deletions src/components/controls/ColourSchemeButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IconButton, IconButtonProps } from "@mui/material";
import { GlobalStyles, IconButton, IconButtonProps } from "@mui/material";
import { useColorScheme } from "@mui/material/styles";
import LightModeIcon from "@mui/icons-material/LightMode";
import BedtimeIcon from "@mui/icons-material/Bedtime";
Expand All @@ -7,30 +7,51 @@ export const ColourSchemeButton = (props: IconButtonProps) => {
const { mode, systemMode, setMode } = useColorScheme();

const resolvedMode = mode === "system" ? systemMode : mode;
const isReady = resolvedMode === "light" || resolvedMode === "dark";
const isDark = resolvedMode === "dark";

return (
<IconButton
aria-label={`Colour scheme switcher: ${resolvedMode ?? "unknown"}`}
{...props}
onClick={(event) => {
setMode(isDark ? "light" : "dark");
props.onClick?.(event);
}}
sx={(theme) => ({
ml: 1,
width: 32,
height: 32,
borderRadius: 1,
backgroundColor: theme.palette.surface.strong,
color: theme.palette.text.primary,
"&:hover": {
<>
<GlobalStyles
styles={`
html.ds-mode-changing *,
html.ds-mode-changing *::before,
html.ds-mode-changing *::after {
transition: none !important;
animation: none !important;
}
`}
/>

<IconButton
aria-label={`Colour scheme switcher: ${resolvedMode ?? "unknown"}`}
{...props}
onClick={(event) => {
document.documentElement.classList.add("ds-mode-changing");

setMode(isDark ? "light" : "dark");

window.setTimeout(() => {
document.documentElement.classList.remove("ds-mode-changing");
}, 250);

props.onClick?.(event);
}}
sx={(theme) => ({
ml: 1,
width: 32,
height: 32,
borderRadius: 1,
backgroundColor: theme.palette.surface.strong,
borderColor: theme.palette.border.subtle,
},
})}
>
{isDark ? <LightModeIcon /> : <BedtimeIcon />}
</IconButton>
color: theme.palette.text.primary,
"&:hover": {
backgroundColor: theme.palette.surface.strong,
borderColor: theme.palette.border.subtle,
},
})}
>
{isReady ? isDark ? <LightModeIcon /> : <BedtimeIcon /> : null}
</IconButton>
</>
);
};
Loading