@zendeskgarden/react-theming 9.5.0 → 9.5.1
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.
|
@@ -4,12 +4,12 @@
|
|
|
4
4
|
* Use of this source code is governed under the Apache License, Version 2.0
|
|
5
5
|
* found at http://www.apache.org/licenses/LICENSE-2.0.
|
|
6
6
|
*/
|
|
7
|
-
import React, { createContext, useMemo, useCallback, useState
|
|
7
|
+
import React, { createContext, useMemo, useEffect, useCallback, useState } from 'react';
|
|
8
|
+
import PropTypes from 'prop-types';
|
|
8
9
|
|
|
9
|
-
const
|
|
10
|
-
|
|
10
|
+
const mediaQuery = typeof window === 'undefined' ? undefined : window.matchMedia('(prefers-color-scheme: dark)');
|
|
11
|
+
const useColorScheme = (initialState, colorSchemeKey) => {
|
|
11
12
|
const localStorage = typeof window === 'undefined' ? undefined : window.localStorage;
|
|
12
|
-
const mediaQuery = typeof window === 'undefined' ? undefined : window.matchMedia('(prefers-color-scheme: dark)');
|
|
13
13
|
const getState = useCallback(_state => {
|
|
14
14
|
const isSystem = _state === 'system' || _state === undefined || _state === null;
|
|
15
15
|
let colorScheme;
|
|
@@ -22,21 +22,8 @@ const useColorScheme = function (initialState) {
|
|
|
22
22
|
isSystem,
|
|
23
23
|
colorScheme
|
|
24
24
|
};
|
|
25
|
-
}, [
|
|
25
|
+
}, []);
|
|
26
26
|
const [state, setState] = useState(getState(localStorage?.getItem(colorSchemeKey) || initialState));
|
|
27
|
-
useEffect(() => {
|
|
28
|
-
const eventListener = () => {
|
|
29
|
-
setState(getState('system'));
|
|
30
|
-
};
|
|
31
|
-
if (state.isSystem) {
|
|
32
|
-
mediaQuery?.addEventListener('change', eventListener);
|
|
33
|
-
} else {
|
|
34
|
-
mediaQuery?.removeEventListener('change', eventListener);
|
|
35
|
-
}
|
|
36
|
-
return () => {
|
|
37
|
-
mediaQuery?.removeEventListener('change', eventListener);
|
|
38
|
-
};
|
|
39
|
-
}, [getState, state.isSystem, mediaQuery]);
|
|
40
27
|
return {
|
|
41
28
|
isSystem: state.isSystem,
|
|
42
29
|
colorScheme: state.colorScheme,
|
|
@@ -50,8 +37,8 @@ const ColorSchemeContext = createContext(undefined);
|
|
|
50
37
|
const ColorSchemeProvider = _ref => {
|
|
51
38
|
let {
|
|
52
39
|
children,
|
|
53
|
-
colorSchemeKey,
|
|
54
|
-
initialColorScheme
|
|
40
|
+
colorSchemeKey = 'color-scheme',
|
|
41
|
+
initialColorScheme = 'system'
|
|
55
42
|
} = _ref;
|
|
56
43
|
const {
|
|
57
44
|
isSystem,
|
|
@@ -63,9 +50,26 @@ const ColorSchemeProvider = _ref => {
|
|
|
63
50
|
isSystem,
|
|
64
51
|
setColorScheme
|
|
65
52
|
}), [isSystem, colorScheme, setColorScheme]);
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
const eventListener = () => {
|
|
55
|
+
setColorScheme('system');
|
|
56
|
+
};
|
|
57
|
+
if (isSystem) {
|
|
58
|
+
mediaQuery?.addEventListener('change', eventListener);
|
|
59
|
+
} else {
|
|
60
|
+
mediaQuery?.removeEventListener('change', eventListener);
|
|
61
|
+
}
|
|
62
|
+
return () => {
|
|
63
|
+
mediaQuery?.removeEventListener('change', eventListener);
|
|
64
|
+
};
|
|
65
|
+
}, [isSystem, setColorScheme]);
|
|
66
66
|
return React.createElement(ColorSchemeContext.Provider, {
|
|
67
67
|
value: contextValue
|
|
68
68
|
}, children);
|
|
69
69
|
};
|
|
70
|
+
ColorSchemeProvider.propTypes = {
|
|
71
|
+
colorSchemeKey: PropTypes.string,
|
|
72
|
+
initialColorScheme: PropTypes.oneOf(['light', 'dark', 'system'])
|
|
73
|
+
};
|
|
70
74
|
|
|
71
75
|
export { ColorSchemeContext, ColorSchemeProvider };
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Use of this source code is governed under the Apache License, Version 2.0
|
|
5
5
|
* found at http://www.apache.org/licenses/LICENSE-2.0.
|
|
6
6
|
*/
|
|
7
|
-
import { getScale, parseToRgba } from 'color2k';
|
|
7
|
+
import { getScale, toHex as toHex$1, parseToRgba } from 'color2k';
|
|
8
8
|
import { getContrast, rgba, darken, lighten } from 'polished';
|
|
9
9
|
import get from 'lodash.get';
|
|
10
10
|
import memoize from 'lodash.memoize';
|
|
@@ -97,7 +97,7 @@ const generateColorScale = memoize(color => {
|
|
|
97
97
|
const colors = [];
|
|
98
98
|
const contrastRatios = [];
|
|
99
99
|
for (let i = 0; i <= scaleSize; i++) {
|
|
100
|
-
const _color = scale(i);
|
|
100
|
+
const _color = toHex$1(scale(i));
|
|
101
101
|
colors.push(_color);
|
|
102
102
|
contrastRatios.push(getContrast('#FFF', _color));
|
|
103
103
|
}
|
package/dist/index.cjs.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
9
|
var React = require('react');
|
|
10
|
+
var PropTypes = require('prop-types');
|
|
10
11
|
var styled = require('styled-components');
|
|
11
12
|
var color2k = require('color2k');
|
|
12
13
|
var polished = require('polished');
|
|
@@ -16,19 +17,19 @@ var memoize = require('lodash.memoize');
|
|
|
16
17
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
17
18
|
|
|
18
19
|
var React__default = /*#__PURE__*/_interopDefault(React);
|
|
20
|
+
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);
|
|
19
21
|
var styled__default = /*#__PURE__*/_interopDefault(styled);
|
|
20
22
|
var get__default = /*#__PURE__*/_interopDefault(get);
|
|
21
23
|
var memoize__default = /*#__PURE__*/_interopDefault(memoize);
|
|
22
24
|
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
+
const mediaQuery$1 = typeof window === 'undefined' ? undefined : window.matchMedia('(prefers-color-scheme: dark)');
|
|
26
|
+
const useColorScheme$1 = (initialState, colorSchemeKey) => {
|
|
25
27
|
const localStorage = typeof window === 'undefined' ? undefined : window.localStorage;
|
|
26
|
-
const mediaQuery = typeof window === 'undefined' ? undefined : window.matchMedia('(prefers-color-scheme: dark)');
|
|
27
28
|
const getState = React.useCallback(_state => {
|
|
28
29
|
const isSystem = _state === 'system' || _state === undefined || _state === null;
|
|
29
30
|
let colorScheme;
|
|
30
31
|
if (isSystem) {
|
|
31
|
-
colorScheme = mediaQuery?.matches ? 'dark' : 'light';
|
|
32
|
+
colorScheme = mediaQuery$1?.matches ? 'dark' : 'light';
|
|
32
33
|
} else {
|
|
33
34
|
colorScheme = _state;
|
|
34
35
|
}
|
|
@@ -36,21 +37,8 @@ const useColorScheme$1 = function (initialState) {
|
|
|
36
37
|
isSystem,
|
|
37
38
|
colorScheme
|
|
38
39
|
};
|
|
39
|
-
}, [
|
|
40
|
+
}, []);
|
|
40
41
|
const [state, setState] = React.useState(getState(localStorage?.getItem(colorSchemeKey) || initialState));
|
|
41
|
-
React.useEffect(() => {
|
|
42
|
-
const eventListener = () => {
|
|
43
|
-
setState(getState('system'));
|
|
44
|
-
};
|
|
45
|
-
if (state.isSystem) {
|
|
46
|
-
mediaQuery?.addEventListener('change', eventListener);
|
|
47
|
-
} else {
|
|
48
|
-
mediaQuery?.removeEventListener('change', eventListener);
|
|
49
|
-
}
|
|
50
|
-
return () => {
|
|
51
|
-
mediaQuery?.removeEventListener('change', eventListener);
|
|
52
|
-
};
|
|
53
|
-
}, [getState, state.isSystem, mediaQuery]);
|
|
54
42
|
return {
|
|
55
43
|
isSystem: state.isSystem,
|
|
56
44
|
colorScheme: state.colorScheme,
|
|
@@ -64,8 +52,8 @@ const ColorSchemeContext = React.createContext(undefined);
|
|
|
64
52
|
const ColorSchemeProvider = _ref => {
|
|
65
53
|
let {
|
|
66
54
|
children,
|
|
67
|
-
colorSchemeKey,
|
|
68
|
-
initialColorScheme
|
|
55
|
+
colorSchemeKey = 'color-scheme',
|
|
56
|
+
initialColorScheme = 'system'
|
|
69
57
|
} = _ref;
|
|
70
58
|
const {
|
|
71
59
|
isSystem,
|
|
@@ -77,10 +65,27 @@ const ColorSchemeProvider = _ref => {
|
|
|
77
65
|
isSystem,
|
|
78
66
|
setColorScheme
|
|
79
67
|
}), [isSystem, colorScheme, setColorScheme]);
|
|
68
|
+
React.useEffect(() => {
|
|
69
|
+
const eventListener = () => {
|
|
70
|
+
setColorScheme('system');
|
|
71
|
+
};
|
|
72
|
+
if (isSystem) {
|
|
73
|
+
mediaQuery$1?.addEventListener('change', eventListener);
|
|
74
|
+
} else {
|
|
75
|
+
mediaQuery$1?.removeEventListener('change', eventListener);
|
|
76
|
+
}
|
|
77
|
+
return () => {
|
|
78
|
+
mediaQuery$1?.removeEventListener('change', eventListener);
|
|
79
|
+
};
|
|
80
|
+
}, [isSystem, setColorScheme]);
|
|
80
81
|
return React__default.default.createElement(ColorSchemeContext.Provider, {
|
|
81
82
|
value: contextValue
|
|
82
83
|
}, children);
|
|
83
84
|
};
|
|
85
|
+
ColorSchemeProvider.propTypes = {
|
|
86
|
+
colorSchemeKey: PropTypes__default.default.string,
|
|
87
|
+
initialColorScheme: PropTypes__default.default.oneOf(['light', 'dark', 'system'])
|
|
88
|
+
};
|
|
84
89
|
|
|
85
90
|
const PALETTE = {
|
|
86
91
|
black: '#000',
|
|
@@ -714,7 +719,7 @@ const generateColorScale = memoize__default.default(color => {
|
|
|
714
719
|
const colors = [];
|
|
715
720
|
const contrastRatios = [];
|
|
716
721
|
for (let i = 0; i <= scaleSize; i++) {
|
|
717
|
-
const _color = scale(i);
|
|
722
|
+
const _color = color2k.toHex(scale(i));
|
|
718
723
|
colors.push(_color);
|
|
719
724
|
contrastRatios.push(polished.getContrast('#FFF', _color));
|
|
720
725
|
}
|
|
@@ -5,6 +5,13 @@
|
|
|
5
5
|
* found at http://www.apache.org/licenses/LICENSE-2.0.
|
|
6
6
|
*/
|
|
7
7
|
import React, { PropsWithChildren } from 'react';
|
|
8
|
+
import PropTypes from 'prop-types';
|
|
8
9
|
import { IColorSchemeContext, IColorSchemeProviderProps } from '../types';
|
|
9
10
|
export declare const ColorSchemeContext: React.Context<IColorSchemeContext | undefined>;
|
|
10
|
-
export declare const ColorSchemeProvider:
|
|
11
|
+
export declare const ColorSchemeProvider: {
|
|
12
|
+
({ children, colorSchemeKey, initialColorScheme }: PropsWithChildren<IColorSchemeProviderProps>): React.JSX.Element;
|
|
13
|
+
propTypes: {
|
|
14
|
+
colorSchemeKey: PropTypes.Requireable<string>;
|
|
15
|
+
initialColorScheme: PropTypes.Requireable<string>;
|
|
16
|
+
};
|
|
17
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zendeskgarden/react-theming",
|
|
3
|
-
"version": "9.5.
|
|
3
|
+
"version": "9.5.1",
|
|
4
4
|
"description": "Theming utilities and components within the Garden Design System",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Zendesk Garden <garden@zendesk.com>",
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"access": "public"
|
|
48
48
|
},
|
|
49
49
|
"zendeskgarden:src": "src/index.ts",
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "2571d0225b784bb7c765316ed584d289d35d4605"
|
|
51
51
|
}
|