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

Theming: Set themes.normal according to user preference and export getPreferredColorScheme #28721

Open
wants to merge 5 commits into
base: next-8.5
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions code/core/src/manager/globals/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ export default {
'css',
'darken',
'ensure',
'getPreferredColorScheme',
'ignoreSsrWarning',
'isPropValid',
'jsx',
Expand All @@ -701,6 +702,7 @@ export default {
'css',
'darken',
'ensure',
'getPreferredColorScheme',
'ignoreSsrWarning',
'isPropValid',
'jsx',
Expand All @@ -727,6 +729,7 @@ export default {
'css',
'darken',
'ensure',
'getPreferredColorScheme',
'ignoreSsrWarning',
'isPropValid',
'jsx',
Expand Down
18 changes: 14 additions & 4 deletions code/core/src/theming/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@ import lightThemeVars from './themes/light';
import type { ThemeVars, ThemeVarsPartial } from './types';
import { getPreferredColorScheme } from './utils';

export const themes: { light: ThemeVars; dark: ThemeVars; normal: ThemeVars } = {
interface Themes {
light: ThemeVars;
dark: ThemeVars;
normal: ThemeVars;
}

const themesBase: Omit<Themes, 'normal'> = {
light: lightThemeVars,
dark: darkThemeVars,
normal: lightThemeVars,
};

const preferredColorScheme = getPreferredColorScheme();

export const themes: Themes = {
...themesBase,
normal: themesBase[preferredColorScheme],
};

interface Rest {
[key: string]: any;
}

const preferredColorScheme = getPreferredColorScheme();

export const create = (
vars: ThemeVarsPartial = { base: preferredColorScheme },
rest?: Rest
Expand Down
2 changes: 1 addition & 1 deletion code/core/src/theming/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export * from './create';
export * from './convert';
export * from './ensure';

export { lightenColor as lighten, darkenColor as darken } from './utils';
export { lightenColor as lighten, darkenColor as darken, getPreferredColorScheme } from './utils';

export const ignoreSsrWarning =
'/* emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason */';
27 changes: 27 additions & 0 deletions code/core/src/theming/tests/create.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { describe, expect, it } from 'vitest';

import { create } from '../create';
import { getPreferredColorScheme } from './../utils';

vi.mock('./../utils', () => ({
getPreferredColorScheme: vi.fn().mockReturnValue('light'),
}));

describe('create base', () => {
it('should create a theme with minimal viable theme', () => {
Expand Down Expand Up @@ -142,3 +147,25 @@ describe('create extend', () => {
expect(result.base).toEqual('light');
});
});

describe('themes', () => {
beforeEach(() => {
vi.resetModules();
});

it('should set `normal` to `light` theme when user preference is `light`', async () => {
getPreferredColorScheme.mockReturnValue('light');

const { themes } = await import('./../create');

expect(themes.normal).toBe(themes.light);
});

it('should set `normal` to `dark` theme when user preference is `dark`', async () => {
getPreferredColorScheme.mockReturnValue('dark');

const { themes } = await import('./../create');

expect(themes.normal).toBe(themes.dark);
});
});
Loading