Skip to content

Commit

Permalink
fix: add dark theme to some iframes
Browse files Browse the repository at this point in the history
  • Loading branch information
hinakhadim committed Nov 28, 2024
1 parent 7f00e20 commit da6643b
Showing 1 changed file with 38 additions and 0 deletions.
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 ThemeToggleButton = () => {

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

const addDarkThemeToIframes = () => {

Check failure on line 21 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (18)

Multiple spaces found before '('

Check failure on line 21 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (20)

Multiple spaces found before '('
const iframes = document.getElementsByTagName('iframe');
const iframesLength = iframes.length;
if (iframesLength > 0){

Check failure on line 24 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (18)

Missing space before opening brace

Check failure on line 24 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (20)

Missing space before opening brace
Array.from({length: iframesLength}).forEach((_, ind)=>{

Check failure on line 25 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (18)

Expected indentation of 6 spaces but found 8

Check failure on line 25 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (18)

A space is required after '{'

Check failure on line 25 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (18)

Extra space before value for key 'length'

Check failure on line 25 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (18)

A space is required before '}'

Check failure on line 25 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (18)

Missing space before =>

Check failure on line 25 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (18)

Missing space after =>

Check failure on line 25 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (20)

Expected indentation of 6 spaces but found 8

Check failure on line 25 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (20)

A space is required after '{'

Check failure on line 25 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (20)

Extra space before value for key 'length'

Check failure on line 25 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (20)

A space is required before '}'

Check failure on line 25 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (20)

Missing space before =>

Check failure on line 25 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (20)

Missing space after =>
let style = document.createElement('style');

Check failure on line 26 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (18)

Expected indentation of 8 spaces but found 12

Check failure on line 26 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (18)

'style' is never reassigned. Use 'const' instead

Check failure on line 26 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (20)

Expected indentation of 8 spaces but found 12

Check failure on line 26 in src/ThemeToggleButton.jsx

View workflow job for this annotation

GitHub Actions / tests (20)

'style' is never reassigned. Use 'const' instead
style.textContent =
'body {' +
' background-color: #0D0D0E;' +
' color: #ccc;' +
'}'
;
if (iframes[ind].contentDocument) {iframes[ind].contentDocument.head.appendChild(style);}
});
}
};

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

Array.from({ length: iframesLength }).forEach((_, ind) => {
if (iframes[ind].contentDocument) {
const iframeHead = iframes[ind].contentDocument.head;
const styleTag = Array.from(iframeHead.querySelectorAll('style')).find(
(style) =>
style.textContent.includes('background-color: #0D0D0E;') &&
style.textContent.includes('color: #ccc;')
);
if (styleTag) {
styleTag.remove();
}
}
});
};

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();
theme = 'light';
} else {
document.body.classList.add('indigo-dark-theme');
addDarkThemeToIframes();
theme = 'dark';
}
cookies.set(themeCookie, theme, getCookieOptions(serverURL));
Expand Down

0 comments on commit da6643b

Please sign in to comment.