From d87cc00fc83bb2bc5c1ffa69b4dad71d51e1cc66 Mon Sep 17 00:00:00 2001 From: Haishan Date: Sun, 6 Mar 2022 16:46:05 +0800 Subject: Make theme switch a native select/option menu --- src/components/shared/ThemeSwitcher.module.css | 28 ------------ src/components/shared/ThemeSwitcher.module.scss | 58 +++++++++++++++++++++++++ src/components/shared/ThemeSwitcher.tsx | 49 +++++++++------------ 3 files changed, 78 insertions(+), 57 deletions(-) delete mode 100644 src/components/shared/ThemeSwitcher.module.css create mode 100644 src/components/shared/ThemeSwitcher.module.scss (limited to 'src/components/shared') diff --git a/src/components/shared/ThemeSwitcher.module.css b/src/components/shared/ThemeSwitcher.module.css deleted file mode 100644 index 919c86c..0000000 --- a/src/components/shared/ThemeSwitcher.module.css +++ /dev/null @@ -1,28 +0,0 @@ -.iconWrapper { - --sz: 40px; - - width: var(--sz); - height: var(--sz); - display: flex; - justify-content: center; - align-items: center; - - outline: none; - padding: 5px; - color: var(--color-text); - border-radius: 100%; - border: 1px solid transparent; -} -.iconWrapper:hover { - opacity: 0.6; -} -.iconWrapper:focus { - border-color: var(--color-focus-blue); -} - -.themeSwitchContainer { - appearance: none; - user-select: none; - background: none; - cursor: pointer; -} diff --git a/src/components/shared/ThemeSwitcher.module.scss b/src/components/shared/ThemeSwitcher.module.scss new file mode 100644 index 0000000..c5de126 --- /dev/null +++ b/src/components/shared/ThemeSwitcher.module.scss @@ -0,0 +1,58 @@ +.iconWrapper { + --sz: 40px; + + width: var(--sz); + height: var(--sz); + display: flex; + justify-content: center; + align-items: center; + + outline: none; + padding: 5px; + color: var(--color-text); + border-radius: 100%; + border: 1px solid transparent; +} +.iconWrapper:hover { + opacity: 0.6; +} +.iconWrapper:focus { + border-color: var(--color-focus-blue); +} + +.themeSwitchContainer { + --sz: 40px; + + position: relative; + display: flex; + align-items: center; + height: var(--sz); + select { + cursor: pointer; + padding-left: var(--sz); + width: var(--sz); + height: var(--sz); + appearance: none; + outline: none; + border-radius: 100%; + border: 1px solid transparent; + background: var(--color-bg-sidebar); + &:focus { + border-color: var(--color-focus-blue); + } + option { + // this has effect in Firefox + // Chrome and Safari use the native menu + background: var(--color-bg-sidebar); + } + } + .iconWrapper { + pointer-events: none; + display: inline-flex; + align-items: center; + justify-content: center; + position: absolute; + left: 0; + top: 0; + } +} diff --git a/src/components/shared/ThemeSwitcher.tsx b/src/components/shared/ThemeSwitcher.tsx index 90990b6..45b60bc 100644 --- a/src/components/shared/ThemeSwitcher.tsx +++ b/src/components/shared/ThemeSwitcher.tsx @@ -1,5 +1,4 @@ import Tooltip from '@reach/tooltip'; -import cx from 'clsx'; import * as React from 'react'; import { useTranslation } from 'react-i18next'; import { connect } from 'src/components/StateProvider'; @@ -7,36 +6,18 @@ import { framerMotionResouce } from 'src/misc/motion'; import { getTheme, switchTheme } from 'src/store/app'; import { State } from 'src/store/types'; -import s from './ThemeSwitcher.module.css'; +import s from './ThemeSwitcher.module.scss'; export function ThemeSwitcherImpl({ theme, dispatch }) { const { t } = useTranslation(); - const switchThemeHooked = React.useCallback(() => { - dispatch(switchTheme()); - }, [dispatch]); - - const nextThemeName = React.useMemo(() => { - switch (theme) { - case 'light': - return 'dark'; - case 'dark': - return 'auto'; - case 'auto': - return 'light'; - default: - console.assert(false, 'Unknown theme'); - return 'unknown'; - } - }, [theme]); - const themeIcon = React.useMemo(() => { switch (theme) { - case 'light': - return ; case 'dark': - return ; + return ; case 'auto': + return ; + case 'light': return ; default: console.assert(false, 'Unknown theme'); @@ -44,11 +25,21 @@ export function ThemeSwitcherImpl({ theme, dispatch }) { } }, [theme]); + const onChange = React.useCallback( + (e: React.ChangeEvent) => dispatch(switchTheme(e.target.value)), + [dispatch] + ); + return ( - - + +
+ {themeIcon} + +
); } @@ -95,7 +86,7 @@ function Sun() { strokeLinejoin="round" > - + @@ -137,7 +128,7 @@ function Auto() { transition={{ duration: 0.7 }} /> - + ); } -- cgit v1.3.1