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

Automatic dark theme is not working #289

Open
Azq2 opened this issue Apr 20, 2024 · 1 comment
Open

Automatic dark theme is not working #289

Azq2 opened this issue Apr 20, 2024 · 1 comment

Comments

@Azq2
Copy link

Azq2 commented Apr 20, 2024

function App() {
	let drawerWidth = 240;

	let prefersDarkMode = useMediaQuery('(prefers-color-scheme: dark)');
	let theme = createMemo(() => {
		console.log('prefersDarkMode=', prefersDarkMode());
		return createTheme({
			palette: {
				mode: prefersDarkMode() ? 'dark' : 'light',
				primary: {
					main: prefersDarkMode() ? '#b39ddb' : '#673ab7',
				},
				secondary: {
					main: prefersDarkMode() ? '#673ab7' : '#651fff',
				},
			},
			typography: {
				fontFamily: [
					'-apple-system',
					'BlinkMacSystemFont',
					'"Segoe UI"',
					'Roboto',
					'"Helvetica Neue"',
					'Arial',
					'sans-serif',
					'"Apple Color Emoji"',
					'"Segoe UI Emoji"',
					'"Segoe UI Symbol"',
				].join(','),
			}
		});
	});

	return (
		<SerialProvider>
			<ThemeProvider theme={theme()}>
				....
			</ThemeProvider>
		</SerialProvider>
	);
}

I see console.log('prefersDarkMode=', prefersDarkMode()); on the console, but the theme is not changed.

@s0ftik3
Copy link

s0ftik3 commented Jun 11, 2024

Encountered the same issue just recently.
Basically you don't need to use createMemo for theme variable.
Below is the code that worked for me.

const palette = createMemo(() => {
    return createPalette(mode() === 'light' ? light : dark)
})
const theme = createTheme({ palette })
    
<ThemeProvider theme={theme}></...>

yet you can read this to see more details about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants