From 4930c3b48169cb0031a861363345a828688fe61f Mon Sep 17 00:00:00 2001 From: Olivier Tassinari Date: Tue, 12 Sep 2023 01:04:07 +0200 Subject: [PATCH] [website] Fix lighthouse issues Improve the experience on the pages. --- .../src/components/header/ThemeModeToggle.tsx | 52 ++++++++++++++----- docs/src/layouts/AppHeader.tsx | 34 +----------- docs/src/modules/components/AppSearch.js | 2 +- 3 files changed, 42 insertions(+), 46 deletions(-) diff --git a/docs/src/components/header/ThemeModeToggle.tsx b/docs/src/components/header/ThemeModeToggle.tsx index ce03dfc1d7db37..ff22e076ae3797 100644 --- a/docs/src/components/header/ThemeModeToggle.tsx +++ b/docs/src/components/header/ThemeModeToggle.tsx @@ -4,6 +4,8 @@ import IconButton from '@mui/material/IconButton'; import Tooltip from '@mui/material/Tooltip'; import DarkModeOutlined from '@mui/icons-material/DarkModeOutlined'; import LightModeOutlined from '@mui/icons-material/LightModeOutlined'; +import useMediaQuery from '@mui/material/useMediaQuery'; +import { useChangeTheme } from 'docs/src/modules/components/ThemeContext'; function CssVarsModeToggle(props: { onChange: (checked: boolean) => void }) { const [mounted, setMounted] = React.useState(false); @@ -34,29 +36,55 @@ function CssVarsModeToggle(props: { onChange: (checked: boolean) => void }) { ); } -export default function ThemeModeToggle(props: { - checked: boolean; - onChange: (checked: boolean) => void; -}) { +export default function ThemeModeToggle() { const theme = useTheme(); + const changeTheme = useChangeTheme(); + const [mode, setMode] = React.useState(null); + const prefersDarkMode = useMediaQuery('(prefers-color-scheme: dark)'); + + React.useEffect(() => { + let initialMode = 'system'; + try { + initialMode = localStorage.getItem('mui-mode') || initialMode; + } catch (error) { + // do nothing + } + setMode(initialMode); + }, []); + + const handleChangeThemeMode = (checked: boolean) => { + const paletteMode = checked ? 'dark' : 'light'; + setMode(paletteMode); + + try { + localStorage.setItem('mui-mode', paletteMode); // syncing with homepage, can be removed once all pages are migrated to CSS variables + } catch (error) { + // do nothing + } + changeTheme({ paletteMode }); + }; + + if (mode === null) { + return ; + } + if (theme.vars) { // Temporarily renders conditionally because `useColorScheme` could not be used in the pages that haven't migrated to CSS theme variables. - return ; + return ; } + + const checked = mode === 'system' ? prefersDarkMode : mode === 'dark'; + return ( - + { - props.onChange(!props.checked); + handleChangeThemeMode(!checked); }} > - {props.checked ? ( - - ) : ( - - )} + {checked ? : } ); diff --git a/docs/src/layouts/AppHeader.tsx b/docs/src/layouts/AppHeader.tsx index 21d5c8ee80512d..8aba9bb331058d 100644 --- a/docs/src/layouts/AppHeader.tsx +++ b/docs/src/layouts/AppHeader.tsx @@ -1,6 +1,5 @@ import * as React from 'react'; import { styled, alpha } from '@mui/material/styles'; -import useMediaQuery from '@mui/material/useMediaQuery'; import GlobalStyles from '@mui/material/GlobalStyles'; import Box from '@mui/material/Box'; import Stack from '@mui/material/Stack'; @@ -12,7 +11,6 @@ import SvgMuiLogomark from 'docs/src/icons/SvgMuiLogomark'; import HeaderNavBar from 'docs/src/components/header/HeaderNavBar'; import HeaderNavDropdown from 'docs/src/components/header/HeaderNavDropdown'; import ThemeModeToggle from 'docs/src/components/header/ThemeModeToggle'; -import { useChangeTheme } from 'docs/src/modules/components/ThemeContext'; import Link from 'docs/src/modules/components/Link'; import { DeferredAppSearch } from 'docs/src/modules/components/AppFrame'; import { useTranslate } from 'docs/src/modules/utils/i18n'; @@ -41,34 +39,9 @@ interface AppHeaderProps { export default function AppHeader(props: AppHeaderProps) { const { gitHubRepository = 'https://github.com/mui' } = props; - const changeTheme = useChangeTheme(); - const [mode, setMode] = React.useState(null); - const prefersDarkMode = useMediaQuery('(prefers-color-scheme: dark)'); const t = useTranslate(); - React.useEffect(() => { - let initialMode = 'system'; - try { - initialMode = localStorage.getItem('mui-mode') || initialMode; - } catch (error) { - // do nothing - } - setMode(initialMode); - }, []); - - const handleChangeThemeMode = (checked: boolean) => { - const paletteMode = checked ? 'dark' : 'light'; - setMode(paletteMode); - - try { - localStorage.setItem('mui-mode', paletteMode); // syncing with homepage, can be removed once all pages are migrated to CSS variables - } catch (error) { - // do nothing - } - changeTheme({ paletteMode }); - }; - return (
- {mode !== null ? ( - - ) : null} + diff --git a/docs/src/modules/components/AppSearch.js b/docs/src/modules/components/AppSearch.js index 1026b22fe92a25..25243d2056006d 100644 --- a/docs/src/modules/components/AppSearch.js +++ b/docs/src/modules/components/AppSearch.js @@ -332,7 +332,7 @@ export default function AppSearch(props) { })} /> {search} - +