Skip to content
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
6 changes: 5 additions & 1 deletion src/ThemeToggleButton.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import { getConfig } from '@edx/frontend-platform';
import Cookies from 'universal-cookie';
import { Icon } from '@openedx/paragon';
Expand All @@ -10,6 +10,7 @@
const themeCookieExpiry = 90; // days

const ThemeToggleButton = ({ intl }) => {
const [isDarkThemeEnabled, setIsDarkThemeEnabled] = useState(false);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a need for a state? This file already uses themeCookie/cookie to manage the dark theme toggle across sessions. Can't the same be followed for a11y?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DawoudSheraz The state ensures the UI updates immediately when toggled, while the cookie handles persistence. Without state, the toggle’s visual feedback and screen reader announcements wouldn’t sync until a page reload, hurting both UX and accessibility.

const cookies = new Cookies();
const isThemeToggleEnabled = getConfig().INDIGO_ENABLE_DARK_TOGGLE;

Expand Down Expand Up @@ -63,10 +64,12 @@
if (cookies.get(themeCookie) === 'dark') {
document.body.classList.remove('indigo-dark-theme');
removeDarkThemeFromiframes();
setIsDarkThemeEnabled(false);

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 = 'light';
} else {
document.body.classList.add('indigo-dark-theme');
addDarkThemeToIframes();
setIsDarkThemeEnabled(true);
theme = 'dark';
}
cookies.set(themeCookie, theme, getCookieOptions(serverURL));
Expand Down Expand Up @@ -94,6 +97,7 @@
<label htmlFor="theme-toggle-checkbox" className="switch">
<input id="theme-toggle-checkbox" defaultChecked={cookies.get(themeCookie) === 'dark'} onChange={onToggleTheme} onKeyUp={hanldeKeyUp} type="checkbox" title={intl.formatMessage(messages['header.user.theme'])} />
<span className="slider round" />
<span id="theme-label" className="sr-only">{`Switch to ${isDarkThemeEnabled ? 'Light' : 'Dark'} Mode`}</span>
</label>
</div>
<div className="dark-theme-icon"><Icon src={Nightlight} /></div>
Expand Down