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

fix: dark mode flickering by using native css vars #208

Merged
merged 2 commits into from
May 17, 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
5 changes: 2 additions & 3 deletions website/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CssBaseline } from "@mui/material";
import "../styles/globals.css";
import { AppRouterCacheProvider } from "@mui/material-nextjs/v13-appRouter";
import localFont from "next/font/local";
Expand Down Expand Up @@ -26,7 +25,7 @@ export default function RootLayout({
children: React.ReactNode;
}) {
return (
<html lang="en" className={inter.className}>
<html lang="en" className={inter.className} data-color-scheme="light">
<head>
{/* <link rel="icon" href="/favicon.png" /> */}
<link
Expand All @@ -43,7 +42,7 @@ export default function RootLayout({
}}
>
<ClientSideLayoutContext>
<CssBaseline />
{/* <CssBaseline /> */}
{children}
</ClientSideLayoutContext>
</AppRouterCacheProvider>
Expand Down
4 changes: 2 additions & 2 deletions website/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LandingPageLayout } from "@/components/layout";
import { FilterProvider } from "@/components/layout/filterContext";
import { SearchInput } from "@/components/searchInput";
import { Box, Typography, Link } from "@mui/material";

import type {} from "@mui/material/themeCssVarsAugmentation";
import localFont from "next/font/local";
import { Suspense } from "react";

Expand Down Expand Up @@ -35,7 +35,7 @@ export default function Home() {
fontVariantLigatures: "normal",
}}
>
Noogλe :: 2024
Noogλe
</Typography>
</Link>

Expand Down
60 changes: 44 additions & 16 deletions website/src/components/ClientSideLayoutContext.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
"use client";
import {
CssBaseline,
ThemeProvider,
createTheme,
useMediaQuery,
} from "@mui/material";
import { darkThemeOptions, lightThemeOptions } from "@/styles/theme";
import { ReactNode } from "react";
import { CssBaseline, useMediaQuery } from "@mui/material";
import { cssThemeOptions } from "@/styles/theme";
import { ReactNode, useEffect, useState } from "react";
import { Toaster } from "react-hot-toast";
import {
Experimental_CssVarsProvider as CssVarsProvider,
experimental_extendTheme as extendTheme,
getInitColorSchemeScript,
useColorScheme,
} from "@mui/material/styles";

const theme = extendTheme(cssThemeOptions);

const ModeTracker = () => {
const userPrefersDarkmode = useMediaQuery("(prefers-color-scheme: dark)");
const { setMode } = useColorScheme();

const [mounted, setMounted] = useState(false);

const darkTheme = createTheme(darkThemeOptions);
const lightTheme = createTheme(lightThemeOptions);
useEffect(
() => {
setMounted(true);

setMode(userPrefersDarkmode ? "dark" : "light");
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[userPrefersDarkmode]
);

if (!mounted) {
// for server-side rendering
// learn more at https://github.com/pacocoursey/next-themes#avoid-hydration-mismatch
return null;
}

return <></>;
};

export const ClientSideLayoutContext = ({
children,
}: {
children: ReactNode;
}) => {
const userPrefersDarkmode = useMediaQuery("(prefers-color-scheme: dark)");
return (
<ThemeProvider theme={userPrefersDarkmode ? darkTheme : lightTheme}>
<CssBaseline />
<Toaster />
{children}
</ThemeProvider>
<>
{getInitColorSchemeScript()}
<CssVarsProvider theme={theme}>
<CssBaseline />
<ModeTracker />
<Toaster />
{children}
</CssVarsProvider>
</>
);
};
15 changes: 8 additions & 7 deletions website/src/components/HighlightBaseline.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
"use client";
import { useTheme } from "@mui/material";
import { useColorScheme } from "@mui/material";
import { useEffect } from "react";

export const HighlightBaseline = () => {
const theme = useTheme();
const { mode } = useColorScheme();
useEffect(() => {
if (theme.palette.mode === "dark") {
// @ts-ignore - don't check type of css module
import("highlight.js/styles/github-dark.css");
console.log({ mode });
// @ts-ignore - don't check type of css module
import("highlight.js/styles/github-dark.css");
if (mode === "dark") {
} else {
// @ts-ignore - don't check type of css module
import("highlight.js/styles/github.css");
// import("highlight.js/styles/github.css");
}
}, [theme]);
}, [mode]);
return <></>;
};
35 changes: 16 additions & 19 deletions website/src/components/functionOfTheDay/functionOfTheDay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
CardContent,
CardHeader,
Divider,
useTheme,
styled,
} from "@mui/material";
import { useMemo, useState } from "react";
import seedrandom from "seedrandom";
Expand Down Expand Up @@ -37,10 +37,20 @@ function getRandomIntInclusive(min: number, max: number, config?: Config) {
return Math.floor(randomNumber * (max - min + 1) + min); // The maximum is inclusive and the minimum is inclusive
}

const FunctionCard = styled(
Card,
{}
)(({ theme }) => ({
width: "100%",
borderImageSlice: 1,
borderImageSource: `linear-gradient(to left, ${theme.vars.palette.info.light},${theme.vars.palette.error.main})`,
// : `linear-gradient(to left, ${info.light},${info.dark})`,
borderWidth: 4,
borderStyle: "solid",
}));

