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 theme picker text when unspecified theme #42671

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions web/packages/design/src/ThemeProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ function themePreferenceToTheme(themePreference: Theme) {
return themePreference === Theme.LIGHT ? lightTheme : darkTheme;
}

// because unspecified can exist but only used as a fallback and not an option,
// we need to get the current/next themes with getPrefersDark in mind.
// TODO (avatus) when we add user settings page, we can add a Theme.SYSTEM option
// and remove the checks for unspecified
export function getCurrentTheme(currentTheme: Theme): Theme {
if (currentTheme === Theme.UNSPECIFIED) {
return getPrefersDark() ? Theme.DARK : Theme.LIGHT;
Copy link
Contributor

@kimlisa kimlisa Jun 17, 2024

Choose a reason for hiding this comment

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

how do i test this? i created a new user, and it defaulted to light theme (using brave)

nvm it worked, not sure what happened before lol.

when i do this flow:

new user -> login -> switch theme

the first click did not work, i had to click a second time to switch the theme

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't able to reproduce this first click not working in incognito or normal mode

}

return currentTheme;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you intentionally not returning anything if currentTheme !== Theme.UNSPECIFIED?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. Thanks!


export function getNextTheme(currentTheme: Theme): Theme {
return getCurrentTheme(currentTheme) === Theme.LIGHT
? Theme.DARK
: Theme.LIGHT;
}

export function getPrefersDark(): boolean {
return (
window.matchMedia &&
Expand Down
11 changes: 5 additions & 6 deletions web/packages/teleport/src/components/UserMenuNav/UserMenuNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import styled, { useTheme } from 'styled-components';
import { Moon, Sun, ChevronDown, Logout as LogoutIcon } from 'design/Icon';
import { Text } from 'design';
import { useRefClickOutside } from 'shared/hooks/useRefClickOutside';
import { getCurrentTheme, getNextTheme } from 'design/ThemeProvider';

import { Theme } from 'gen-proto-ts/teleport/userpreferences/v1/theme_pb';

Expand Down Expand Up @@ -124,11 +125,10 @@ export function UserMenuNav({ username, iconSize }: UserMenuNavProps) {
const ctx = useTeleport();
const clusterId = ctx.storeUser.getClusterId();
const features = useFeatures();
const currentTheme = getCurrentTheme(preferences.theme);
const nextTheme = getNextTheme(preferences.theme);

const onThemeChange = () => {
const nextTheme =
preferences.theme === Theme.LIGHT ? Theme.DARK : Theme.LIGHT;

updatePreferences({ theme: nextTheme });
setOpen(false);
};
Expand Down Expand Up @@ -182,10 +182,9 @@ export function UserMenuNav({ username, iconSize }: UserMenuNavProps) {
<DropdownItem open={open} $transitionDelay={transitionDelay}>
<DropdownItemButton onClick={onThemeChange}>
<DropdownItemIcon>
{preferences.theme === Theme.DARK ? <Sun /> : <Moon />}
{currentTheme === Theme.DARK ? <Sun /> : <Moon />}
</DropdownItemIcon>
Switch to {preferences.theme === Theme.DARK ? 'Light' : 'Dark'}{' '}
Theme
Switch to {currentTheme === Theme.DARK ? 'Light' : 'Dark'} Theme
</DropdownItemButton>
</DropdownItem>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { getCurrentTheme, getNextTheme } from 'design/ThemeProvider';
import { Theme } from 'gen-proto-ts/teleport/userpreferences/v1/theme_pb';
import { UserPreferences } from 'gen-proto-ts/teleport/userpreferences/v1/userpreferences_pb';

Expand Down Expand Up @@ -72,3 +73,45 @@ test('should convert the user preferences back to the old format when updating',
actualUserPreferences.clusterPreferences.pinnedResources.resourceIds
);
});

test('getCurrentTheme', () => {
mockMatchMediaWindow('dark');
let currentTheme = getCurrentTheme(Theme.UNSPECIFIED);
expect(currentTheme).toBe(Theme.DARK);

mockMatchMediaWindow('light');
currentTheme = getCurrentTheme(Theme.UNSPECIFIED);
expect(currentTheme).toBe(Theme.LIGHT);

currentTheme = getCurrentTheme(Theme.LIGHT);
expect(currentTheme).toBe(Theme.LIGHT);

currentTheme = getCurrentTheme(Theme.DARK);
expect(currentTheme).toBe(Theme.DARK);
});

test('getNextTheme', () => {
mockMatchMediaWindow('dark');
let nextTheme = getNextTheme(Theme.UNSPECIFIED);
expect(nextTheme).toBe(Theme.LIGHT);

mockMatchMediaWindow('light');
nextTheme = getNextTheme(Theme.UNSPECIFIED);
expect(nextTheme).toBe(Theme.DARK);

nextTheme = getNextTheme(Theme.LIGHT);
expect(nextTheme).toBe(Theme.DARK);

nextTheme = getNextTheme(Theme.DARK);
expect(nextTheme).toBe(Theme.LIGHT);
});

function mockMatchMediaWindow(prefers: 'light' | 'dark') {
return Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: query === `(prefers-color-scheme: ${prefers})`,
media: query,
})),
});
}
Loading