@fluentui-react-native/use-tokens 0.7.0 → 0.7.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.
- package/CHANGELOG.md +10 -0
- package/lib/applyPropsToTokens.d.ts +2 -7
- package/lib/applyPropsToTokens.js +7 -7
- package/lib/applyPropsToTokens.test.d.ts +1 -1
- package/lib/applyPropsToTokens.test.js +19 -19
- package/lib/applyTokenLayers.d.ts +2 -7
- package/lib/applyTokenLayers.js +13 -16
- package/lib/applyTokenLayers.test.d.ts +1 -1
- package/lib/applyTokenLayers.test.js +42 -42
- package/lib/buildUseTokens.d.ts +3 -6
- package/lib/buildUseTokens.js +22 -22
- package/lib/buildUseTokens.test.d.ts +1 -1
- package/lib/buildUseTokens.test.js +68 -68
- package/lib/customizable.d.ts +3 -6
- package/lib/customizable.js +7 -7
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/patchTokens.d.ts +2 -6
- package/lib/patchTokens.js +12 -12
- package/lib/patchTokens.test.d.ts +1 -1
- package/lib/patchTokens.test.js +32 -32
- package/lib/useTokens.samples.test.d.ts +1 -1
- package/lib/useTokens.samples.test.js +119 -133
- package/lib-commonjs/applyPropsToTokens.d.ts +2 -7
- package/lib-commonjs/applyPropsToTokens.js +9 -9
- package/lib-commonjs/applyPropsToTokens.test.d.ts +1 -1
- package/lib-commonjs/applyPropsToTokens.test.js +23 -23
- package/lib-commonjs/applyTokenLayers.d.ts +2 -7
- package/lib-commonjs/applyTokenLayers.js +16 -19
- package/lib-commonjs/applyTokenLayers.test.d.ts +1 -1
- package/lib-commonjs/applyTokenLayers.test.js +46 -46
- package/lib-commonjs/buildUseTokens.d.ts +3 -6
- package/lib-commonjs/buildUseTokens.js +26 -29
- package/lib-commonjs/buildUseTokens.test.d.ts +1 -1
- package/lib-commonjs/buildUseTokens.test.js +71 -71
- package/lib-commonjs/customizable.d.ts +3 -6
- package/lib-commonjs/customizable.js +9 -9
- package/lib-commonjs/index.d.ts +1 -1
- package/lib-commonjs/index.js +13 -38
- package/lib-commonjs/patchTokens.d.ts +2 -6
- package/lib-commonjs/patchTokens.js +14 -14
- package/lib-commonjs/patchTokens.test.d.ts +1 -1
- package/lib-commonjs/patchTokens.test.js +36 -36
- package/lib-commonjs/useTokens.samples.test.d.ts +1 -1
- package/lib-commonjs/useTokens.samples.test.js +156 -191
- package/package.json +14 -13
package/lib/patchTokens.test.js
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
1
|
import { getMemoCache } from '@fluentui-react-native/framework-base';
|
|
2
2
|
import { patchTokens } from './patchTokens';
|
|
3
3
|
const themeTokens = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
uno: 'uno',
|
|
5
|
+
dos: 'dos',
|
|
6
|
+
tres: 3,
|
|
7
|
+
quatro: 4,
|
|
8
|
+
cinco: true,
|
|
9
9
|
};
|
|
10
10
|
describe('patchTokens tests', () => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
11
|
+
test('props get copied', () => {
|
|
12
|
+
const cache = getMemoCache();
|
|
13
|
+
const patchValues = { uno: 'one', quatro: 'quatro' };
|
|
14
|
+
const [tokens] = patchTokens(themeTokens, cache, patchValues);
|
|
15
|
+
expect(tokens).not.toBe(themeTokens);
|
|
16
|
+
for (const key in patchValues) {
|
|
17
|
+
expect(tokens[key]).toEqual(patchValues[key]);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
test('no copied props does not change tokens', () => {
|
|
21
|
+
const cache = getMemoCache();
|
|
22
|
+
const patchValues1 = {};
|
|
23
|
+
const [tokens] = patchTokens(themeTokens, cache, patchValues1);
|
|
24
|
+
expect(tokens).toBe(themeTokens);
|
|
25
|
+
const patchValues2 = { tres: undefined };
|
|
26
|
+
const [tokens2] = patchTokens(themeTokens, cache, patchValues2);
|
|
27
|
+
expect(tokens2).toBe(themeTokens);
|
|
28
|
+
});
|
|
29
|
+
test('patching tokens cache independent of order', () => {
|
|
30
|
+
const cache = getMemoCache();
|
|
31
|
+
const patch1 = { uno: 'one', cinco: false };
|
|
32
|
+
const patch2 = { cinco: false, uno: 'one' };
|
|
33
|
+
const [tokens1] = patchTokens(themeTokens, cache, patch1);
|
|
34
|
+
const [tokens2] = patchTokens(themeTokens, cache, patch2);
|
|
35
|
+
expect(tokens1).toBe(tokens2);
|
|
36
|
+
});
|
|
37
37
|
});
|
|
38
|
-
//# sourceMappingURL=patchTokens.test.js.map
|
|
38
|
+
//# sourceMappingURL=patchTokens.test.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export {};
|
|
2
|
-
//# sourceMappingURL=useTokens.samples.test.d.ts.map
|
|
2
|
+
//# sourceMappingURL=useTokens.samples.test.d.ts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { jsx as _jsx, jsxs as _jsxs } from
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { act } from 'react';
|
|
3
3
|
import { Text, View } from 'react-native';
|
|
4
4
|
import { immutableMerge } from '@fluentui-react-native/framework-base';
|
|
@@ -9,19 +9,19 @@ import { buildUseTokens } from './buildUseTokens';
|
|
|
9
9
|
* The default/base theme just contains base values
|
|
10
10
|
*/
|
|
11
11
|
const baseTheme = {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
12
|
+
globals: {
|
|
13
|
+
backgroundColor: 'white',
|
|
14
|
+
color: 'black',
|
|
15
|
+
borderColor: 'blue',
|
|
16
|
+
fontFamily: 'Arial',
|
|
17
|
+
fontSize: 12,
|
|
18
|
+
},
|
|
19
|
+
components: {},
|
|
20
20
|
};
|
|
21
21
|
const current = { theme: baseTheme };
|
|
22
22
|
const useTheme = () => current.theme;
|
|
23
23
|
const setActiveTheme = (theme) => {
|
|
24
|
-
|
|
24
|
+
current.theme = (theme && immutableMerge(baseTheme, theme)) || baseTheme;
|
|
25
25
|
};
|
|
26
26
|
/**
|
|
27
27
|
* Helper function used to look up a component in the theme. Having this injected allows this module to not be dependent on the shape of
|
|
@@ -29,135 +29,121 @@ const setActiveTheme = (theme) => {
|
|
|
29
29
|
*/
|
|
30
30
|
const getComponentInfo = (theme, name) => theme.components[name];
|
|
31
31
|
describe('useTokens samples', () => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
getComponentInfo,
|
|
32
|
+
/**
|
|
33
|
+
* Sample #1 - Themeable text element
|
|
34
|
+
*
|
|
35
|
+
* This adds some default opinions for how a text element should be styled but only allows for customization
|
|
36
|
+
* via theming
|
|
37
|
+
*/
|
|
38
|
+
const useTokensSample1 = buildUseTokens(getComponentInfo,
|
|
40
39
|
/** first the default values should come from the global theme section */
|
|
41
40
|
(t) => ({
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}),
|
|
41
|
+
color: t.globals.color,
|
|
42
|
+
fontFamily: t.globals.fontFamily,
|
|
43
|
+
fontSize: t.globals.fontSize,
|
|
44
|
+
}),
|
|
46
45
|
/** next we should look for a component reference to overlay */
|
|
47
|
-
'SampleText'
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
beforeEach(() => {
|
|
81
|
-
setActiveTheme();
|
|
82
|
-
});
|
|
83
|
-
/** first render the component with no updates */
|
|
84
|
-
it('Sample1Text rendering with no overrides', () => {
|
|
85
|
-
let component;
|
|
86
|
-
act(() => {
|
|
87
|
-
component = renderer.create(_jsx(SampleText1, { children: 'Sample1a' }));
|
|
46
|
+
'SampleText');
|
|
47
|
+
const SampleText1 = (props) => {
|
|
48
|
+
// standard props splitting
|
|
49
|
+
const { style, children, ...rest } = props;
|
|
50
|
+
// typically this would start with a call to retrieve the theme from the context via whatever method is appropriate
|
|
51
|
+
const theme = useTheme();
|
|
52
|
+
// next the tokens are resolved from the theme, a cache specific to this theme is returned as well to allow for
|
|
53
|
+
// style objects to not be rebuilt unnecessarily
|
|
54
|
+
const [tokens, cache] = useTokensSample1(theme);
|
|
55
|
+
// build up the text style, or the full props as appropriate
|
|
56
|
+
const [styleFromTokens] = cache(
|
|
57
|
+
/**
|
|
58
|
+
* first build the style object
|
|
59
|
+
* - this executes once for every unique set of keys.
|
|
60
|
+
* - The cache is already unique for this theme
|
|
61
|
+
*/
|
|
62
|
+
() => ({ ...tokens }),
|
|
63
|
+
/**
|
|
64
|
+
* now specify the keys
|
|
65
|
+
* - because the only changing variable is the theme
|
|
66
|
+
* - ...and all the style properties are either constant or come from the tokens
|
|
67
|
+
* - ...no keys need to be specified
|
|
68
|
+
* - this means that only one style object will be created per component + theme pair
|
|
69
|
+
*/
|
|
70
|
+
[]);
|
|
71
|
+
// merge the props from the tokens with anything passed in via style. This is internally cached via object identity
|
|
72
|
+
// so the merged style object won't change identity unless one of the two inputs changes identity.
|
|
73
|
+
const mergedStyle = mergeStyles(styleFromTokens, style);
|
|
74
|
+
// now just render the element, forwarding the props, setting the merged style, then passing the children as appropriate
|
|
75
|
+
return (_jsx(Text, { ...rest, style: mergedStyle, children: children }));
|
|
76
|
+
};
|
|
77
|
+
beforeEach(() => {
|
|
78
|
+
setActiveTheme();
|
|
88
79
|
});
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
color: 'pink',
|
|
97
|
-
fontSize: 24,
|
|
98
|
-
},
|
|
99
|
-
},
|
|
80
|
+
/** first render the component with no updates */
|
|
81
|
+
it('Sample1Text rendering with no overrides', () => {
|
|
82
|
+
let component;
|
|
83
|
+
act(() => {
|
|
84
|
+
component = renderer.create(_jsx(SampleText1, { children: "Sample1a" }));
|
|
85
|
+
});
|
|
86
|
+
expect(component.toJSON()).toMatchSnapshot();
|
|
100
87
|
});
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
88
|
+
/** now re-theme the component via the components in the theme */
|
|
89
|
+
it('Sample1Text rendering with some custom settings in the theme', () => {
|
|
90
|
+
setActiveTheme({
|
|
91
|
+
components: {
|
|
92
|
+
SampleText: {
|
|
93
|
+
color: 'pink',
|
|
94
|
+
fontSize: 24,
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
let component;
|
|
99
|
+
act(() => {
|
|
100
|
+
component = renderer.create(_jsx(SampleText1, { children: "Sample1b" }));
|
|
101
|
+
});
|
|
102
|
+
expect(component.toJSON()).toMatchSnapshot();
|
|
104
103
|
});
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
children: [
|
|
131
|
-
_jsx(SampleText2, { children: 'Sample2 with defaults' }),
|
|
132
|
-
_jsx(SampleText2, { color: 'green', children: 'Sample2 with color override via prop' }),
|
|
133
|
-
],
|
|
134
|
-
}),
|
|
135
|
-
);
|
|
104
|
+
// the Sample2Text component is built the same way as sample1, just using the new hook that has been created
|
|
105
|
+
const SampleText2 = (props) => {
|
|
106
|
+
const { color, style, children, ...rest } = props;
|
|
107
|
+
const theme = useTheme();
|
|
108
|
+
// this starts the same as sample1, extract tokens from the theme and get a theme specific cache object
|
|
109
|
+
const [tokens, cache] = useTokensSample1(theme);
|
|
110
|
+
// now when building up the style this time, the resulting style object is based upon both the theme and the passed
|
|
111
|
+
// in value of colors. Because the theme is already part of the cache definition, only color needs to be a key
|
|
112
|
+
const styleFromTokens = cache(
|
|
113
|
+
/** build the style, only patch the color if it has a value, otherwise the theme value would get stomped if color was undefined */
|
|
114
|
+
() => ({ ...tokens, ...(color && { color }) }),
|
|
115
|
+
/** use color as an additional key for the style */
|
|
116
|
+
[color]);
|
|
117
|
+
// now just render, this time merging styles inline to make it a bit shorter
|
|
118
|
+
return (_jsx(Text, { ...rest, style: mergeStyles(styleFromTokens, style), children: children }));
|
|
119
|
+
};
|
|
120
|
+
/** rendering the Sample2 component with the base theme */
|
|
121
|
+
it('Sample2Text rendering with defaults and a color override', () => {
|
|
122
|
+
let component;
|
|
123
|
+
act(() => {
|
|
124
|
+
component = renderer.create(_jsxs(View, { children: [
|
|
125
|
+
_jsx(SampleText2, { children: "Sample2 with defaults" }), _jsx(SampleText2, { color: "green", children: "Sample2 with color override via prop" })
|
|
126
|
+
] }));
|
|
127
|
+
});
|
|
128
|
+
expect(component.toJSON()).toMatchSnapshot();
|
|
136
129
|
});
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
|
|
130
|
+
/** now re-theme the component via the components in the theme */
|
|
131
|
+
it('Sample2Text rendering with some custom settings in the theme', () => {
|
|
132
|
+
setActiveTheme({
|
|
133
|
+
components: {
|
|
134
|
+
SampleText: {
|
|
135
|
+
fontSize: 18,
|
|
136
|
+
fontFamily: 'Helvetica',
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
let component;
|
|
141
|
+
act(() => {
|
|
142
|
+
component = renderer.create(_jsxs(View, { children: [
|
|
143
|
+
_jsx(SampleText2, { children: "Sample2 with theme overrides set" }), _jsx(SampleText2, { color: "purple", children: "Sample2 with theme and color prop override" })
|
|
144
|
+
] }));
|
|
145
|
+
});
|
|
146
|
+
expect(component.toJSON()).toMatchSnapshot();
|
|
148
147
|
});
|
|
149
|
-
let component;
|
|
150
|
-
act(() => {
|
|
151
|
-
component = renderer.create(
|
|
152
|
-
_jsxs(View, {
|
|
153
|
-
children: [
|
|
154
|
-
_jsx(SampleText2, { children: 'Sample2 with theme overrides set' }),
|
|
155
|
-
_jsx(SampleText2, { color: 'purple', children: 'Sample2 with theme and color prop override' }),
|
|
156
|
-
],
|
|
157
|
-
}),
|
|
158
|
-
);
|
|
159
|
-
});
|
|
160
|
-
expect(component.toJSON()).toMatchSnapshot();
|
|
161
|
-
});
|
|
162
148
|
});
|
|
163
|
-
//# sourceMappingURL=useTokens.samples.test.js.map
|
|
149
|
+
//# sourceMappingURL=useTokens.samples.test.js.map
|
|
@@ -1,8 +1,3 @@
|
|
|
1
1
|
import type { GetTypedMemoValue } from '@fluentui-react-native/framework-base';
|
|
2
|
-
export declare function applyPropsToTokens<TProps, TTokens>(
|
|
3
|
-
|
|
4
|
-
tokens: TTokens,
|
|
5
|
-
cache: GetTypedMemoValue<TTokens>,
|
|
6
|
-
keys: (keyof TTokens)[],
|
|
7
|
-
): [TTokens, GetTypedMemoValue<TTokens>];
|
|
8
|
-
//# sourceMappingURL=applyPropsToTokens.d.ts.map
|
|
2
|
+
export declare function applyPropsToTokens<TProps, TTokens>(props: TProps, tokens: TTokens, cache: GetTypedMemoValue<TTokens>, keys: (keyof TTokens)[]): [TTokens, GetTypedMemoValue<TTokens>];
|
|
3
|
+
//# sourceMappingURL=applyPropsToTokens.d.ts.map
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.applyPropsToTokens = applyPropsToTokens;
|
|
4
4
|
function applyPropsToTokens(props, tokens, cache, keys) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
for (const key of keys) {
|
|
6
|
+
const sourceValue = props[key];
|
|
7
|
+
const setValue = sourceValue === tokens[key] ? undefined : sourceValue;
|
|
8
|
+
[tokens, cache] = cache(() => (setValue === undefined ? tokens : { ...tokens, [key]: setValue }), [setValue]);
|
|
9
|
+
}
|
|
10
|
+
return [tokens, cache];
|
|
11
11
|
}
|
|
12
|
-
//# sourceMappingURL=applyPropsToTokens.js.map
|
|
12
|
+
//# sourceMappingURL=applyPropsToTokens.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export {};
|
|
2
|
-
//# sourceMappingURL=applyPropsToTokens.test.d.ts.map
|
|
2
|
+
//# sourceMappingURL=applyPropsToTokens.test.d.ts.map
|
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
3
|
-
const framework_base_1 = require(
|
|
4
|
-
const applyPropsToTokens_1 = require(
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const framework_base_1 = require("@fluentui-react-native/framework-base");
|
|
4
|
+
const applyPropsToTokens_1 = require("./applyPropsToTokens");
|
|
5
5
|
const tokenProps = ['dos', 'quatro', 'cinco'];
|
|
6
6
|
const themeTokens = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
uno: 'uno',
|
|
8
|
+
dos: 'dos',
|
|
9
|
+
tres: 3,
|
|
10
|
+
quatro: 4,
|
|
11
|
+
cinco: true,
|
|
12
12
|
};
|
|
13
13
|
const props1 = { dos: 'two', quatro: 'four', cinco: false, foo: 'foo', bar: 'bar' };
|
|
14
14
|
// const props2: Props = { dos: 'two' };
|
|
15
15
|
// const props3: Props = { foo: 'foo', bar: 'bar' };
|
|
16
16
|
describe('applyPropsToTokens tests', () => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
17
|
+
test('props get copied', () => {
|
|
18
|
+
const cache = (0, framework_base_1.getTypedMemoCache)();
|
|
19
|
+
const [tokens] = (0, applyPropsToTokens_1.applyPropsToTokens)(props1, themeTokens, cache, tokenProps);
|
|
20
|
+
expect(tokens).not.toBe(themeTokens);
|
|
21
|
+
for (const key of tokenProps) {
|
|
22
|
+
expect(tokens[key]).toEqual(props1[key]);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
test('no copied props does not change tokens', () => {
|
|
26
|
+
const cache = (0, framework_base_1.getTypedMemoCache)();
|
|
27
|
+
const [tokens] = (0, applyPropsToTokens_1.applyPropsToTokens)({}, themeTokens, cache, tokenProps);
|
|
28
|
+
expect(tokens).toBe(themeTokens);
|
|
29
|
+
});
|
|
30
30
|
});
|
|
31
|
-
//# sourceMappingURL=applyPropsToTokens.test.js.map
|
|
31
|
+
//# sourceMappingURL=applyPropsToTokens.test.js.map
|
|
@@ -12,10 +12,5 @@ export type HasLayer = (name: string) => boolean;
|
|
|
12
12
|
* @param subCache - cache scoped to the root object with no layers applied
|
|
13
13
|
* @param hasLayer - a function which returns whether a given layer should be applied
|
|
14
14
|
*/
|
|
15
|
-
export declare function applyTokenLayers<TTokens>(
|
|
16
|
-
|
|
17
|
-
states: string[],
|
|
18
|
-
subCache: GetTypedMemoValue<TTokens>,
|
|
19
|
-
hasLayer: HasLayer,
|
|
20
|
-
): [TTokens, GetTypedMemoValue<TTokens>];
|
|
21
|
-
//# sourceMappingURL=applyTokenLayers.d.ts.map
|
|
15
|
+
export declare function applyTokenLayers<TTokens>(tokens: TTokens, states: string[], subCache: GetTypedMemoValue<TTokens>, hasLayer: HasLayer): [TTokens, GetTypedMemoValue<TTokens>];
|
|
16
|
+
//# sourceMappingURL=applyTokenLayers.d.ts.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.applyTokenLayers = applyTokenLayers;
|
|
4
|
-
const framework_base_1 = require(
|
|
4
|
+
const framework_base_1 = require("@fluentui-react-native/framework-base");
|
|
5
5
|
/**
|
|
6
6
|
* Apply token layers, building them up applied layer by applied layer, using the cache to store intermediate
|
|
7
7
|
* values
|
|
@@ -12,20 +12,17 @@ const framework_base_1 = require('@fluentui-react-native/framework-base');
|
|
|
12
12
|
* @param hasLayer - a function which returns whether a given layer should be applied
|
|
13
13
|
*/
|
|
14
14
|
function applyTokenLayers(tokens, states, subCache, hasLayer) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}, final);
|
|
28
|
-
}
|
|
29
|
-
return [final.tokens, final.subCache];
|
|
15
|
+
let final = { tokens, subCache };
|
|
16
|
+
if (states && states.length > 0) {
|
|
17
|
+
// now walk the overrides that are set, merging in props, caching results, and getting a new sub cache
|
|
18
|
+
final = states
|
|
19
|
+
.filter((val) => hasLayer(val))
|
|
20
|
+
.reduce((previous, layerName) => {
|
|
21
|
+
const layer = previous.tokens[layerName];
|
|
22
|
+
const [tokens, subCache] = previous.subCache(() => (layer && typeof layer === 'object' ? (0, framework_base_1.immutableMerge)(previous.tokens, layer) : previous.tokens), [layer]);
|
|
23
|
+
return { tokens, subCache };
|
|
24
|
+
}, final);
|
|
25
|
+
}
|
|
26
|
+
return [final.tokens, final.subCache];
|
|
30
27
|
}
|
|
31
|
-
//# sourceMappingURL=applyTokenLayers.js.map
|
|
28
|
+
//# sourceMappingURL=applyTokenLayers.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export {};
|
|
2
|
-
//# sourceMappingURL=applyTokenLayers.test.d.ts.map
|
|
2
|
+
//# sourceMappingURL=applyTokenLayers.test.d.ts.map
|
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
3
|
-
const framework_base_1 = require(
|
|
4
|
-
const applyTokenLayers_1 = require(
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const framework_base_1 = require("@fluentui-react-native/framework-base");
|
|
4
|
+
const applyTokenLayers_1 = require("./applyTokenLayers");
|
|
5
5
|
const stateOrder = ['hover', 'press', 'disabled'];
|
|
6
6
|
const tokens1 = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
a: 'a',
|
|
8
|
+
b: 'b',
|
|
9
|
+
c: 'c',
|
|
10
|
+
hover: {
|
|
11
|
+
a: 'a-hover',
|
|
12
|
+
b: 'b-hover',
|
|
13
|
+
press: {
|
|
14
|
+
a: 'a-hover-press',
|
|
15
|
+
},
|
|
16
|
+
},
|
|
13
17
|
press: {
|
|
14
|
-
|
|
18
|
+
a: 'a-press',
|
|
19
|
+
b: 'b-press',
|
|
20
|
+
},
|
|
21
|
+
disabled: {
|
|
22
|
+
a: 'disabled',
|
|
23
|
+
b: 'disabled',
|
|
24
|
+
c: 'disabled',
|
|
15
25
|
},
|
|
16
|
-
},
|
|
17
|
-
press: {
|
|
18
|
-
a: 'a-press',
|
|
19
|
-
b: 'b-press',
|
|
20
|
-
},
|
|
21
|
-
disabled: {
|
|
22
|
-
a: 'disabled',
|
|
23
|
-
b: 'disabled',
|
|
24
|
-
c: 'disabled',
|
|
25
|
-
},
|
|
26
26
|
};
|
|
27
27
|
function stripLayers(tokens) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
const t = { ...tokens };
|
|
29
|
+
delete t.hover;
|
|
30
|
+
delete t.press;
|
|
31
|
+
delete t.disabled;
|
|
32
|
+
return t;
|
|
33
33
|
}
|
|
34
34
|
describe('applyLayers tests', () => {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
35
|
+
test('no layers returns tokens', () => {
|
|
36
|
+
const cache = (0, framework_base_1.getMemoCache)();
|
|
37
|
+
const tokens = (0, applyTokenLayers_1.applyTokenLayers)(tokens1, stateOrder, cache, () => false)[0];
|
|
38
|
+
expect(tokens).toBe(tokens1);
|
|
39
|
+
});
|
|
40
|
+
test('apply hover works', () => {
|
|
41
|
+
const cache = (0, framework_base_1.getMemoCache)();
|
|
42
|
+
const lookup = (layer) => layer === 'hover';
|
|
43
|
+
const result1 = (0, applyTokenLayers_1.applyTokenLayers)(tokens1, stateOrder, cache, lookup)[0];
|
|
44
|
+
expect((0, applyTokenLayers_1.applyTokenLayers)(tokens1, stateOrder, cache, lookup)[0]).toBe(result1);
|
|
45
|
+
expect(stripLayers(result1)).toEqual({ a: 'a-hover', b: 'b-hover', c: 'c' });
|
|
46
|
+
});
|
|
47
|
+
test('apply hover and press layer correctly', () => {
|
|
48
|
+
const cache = (0, framework_base_1.getMemoCache)();
|
|
49
|
+
const lookup = (layer) => layer === 'hover' || layer === 'press';
|
|
50
|
+
const result1 = (0, applyTokenLayers_1.applyTokenLayers)(tokens1, stateOrder, cache, lookup)[0];
|
|
51
|
+
expect((0, applyTokenLayers_1.applyTokenLayers)(tokens1, stateOrder, cache, lookup)[0]).toBe(result1);
|
|
52
|
+
expect(stripLayers(result1)).toEqual({ a: 'a-hover-press', b: 'b-press', c: 'c' });
|
|
53
|
+
});
|
|
54
54
|
});
|
|
55
|
-
//# sourceMappingURL=applyTokenLayers.test.js.map
|
|
55
|
+
//# sourceMappingURL=applyTokenLayers.test.js.map
|