@fluentui-react-native/use-styling 0.13.12 → 0.14.0
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.json +1 -1
- package/CHANGELOG.md +39 -2
- package/README.md +1 -1
- package/lib/buildProps.d.ts +14 -6
- package/lib/buildProps.js +28 -24
- package/lib/buildProps.js.map +1 -1
- package/lib/buildProps.test.d.ts +1 -1
- package/lib/buildProps.test.js +28 -28
- package/lib/buildProps.test.js.map +1 -1
- package/lib/buildUseStyling.d.ts +30 -27
- package/lib/buildUseStyling.js +30 -31
- package/lib/buildUseStyling.js.map +1 -1
- package/lib/buildUseStyling.test.d.ts +1 -1
- package/lib/buildUseStyling.test.js +67 -64
- package/lib/buildUseStyling.test.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/useStyling.samples.test.d.ts +1 -1
- package/lib/useStyling.samples.test.js +171 -148
- package/lib/useStyling.samples.test.js.map +1 -1
- package/lib-commonjs/buildProps.d.ts +14 -6
- package/lib-commonjs/buildProps.js +32 -29
- package/lib-commonjs/buildProps.js.map +1 -1
- package/lib-commonjs/buildProps.test.d.ts +1 -1
- package/lib-commonjs/buildProps.test.js +32 -32
- package/lib-commonjs/buildProps.test.js.map +1 -1
- package/lib-commonjs/buildUseStyling.d.ts +30 -27
- package/lib-commonjs/buildUseStyling.js +35 -37
- package/lib-commonjs/buildUseStyling.js.map +1 -1
- package/lib-commonjs/buildUseStyling.test.d.ts +1 -1
- package/lib-commonjs/buildUseStyling.test.js +72 -69
- package/lib-commonjs/buildUseStyling.test.js.map +1 -1
- package/lib-commonjs/index.d.ts +1 -1
- package/lib-commonjs/index.js +30 -10
- package/lib-commonjs/index.js.map +1 -1
- package/lib-commonjs/useStyling.samples.test.d.ts +1 -1
- package/lib-commonjs/useStyling.samples.test.js +227 -177
- package/lib-commonjs/useStyling.samples.test.js.map +1 -1
- package/package.json +46 -46
- package/src/buildProps.ts +2 -2
- package/src/useStyling.samples.test.tsx +23 -14
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from 'react/jsx-runtime';
|
|
2
|
+
import { act } from 'react';
|
|
2
3
|
import { Text, View } from 'react-native';
|
|
3
4
|
import * as renderer from 'react-test-renderer';
|
|
4
5
|
import { buildProps } from './buildProps';
|
|
@@ -7,14 +8,14 @@ import { buildUseStyling } from './buildUseStyling';
|
|
|
7
8
|
* The default/base theme just contains base values
|
|
8
9
|
*/
|
|
9
10
|
const baseTheme = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
globals: {
|
|
12
|
+
backgroundColor: 'white',
|
|
13
|
+
color: 'black',
|
|
14
|
+
borderColor: 'blue',
|
|
15
|
+
fontFamily: 'Arial',
|
|
16
|
+
fontSize: 12,
|
|
17
|
+
},
|
|
18
|
+
components: {},
|
|
18
19
|
};
|
|
19
20
|
const current = { theme: baseTheme };
|
|
20
21
|
/**
|
|
@@ -23,156 +24,178 @@ const current = { theme: baseTheme };
|
|
|
23
24
|
* whatever system they desire
|
|
24
25
|
*/
|
|
25
26
|
const themeHelper = {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
useTheme: () => current.theme,
|
|
28
|
+
getComponentInfo: (theme, name) => theme.components[name],
|
|
29
|
+
setActive: (theme) => {
|
|
30
|
+
current.theme = theme ? { ...baseTheme, ...theme } : baseTheme;
|
|
31
|
+
},
|
|
31
32
|
};
|
|
32
33
|
describe('useStyling samples', () => {
|
|
34
|
+
/**
|
|
35
|
+
* Sample #1 - Themeable text element
|
|
36
|
+
*
|
|
37
|
+
* This adds some default opinions for how a text element should be styled but only allows for customization
|
|
38
|
+
* via theming
|
|
39
|
+
*/
|
|
40
|
+
// now create the styling hook, first the options so they can be reused later
|
|
41
|
+
const sample1StylingOptions = {
|
|
33
42
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* This adds some default opinions for how a text element should be styled but only allows for customization
|
|
37
|
-
* via theming
|
|
43
|
+
* tell the styling hook how to build up the tokens
|
|
38
44
|
*/
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
tokens: [
|
|
46
|
+
/** first the default values should come from the global theme section */
|
|
47
|
+
(t) => ({
|
|
48
|
+
color: t.globals.color,
|
|
49
|
+
fontFamily: t.globals.fontFamily,
|
|
50
|
+
fontSize: t.globals.fontSize,
|
|
51
|
+
}),
|
|
52
|
+
/** next we should look for a component reference to overlay */
|
|
53
|
+
'Sample1',
|
|
54
|
+
],
|
|
55
|
+
/**
|
|
56
|
+
* Now provide the recipe for how to build props for the sub-components given the tokens
|
|
57
|
+
*/
|
|
58
|
+
slotProps: {
|
|
59
|
+
/** only one sub-component, a Text element called content, as a result this needs to build up TextProps */
|
|
60
|
+
content: buildProps(
|
|
41
61
|
/**
|
|
42
|
-
*
|
|
62
|
+
* first input for buildProps is a function which takes tokens and a theme and returns props
|
|
43
63
|
*/
|
|
44
|
-
tokens:
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
fontSize: t.globals.fontSize,
|
|
50
|
-
}),
|
|
51
|
-
/** next we should look for a component reference to overlay */
|
|
52
|
-
'Sample1',
|
|
53
|
-
],
|
|
64
|
+
(tokens /*, theme: Theme */) => {
|
|
65
|
+
return {
|
|
66
|
+
style: { ...tokens },
|
|
67
|
+
};
|
|
68
|
+
},
|
|
54
69
|
/**
|
|
55
|
-
*
|
|
70
|
+
* The second input are the tokens used as inputs for the above function. This is similar to the way the useEffect hook
|
|
71
|
+
* works in react.
|
|
56
72
|
*/
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
+
['color', 'fontFamily', 'fontSize'],
|
|
74
|
+
),
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
// now build the actual hook from the options and theme helper
|
|
78
|
+
const useStylingSample1 = buildUseStyling(sample1StylingOptions, themeHelper);
|
|
79
|
+
// now the sample 1 component becomes simple to build, just merge the styled props with the input props
|
|
80
|
+
const Sample1Text = (props) => {
|
|
81
|
+
const styledProps = useStylingSample1(props).content;
|
|
82
|
+
const merged = { ...props, ...styledProps };
|
|
83
|
+
return _jsx(Text, { ...merged, children: props.children });
|
|
84
|
+
};
|
|
85
|
+
/** first render the component with no updates */
|
|
86
|
+
it('Sample1Text rendering with no overrides', () => {
|
|
87
|
+
let component;
|
|
88
|
+
act(() => {
|
|
89
|
+
component = renderer.create(_jsx(Sample1Text, { children: 'Sample1a' }));
|
|
90
|
+
});
|
|
91
|
+
expect(component.toJSON()).toMatchSnapshot();
|
|
92
|
+
});
|
|
93
|
+
/** now re-theme the component via the components in the theme */
|
|
94
|
+
it('Sample1Text rendering with some custom settings in the theme', () => {
|
|
95
|
+
themeHelper.setActive({
|
|
96
|
+
components: {
|
|
97
|
+
Sample1: {
|
|
98
|
+
color: 'pink',
|
|
99
|
+
fontSize: 24,
|
|
73
100
|
},
|
|
74
|
-
|
|
75
|
-
// now build the actual hook from the options and theme helper
|
|
76
|
-
const useStylingSample1 = buildUseStyling(sample1StylingOptions, themeHelper);
|
|
77
|
-
// now the sample 1 component becomes simple to build, just merge the styled props with the input props
|
|
78
|
-
const Sample1Text = (props) => {
|
|
79
|
-
const styledProps = useStylingSample1(props).content;
|
|
80
|
-
const merged = { ...props, ...styledProps };
|
|
81
|
-
return React.createElement(Text, { ...merged }, props.children);
|
|
82
|
-
};
|
|
83
|
-
/** first render the component with no updates */
|
|
84
|
-
it('Sample1Text rendering with no overrides', () => {
|
|
85
|
-
const tree = renderer.create(React.createElement(Sample1Text, null, "Sample1a")).toJSON();
|
|
86
|
-
expect(tree).toMatchSnapshot();
|
|
101
|
+
},
|
|
87
102
|
});
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
components: {
|
|
92
|
-
Sample1: {
|
|
93
|
-
color: 'pink',
|
|
94
|
-
fontSize: 24,
|
|
95
|
-
},
|
|
96
|
-
},
|
|
97
|
-
});
|
|
98
|
-
const tree = renderer.create(React.createElement(Sample1Text, null, "Sample1b")).toJSON();
|
|
99
|
-
expect(tree).toMatchSnapshot();
|
|
103
|
+
let component;
|
|
104
|
+
act(() => {
|
|
105
|
+
component = renderer.create(_jsx(Sample1Text, { children: 'Sample1b' }));
|
|
100
106
|
});
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
107
|
+
expect(component.toJSON()).toMatchSnapshot();
|
|
108
|
+
});
|
|
109
|
+
/**
|
|
110
|
+
* Build the styling hook for sample2. Because this isn't being recombined this is being specified inline rather
|
|
111
|
+
* than using a separate options object. Both are fine.
|
|
112
|
+
*/
|
|
113
|
+
const useStylingSample2 = buildUseStyling(
|
|
114
|
+
{
|
|
115
|
+
/**
|
|
116
|
+
* This just starts with the baseline styling from sample1, in particular we are using the recipes of how to turn
|
|
117
|
+
* token values into the props for the internal sub-components. While this example is not super complex, for real-world
|
|
118
|
+
* components, re-using these can be extremely valuable.
|
|
119
|
+
*
|
|
120
|
+
* With that in mind, this copies over the recipes for how to turn tokens into props, the customizations that
|
|
121
|
+
* will be made are about how to ensure the tokens are set up correctly.
|
|
122
|
+
*/
|
|
123
|
+
...sample1StylingOptions,
|
|
124
|
+
/**
|
|
125
|
+
* In sample1 tokens are set to defaults from the global theme section, then patched with any values looked up with
|
|
126
|
+
* the string 'Sample1'
|
|
127
|
+
*
|
|
128
|
+
* We want to maintain the logic of setting up the globals, but add an additional lookup for 'Sample2'. This might correspond
|
|
129
|
+
* to saying that if we were making a variant of a 'Text' component called 'HeaderText', we might want to look up
|
|
130
|
+
* customizations from 'Text' first, then override those customizations with those from 'HeaderText'
|
|
131
|
+
*
|
|
132
|
+
* If we didn't want to add the extra 'Sample2' lookup this line would be omitted. If we didn't want to look up 'Sample1' first
|
|
133
|
+
* that could be filtered out of the array that is being copied
|
|
134
|
+
*/
|
|
135
|
+
tokens: [...sample1StylingOptions.tokens, 'Sample2'],
|
|
136
|
+
/**
|
|
137
|
+
* This is the final bit of magic. The tokens will already have values set from the global theme, they will then be patched with
|
|
138
|
+
* any customizations set into Sample1 and/or Sample2.
|
|
139
|
+
*
|
|
140
|
+
* If this value was omitted then the tokens would be passed to the slotProps recipies as is. To have those values patched from
|
|
141
|
+
* the component props we add a list of the props which need to be passed into tokens. If all props should be spread into the
|
|
142
|
+
* tokens then this value can be set to 'all'. If none should be passed it can be omitted or set to 'none'
|
|
143
|
+
*/
|
|
144
|
+
tokensThatAreAlsoProps: ['color'],
|
|
145
|
+
},
|
|
146
|
+
themeHelper,
|
|
147
|
+
);
|
|
148
|
+
// the Sample2Text component is built the same way as sample1, just using the new hook that has been created
|
|
149
|
+
const Sample2Text = (props) => {
|
|
150
|
+
const styledProps = useStylingSample2(props).content;
|
|
151
|
+
const merged = { ...props, ...styledProps };
|
|
152
|
+
// delete the color key to not pass it through to the text props, could be done via destructuring, filtering, or any number of ways
|
|
153
|
+
delete merged.color;
|
|
154
|
+
// render the text
|
|
155
|
+
return _jsx(Text, { ...merged, children: props.children });
|
|
156
|
+
};
|
|
157
|
+
/** rendering the Sample2 component with the base theme */
|
|
158
|
+
it('Sample2Text rendering with defaults and a color override', () => {
|
|
159
|
+
themeHelper.setActive();
|
|
160
|
+
let component;
|
|
161
|
+
act(() => {
|
|
162
|
+
component = renderer.create(
|
|
163
|
+
_jsxs(View, {
|
|
164
|
+
children: [
|
|
165
|
+
_jsx(Sample2Text, { children: 'Sample2 with defaults' }),
|
|
166
|
+
_jsx(Sample2Text, { color: 'green', children: 'Sample2 with color override via prop' }),
|
|
167
|
+
],
|
|
168
|
+
}),
|
|
169
|
+
);
|
|
170
|
+
});
|
|
171
|
+
expect(component.toJSON()).toMatchSnapshot();
|
|
172
|
+
});
|
|
173
|
+
/** now re-theme the component via the components in the theme */
|
|
174
|
+
it('Sample2Text rendering with some custom settings in the theme', () => {
|
|
175
|
+
themeHelper.setActive({
|
|
176
|
+
components: {
|
|
177
|
+
Sample1: {
|
|
178
|
+
color: 'pink',
|
|
179
|
+
fontSize: 24,
|
|
180
|
+
},
|
|
181
|
+
Sample2: {
|
|
182
|
+
fontSize: 18,
|
|
183
|
+
fontFamily: 'Helvetica',
|
|
184
|
+
},
|
|
185
|
+
},
|
|
155
186
|
});
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
fontFamily: 'Helvetica',
|
|
167
|
-
},
|
|
168
|
-
},
|
|
169
|
-
});
|
|
170
|
-
const tree = renderer
|
|
171
|
-
.create(React.createElement(View, null,
|
|
172
|
-
React.createElement(Sample2Text, null, "Sample2 with theme overrides set"),
|
|
173
|
-
React.createElement(Sample2Text, { color: "purple" }, "Sample2 with theme and color prop override")))
|
|
174
|
-
.toJSON();
|
|
175
|
-
expect(tree).toMatchSnapshot();
|
|
187
|
+
let component;
|
|
188
|
+
act(() => {
|
|
189
|
+
component = renderer.create(
|
|
190
|
+
_jsxs(View, {
|
|
191
|
+
children: [
|
|
192
|
+
_jsx(Sample2Text, { children: 'Sample2 with theme overrides set' }),
|
|
193
|
+
_jsx(Sample2Text, { color: 'purple', children: 'Sample2 with theme and color prop override' }),
|
|
194
|
+
],
|
|
195
|
+
}),
|
|
196
|
+
);
|
|
176
197
|
});
|
|
198
|
+
expect(component.toJSON()).toMatchSnapshot();
|
|
199
|
+
});
|
|
177
200
|
});
|
|
178
|
-
//# sourceMappingURL=useStyling.samples.test.js.map
|
|
201
|
+
//# sourceMappingURL=useStyling.samples.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStyling.samples.test.js","sourceRoot":"","sources":["../src/useStyling.samples.test.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useStyling.samples.test.js","sourceRoot":"","sources":["../src/useStyling.samples.test.tsx"],"names":[],"mappings":";AACA,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAE5B,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAE1C,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAC;AAEhD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAuBpD;;GAEG;AACH,MAAM,SAAS,GAAU;IACvB,OAAO,EAAE;QACP,eAAe,EAAE,OAAO;QACxB,KAAK,EAAE,OAAO;QACd,WAAW,EAAE,MAAM;QACnB,UAAU,EAAE,OAAO;QACnB,QAAQ,EAAE,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F;SACF,CAAC,CAAC;QACH,IAAI,SAAqC,CAAC;QAC1C,GAAG,CAAC,GAAG,EAAE,CAAC;YACR,SAAS,GAAG,QAAQ,CAAC,MAAM,CACzB,MAAC,IAAI;oBACH,KAAC,WAAW,mDAA+C,EAC3D,KAAC,WAAW,IAAC,KAAK,EAAC,QAAQ,2DAAyD;oBAC/E,CACR,CAAC;QAAA,CACH,CAAC,CAAC;QACH,MAAM,CAAC,SAAU,CAAC,MAAM,EAAE,CAAC,CAAC,eAAe,EAAE,CAAC;IAAA,CAC/C,CAAC,CAAC;AAAA,CACJ,CAAC,CAAC"}
|
|
@@ -21,18 +21,20 @@ export type BuildPropsBase<TProps, TTokens, TTheme> = (tokens: TTokens, theme: T
|
|
|
21
21
|
* A refine function allows style functions to be updated based on tokens that are also props. Only those tokens that are also
|
|
22
22
|
* props need to be considered as a key for caching
|
|
23
23
|
*/
|
|
24
|
-
export type RefineFunctionBase<TProps, TTokens, TTheme> = (
|
|
24
|
+
export type RefineFunctionBase<TProps, TTokens, TTheme> = (
|
|
25
|
+
mask?: TokensThatAreAlsoProps<TTokens>,
|
|
26
|
+
) => BuildPropsBase<TProps, TTokens, TTheme>;
|
|
25
27
|
/**
|
|
26
28
|
* Signature for a style function which can be optionally refined by the styling hook if prop masks are provided
|
|
27
29
|
*/
|
|
28
30
|
export type RefinableBuildPropsBase<TProps, TTokens, TTheme> = BuildPropsBase<TProps, TTokens, TTheme> & {
|
|
29
|
-
|
|
31
|
+
refine?: RefineFunctionBase<TProps, TTokens, TTheme>;
|
|
30
32
|
};
|
|
31
33
|
/**
|
|
32
34
|
* Style functions can be plain functions, refinable functions, or just raw props
|
|
33
35
|
*/
|
|
34
36
|
export type BuildSlotProps<TSlotProps, TTokens, TTheme> = {
|
|
35
|
-
|
|
37
|
+
[K in keyof TSlotProps]?: RefinableBuildPropsBase<TSlotProps[K], TTokens, TTheme> | TSlotProps[K];
|
|
36
38
|
};
|
|
37
39
|
/**
|
|
38
40
|
* Standard wrapper for a function that provides props for a component based on tokens and theme.
|
|
@@ -40,12 +42,18 @@ export type BuildSlotProps<TSlotProps, TTokens, TTheme> = {
|
|
|
40
42
|
* @param fn - function which does the work of producing props for the tokens and theme provided
|
|
41
43
|
* @param keys - which token properties are used by this style, this determines the keys to use for caching
|
|
42
44
|
*/
|
|
43
|
-
export declare function buildProps<TProps, TTokens, TTheme>(
|
|
45
|
+
export declare function buildProps<TProps, TTokens, TTheme>(
|
|
46
|
+
fn: (tokens: TTokens, theme: TTheme) => TProps,
|
|
47
|
+
keys?: (keyof TTokens)[],
|
|
48
|
+
): RefinableBuildPropsBase<TProps, TTokens, TTheme>;
|
|
44
49
|
/**
|
|
45
50
|
* Utility function to check the type and refinement capabilities of a styleFunction and refine it if appropriate
|
|
46
51
|
*
|
|
47
52
|
* @param fn - function or props to potentially refine
|
|
48
53
|
* @param mask - prop mask to use for refinement
|
|
49
54
|
*/
|
|
50
|
-
export declare function refinePropsFunctions<TSlotProps, TTokens, TTheme>(
|
|
51
|
-
|
|
55
|
+
export declare function refinePropsFunctions<TSlotProps, TTokens, TTheme>(
|
|
56
|
+
styles: BuildSlotProps<TSlotProps, TTokens, TTheme>,
|
|
57
|
+
mask: TokensThatAreAlsoProps<TTokens>,
|
|
58
|
+
): BuildSlotProps<TSlotProps, TTokens, TTheme>;
|
|
59
|
+
//# sourceMappingURL=buildProps.d.ts.map
|
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
3
|
-
exports.
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.buildProps = buildProps;
|
|
4
|
+
exports.refinePropsFunctions = refinePropsFunctions;
|
|
4
5
|
function cacheStyleClosure(fn, keys) {
|
|
5
|
-
|
|
6
|
+
return (tokens, theme, cache) =>
|
|
7
|
+
cache(
|
|
8
|
+
() => fn(tokens, theme),
|
|
9
|
+
(keys || []).map((key) => tokens[key]),
|
|
10
|
+
)[0];
|
|
6
11
|
}
|
|
7
12
|
function refineKeys(keys, mask) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
return typeof mask === 'object' && Array.isArray(mask)
|
|
14
|
+
? keys.filter((key) => mask.findIndex((val) => val === key) !== -1)
|
|
15
|
+
: mask
|
|
16
|
+
? keys
|
|
17
|
+
: [];
|
|
13
18
|
}
|
|
14
19
|
/**
|
|
15
20
|
* Standard wrapper for a function that provides props for a component based on tokens and theme.
|
|
@@ -18,19 +23,18 @@ function refineKeys(keys, mask) {
|
|
|
18
23
|
* @param keys - which token properties are used by this style, this determines the keys to use for caching
|
|
19
24
|
*/
|
|
20
25
|
function buildProps(fn, keys) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
// wrap the provided function in the standard caching layer, basing it upon the provided keys
|
|
27
|
+
const result = cacheStyleClosure(fn, keys);
|
|
28
|
+
// if results are being cached on keys, provide the ability to refine the function if a prop mask is specified
|
|
29
|
+
result.refine =
|
|
30
|
+
keys && keys.length > 0
|
|
31
|
+
? (mask) => {
|
|
32
|
+
return cacheStyleClosure(fn, refineKeys(keys, mask));
|
|
33
|
+
}
|
|
34
|
+
: undefined;
|
|
35
|
+
// return the style function decorated with the refine function
|
|
36
|
+
return result;
|
|
32
37
|
}
|
|
33
|
-
exports.buildProps = buildProps;
|
|
34
38
|
/**
|
|
35
39
|
* Utility function to check the type and refinement capabilities of a styleFunction and refine it if appropriate
|
|
36
40
|
*
|
|
@@ -38,12 +42,11 @@ exports.buildProps = buildProps;
|
|
|
38
42
|
* @param mask - prop mask to use for refinement
|
|
39
43
|
*/
|
|
40
44
|
function refinePropsFunctions(styles, mask) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
const result = {};
|
|
46
|
+
for (const key of Object.keys(styles)) {
|
|
47
|
+
const refine = typeof styles[key] === 'function' && styles[key].refine;
|
|
48
|
+
result[key] = refine ? refine(mask) : styles[key];
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
47
51
|
}
|
|
48
|
-
|
|
49
|
-
//# sourceMappingURL=buildProps.js.map
|
|
52
|
+
//# sourceMappingURL=buildProps.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildProps.js","sourceRoot":"","sources":["../src/buildProps.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"buildProps.js","sourceRoot":"","sources":["../src/buildProps.ts"],"names":[],"mappings":";;;;AA4CA,SAAS,iBAAiB,CACxB,EAA8C,EAC9C,IAAwB,EAC0B;IAClD,OAAO,CAAC,MAAe,EAAE,KAAa,EAAE,KAAmB,EAAE,EAAE,CAC7D,KAAK,CACH,GAAG,EAAE,CAAC,EAAE,CAAC,MAAM,EAAE,KAAK,CAAC,EACvB,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CACvC,CAAC,CAAC,CAAC,CAAC;AAAA,CACR;AAED,SAAS,UAAU,CAAU,IAAuB,EAAE,IAAsC,EAAqB;IAC/G,OAAO,OAAO,IAAI,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;QACpD,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,KAAK,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC;QACnE,CAAC,CAAC,IAAI;YACN,CAAC,CAAC,IAAI;YACN,CAAC,CAAC,EAAE,CAAC;AAAA,CACR;AAED;;;;;GAKG;AACH,oBACE,EAA8C,EAC9C,IAAwB,EAC0B;IAClD,6FAA6F;IAC7F,MAAM,MAAM,GAAG,iBAAiB,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;IAE3C,8GAA8G;IAC9G,MAAM,CAAC,MAAM;QACX,IAAI,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC;YACrB,CAAC,CAAC,CAAC,IAAsC,EAAE,EAAE,CAAC;gBAC1C,OAAO,iBAAiB,CAAC,EAAE,EAAE,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;YAAA,CACtD;YACH,CAAC,CAAC,SAAS,CAAC;IAEhB,+DAA+D;IAC/D,OAAO,MAAM,CAAC;AAAA,CACf;AAED;;;;;GAKG;AACH,8BACE,MAAmD,EACnD,IAAqC,EACQ;IAC7C,MAAM,MAAM,GAAG,EAAE,CAAC;IAClB,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;QACtC,MAAM,MAAM,GACV,OAAO,MAAM,CAAC,GAAG,CAAC,KAAK,UAAU,IAAK,MAAM,CAAC,GAAG,CAA4E,CAAC,MAAM,CAAC;QACtI,MAAM,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACpD,CAAC;IACD,OAAO,MAAM,CAAC;AAAA,CACf"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export {};
|
|
2
|
-
//# sourceMappingURL=buildProps.test.d.ts.map
|
|
2
|
+
//# sourceMappingURL=buildProps.test.d.ts.map
|
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
3
|
-
const framework_base_1 = require(
|
|
4
|
-
const buildProps_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 buildProps_1 = require('./buildProps');
|
|
5
5
|
const theme = { foo: 'foo', bar: 'bar' };
|
|
6
6
|
let instanceCount = 0;
|
|
7
7
|
function munge(tokens, theme) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
return {
|
|
9
|
+
...theme,
|
|
10
|
+
...tokens,
|
|
11
|
+
instance: instanceCount++,
|
|
12
|
+
};
|
|
13
13
|
}
|
|
14
14
|
describe('props function tests', () => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
15
|
+
test('basic build props function caches as expected', () => {
|
|
16
|
+
const cache = (0, framework_base_1.getMemoCache)();
|
|
17
|
+
const styleFn = (0, buildProps_1.buildProps)(munge, ['a', 'b']);
|
|
18
|
+
const p1 = styleFn({ a: 'a', b: 'b', c: 'c' }, theme, cache);
|
|
19
|
+
expect(styleFn({ a: 'a', b: 'b', c: 'foo' }, theme, cache)).toBe(p1);
|
|
20
|
+
const p2 = styleFn({ a: 'b', b: 'b' }, theme, cache);
|
|
21
|
+
expect(p2).not.toBe(p1);
|
|
22
|
+
expect(styleFn({ a: 'b', b: 'b', c: 'bar' }, theme, cache)).toBe(p2);
|
|
23
|
+
});
|
|
24
|
+
test('build props function refinement works with explicit keys', () => {
|
|
25
|
+
const cache = (0, framework_base_1.getMemoCache)();
|
|
26
|
+
const styleFn = (0, buildProps_1.buildProps)(munge, ['a', 'b', 'c', 'd']);
|
|
27
|
+
const refinedFn = styleFn.refine(['a', 'b']);
|
|
28
|
+
const t1 = { a: 'a', b: 'b', c: 'c', d: 'd' };
|
|
29
|
+
const t2 = { a: 'a', b: 'b', c: 'foo', d: 'bar' };
|
|
30
|
+
const p1 = styleFn(t1, theme, cache);
|
|
31
|
+
const p2 = styleFn(t2, theme, cache);
|
|
32
|
+
expect(p2).not.toBe(p1);
|
|
33
|
+
const rp1 = refinedFn(t1, theme, cache);
|
|
34
|
+
const rp2 = refinedFn(t2, theme, cache);
|
|
35
|
+
expect(rp2).toBe(rp1);
|
|
36
|
+
});
|
|
37
37
|
});
|
|
38
|
-
//# sourceMappingURL=buildProps.test.js.map
|
|
38
|
+
//# sourceMappingURL=buildProps.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildProps.test.js","sourceRoot":"","sources":["../src/buildProps.test.ts"],"names":[],"mappings":";;AAAA,0EAAqE;AAErE,6CAA0C;AAM1C,MAAM,KAAK,GAAW,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;AAEjD,IAAI,aAAa,GAAG,CAAC,CAAC;AAEtB,SAAS,KAAK,CAAC,MAAe,EAAE,KAAa;
|
|
1
|
+
{"version":3,"file":"buildProps.test.js","sourceRoot":"","sources":["../src/buildProps.test.ts"],"names":[],"mappings":";;AAAA,0EAAqE;AAErE,6CAA0C;AAM1C,MAAM,KAAK,GAAW,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;AAEjD,IAAI,aAAa,GAAG,CAAC,CAAC;AAEtB,SAAS,KAAK,CAAC,MAAe,EAAE,KAAa,EAAU;IACrD,OAAO;QACL,GAAG,KAAK;QACR,GAAG,MAAM;QACT,QAAQ,EAAE,aAAa,EAAE;KAC1B,CAAC;AAAA,CACH;AAED,QAAQ,CAAC,sBAAsB,EAAE,GAAG,EAAE,CAAC;IACrC,IAAI,CAAC,+CAA+C,EAAE,GAAG,EAAE,CAAC;QAC1D,MAAM,KAAK,GAAG,IAAA,6BAAY,GAAE,CAAC;QAC7B,MAAM,OAAO,GAAG,IA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