Skip to content

Commit

Permalink
feat: send signal to LMS to change theme in iframe
Browse files Browse the repository at this point in the history
  • Loading branch information
hinakhadim committed Sep 4, 2024
1 parent cc49624 commit d066351
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/ThemeToggleButton.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React from 'react';
import { getConfig } from '@edx/frontend-platform';
import Cookies from 'universal-cookie';
import { Icon } from '@openedx/paragon';
import { WbSunny, Nightlight } from '@openedx/paragon/icons';

const ThemeToggleButton = () => {
const cookies = new Cookies();
const themeCookieName = getConfig().THEME_COOKIE_NAME ? getConfig().THEME_COOKIE_NAME : null;

const getNextWeek = () => {
Expand All @@ -11,9 +14,7 @@ const ThemeToggleButton = () => {
};

const onToggleTheme = () => {
const cookies = new Cookies();
const serverURL = new URL(getConfig().LMS_BASE_URL);

const options = { domain: serverURL.hostname, path: '/', expires: getNextWeek() };
let themeName = '';

Expand All @@ -25,6 +26,11 @@ const ThemeToggleButton = () => {
themeName = 'dark';
}
cookies.set(themeCookieName, themeName, options);

const learningMFEUnitIframe = document.getElementById('unit-iframe');
if (learningMFEUnitIframe) {
learningMFEUnitIframe.contentWindow.postMessage({ 'indigo-toggle-dark': themeName }, serverURL.origin);
}
};

if (!themeCookieName) {
Expand All @@ -33,18 +39,14 @@ const ThemeToggleButton = () => {

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 className="light-theme-icon"><Icon src={WbSunny} /></div>
<div className="toggle-switch">
<label htmlFor="theme-toggle-checkbox" className="switch">
<input id="theme-toggle-checkbox" defaultChecked={cookies.get(themeCookieName) === 'dark'} onChange={onToggleTheme} type="checkbox" />
<span className="slider round" />
</label>
</div>
<div className="dark-theme-icon"><Icon src={Nightlight} /></div>
</div>
);
};
Expand Down

0 comments on commit d066351

Please sign in to comment.