export const FunctionOfTheDay = () => {
const {
palette: { info, error },
} = useTheme();
// const { mode } = useColorScheme();

const todaysIdx = useMemo(
() => getRandomIntInclusive(0, data.length - 1),
Expand Down Expand Up @@ -74,20 +84,7 @@ export const FunctionOfTheDay = () => {
};

return (
<Card
elevation={0}
sx={{
width: "100%",
my: 5,
borderImageSlice: 1,
borderImageSource:
idx === todaysIdx
? `linear-gradient(to left, ${info.light},${error.main})`
: `linear-gradient(to left, ${info.light},${info.dark})`,
borderWidth: 4,
borderStyle: "solid",
}}
>
<FunctionCard sx={{ my: 5 }}>
<CardHeader
component="h2"
sx={{ py: 0 }}
Expand Down Expand Up @@ -129,6 +126,6 @@ export const FunctionOfTheDay = () => {
Next
</Button>
</CardActions>
</Card>
</FunctionCard>
);
};
6 changes: 2 additions & 4 deletions website/src/components/layout/Background.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"use client";
import { Box, useTheme } from "@mui/material";
import { Box } from "@mui/material";
import { ReactNode } from "react";

export const Background = ({ children }: { children: ReactNode }) => {
const theme = useTheme();
return (
<Box
sx={{
Expand All @@ -12,8 +11,7 @@ export const Background = ({ children }: { children: ReactNode }) => {
flexDirection: "column",
overflowX: "hidden",
overflowY: "scroll",
bgcolor:
theme.palette.mode === "light" ? "rgb(242, 248, 253)" : "#070c16",
bgcolor: "background.paper",
}}
>
{children}
Expand Down
7 changes: 2 additions & 5 deletions website/src/components/layout/header.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import { Box, LinearProgress, Link, useTheme } from "@mui/material";
import { Box, LinearProgress, Link } from "@mui/material";
import { SearchInput } from "../searchInput";
import { Filter } from "../filter";
import { Suspense } from "react";
Expand All @@ -13,8 +13,6 @@ const fira = localFont({
});

export const Header = () => {
const theme = useTheme();

return (
<>
<Box
Expand All @@ -24,8 +22,7 @@ export const Header = () => {
width: "100%",
py: 1.2,
zIndex: 1000,
backgroundColor:
theme.palette.mode === "light" ? "primary.main" : "#101010",
backgroundColor: "header.default",
display: "grid",
gridTemplateColumns: {
xs: "1fr",
Expand Down
2 changes: 2 additions & 0 deletions website/src/components/layout/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "@mui/material";
import GitHubIcon from "@mui/icons-material/GitHub";
import { Background } from "./Background";
import { ThemeSwitch } from "./themeSwitch";

export interface LayoutProps {
children: React.ReactNode;
Expand Down Expand Up @@ -38,6 +39,7 @@ export function LandingPageLayout(props: LayoutProps) {
<Background>
<Box sx={{ textAlign: "end", px: 2, py: 1 }}>
<SocialIcons />
<ThemeSwitch />
</Box>
<main
style={{
Expand Down
25 changes: 25 additions & 0 deletions website/src/components/layout/themeSwitch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use client";
import { DarkMode, LightMode } from "@mui/icons-material";
import { IconButton, Tooltip, useColorScheme } from "@mui/material";

export const ThemeSwitch = () => {
const { mode, setMode } = useColorScheme();

return (
<>
{mode === "dark" ? (
<Tooltip title="Turn on the light">
<IconButton onClick={() => setMode("light")}>
<LightMode />
</IconButton>
</Tooltip>
) : (
<Tooltip title="Turn on the dark">
<IconButton onClick={() => setMode("dark")}>
<DarkMode />
</IconButton>
</Tooltip>
)}
</>
);
};
15 changes: 8 additions & 7 deletions website/src/components/markdownPreview/MarkdownPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import { useTheme } from "@mui/material";
import { useColorScheme } from "@mui/material";
import nix from "highlight.js/lib/languages/nix";
import haskell from "highlight.js/lib/languages/haskell";
import bash from "highlight.js/lib/languages/bash";
Expand All @@ -23,16 +23,17 @@ interface MarkdownPreviewProps {
}
export const MarkdownPreview = (props: MarkdownPreviewProps) => {
const { description } = props;
const theme = useTheme();
const { mode } = useColorScheme();
useEffect(() => {
if (theme.palette.mode === "dark") {
// @ts-ignore - don't check type of css module
import("highlight.js/styles/github-dark.css");
console.log({ mode });
// @ts-ignore - don't check type of css module
import("highlight.js/styles/github-dark.css");
if (mode === "dark") {
} else {
// @ts-ignore - don't check type of css module
import("highlight.js/styles/github.css");
// import("highlight.js/styles/github.css");
}
}, [theme]);
}, [mode]);

return (
<>
Expand Down
72 changes: 0 additions & 72 deletions website/src/components/themeRegistry.tsx

This file was deleted.

Loading
Loading