@mui/system 5.10.0 → 5.10.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Box/Box.d.ts +10 -10
- package/Box/Box.spec.d.ts +1 -1
- package/CHANGELOG.md +176 -0
- package/Container/Container.d.ts +13 -13
- package/Container/ContainerProps.d.ts +40 -40
- package/Container/containerClasses.d.ts +22 -22
- package/Container/createContainer.d.ts +18 -18
- package/Container/index.d.ts +1 -1
- package/Stack/Stack.d.ts +12 -12
- package/Stack/StackProps.d.ts +42 -42
- package/Stack/createStack.d.ts +21 -21
- package/Stack/index.d.ts +5 -5
- package/Stack/stackClasses.d.ts +8 -8
- package/Unstable_Grid/Grid.d.ts +12 -12
- package/Unstable_Grid/GridProps.d.ts +162 -162
- package/Unstable_Grid/createGrid.d.ts +11 -11
- package/Unstable_Grid/gridClasses.d.ts +20 -20
- package/Unstable_Grid/gridGenerator.d.ts +26 -26
- package/Unstable_Grid/index.d.ts +5 -5
- package/createBox.spec.d.ts +1 -1
- package/createTheme/createSpacing.d.ts +10 -10
- package/cssVars/createCssVarsProvider.spec.d.ts +1 -1
- package/cssVars/createGetCssVar.d.ts +5 -5
- package/cssVars/cssVarsParser.d.ts +65 -65
- package/cssVars/getInitColorSchemeScript.d.ts +45 -45
- package/cssVars/index.d.ts +3 -3
- package/cssVars/useCurrentColorScheme.d.ts +53 -53
- package/cssVars/useCurrentColorScheme.js +60 -49
- package/esm/cssVars/useCurrentColorScheme.js +60 -47
- package/index.js +1 -1
- package/index.spec.d.ts +1 -1
- package/legacy/cssVars/useCurrentColorScheme.js +60 -47
- package/legacy/index.js +1 -1
- package/modern/cssVars/useCurrentColorScheme.js +60 -47
- package/modern/index.js +1 -1
- package/package.json +6 -6
- package/styleFunctionSx/styleFunctionSx.spec.d.ts +1 -1
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
declare type NestedRecord<V = any> = {
|
|
2
|
-
[k: string | number]: NestedRecord<V> | V;
|
|
3
|
-
};
|
|
4
|
-
/**
|
|
5
|
-
* This function create an object from keys, value and then assign to target
|
|
6
|
-
*
|
|
7
|
-
* @param {Object} obj : the target object to be assigned
|
|
8
|
-
* @param {string[]} keys
|
|
9
|
-
* @param {string | number} value
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* const source = {}
|
|
13
|
-
* assignNestedKeys(source, ['palette', 'primary'], 'var(--palette-primary)')
|
|
14
|
-
* console.log(source) // { palette: { primary: 'var(--palette-primary)' } }
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* const source = { palette: { primary: 'var(--palette-primary)' } }
|
|
18
|
-
* assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
|
|
19
|
-
* console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
|
|
20
|
-
*/
|
|
21
|
-
export declare const assignNestedKeys: <Object_1 = NestedRecord<any>, Value = any>(obj: Object_1, keys: Array<string>, value: Value, arrayKeys?: Array<string>) => void;
|
|
22
|
-
/**
|
|
23
|
-
*
|
|
24
|
-
* @param {Object} obj : source object
|
|
25
|
-
* @param {Function} callback : a function that will be called when
|
|
26
|
-
* - the deepest key in source object is reached
|
|
27
|
-
* - the value of the deepest key is NOT `undefined` | `null`
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* walkObjectDeep({ palette: { primary: { main: '#000000' } } }, console.log)
|
|
31
|
-
* // ['palette', 'primary', 'main'] '#000000'
|
|
32
|
-
*/
|
|
33
|
-
export declare const walkObjectDeep: <Value, T = Record<string, any>>(obj: T, callback: (keys: Array<string>, value: Value, arrayKeys: Array<string>) => void, shouldSkipPaths?: ((keys: Array<string>) => boolean) | undefined) => void;
|
|
34
|
-
/**
|
|
35
|
-
* a function that parse theme and return { css, vars }
|
|
36
|
-
*
|
|
37
|
-
* @param {Object} theme
|
|
38
|
-
* @param {{
|
|
39
|
-
* prefix?: string,
|
|
40
|
-
* shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean
|
|
41
|
-
* }} options.
|
|
42
|
-
* `prefix`: The prefix of the generated CSS variables. This function does not change the value.
|
|
43
|
-
*
|
|
44
|
-
* @returns {{ css: Object, vars: Object, parsedTheme: typeof theme }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme), and `parsedTheme` is the cloned version of theme.
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* const { css, vars, parsedTheme } = parser({
|
|
48
|
-
* fontSize: 12,
|
|
49
|
-
* lineHeight: 1.2,
|
|
50
|
-
* palette: { primary: { 500: 'var(--color)' } }
|
|
51
|
-
* }, { prefix: 'foo' })
|
|
52
|
-
*
|
|
53
|
-
* console.log(css) // { '--foo-fontSize': '12px', '--foo-lineHeight': 1.2, '--foo-palette-primary-500': 'var(--color)' }
|
|
54
|
-
* console.log(vars) // { fontSize: 'var(--foo-fontSize)', lineHeight: 'var(--foo-lineHeight)', palette: { primary: { 500: 'var(--foo-palette-primary-500)' } } }
|
|
55
|
-
* console.log(parsedTheme) // { fontSize: 12, lineHeight: 1.2, palette: { primary: { 500: 'var(--color)' } } }
|
|
56
|
-
*/
|
|
57
|
-
export default function cssVarsParser<T extends Record<string, any>>(theme: T, options?: {
|
|
58
|
-
prefix?: string;
|
|
59
|
-
shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean;
|
|
60
|
-
}): {
|
|
61
|
-
css: NestedRecord<string>;
|
|
62
|
-
vars: NestedRecord<string>;
|
|
63
|
-
parsedTheme: T;
|
|
64
|
-
};
|
|
65
|
-
export {};
|
|
1
|
+
declare type NestedRecord<V = any> = {
|
|
2
|
+
[k: string | number]: NestedRecord<V> | V;
|
|
3
|
+
};
|
|
4
|
+
/**
|
|
5
|
+
* This function create an object from keys, value and then assign to target
|
|
6
|
+
*
|
|
7
|
+
* @param {Object} obj : the target object to be assigned
|
|
8
|
+
* @param {string[]} keys
|
|
9
|
+
* @param {string | number} value
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const source = {}
|
|
13
|
+
* assignNestedKeys(source, ['palette', 'primary'], 'var(--palette-primary)')
|
|
14
|
+
* console.log(source) // { palette: { primary: 'var(--palette-primary)' } }
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* const source = { palette: { primary: 'var(--palette-primary)' } }
|
|
18
|
+
* assignNestedKeys(source, ['palette', 'secondary'], 'var(--palette-secondary)')
|
|
19
|
+
* console.log(source) // { palette: { primary: 'var(--palette-primary)', secondary: 'var(--palette-secondary)' } }
|
|
20
|
+
*/
|
|
21
|
+
export declare const assignNestedKeys: <Object_1 = NestedRecord<any>, Value = any>(obj: Object_1, keys: Array<string>, value: Value, arrayKeys?: Array<string>) => void;
|
|
22
|
+
/**
|
|
23
|
+
*
|
|
24
|
+
* @param {Object} obj : source object
|
|
25
|
+
* @param {Function} callback : a function that will be called when
|
|
26
|
+
* - the deepest key in source object is reached
|
|
27
|
+
* - the value of the deepest key is NOT `undefined` | `null`
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* walkObjectDeep({ palette: { primary: { main: '#000000' } } }, console.log)
|
|
31
|
+
* // ['palette', 'primary', 'main'] '#000000'
|
|
32
|
+
*/
|
|
33
|
+
export declare const walkObjectDeep: <Value, T = Record<string, any>>(obj: T, callback: (keys: Array<string>, value: Value, arrayKeys: Array<string>) => void, shouldSkipPaths?: ((keys: Array<string>) => boolean) | undefined) => void;
|
|
34
|
+
/**
|
|
35
|
+
* a function that parse theme and return { css, vars }
|
|
36
|
+
*
|
|
37
|
+
* @param {Object} theme
|
|
38
|
+
* @param {{
|
|
39
|
+
* prefix?: string,
|
|
40
|
+
* shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean
|
|
41
|
+
* }} options.
|
|
42
|
+
* `prefix`: The prefix of the generated CSS variables. This function does not change the value.
|
|
43
|
+
*
|
|
44
|
+
* @returns {{ css: Object, vars: Object, parsedTheme: typeof theme }} `css` is the stylesheet, `vars` is an object to get css variable (same structure as theme), and `parsedTheme` is the cloned version of theme.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* const { css, vars, parsedTheme } = parser({
|
|
48
|
+
* fontSize: 12,
|
|
49
|
+
* lineHeight: 1.2,
|
|
50
|
+
* palette: { primary: { 500: 'var(--color)' } }
|
|
51
|
+
* }, { prefix: 'foo' })
|
|
52
|
+
*
|
|
53
|
+
* console.log(css) // { '--foo-fontSize': '12px', '--foo-lineHeight': 1.2, '--foo-palette-primary-500': 'var(--color)' }
|
|
54
|
+
* console.log(vars) // { fontSize: 'var(--foo-fontSize)', lineHeight: 'var(--foo-lineHeight)', palette: { primary: { 500: 'var(--foo-palette-primary-500)' } } }
|
|
55
|
+
* console.log(parsedTheme) // { fontSize: 12, lineHeight: 1.2, palette: { primary: { 500: 'var(--color)' } } }
|
|
56
|
+
*/
|
|
57
|
+
export default function cssVarsParser<T extends Record<string, any>>(theme: T, options?: {
|
|
58
|
+
prefix?: string;
|
|
59
|
+
shouldSkipGeneratingVar?: (objectPathKeys: Array<string>, value: string | number) => boolean;
|
|
60
|
+
}): {
|
|
61
|
+
css: NestedRecord<string>;
|
|
62
|
+
vars: NestedRecord<string>;
|
|
63
|
+
parsedTheme: T;
|
|
64
|
+
};
|
|
65
|
+
export {};
|
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
|
-
export declare const DEFAULT_MODE_STORAGE_KEY = "mode";
|
|
3
|
-
export declare const DEFAULT_COLOR_SCHEME_STORAGE_KEY = "color-scheme";
|
|
4
|
-
export declare const DEFAULT_ATTRIBUTE = "data-color-scheme";
|
|
5
|
-
export interface GetInitColorSchemeScriptOptions {
|
|
6
|
-
/**
|
|
7
|
-
* Indicate to the browser which color scheme is used (light or dark) for rendering built-in UI
|
|
8
|
-
* @default true
|
|
9
|
-
*/
|
|
10
|
-
enableColorScheme?: boolean;
|
|
11
|
-
/**
|
|
12
|
-
* If `true`, the initial color scheme is set to the user's prefers-color-scheme mode
|
|
13
|
-
* @default false
|
|
14
|
-
*/
|
|
15
|
-
enableSystem?: boolean;
|
|
16
|
-
/**
|
|
17
|
-
* The default color scheme to be used on the light mode
|
|
18
|
-
*/
|
|
19
|
-
defaultLightColorScheme?: string;
|
|
20
|
-
/**
|
|
21
|
-
* The default color scheme to be used on the dark mode
|
|
22
|
-
*/
|
|
23
|
-
defaultDarkColorScheme?: string;
|
|
24
|
-
/**
|
|
25
|
-
* The node (provided as string) used to attach the color-scheme attribute
|
|
26
|
-
* @default 'document.documentElement'
|
|
27
|
-
*/
|
|
28
|
-
colorSchemeNode?: string;
|
|
29
|
-
/**
|
|
30
|
-
* localStorage key used to store `mode`
|
|
31
|
-
* @default 'mode'
|
|
32
|
-
*/
|
|
33
|
-
modeStorageKey?: string;
|
|
34
|
-
/**
|
|
35
|
-
* localStorage key used to store `colorScheme`
|
|
36
|
-
* @default 'color-scheme'
|
|
37
|
-
*/
|
|
38
|
-
colorSchemeStorageKey?: string;
|
|
39
|
-
/**
|
|
40
|
-
* DOM attribute for applying color scheme
|
|
41
|
-
* @default 'data-color-scheme'
|
|
42
|
-
*/
|
|
43
|
-
attribute?: string;
|
|
44
|
-
}
|
|
45
|
-
export default function getInitColorSchemeScript(options?: GetInitColorSchemeScriptOptions): JSX.Element;
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export declare const DEFAULT_MODE_STORAGE_KEY = "mode";
|
|
3
|
+
export declare const DEFAULT_COLOR_SCHEME_STORAGE_KEY = "color-scheme";
|
|
4
|
+
export declare const DEFAULT_ATTRIBUTE = "data-color-scheme";
|
|
5
|
+
export interface GetInitColorSchemeScriptOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Indicate to the browser which color scheme is used (light or dark) for rendering built-in UI
|
|
8
|
+
* @default true
|
|
9
|
+
*/
|
|
10
|
+
enableColorScheme?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* If `true`, the initial color scheme is set to the user's prefers-color-scheme mode
|
|
13
|
+
* @default false
|
|
14
|
+
*/
|
|
15
|
+
enableSystem?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* The default color scheme to be used on the light mode
|
|
18
|
+
*/
|
|
19
|
+
defaultLightColorScheme?: string;
|
|
20
|
+
/**
|
|
21
|
+
* The default color scheme to be used on the dark mode
|
|
22
|
+
*/
|
|
23
|
+
defaultDarkColorScheme?: string;
|
|
24
|
+
/**
|
|
25
|
+
* The node (provided as string) used to attach the color-scheme attribute
|
|
26
|
+
* @default 'document.documentElement'
|
|
27
|
+
*/
|
|
28
|
+
colorSchemeNode?: string;
|
|
29
|
+
/**
|
|
30
|
+
* localStorage key used to store `mode`
|
|
31
|
+
* @default 'mode'
|
|
32
|
+
*/
|
|
33
|
+
modeStorageKey?: string;
|
|
34
|
+
/**
|
|
35
|
+
* localStorage key used to store `colorScheme`
|
|
36
|
+
* @default 'color-scheme'
|
|
37
|
+
*/
|
|
38
|
+
colorSchemeStorageKey?: string;
|
|
39
|
+
/**
|
|
40
|
+
* DOM attribute for applying color scheme
|
|
41
|
+
* @default 'data-color-scheme'
|
|
42
|
+
*/
|
|
43
|
+
attribute?: string;
|
|
44
|
+
}
|
|
45
|
+
export default function getInitColorSchemeScript(options?: GetInitColorSchemeScriptOptions): JSX.Element;
|
package/cssVars/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { default } from './createCssVarsProvider';
|
|
2
|
-
export type { CreateCssVarsProviderResult, CssVarsProviderConfig, ColorSchemeContextValue, } from './createCssVarsProvider';
|
|
3
|
-
export { default as getInitColorSchemeScript } from './getInitColorSchemeScript';
|
|
1
|
+
export { default } from './createCssVarsProvider';
|
|
2
|
+
export type { CreateCssVarsProviderResult, CssVarsProviderConfig, ColorSchemeContextValue, } from './createCssVarsProvider';
|
|
3
|
+
export { default as getInitColorSchemeScript } from './getInitColorSchemeScript';
|
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
export declare type Mode = 'light' | 'dark' | 'system';
|
|
2
|
-
export declare type SystemMode = Exclude<Mode, 'system'>;
|
|
3
|
-
export interface State<SupportedColorScheme extends string> {
|
|
4
|
-
/**
|
|
5
|
-
* User selected mode.
|
|
6
|
-
* Note: on the server, mode is always undefined
|
|
7
|
-
*/
|
|
8
|
-
mode: Mode | undefined;
|
|
9
|
-
/**
|
|
10
|
-
* Only valid if `mode: 'system'`, either 'light' | 'dark'.
|
|
11
|
-
*/
|
|
12
|
-
systemMode: SystemMode | undefined;
|
|
13
|
-
/**
|
|
14
|
-
* The color scheme for the light mode.
|
|
15
|
-
*/
|
|
16
|
-
lightColorScheme: SupportedColorScheme;
|
|
17
|
-
/**
|
|
18
|
-
* The color scheme for the dark mode.
|
|
19
|
-
*/
|
|
20
|
-
darkColorScheme: SupportedColorScheme;
|
|
21
|
-
}
|
|
22
|
-
export declare type Result<SupportedColorScheme extends string> = State<SupportedColorScheme> & {
|
|
23
|
-
/**
|
|
24
|
-
* The current application color scheme. It is always `undefined` on the server.
|
|
25
|
-
*/
|
|
26
|
-
colorScheme: SupportedColorScheme | undefined;
|
|
27
|
-
/**
|
|
28
|
-
* `mode` is saved to internal state and localStorage
|
|
29
|
-
* If `mode` is null, it will be reset to the defaultMode
|
|
30
|
-
*/
|
|
31
|
-
setMode: (mode: Mode | null) => void;
|
|
32
|
-
/**
|
|
33
|
-
* `colorScheme` is saved to internal state and localStorage
|
|
34
|
-
* If `colorScheme` is null, it will be reset to the defaultColorScheme (light | dark)
|
|
35
|
-
*/
|
|
36
|
-
setColorScheme: (colorScheme: SupportedColorScheme | Partial<{
|
|
37
|
-
light: SupportedColorScheme | null;
|
|
38
|
-
dark: SupportedColorScheme | null;
|
|
39
|
-
}> | null) => void;
|
|
40
|
-
};
|
|
41
|
-
export declare function getSystemMode(mode: undefined | string): SystemMode | undefined;
|
|
42
|
-
export declare function getColorScheme<SupportedColorScheme extends string>(state: State<SupportedColorScheme>): SupportedColorScheme | undefined;
|
|
43
|
-
interface UseCurrentColoSchemeOptions<SupportedColorScheme extends string> {
|
|
44
|
-
defaultLightColorScheme: SupportedColorScheme;
|
|
45
|
-
defaultDarkColorScheme: SupportedColorScheme;
|
|
46
|
-
supportedColorSchemes: Array<SupportedColorScheme>;
|
|
47
|
-
defaultMode?: Mode;
|
|
48
|
-
modeStorageKey?: string;
|
|
49
|
-
colorSchemeStorageKey?: string;
|
|
50
|
-
storageWindow?: Window | null;
|
|
51
|
-
}
|
|
52
|
-
export default function useCurrentColorScheme<SupportedColorScheme extends string>(options: UseCurrentColoSchemeOptions<SupportedColorScheme>): Result<SupportedColorScheme>;
|
|
53
|
-
export {};
|
|
1
|
+
export declare type Mode = 'light' | 'dark' | 'system';
|
|
2
|
+
export declare type SystemMode = Exclude<Mode, 'system'>;
|
|
3
|
+
export interface State<SupportedColorScheme extends string> {
|
|
4
|
+
/**
|
|
5
|
+
* User selected mode.
|
|
6
|
+
* Note: on the server, mode is always undefined
|
|
7
|
+
*/
|
|
8
|
+
mode: Mode | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Only valid if `mode: 'system'`, either 'light' | 'dark'.
|
|
11
|
+
*/
|
|
12
|
+
systemMode: SystemMode | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* The color scheme for the light mode.
|
|
15
|
+
*/
|
|
16
|
+
lightColorScheme: SupportedColorScheme;
|
|
17
|
+
/**
|
|
18
|
+
* The color scheme for the dark mode.
|
|
19
|
+
*/
|
|
20
|
+
darkColorScheme: SupportedColorScheme;
|
|
21
|
+
}
|
|
22
|
+
export declare type Result<SupportedColorScheme extends string> = State<SupportedColorScheme> & {
|
|
23
|
+
/**
|
|
24
|
+
* The current application color scheme. It is always `undefined` on the server.
|
|
25
|
+
*/
|
|
26
|
+
colorScheme: SupportedColorScheme | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* `mode` is saved to internal state and localStorage
|
|
29
|
+
* If `mode` is null, it will be reset to the defaultMode
|
|
30
|
+
*/
|
|
31
|
+
setMode: (mode: Mode | null) => void;
|
|
32
|
+
/**
|
|
33
|
+
* `colorScheme` is saved to internal state and localStorage
|
|
34
|
+
* If `colorScheme` is null, it will be reset to the defaultColorScheme (light | dark)
|
|
35
|
+
*/
|
|
36
|
+
setColorScheme: (colorScheme: SupportedColorScheme | Partial<{
|
|
37
|
+
light: SupportedColorScheme | null;
|
|
38
|
+
dark: SupportedColorScheme | null;
|
|
39
|
+
}> | null) => void;
|
|
40
|
+
};
|
|
41
|
+
export declare function getSystemMode(mode: undefined | string): SystemMode | undefined;
|
|
42
|
+
export declare function getColorScheme<SupportedColorScheme extends string>(state: State<SupportedColorScheme>): SupportedColorScheme | undefined;
|
|
43
|
+
interface UseCurrentColoSchemeOptions<SupportedColorScheme extends string> {
|
|
44
|
+
defaultLightColorScheme: SupportedColorScheme;
|
|
45
|
+
defaultDarkColorScheme: SupportedColorScheme;
|
|
46
|
+
supportedColorSchemes: Array<SupportedColorScheme>;
|
|
47
|
+
defaultMode?: Mode;
|
|
48
|
+
modeStorageKey?: string;
|
|
49
|
+
colorSchemeStorageKey?: string;
|
|
50
|
+
storageWindow?: Window | null;
|
|
51
|
+
}
|
|
52
|
+
export default function useCurrentColorScheme<SupportedColorScheme extends string>(options: UseCurrentColoSchemeOptions<SupportedColorScheme>): Result<SupportedColorScheme>;
|
|
53
|
+
export {};
|
|
@@ -59,7 +59,7 @@ function getColorScheme(state) {
|
|
|
59
59
|
});
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
function
|
|
62
|
+
function initializeValue(key, defaultValue) {
|
|
63
63
|
if (typeof window === 'undefined') {
|
|
64
64
|
return undefined;
|
|
65
65
|
}
|
|
@@ -68,6 +68,11 @@ function resolveValue(key, defaultValue) {
|
|
|
68
68
|
|
|
69
69
|
try {
|
|
70
70
|
value = localStorage.getItem(key) || undefined;
|
|
71
|
+
|
|
72
|
+
if (!value) {
|
|
73
|
+
// the first time that user enters the site.
|
|
74
|
+
localStorage.setItem(key, defaultValue);
|
|
75
|
+
}
|
|
71
76
|
} catch (e) {// Unsupported
|
|
72
77
|
}
|
|
73
78
|
|
|
@@ -86,25 +91,29 @@ function useCurrentColorScheme(options) {
|
|
|
86
91
|
} = options;
|
|
87
92
|
const joinedColorSchemes = supportedColorSchemes.join(',');
|
|
88
93
|
const [state, setState] = React.useState(() => {
|
|
89
|
-
const initialMode =
|
|
94
|
+
const initialMode = initializeValue(modeStorageKey, defaultMode);
|
|
95
|
+
const lightColorScheme = initializeValue(`${colorSchemeStorageKey}-light`, defaultLightColorScheme);
|
|
96
|
+
const darkColorScheme = initializeValue(`${colorSchemeStorageKey}-dark`, defaultDarkColorScheme);
|
|
90
97
|
return {
|
|
91
98
|
mode: initialMode,
|
|
92
99
|
systemMode: getSystemMode(initialMode),
|
|
93
|
-
lightColorScheme
|
|
94
|
-
darkColorScheme
|
|
100
|
+
lightColorScheme,
|
|
101
|
+
darkColorScheme
|
|
95
102
|
};
|
|
96
103
|
});
|
|
97
104
|
const colorScheme = getColorScheme(state);
|
|
98
105
|
const setMode = React.useCallback(mode => {
|
|
99
106
|
setState(currentState => {
|
|
100
|
-
const newMode = !mode ? defaultMode : mode;
|
|
101
|
-
|
|
102
107
|
if (mode === currentState.mode) {
|
|
108
|
+
// do nothing if mode does not change
|
|
103
109
|
return currentState;
|
|
104
110
|
}
|
|
105
111
|
|
|
106
|
-
|
|
112
|
+
const newMode = !mode ? defaultMode : mode;
|
|
113
|
+
|
|
114
|
+
try {
|
|
107
115
|
localStorage.setItem(modeStorageKey, newMode);
|
|
116
|
+
} catch (e) {// Unsupported
|
|
108
117
|
}
|
|
109
118
|
|
|
110
119
|
return (0, _extends2.default)({}, currentState, {
|
|
@@ -114,22 +123,30 @@ function useCurrentColorScheme(options) {
|
|
|
114
123
|
});
|
|
115
124
|
}, [modeStorageKey, defaultMode]);
|
|
116
125
|
const setColorScheme = React.useCallback(value => {
|
|
117
|
-
if (!value
|
|
126
|
+
if (!value) {
|
|
127
|
+
setState(currentState => {
|
|
128
|
+
try {
|
|
129
|
+
localStorage.setItem(`${colorSchemeStorageKey}-light`, defaultLightColorScheme);
|
|
130
|
+
localStorage.setItem(`${colorSchemeStorageKey}-dark`, defaultDarkColorScheme);
|
|
131
|
+
} catch (e) {// Unsupported
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return (0, _extends2.default)({}, currentState, {
|
|
135
|
+
lightColorScheme: defaultLightColorScheme,
|
|
136
|
+
darkColorScheme: defaultDarkColorScheme
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
} else if (typeof value === 'string') {
|
|
118
140
|
if (value && !joinedColorSchemes.includes(value)) {
|
|
119
141
|
console.error(`\`${value}\` does not exist in \`theme.colorSchemes\`.`);
|
|
120
142
|
} else {
|
|
121
143
|
setState(currentState => {
|
|
122
144
|
const newState = (0, _extends2.default)({}, currentState);
|
|
123
|
-
|
|
124
|
-
if (!value) {
|
|
125
|
-
// reset to default color scheme
|
|
126
|
-
newState.lightColorScheme = defaultLightColorScheme;
|
|
127
|
-
newState.darkColorScheme = defaultDarkColorScheme;
|
|
128
|
-
return newState;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
145
|
processState(currentState, mode => {
|
|
132
|
-
|
|
146
|
+
try {
|
|
147
|
+
localStorage.setItem(`${colorSchemeStorageKey}-${mode}`, value);
|
|
148
|
+
} catch (e) {// Unsupported
|
|
149
|
+
}
|
|
133
150
|
|
|
134
151
|
if (mode === 'light') {
|
|
135
152
|
newState.lightColorScheme = value;
|
|
@@ -142,30 +159,40 @@ function useCurrentColorScheme(options) {
|
|
|
142
159
|
return newState;
|
|
143
160
|
});
|
|
144
161
|
}
|
|
145
|
-
} else if (value.light && !joinedColorSchemes.includes(value.light) || value.dark && !joinedColorSchemes.includes(value.dark)) {
|
|
146
|
-
console.error(`\`${value}\` does not exist in \`theme.colorSchemes\`.`);
|
|
147
162
|
} else {
|
|
148
163
|
setState(currentState => {
|
|
149
164
|
const newState = (0, _extends2.default)({}, currentState);
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
165
|
+
const newLightColorScheme = value.light === null ? defaultLightColorScheme : value.light;
|
|
166
|
+
const newDarkColorScheme = value.dark === null ? defaultDarkColorScheme : value.dark;
|
|
167
|
+
|
|
168
|
+
if (newLightColorScheme) {
|
|
169
|
+
if (!joinedColorSchemes.includes(newLightColorScheme)) {
|
|
170
|
+
console.error(`\`${newLightColorScheme}\` does not exist in \`theme.colorSchemes\`.`);
|
|
171
|
+
} else {
|
|
172
|
+
newState.lightColorScheme = newLightColorScheme;
|
|
173
|
+
|
|
174
|
+
try {
|
|
175
|
+
localStorage.setItem(`${colorSchemeStorageKey}-light`, newLightColorScheme);
|
|
176
|
+
} catch (error) {// Unsupported
|
|
177
|
+
}
|
|
178
|
+
}
|
|
153
179
|
}
|
|
154
180
|
|
|
155
|
-
if (
|
|
156
|
-
|
|
181
|
+
if (newDarkColorScheme) {
|
|
182
|
+
if (!joinedColorSchemes.includes(newDarkColorScheme)) {
|
|
183
|
+
console.error(`\`${newDarkColorScheme}\` does not exist in \`theme.colorSchemes\`.`);
|
|
184
|
+
} else {
|
|
185
|
+
newState.darkColorScheme = newDarkColorScheme;
|
|
186
|
+
|
|
187
|
+
try {
|
|
188
|
+
localStorage.setItem(`${colorSchemeStorageKey}-dark`, newDarkColorScheme);
|
|
189
|
+
} catch (error) {// Unsupported
|
|
190
|
+
}
|
|
191
|
+
}
|
|
157
192
|
}
|
|
158
193
|
|
|
159
194
|
return newState;
|
|
160
195
|
});
|
|
161
|
-
|
|
162
|
-
if (value.light) {
|
|
163
|
-
localStorage.setItem(`${colorSchemeStorageKey}-light`, value.light);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (value.dark) {
|
|
167
|
-
localStorage.setItem(`${colorSchemeStorageKey}-dark`, value.dark);
|
|
168
|
-
}
|
|
169
196
|
}
|
|
170
197
|
}, [joinedColorSchemes, colorSchemeStorageKey, defaultLightColorScheme, defaultDarkColorScheme]);
|
|
171
198
|
const handleMediaQuery = React.useCallback(e => {
|
|
@@ -187,23 +214,7 @@ function useCurrentColorScheme(options) {
|
|
|
187
214
|
media.addListener(handler);
|
|
188
215
|
handler(media);
|
|
189
216
|
return () => media.removeListener(handler);
|
|
190
|
-
}, []); //
|
|
191
|
-
|
|
192
|
-
React.useEffect(() => {
|
|
193
|
-
if (state.mode) {
|
|
194
|
-
localStorage.setItem(modeStorageKey, state.mode);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
processState(state, mode => {
|
|
198
|
-
if (mode === 'light') {
|
|
199
|
-
localStorage.setItem(`${colorSchemeStorageKey}-light`, state.lightColorScheme);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
if (mode === 'dark') {
|
|
203
|
-
localStorage.setItem(`${colorSchemeStorageKey}-dark`, state.darkColorScheme);
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
}, [state, colorSchemeStorageKey, modeStorageKey]); // Handle when localStorage has changed
|
|
217
|
+
}, []); // Handle when localStorage has changed
|
|
207
218
|
|
|
208
219
|
React.useEffect(() => {
|
|
209
220
|
const handleStorage = event => {
|