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

Add Theme Switching Functionality to Translate View #3002

Merged
merged 14 commits into from
Oct 26, 2023

Conversation

ayanaar
Copy link
Contributor

@ayanaar ayanaar commented Oct 24, 2023

This pull request introduces theme switching functionality directly within the translate view, allowing users to easily switch between light, dark, and system themes in the profile menu. (Issue #2936 )

Changes:

  • New UI Element: Added buttons in the profile menu allowing users to select their preferred theme: Dark, Light, or Match System.
  • Theme Persistence: User theme preference is stored in their profile, ensuring a consistent experience across sessions.

@ayanaar ayanaar requested a review from mathjazz October 24, 2023 22:07
Comment on lines 133 to 148
.toggle-button button {
background: var(--black-3);
border: 1px solid var(--light-grey-3);
border-radius: 3px;
box-sizing: border-box;
color: var(--grey-6);
font-size: 14px;
font-weight: 100;
padding: 4px;
width: 78px;
margin: 0;
float: none;
}

.toggle-button button:first-child {
border-right-color: var(--grey-3);
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}

.toggle-button button:nth-child(2) {
margin: 0 8px;
}

.toggle-button button:last-child {
border-left: 1px solid var(--light-grey-3);
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}

.toggle-button button:hover {
color: var(--light-grey-6);
}

.toggle-button button.active {
background: var(--dark-grey-1);
border-color: var(--grey-3);
color: var(--light-grey-7);
font-weight: 400;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

We'll need to sync these colors with the ones used in https://github.com/mozilla/pontoon/pull/2997/files#diff-30336dc4648b06dd8f0228e1c1013ab8260d8a5404cb86cba8187ed141519f1bR1461, but that should be done in the theme CSS PR.

}, [theme]);

const handleThemeButtonClick = (selectedTheme: string) => {
setTheme(selectedTheme); // Update the local state
Copy link
Collaborator

@mathjazz mathjazz Oct 25, 2023

Choose a reason for hiding this comment

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

State already gets updated through the onThemeChange() method, which ends up calling the saveTheme() action.

The only thing we should call here is applyTheme(). We should factor that function out and then import it here and in Theme.tsx. Then you can remove lines 43-87.

Copy link
Collaborator

@mathjazz mathjazz Oct 26, 2023

Choose a reason for hiding this comment

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

We should instead call applyTheme() here.

@mathjazz mathjazz marked this pull request as ready for review October 25, 2023 18:09
Copy link
Collaborator

@mathjazz mathjazz left a comment

Choose a reason for hiding this comment

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

Getting there! Let's factor out the applyTheme() as suggested.

@@ -1,4 +1,4 @@
# Generated by Django 3.2.15 on 2023-10-18 21:47
# Generated by Django 3.2.15 on 2023-10-25 16:35
Copy link
Collaborator

Choose a reason for hiding this comment

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

Whoops.

Comment on lines 16 to 10
function applyTheme(newTheme: string) {
if (newTheme === 'system') {
newTheme = getSystemTheme();
}
document.body.classList.remove(
'dark-theme',
'light-theme',
'system-theme',
);
document.body.classList.add(`${newTheme}-theme`);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

That and getSystemTheme() is all that should remain in this hook.

Everything else should go back to Theme.tsx, because it's only needed there.

@@ -37,6 +40,19 @@ export function UserMenuDialog({
const ref = useRef<HTMLUListElement>(null);
useOnDiscard(ref, onDiscard);

const [theme, setTheme] = useState<string>(
() => document.body.getAttribute('data-theme') || 'dark',
);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can get the theme from the user state, so no need for this block.

}, [theme]);

const handleThemeButtonClick = (selectedTheme: string) => {
setTheme(selectedTheme); // Update the local state
Copy link
Collaborator

@mathjazz mathjazz Oct 26, 2023

Choose a reason for hiding this comment

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

We should instead call applyTheme() here.

onClick={() => handleThemeButtonClick('dark')}
>
<i className='icon far fa-moon'></i>Dark
</button>
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should make the buttons and the label above localizable. Ideally, we should also factor out the button template.

Comment on lines 36 to 52
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
function handleThemeChange(e: MediaQueryListEvent) {
let userThemeSetting = document.body.getAttribute('data-theme') || 'dark';

if (userThemeSetting === 'system') {
applyTheme(e.matches ? 'dark' : 'light');
}
}

mediaQuery.addEventListener('change', handleThemeChange);

applyTheme(theme);

return () => {
mediaQuery.removeEventListener('change', handleThemeChange);
};
}, [theme]);
Copy link
Collaborator

@mathjazz mathjazz Oct 26, 2023

Choose a reason for hiding this comment

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

We still need the event handlers for system theme changes.

@mathjazz mathjazz force-pushed the 2936-user-profile-toggle branch from 10bc55d to 8f0d730 Compare October 26, 2023 13:38
@mathjazz mathjazz self-requested a review October 26, 2023 14:29
@mathjazz mathjazz merged commit ee337e7 into mozilla:main Oct 26, 2023
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants