-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: initial dark mode toggle js and prefers-color-scheme media query * chore: remove css variables test from _core.scss * fix: dark mode bootstrap in index.js * chore: clean up import in index.js * fix: use data attribute in _core.scss and refactor darkMode.js * chore: add script to run 'styles' package storybook from root directory and update readme
- Loading branch information
1 parent
fbd2de6
commit 5a825f7
Showing
6 changed files
with
58 additions
and
7 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
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
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,16 @@ | ||
@use 'colors' as *; | ||
@use '../tokens/font' as *; | ||
|
||
html { | ||
html[data-cbp-theme="light"] { | ||
color: $cbp-text-darkest; | ||
font-family: $cbp-font-family-roboto; | ||
background-color: $cbp-bg-light; | ||
} | ||
|
||
[data-theme="dark"] { | ||
background-color: $cbp-bg-dark; | ||
html[data-cbp-theme="dark"] { | ||
color: $cbp-text-lightest; | ||
} | ||
background-color: $cbp-bg-dark; | ||
} | ||
|
||
body { | ||
font-family: $cbp-font-family-roboto; | ||
} |
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,34 @@ | ||
class DarkMode { | ||
constructor(themeToggle) { | ||
// Toggle component needs to have 'data-theme-toggle' attribute in order for this to work. | ||
this.themeToggle = themeToggle; | ||
this.prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)'); | ||
this.storedTheme = localStorage.getItem('theme'); | ||
|
||
this.preferredTheme() === 'dark' ? this.themeToggle.checked = true : this.themeToggle.checked = false; | ||
|
||
this.setTheme(this.preferredTheme()); | ||
|
||
this.handleThemeToggle(this.themeToggle); | ||
} | ||
|
||
preferredTheme() { | ||
if (this.storedTheme) { | ||
return this.storedTheme; | ||
} | ||
return this.prefersDarkMode.matches ? 'dark' : 'light'; | ||
} | ||
|
||
setTheme(theme) { | ||
localStorage.setItem('theme', theme) | ||
document.documentElement.setAttribute('data-cbp-theme', theme); | ||
} | ||
|
||
handleThemeToggle(themeToggle) { | ||
themeToggle.addEventListener('click', () => { | ||
themeToggle.checked ? this.setTheme('dark') : this.setTheme('light'); | ||
}) | ||
} | ||
} | ||
|
||
export default DarkMode; |