Skip to content

Commit

Permalink
Merge pull request #1136 from yieldprotocol/feat/dark-mode-enhancement
Browse files Browse the repository at this point in the history
enhancing dark mode settings
  • Loading branch information
brucedonovan authored Mar 15, 2023
2 parents ad9afa5 + 02aa8e4 commit c97cf65
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 78 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "app-v2",
"version": "2.5.10",
"version": "2.5.11",
"private": true,
"dependencies": {
"@ethersproject/providers": "^5.6.8",
Expand Down
6 changes: 3 additions & 3 deletions src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Box, Layer, ResponsiveContext, Tip, Text } from 'grommet';

import { FiEye } from 'react-icons/fi';
import { useAccountModal } from '@rainbow-me/rainbowkit';

import { abbreviateHash, clearCachedItems } from '../utils/appUtils';
import BackButton from './buttons/BackButton';
import GeneralButton from './buttons/GeneralButton';
Expand Down Expand Up @@ -48,7 +49,6 @@ const Sidebar = ({ settingsOpen, setSettingsOpen }: any) => {
width={mobile ? undefined : '400px'}
background="lightBackground"
elevation="xlarge"
justify="between"
style={{ overflow: 'auto' }}
>
<Box flex={false}>
Expand Down Expand Up @@ -124,13 +124,13 @@ const Sidebar = ({ settingsOpen, setSettingsOpen }: any) => {
{!mobile && (
<Box background="gradient-transparent" flex={false}>
<Box pad="medium" background="gradient-transparent">
<NetworkSetting />
<NetworkSetting />
</Box>
</Box>
)}
</Box>

<Box pad="medium" gap="medium" flex={false} style={{ overflow: 'auto' }}>
<Box pad={{ top: 'xsmall', horizontal: 'medium' }} gap="medium" flex={false} style={{ overflow: 'auto' }}>
<ThemeSetting />
<ApprovalSetting />
<UnwrapSetting />
Expand Down
46 changes: 7 additions & 39 deletions src/components/settings/ThemeSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,66 +8,34 @@ import { Settings } from '../../contexts/types/settings';

const ThemeSettings = () => {
const {
settingsState: { darkMode, autoTheme },
settingsState: { darkMode },
settingsActions: { updateSetting },
} = useContext(SettingsContext);

return (
<Box gap="small" pad={{ vertical: 'small' }}>
<Box direction="row" justify="between">
<Text size="small">Use System Color Theme</Text>
<Text size="small" color={'text'}>
Color Theme
</Text>
<Switch
width={55}
checked={autoTheme}
checked={darkMode}
uncheckedIcon={
<Box align="center" justify="center" fill pad="xsmall">
<Text size="0.5em"> Off </Text>
<FiSun color="text" style={{ strokeWidth: '3' }} />
</Box>
}
checkedIcon={
<Box align="center" justify="center" fill pad="xsmall">
<Text size="0.5em"> Auto </Text>
<FiMoon color="text" style={{ strokeWidth: '3', fill: 'Background' }} />
</Box>
}
offColor="#BFDBFE"
onColor="#60A5FA"
onChange={(val: boolean) => updateSetting(Settings.AUTO_THEME, val)}
handleDiameter={20}
borderRadius={20}
/>
</Box>

<Box direction="row" justify="between">
<Text size="small" color={autoTheme ? 'text-xweak' : 'text'}>
Color Theme
</Text>
<Switch
width={55}
checked={darkMode}
uncheckedIcon={
autoTheme ? (
false
) : (
<Box align="center" justify="center" fill pad="xsmall">
<FiSun color="text" style={{ strokeWidth: '3' }} />
</Box>
)
}
checkedIcon={
autoTheme ? (
false
) : (
<Box align="center" justify="center" fill pad="xsmall">
<FiMoon color="text" style={{ strokeWidth: '3', fill: 'Background' }} />
</Box>
)
}
offColor="#BFDBFE"
onColor="#60A5FA"
onChange={(val: boolean) => updateSetting(Settings.DARK_MODE, val)}
handleDiameter={20}
borderRadius={20}
disabled={autoTheme}
/>
</Box>
</Box>
Expand Down
2 changes: 0 additions & 2 deletions src/contexts/SettingsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ const initState: ISettingsContextState = {

/* Color theme */
darkMode: false,
/* Set color theme based on system */
autoTheme: false,

/* Has the usage disclaimer been checked? */
disclaimerChecked: false,
Expand Down
7 changes: 1 addition & 6 deletions src/contexts/types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export enum Settings {
SLIPPAGE_TOLERANCE = 'slippageTolerance',
DIAGNOSTICS = 'diagnostics',
DARK_MODE = 'darkMode',
AUTO_THEME = 'autoTheme',
DISCLAIMER_CHECKED = 'disclaimerChecked',
POWER_USER = 'powerUser',
FORCE_TRANSACTIONS = 'forceTransactions',
Expand All @@ -32,16 +31,14 @@ export interface ISettingsContext {
}

export interface ISettingsContextActions {
updateSetting: (setting: Settings, value: string | number | boolean ) => void;
updateSetting: (setting: Settings, value: string | number | boolean) => void;
}
export type SettingsContextAction = { type: Settings; payload: string | number | boolean };

export interface ISettingsContextState {
/* User Settings ( getting from the cache first ) */
slippageTolerance: number;
darkMode: boolean;
autoTheme: boolean;

approvalMethod: ApprovalType;
approveMax: boolean;
disclaimerChecked: boolean;
Expand All @@ -68,6 +65,4 @@ export interface ISettingsContextState {

useMockedUser: boolean;
mockUserAddress: `0x${string}`;

}

32 changes: 22 additions & 10 deletions src/hooks/useColorScheme.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
import { useContext, useEffect, useState } from 'react';
import { SettingsContext } from '../contexts/SettingsContext';
import { Settings } from '../contexts/types/settings';

/* Hook for using a color scheme provided by user autotheme or darkMode settings */
export const useColorScheme = () => {
const [colorScheme, setColorScheme] = useState<'light' | 'dark'>('light');
const {
settingsState: { autoTheme, darkMode },
settingsState: { darkMode },
settingsActions: { updateSetting },
} = useContext(SettingsContext);

useEffect(() => {
if (autoTheme && typeof window !== 'undefined') {
window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
? setColorScheme('dark')
: setColorScheme('light');
const storedDarkMode = localStorage.getItem('darkMode');
const hasStoredDarkMode = storedDarkMode !== null;
if (hasStoredDarkMode) {
updateSetting(Settings.DARK_MODE, storedDarkMode === 'true');
setColorScheme(storedDarkMode === 'true' ? 'dark' : 'light');
} else if (typeof window !== 'undefined') {
const systemDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
updateSetting(Settings.DARK_MODE, systemDarkMode);
setColorScheme(systemDarkMode ? 'dark' : 'light');

window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
const newColorScheme = e.matches ? 'dark' : 'light';
autoTheme && setColorScheme(newColorScheme);
const newDarkMode = e.matches;
updateSetting(Settings.DARK_MODE, newDarkMode);
setColorScheme(newDarkMode ? 'dark' : 'light');
});
} else {
setColorScheme(darkMode ? 'dark' : 'light' || 'light');
updateSetting(Settings.DARK_MODE, false);
setColorScheme('light');
}
}, [autoTheme, darkMode]);
}, []);

useEffect(() => {
setColorScheme(darkMode ? 'dark' : 'light');
}, [darkMode]);

return colorScheme;
};
17 changes: 0 additions & 17 deletions src/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,6 @@ export default class MyDocument extends Document {
<link rel="shortcut icon" href="/favicons/favicon.ico" />
</Head>
<body>
<script
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
__html: `
function getColorScheme() {
if (JSON.parse(localStorage.getItem('autoTheme'))) {
return window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light';
} else {
return JSON.parse(localStorage.getItem('darkMode')) ? 'dark' : 'light'
}
}
document.body.dataset.theme = getColorScheme();
`,
}}
/>
<Main />
<NextScript />
</body>
Expand Down

0 comments on commit c97cf65

Please sign in to comment.