forked from openedx/frontend-component-header
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: read theme cookies * styled the toggle theme buutton * fix: update test snapshots
- Loading branch information
1 parent
f25b9b3
commit 48428ac
Showing
7 changed files
with
250 additions
and
22 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
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,47 @@ | ||
import React from 'react'; | ||
import { getConfig } from '@edx/frontend-platform'; | ||
import Cookies from 'universal-cookie'; | ||
|
||
const ThemeToggleButton = () => { | ||
const getNextWeek = () => { | ||
const today = new Date(); | ||
return new Date(today.getFullYear(), today.getMonth(), today.getDate() + 7); | ||
}; | ||
|
||
const onToggleTheme = () => { | ||
const cookies = new Cookies(); | ||
const serverURL = new URL(getConfig().LMS_BASE_URL); | ||
const themeCookieName = getConfig().THEME_COOKIE_NAME; | ||
|
||
const options = { domain: serverURL.hostname, path: '/', expires: getNextWeek() }; | ||
let themeName = ''; | ||
|
||
if (cookies.get(themeCookieName) === 'dark') { | ||
document.body.classList.remove('indigo-dark-theme'); | ||
themeName = 'light'; | ||
} else { | ||
document.body.classList.add('indigo-dark-theme'); | ||
themeName = 'dark'; | ||
} | ||
cookies.set(themeCookieName, themeName, options); | ||
}; | ||
|
||
return ( | ||
<div className="theme-toggle-button"> | ||
<span id="darkmode" role="button" tabIndex="0" onClick={onToggleTheme} onKeyDown={onToggleTheme}> | ||
<div className="darkmode_icon"> | ||
<span className="ray" /> | ||
<span className="ray" /> | ||
<span className="ray" /> | ||
<span className="ray" /> | ||
<span className="ray" /> | ||
<span className="ray" /> | ||
<span className="ray" /> | ||
<span className="ray" /> | ||
</div> | ||
</span> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ThemeToggleButton; |
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