|
| 1 | + |
| 2 | +const ToggleThemeButton = () => { |
| 3 | + const intl = useIntl(); |
| 4 | + const [isDarkThemeEnabled, setIsDarkThemeEnabled] = useState(false); |
| 5 | + |
| 6 | + const themeCookie = 'selected-paragon-theme-variant'; |
| 7 | + const themeCookieExpiry = 90; // days |
| 8 | + const isThemeToggleEnabled = getConfig().INDIGO_ENABLE_DARK_TOGGLE; |
| 9 | + |
| 10 | + const getCookie = (name) => { |
| 11 | + return document.cookie |
| 12 | + .split("; ") |
| 13 | + .find((row) => row.startsWith(name + "=")) |
| 14 | + ?.split("=")[1]; |
| 15 | + }; |
| 16 | + |
| 17 | + const setCookie = (name, value, { domain, path, expires }) => { |
| 18 | + document.cookie = `${name}=${value}; domain=${domain}; path=${path}; expires=${expires.toUTCString()}; SameSite=Lax`; |
| 19 | + }; |
| 20 | + |
| 21 | + const serverURL = new URL(getConfig().LMS_BASE_URL); |
| 22 | + |
| 23 | + const getCookieExpiry = () => { |
| 24 | + const today = new Date(); |
| 25 | + return new Date( |
| 26 | + today.getFullYear(), |
| 27 | + today.getMonth(), |
| 28 | + today.getDate() + themeCookieExpiry |
| 29 | + ); |
| 30 | + }; |
| 31 | + |
| 32 | + const getCookieOptions = (serverURL) => ({ |
| 33 | + domain: serverURL.hostname, |
| 34 | + path: '/', |
| 35 | + expires: getCookieExpiry(), |
| 36 | + }); |
| 37 | + |
| 38 | + const onToggleTheme = () => { |
| 39 | + let theme = ''; |
| 40 | + |
| 41 | + if (getCookie(themeCookie) === 'dark') { |
| 42 | + document.documentElement.setAttribute('data-paragon-theme-variant', 'light'); |
| 43 | + setIsDarkThemeEnabled(false); |
| 44 | + theme = 'light'; |
| 45 | + } else { |
| 46 | + document.documentElement.setAttribute('data-paragon-theme-variant', 'dark'); |
| 47 | + setIsDarkThemeEnabled(true); |
| 48 | + theme = 'dark'; |
| 49 | + } |
| 50 | + |
| 51 | + window.localStorage.setItem(themeCookie, theme); |
| 52 | + setTimeout(() => { |
| 53 | + setCookie(themeCookie, theme, getCookieOptions(serverURL)); |
| 54 | + window.location.reload(); |
| 55 | + }, 1); |
| 56 | + }; |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + if (!getCookie(themeCookie) || getCookie(themeCookie) === 'undefined') { |
| 60 | + return; |
| 61 | + } |
| 62 | + if (getCookie(themeCookie) !== window.localStorage.getItem(themeCookie)) { |
| 63 | + window.localStorage.setItem(themeCookie, getCookie(themeCookie)); |
| 64 | + window.location.reload(); |
| 65 | + } |
| 66 | + }, []); |
| 67 | + |
| 68 | + const handleKeyUp = (event) => { |
| 69 | + if (event.key === "Enter") { |
| 70 | + onToggleTheme(); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + if (!isThemeToggleEnabled) { |
| 75 | + return <div />; |
| 76 | + } |
| 77 | + |
| 78 | + const messages = { |
| 79 | + "header.user.theme": { |
| 80 | + id: "header.user.theme", |
| 81 | + defaultMessage: "Toggle Theme", |
| 82 | + description: "Toggle between light and dark theme", |
| 83 | + }, |
| 84 | + }; |
| 85 | + |
| 86 | + return ( |
| 87 | + <div className="theme-toggle-button mr-3"> |
| 88 | + <div className="light-theme-icon"> |
| 89 | + <Icon src={WbSunny} /> |
| 90 | + </div> |
| 91 | + <div className="toggle-switch"> |
| 92 | + <label htmlFor="theme-toggle-checkbox" className="switch"> |
| 93 | + <input |
| 94 | + id="theme-toggle-checkbox" |
| 95 | + defaultChecked={getCookie(themeCookie) === "dark"} |
| 96 | + onChange={onToggleTheme} |
| 97 | + onKeyUp={handleKeyUp} |
| 98 | + type="checkbox" |
| 99 | + title={intl.formatMessage(messages["header.user.theme"])} |
| 100 | + /> |
| 101 | + <span className="slider round" /> |
| 102 | + <span id="theme-label" className="sr-only">{`Switch to ${isDarkThemeEnabled ? "Light" : "Dark" |
| 103 | + } Mode`}</span> |
| 104 | + </label> |
| 105 | + </div> |
| 106 | + <div className="dark-theme-icon"> |
| 107 | + <Icon src={Nightlight} /> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + ); |
| 111 | +}; |
0 commit comments