Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add dark theme to some iframes #26

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/ThemeToggleButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,53 @@

const getCookieOptions = (serverURL) => ({ domain: serverURL.hostname, path: '/', expires: getCookieExpiry() });

const addDarkThemeToIframes = () => {
const iframes = document.getElementsByTagName('iframe');
const iframesLength = iframes.length;

Check warning on line 23 in src/ThemeToggleButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/ThemeToggleButton.jsx#L22-L23

Added lines #L22 - L23 were not covered by tests
if (iframesLength > 0) {
Array.from({ length: iframesLength }).forEach((_, ind) => {
const style = document.createElement('style');
style.textContent = `

Check warning on line 27 in src/ThemeToggleButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/ThemeToggleButton.jsx#L25-L27

Added lines #L25 - L27 were not covered by tests
body{
background-color: #0D0D0E;
color: #ccc;
}
a {color: #ccc;}
a:hover{color: #d3d3d3;}
`;
if (iframes[ind].contentDocument) { iframes[ind].contentDocument.head.appendChild(style); }
});
}
};

const removeDarkThemeFromiframes = () => {
const iframes = document.getElementsByTagName('iframe');
const iframesLength = iframes.length;

Check warning on line 42 in src/ThemeToggleButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/ThemeToggleButton.jsx#L41-L42

Added lines #L41 - L42 were not covered by tests

Array.from({ length: iframesLength }).forEach((_, ind) => {

Check warning on line 44 in src/ThemeToggleButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/ThemeToggleButton.jsx#L44

Added line #L44 was not covered by tests
if (iframes[ind].contentDocument) {
const iframeHead = iframes[ind].contentDocument.head;
const styleTag = Array.from(iframeHead.querySelectorAll('style')).find(

Check warning on line 47 in src/ThemeToggleButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/ThemeToggleButton.jsx#L46-L47

Added lines #L46 - L47 were not covered by tests
(style) => style.textContent.includes('background-color: #0D0D0E;') && style.textContent.includes('color: #ccc;'),
);
if (styleTag) {
styleTag.remove();

Check warning on line 51 in src/ThemeToggleButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/ThemeToggleButton.jsx#L51

Added line #L51 was not covered by tests
}
}
});
};

const onToggleTheme = () => {
const serverURL = new URL(getConfig().LMS_BASE_URL);
let theme = '';

if (cookies.get(themeCookie) === 'dark') {
document.body.classList.remove('indigo-dark-theme');
removeDarkThemeFromiframes();

Check warning on line 63 in src/ThemeToggleButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/ThemeToggleButton.jsx#L63

Added line #L63 was not covered by tests
theme = 'light';
} else {
document.body.classList.add('indigo-dark-theme');
addDarkThemeToIframes();

Check warning on line 67 in src/ThemeToggleButton.jsx

View check run for this annotation

Codecov / codecov/patch

src/ThemeToggleButton.jsx#L67

Added line #L67 was not covered by tests
theme = 'dark';
}
cookies.set(themeCookie, theme, getCookieOptions(serverURL));
Expand Down
Loading