Skip to content

Commit 0a749f9

Browse files
committed
Persist the darkmode state
1 parent 8de7c20 commit 0a749f9

File tree

6 files changed

+26
-10
lines changed

6 files changed

+26
-10
lines changed

src/components/common/Diff/DiffCommentReminder.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ const DiffCommentReminder = styled(
3232
}
3333
)`
3434
display: inline;
35-
background-color: #fffbe6;
35+
background-color: ${({ theme }) => theme.yellowBackground};
3636
padding: 5px;
3737
border-radius: 3px;
3838
margin-left: 10px;
39-
border: 1px solid #ffe58f;
39+
border: 1px solid ${({ theme }) => theme.yellowBorder};
4040
4141
& > .icon {
4242
margin-right: 6px;

src/components/common/Diff/DiffViewStyleOptions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const DiffViewStyleOptions = styled(
3232
&,
3333
&:hover,
3434
&:focus {
35-
color: #24292e;
35+
color: ${({ theme }) => theme.text};
3636
}
3737
`
3838
export default DiffViewStyleOptions

src/components/common/Markdown.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const Link = styled((props) => (
1212
/>
1313
))`
1414
text-decoration: none;
15-
color: #045dc1;
15+
color: ${({ theme }) => theme.link};
1616
`
1717

1818
const InlineCode = styled.em`

src/components/common/UpgradeSupportAlert.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ const UpgradeSupportAlert = styled((props) => (
2525
</div>
2626
))`
2727
span > a {
28-
color: #045dc1;
28+
color: ${({ theme }) => theme.link}};
2929
3030
&:hover {
31-
color: #40a9ff;
31+
color: ${({ theme }) => theme.linkHover}};
3232
}
3333
}
3434
`

src/components/pages/Home.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ThemeProvider } from '@emotion/react'
44
import { Card, Input, Typography, ConfigProvider, theme } from 'antd'
55
import GitHubButton from 'react-github-btn'
66
import ReactGA from 'react-ga'
7+
import createPersistedState from 'use-persisted-state'
78
import VersionSelector from '../common/VersionSelector'
89
import DiffViewer from '../common/DiffViewer'
910
import Settings from '../common/Settings'
@@ -114,6 +115,10 @@ const StarButton = styled(({ className, ...props }) => (
114115
margin-right: auto;
115116
`
116117

118+
// Set up a persisted state hook for for dark mode so users coming back
119+
// will have dark mode automatically if they've selected it previously.
120+
const useDarkModeState = createPersistedState('darkMode')
121+
117122
const Home = () => {
118123
const { packageName: defaultPackageName, isPackageNameDefinedInURL } =
119124
useGetPackageNameFromURL()
@@ -185,10 +190,17 @@ const Home = () => {
185190
setSettings(normalizedIncomingSettings)
186191
}
187192

188-
const { defaultAlgorithm, darkAlgorithm } = theme
189-
const [isDarkMode, setIsDarkMode] = useState(false)
193+
// Dark Mode Setup:
194+
const { defaultAlgorithm, darkAlgorithm } = theme // Get default and dark mode states from antd.
195+
const [isDarkMode, setIsDarkMode] = useDarkModeState(false) // Remembers dark mode state between sessions.
190196
const toggleDarkMode = () => setIsDarkMode((previousValue) => !previousValue)
191197
const themeString = isDarkMode ? 'dark' : 'light'
198+
useEffect(() => {
199+
// Set the document's background color to the theme's body color.
200+
document.body.style.backgroundColor = isDarkMode
201+
? darkTheme.background
202+
: lightTheme.background
203+
}, [isDarkMode])
192204

193205
return (
194206
<ConfigProvider

src/theme/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
export const lightTheme = {
2-
body: '#FFF',
32
background: '#ffffff',
43

54
text: '#363537',
65
textHover: 'rgba(27, 31, 35, 0.6)',
6+
link: '#045dc1',
7+
linkHover: '#40a9ff',
78
border: '#e8e8e8',
89
greenBorder: '#bef5cb',
910
yellowBorder: '#ffe58f',
@@ -42,10 +43,13 @@ export const lightTheme = {
4243
},
4344
}
4445
export const darkTheme = {
45-
body: '#363537',
4646
background: '#363537',
47+
4748
text: '#FAFAFA',
4849
textHover: '#999',
50+
link: '#045dc1',
51+
linkHover: '#40a9ff',
52+
4953
border: '#888',
5054
greenBorder: '#bef5cb',
5155
yellowBorder: '#c69026',

0 commit comments

Comments
 (0)