-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP * DarkMode is a Component not Service * Give the theme button a right margin * Use Dropdown from `react-bootstrap` and dynamically create dropdown items * Rename to ColorScheme * move colorScheme to AppContext * Stop setting localStorage here * Simplify setTheme logic and ensure data-bs-theme is 'light' not 'auto' when light system scheme * Remove unused imports * More contrast for the navbar
- Loading branch information
1 parent
8221d2f
commit aea5a4b
Showing
6 changed files
with
77 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,54 @@ | ||
import React, { useContext } from 'react'; | ||
import { Dropdown } from 'react-bootstrap'; | ||
import { AppContext } from '../Contexts/AppContext'; | ||
|
||
|
||
const dropdownOptions = [ | ||
{ scheme: 'light', icon: 'bi-sun-fill', label: 'Light' }, | ||
{ scheme: 'dark', icon: 'bi-moon-stars-fill', label: 'Dark' }, | ||
{ scheme: 'auto', icon: 'bi-circle-half', label: 'Auto' }, | ||
]; | ||
|
||
const setTheme = function (theme) { | ||
const rootElement = document.documentElement; | ||
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches; | ||
|
||
if (theme === 'auto') { | ||
rootElement.setAttribute('data-bs-theme', prefersDarkMode ? 'dark' : 'light'); | ||
} else { | ||
rootElement.setAttribute('data-bs-theme', theme); | ||
} | ||
} | ||
|
||
const ColorScheme = () => { | ||
const { savedColorScheme, setSavedColorScheme } = useContext(AppContext); | ||
|
||
setTheme(savedColorScheme); | ||
|
||
const setThemeAndState = (newColorScheme) => { | ||
setTheme(newColorScheme); | ||
setSavedColorScheme(newColorScheme); | ||
} | ||
|
||
return ( | ||
<Dropdown> | ||
<Dropdown.Toggle variant="secondary" style={{ marginRight: "7px" }}> | ||
<i class="bi-moon-stars-fill"></i> | ||
</Dropdown.Toggle> | ||
|
||
<Dropdown.Menu> | ||
{dropdownOptions.map((option) => ( | ||
<Dropdown.Item | ||
key={option.theme} | ||
className={`dropdown-item ${savedColorScheme === option.scheme ? 'active' : ''}`} | ||
onClick={() => setThemeAndState(option.scheme)} | ||
> | ||
<i className={option.icon}></i> {option.label} | ||
</Dropdown.Item> | ||
))} | ||
</Dropdown.Menu> | ||
</Dropdown> | ||
); | ||
}; | ||
|
||
export default ColorScheme; |
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 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