Skip to content

Commit 86a058a

Browse files
authored
feat: enhance accessibility by adding descriptive label to theme toggle (#29)
1 parent d015f67 commit 86a058a

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/ThemeToggleButton.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import { getConfig } from '@edx/frontend-platform';
33
import Cookies from 'universal-cookie';
44
import { Icon } from '@openedx/paragon';
@@ -10,6 +10,7 @@ const themeCookie = 'indigo-toggle-dark';
1010
const themeCookieExpiry = 90; // days
1111

1212
const ThemeToggleButton = ({ intl }) => {
13+
const [isDarkThemeEnabled, setIsDarkThemeEnabled] = useState(false);
1314
const cookies = new Cookies();
1415
const isThemeToggleEnabled = getConfig().INDIGO_ENABLE_DARK_TOGGLE;
1516

@@ -63,10 +64,12 @@ const ThemeToggleButton = ({ intl }) => {
6364
if (cookies.get(themeCookie) === 'dark') {
6465
document.body.classList.remove('indigo-dark-theme');
6566
removeDarkThemeFromiframes();
67+
setIsDarkThemeEnabled(false);
6668
theme = 'light';
6769
} else {
6870
document.body.classList.add('indigo-dark-theme');
6971
addDarkThemeToIframes();
72+
setIsDarkThemeEnabled(true);
7073
theme = 'dark';
7174
}
7275
cookies.set(themeCookie, theme, getCookieOptions(serverURL));
@@ -94,6 +97,7 @@ const ThemeToggleButton = ({ intl }) => {
9497
<label htmlFor="theme-toggle-checkbox" className="switch">
9598
<input id="theme-toggle-checkbox" defaultChecked={cookies.get(themeCookie) === 'dark'} onChange={onToggleTheme} onKeyUp={hanldeKeyUp} type="checkbox" title={intl.formatMessage(messages['header.user.theme'])} />
9699
<span className="slider round" />
100+
<span id="theme-label" className="sr-only">{`Switch to ${isDarkThemeEnabled ? 'Light' : 'Dark'} Mode`}</span>
97101
</label>
98102
</div>
99103
<div className="dark-theme-icon"><Icon src={Nightlight} /></div>

0 commit comments

Comments
 (0)