This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Features: - header dark/light mode toggle - configurable theming - configuration: enabled/disabled, setting dark mode as default - setting up user-preferred theme based on browser / system config
- Loading branch information
Showing
12 changed files
with
407 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,3 +103,7 @@ features: | |
matchRegex: "^/" | ||
outputPath: "/rss.xml" | ||
generator: "gidocs" | ||
darkMode: | ||
enabled: true | ||
default: false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react'; | ||
import styled from '@emotion/styled'; | ||
import { Sun as DayImage, Moon as NightImage } from 'react-feather'; | ||
|
||
const StyledSwitch = styled('div')` | ||
display: flex; | ||
justify-content: flex-end; | ||
width: 100%; | ||
margin: 0 20px 0 5px; | ||
padding: 6px; | ||
background-color: ${(props) => props.theme.darkModeSwitch.background}; | ||
border-radius: 50%; | ||
cursor: pointer; | ||
&:hover { | ||
svg { | ||
fill: ${(props) => props.theme.darkModeSwitch.hover}; | ||
stroke: ${(props) => props.theme.darkModeSwitch.hover}; | ||
} | ||
} | ||
svg { | ||
transition: ${(props) => props.theme.transitions.hover}; | ||
fill: ${(props) => props.theme.darkModeSwitch.fill}; | ||
stroke: ${(props) => props.theme.darkModeSwitch.stroke}; | ||
} | ||
`; | ||
|
||
export const DarkModeSwitch = ({ isDarkThemeActive, toggleActiveTheme, ...props }) => { | ||
const img = isDarkThemeActive ? NightImage : DayImage; | ||
return ( | ||
<StyledSwitch {...props} role={'button'} tabIndex={0} onClick={toggleActiveTheme}> | ||
{/* not defining color as a workaround to use css styling instead */} | ||
{img.render({ color: '' })} | ||
</StyledSwitch> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,56 @@ | ||
import * as React from 'react'; | ||
import React from 'react'; | ||
import { ThemeProvider as EmotionThemeProvider } from 'emotion-theming'; | ||
import { default as defaultTheme } from '../theme'; | ||
import 'css'; | ||
|
||
export default function ThemeProvider({ children, theme = {} }) { | ||
return ( | ||
<div> | ||
<EmotionThemeProvider theme={{ ...defaultTheme, ...theme }}>{children}</EmotionThemeProvider> | ||
</div> | ||
); | ||
import { light, dark } from '../theme'; | ||
|
||
class ThemeProvider extends React.Component { | ||
state = { | ||
isDarkThemeActive: false, | ||
}; | ||
|
||
constructor({ darkModeConfig }) { | ||
super(); | ||
this.darkModeConfig = darkModeConfig; | ||
} | ||
|
||
componentDidMount() { | ||
this.retrieveActiveTheme(); | ||
} | ||
|
||
retrieveActiveTheme = () => { | ||
if (!this.darkModeConfig.enabled) { | ||
return false; | ||
} | ||
let isDarkThemeActive = JSON.parse(window.localStorage.getItem('isDarkThemeActive')); | ||
if (isDarkThemeActive == null) { | ||
isDarkThemeActive = | ||
window.matchMedia('(prefers-color-scheme: dark)').matches || this.darkModeConfig.default; | ||
} | ||
this.setState({ isDarkThemeActive }); | ||
return isDarkThemeActive; | ||
}; | ||
|
||
toggleActiveTheme = () => { | ||
if (!this.darkModeConfig.enabled) { | ||
console.warn('Dark mode is disabled, but trying to activate it.'); | ||
return false; | ||
} | ||
this.setState((prevState) => ({ isDarkThemeActive: !prevState.isDarkThemeActive })); | ||
|
||
window.localStorage.setItem('isDarkThemeActive', JSON.stringify(!this.state.isDarkThemeActive)); | ||
return !this.state.isDarkThemeActive; | ||
}; | ||
|
||
render() { | ||
const { children } = this.props; | ||
const { isDarkThemeActive } = this.state; | ||
const currentActiveTheme = isDarkThemeActive ? dark : light; | ||
|
||
return ( | ||
<div> | ||
<EmotionThemeProvider theme={currentActiveTheme}>{children}</EmotionThemeProvider> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default ThemeProvider; |
Oops, something went wrong.