@fluentui-react-native/use-styling 0.14.0 → 0.14.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.
@@ -1,73 +1,56 @@
1
- 'use strict';
2
- var __createBinding =
3
- (this && this.__createBinding) ||
4
- (Object.create
5
- ? function (o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- var desc = Object.getOwnPropertyDescriptor(m, k);
8
- if (!desc || ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
- desc = {
10
- enumerable: true,
11
- get: function () {
12
- return m[k];
13
- },
14
- };
15
- }
16
- Object.defineProperty(o, k2, desc);
17
- }
18
- : function (o, m, k, k2) {
19
- if (k2 === undefined) k2 = k;
20
- o[k2] = m[k];
21
- });
22
- var __setModuleDefault =
23
- (this && this.__setModuleDefault) ||
24
- (Object.create
25
- ? function (o, v) {
26
- Object.defineProperty(o, 'default', { enumerable: true, value: v });
27
- }
28
- : function (o, v) {
29
- o['default'] = v;
30
- });
31
- var __importStar =
32
- (this && this.__importStar) ||
33
- (function () {
34
- var ownKeys = function (o) {
35
- ownKeys =
36
- Object.getOwnPropertyNames ||
37
- function (o) {
38
- var ar = [];
39
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
40
- return ar;
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
41
24
  };
42
- return ownKeys(o);
25
+ return ownKeys(o);
43
26
  };
44
27
  return function (mod) {
45
- if (mod && mod.__esModule) return mod;
46
- var result = {};
47
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== 'default') __createBinding(result, mod, k[i]);
48
- __setModuleDefault(result, mod);
49
- return result;
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
50
33
  };
51
- })();
52
- Object.defineProperty(exports, '__esModule', { value: true });
53
- const jsx_runtime_1 = require('react/jsx-runtime');
54
- const react_1 = require('react');
55
- const react_native_1 = require('react-native');
56
- const renderer = __importStar(require('react-test-renderer'));
57
- const buildProps_1 = require('./buildProps');
58
- const buildUseStyling_1 = require('./buildUseStyling');
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ const jsx_runtime_1 = require("react/jsx-runtime");
37
+ const react_1 = require("react");
38
+ const react_native_1 = require("react-native");
39
+ const renderer = __importStar(require("react-test-renderer"));
40
+ const buildProps_1 = require("./buildProps");
41
+ const buildUseStyling_1 = require("./buildUseStyling");
59
42
  /**
60
43
  * The default/base theme just contains base values
61
44
  */
62
45
  const baseTheme = {
63
- globals: {
64
- backgroundColor: 'white',
65
- color: 'black',
66
- borderColor: 'blue',
67
- fontFamily: 'Arial',
68
- fontSize: 12,
69
- },
70
- components: {},
46
+ globals: {
47
+ backgroundColor: 'white',
48
+ color: 'black',
49
+ borderColor: 'blue',
50
+ fontFamily: 'Arial',
51
+ fontSize: 12,
52
+ },
53
+ components: {},
71
54
  };
72
55
  const current = { theme: baseTheme };
73
56
  /**
@@ -76,178 +59,164 @@ const current = { theme: baseTheme };
76
59
  * whatever system they desire
77
60
  */
78
61
  const themeHelper = {
79
- useTheme: () => current.theme,
80
- getComponentInfo: (theme, name) => theme.components[name],
81
- setActive: (theme) => {
82
- current.theme = theme ? { ...baseTheme, ...theme } : baseTheme;
83
- },
62
+ useTheme: () => current.theme,
63
+ getComponentInfo: (theme, name) => theme.components[name],
64
+ setActive: (theme) => {
65
+ current.theme = theme ? { ...baseTheme, ...theme } : baseTheme;
66
+ },
84
67
  };
85
68
  describe('useStyling samples', () => {
86
- /**
87
- * Sample #1 - Themeable text element
88
- *
89
- * This adds some default opinions for how a text element should be styled but only allows for customization
90
- * via theming
91
- */
92
- // now create the styling hook, first the options so they can be reused later
93
- const sample1StylingOptions = {
94
69
  /**
95
- * tell the styling hook how to build up the tokens
70
+ * Sample #1 - Themeable text element
71
+ *
72
+ * This adds some default opinions for how a text element should be styled but only allows for customization
73
+ * via theming
96
74
  */
97
- tokens: [
98
- /** first the default values should come from the global theme section */
99
- (t) => ({
100
- color: t.globals.color,
101
- fontFamily: t.globals.fontFamily,
102
- fontSize: t.globals.fontSize,
103
- }),
104
- /** next we should look for a component reference to overlay */
105
- 'Sample1',
106
- ],
107
- /**
108
- * Now provide the recipe for how to build props for the sub-components given the tokens
109
- */
110
- slotProps: {
111
- /** only one sub-component, a Text element called content, as a result this needs to build up TextProps */
112
- content: (0, buildProps_1.buildProps)(
75
+ // now create the styling hook, first the options so they can be reused later
76
+ const sample1StylingOptions = {
113
77
  /**
114
- * first input for buildProps is a function which takes tokens and a theme and returns props
78
+ * tell the styling hook how to build up the tokens
115
79
  */
116
- (tokens /*, theme: Theme */) => {
117
- return {
118
- style: { ...tokens },
119
- };
120
- },
80
+ tokens: [
81
+ /** first the default values should come from the global theme section */
82
+ (t) => ({
83
+ color: t.globals.color,
84
+ fontFamily: t.globals.fontFamily,
85
+ fontSize: t.globals.fontSize,
86
+ }),
87
+ /** next we should look for a component reference to overlay */
88
+ 'Sample1',
89
+ ],
121
90
  /**
122
- * The second input are the tokens used as inputs for the above function. This is similar to the way the useEffect hook
123
- * works in react.
91
+ * Now provide the recipe for how to build props for the sub-components given the tokens
124
92
  */
125
- ['color', 'fontFamily', 'fontSize'],
126
- ),
127
- },
128
- };
129
- // now build the actual hook from the options and theme helper
130
- const useStylingSample1 = (0, buildUseStyling_1.buildUseStyling)(sample1StylingOptions, themeHelper);
131
- // now the sample 1 component becomes simple to build, just merge the styled props with the input props
132
- const Sample1Text = (props) => {
133
- const styledProps = useStylingSample1(props).content;
134
- const merged = { ...props, ...styledProps };
135
- return jsx_runtime_1.jsx(react_native_1.Text, { ...merged, children: props.children });
136
- };
137
- /** first render the component with no updates */
138
- it('Sample1Text rendering with no overrides', () => {
139
- let component;
140
- (0, react_1.act)(() => {
141
- component = renderer.create(jsx_runtime_1.jsx(Sample1Text, { children: 'Sample1a' }));
142
- });
143
- expect(component.toJSON()).toMatchSnapshot();
144
- });
145
- /** now re-theme the component via the components in the theme */
146
- it('Sample1Text rendering with some custom settings in the theme', () => {
147
- themeHelper.setActive({
148
- components: {
149
- Sample1: {
150
- color: 'pink',
151
- fontSize: 24,
93
+ slotProps: {
94
+ /** only one sub-component, a Text element called content, as a result this needs to build up TextProps */
95
+ content: (0, buildProps_1.buildProps)(
96
+ /**
97
+ * first input for buildProps is a function which takes tokens and a theme and returns props
98
+ */
99
+ (tokens /*, theme: Theme */) => {
100
+ return {
101
+ style: { ...tokens },
102
+ };
103
+ },
104
+ /**
105
+ * The second input are the tokens used as inputs for the above function. This is similar to the way the useEffect hook
106
+ * works in react.
107
+ */
108
+ ['color', 'fontFamily', 'fontSize']),
152
109
  },
153
- },
154
- });
155
- let component;
156
- (0, react_1.act)(() => {
157
- component = renderer.create(jsx_runtime_1.jsx(Sample1Text, { children: 'Sample1b' }));
110
+ };
111
+ // now build the actual hook from the options and theme helper
112
+ const useStylingSample1 = (0, buildUseStyling_1.buildUseStyling)(sample1StylingOptions, themeHelper);
113
+ // now the sample 1 component becomes simple to build, just merge the styled props with the input props
114
+ const Sample1Text = (props) => {
115
+ const styledProps = useStylingSample1(props).content;
116
+ const merged = { ...props, ...styledProps };
117
+ return jsx_runtime_1.jsx(react_native_1.Text, { ...merged, children: props.children });
118
+ };
119
+ /** first render the component with no updates */
120
+ it('Sample1Text rendering with no overrides', () => {
121
+ let component;
122
+ (0, react_1.act)(() => {
123
+ component = renderer.create(jsx_runtime_1.jsx(Sample1Text, { children: "Sample1a" }));
124
+ });
125
+ expect(component.toJSON()).toMatchSnapshot();
158
126
  });
159
- expect(component.toJSON()).toMatchSnapshot();
160
- });
161
- /**
162
- * Build the styling hook for sample2. Because this isn't being recombined this is being specified inline rather
163
- * than using a separate options object. Both are fine.
164
- */
165
- const useStylingSample2 = (0, buildUseStyling_1.buildUseStyling)(
166
- {
167
- /**
168
- * This just starts with the baseline styling from sample1, in particular we are using the recipes of how to turn
169
- * token values into the props for the internal sub-components. While this example is not super complex, for real-world
170
- * components, re-using these can be extremely valuable.
171
- *
172
- * With that in mind, this copies over the recipes for how to turn tokens into props, the customizations that
173
- * will be made are about how to ensure the tokens are set up correctly.
174
- */
175
- ...sample1StylingOptions,
176
- /**
177
- * In sample1 tokens are set to defaults from the global theme section, then patched with any values looked up with
178
- * the string 'Sample1'
179
- *
180
- * We want to maintain the logic of setting up the globals, but add an additional lookup for 'Sample2'. This might correspond
181
- * to saying that if we were making a variant of a 'Text' component called 'HeaderText', we might want to look up
182
- * customizations from 'Text' first, then override those customizations with those from 'HeaderText'
183
- *
184
- * 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
185
- * that could be filtered out of the array that is being copied
186
- */
187
- tokens: [...sample1StylingOptions.tokens, 'Sample2'],
188
- /**
189
- * This is the final bit of magic. The tokens will already have values set from the global theme, they will then be patched with
190
- * any customizations set into Sample1 and/or Sample2.
191
- *
192
- * If this value was omitted then the tokens would be passed to the slotProps recipies as is. To have those values patched from
193
- * 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
194
- * tokens then this value can be set to 'all'. If none should be passed it can be omitted or set to 'none'
195
- */
196
- tokensThatAreAlsoProps: ['color'],
197
- },
198
- themeHelper,
199
- );
200
- // the Sample2Text component is built the same way as sample1, just using the new hook that has been created
201
- const Sample2Text = (props) => {
202
- const styledProps = useStylingSample2(props).content;
203
- const merged = { ...props, ...styledProps };
204
- // delete the color key to not pass it through to the text props, could be done via destructuring, filtering, or any number of ways
205
- delete merged.color;
206
- // render the text
207
- return jsx_runtime_1.jsx(react_native_1.Text, { ...merged, children: props.children });
208
- };
209
- /** rendering the Sample2 component with the base theme */
210
- it('Sample2Text rendering with defaults and a color override', () => {
211
- themeHelper.setActive();
212
- let component;
213
- (0, react_1.act)(() => {
214
- component = renderer.create(
215
- jsx_runtime_1.jsxs(react_native_1.View, {
216
- children: [
217
- jsx_runtime_1.jsx(Sample2Text, { children: 'Sample2 with defaults' }),
218
- jsx_runtime_1.jsx(Sample2Text, { color: 'green', children: 'Sample2 with color override via prop' }),
219
- ],
220
- }),
221
- );
127
+ /** now re-theme the component via the components in the theme */
128
+ it('Sample1Text rendering with some custom settings in the theme', () => {
129
+ themeHelper.setActive({
130
+ components: {
131
+ Sample1: {
132
+ color: 'pink',
133
+ fontSize: 24,
134
+ },
135
+ },
136
+ });
137
+ let component;
138
+ (0, react_1.act)(() => {
139
+ component = renderer.create(jsx_runtime_1.jsx(Sample1Text, { children: "Sample1b" }));
140
+ });
141
+ expect(component.toJSON()).toMatchSnapshot();
222
142
  });
223
- expect(component.toJSON()).toMatchSnapshot();
224
- });
225
- /** now re-theme the component via the components in the theme */
226
- it('Sample2Text rendering with some custom settings in the theme', () => {
227
- themeHelper.setActive({
228
- components: {
229
- Sample1: {
230
- color: 'pink',
231
- fontSize: 24,
232
- },
233
- Sample2: {
234
- fontSize: 18,
235
- fontFamily: 'Helvetica',
236
- },
237
- },
143
+ /**
144
+ * Build the styling hook for sample2. Because this isn't being recombined this is being specified inline rather
145
+ * than using a separate options object. Both are fine.
146
+ */
147
+ const useStylingSample2 = (0, buildUseStyling_1.buildUseStyling)({
148
+ /**
149
+ * This just starts with the baseline styling from sample1, in particular we are using the recipes of how to turn
150
+ * token values into the props for the internal sub-components. While this example is not super complex, for real-world
151
+ * components, re-using these can be extremely valuable.
152
+ *
153
+ * With that in mind, this copies over the recipes for how to turn tokens into props, the customizations that
154
+ * will be made are about how to ensure the tokens are set up correctly.
155
+ */
156
+ ...sample1StylingOptions,
157
+ /**
158
+ * In sample1 tokens are set to defaults from the global theme section, then patched with any values looked up with
159
+ * the string 'Sample1'
160
+ *
161
+ * We want to maintain the logic of setting up the globals, but add an additional lookup for 'Sample2'. This might correspond
162
+ * to saying that if we were making a variant of a 'Text' component called 'HeaderText', we might want to look up
163
+ * customizations from 'Text' first, then override those customizations with those from 'HeaderText'
164
+ *
165
+ * 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
166
+ * that could be filtered out of the array that is being copied
167
+ */
168
+ tokens: [...sample1StylingOptions.tokens, 'Sample2'],
169
+ /**
170
+ * This is the final bit of magic. The tokens will already have values set from the global theme, they will then be patched with
171
+ * any customizations set into Sample1 and/or Sample2.
172
+ *
173
+ * If this value was omitted then the tokens would be passed to the slotProps recipies as is. To have those values patched from
174
+ * 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
175
+ * tokens then this value can be set to 'all'. If none should be passed it can be omitted or set to 'none'
176
+ */
177
+ tokensThatAreAlsoProps: ['color'],
178
+ }, themeHelper);
179
+ // the Sample2Text component is built the same way as sample1, just using the new hook that has been created
180
+ const Sample2Text = (props) => {
181
+ const styledProps = useStylingSample2(props).content;
182
+ const merged = { ...props, ...styledProps };
183
+ // delete the color key to not pass it through to the text props, could be done via destructuring, filtering, or any number of ways
184
+ delete merged.color;
185
+ // render the text
186
+ return jsx_runtime_1.jsx(react_native_1.Text, { ...merged, children: props.children });
187
+ };
188
+ /** rendering the Sample2 component with the base theme */
189
+ it('Sample2Text rendering with defaults and a color override', () => {
190
+ themeHelper.setActive();
191
+ let component;
192
+ (0, react_1.act)(() => {
193
+ component = renderer.create(jsx_runtime_1.jsxs(react_native_1.View, { children: [
194
+ jsx_runtime_1.jsx(Sample2Text, { children: "Sample2 with defaults" }), jsx_runtime_1.jsx(Sample2Text, { color: "green", children: "Sample2 with color override via prop" })
195
+ ] }));
196
+ });
197
+ expect(component.toJSON()).toMatchSnapshot();
238
198
  });
239
- let component;
240
- (0, react_1.act)(() => {
241
- component = renderer.create(
242
- jsx_runtime_1.jsxs(react_native_1.View, {
243
- children: [
244
- jsx_runtime_1.jsx(Sample2Text, { children: 'Sample2 with theme overrides set' }),
245
- jsx_runtime_1.jsx(Sample2Text, { color: 'purple', children: 'Sample2 with theme and color prop override' }),
246
- ],
247
- }),
248
- );
199
+ /** now re-theme the component via the components in the theme */
200
+ it('Sample2Text rendering with some custom settings in the theme', () => {
201
+ themeHelper.setActive({
202
+ components: {
203
+ Sample1: {
204
+ color: 'pink',
205
+ fontSize: 24,
206
+ },
207
+ Sample2: {
208
+ fontSize: 18,
209
+ fontFamily: 'Helvetica',
210
+ },
211
+ },
212
+ });
213
+ let component;
214
+ (0, react_1.act)(() => {
215
+ component = renderer.create(jsx_runtime_1.jsxs(react_native_1.View, { children: [
216
+ jsx_runtime_1.jsx(Sample2Text, { children: "Sample2 with theme overrides set" }), jsx_runtime_1.jsx(Sample2Text, { color: "purple", children: "Sample2 with theme and color prop override" })
217
+ ] }));
218
+ });
219
+ expect(component.toJSON()).toMatchSnapshot();
249
220
  });
250
- expect(component.toJSON()).toMatchSnapshot();
251
- });
252
221
  });
253
- //# sourceMappingURL=useStyling.samples.test.js.map
222
+ //# sourceMappingURL=useStyling.samples.test.js.map
package/package.json CHANGED
@@ -1,50 +1,51 @@
1
1
  {
2
2
  "name": "@fluentui-react-native/use-styling",
3
- "version": "0.14.0",
3
+ "version": "0.14.1",
4
4
  "description": "Opinionated use styling hook implementation",
5
5
  "keywords": [],
6
+ "license": "MIT",
7
+ "author": "",
6
8
  "repository": {
7
9
  "type": "git",
8
10
  "url": "https://github.com/microsoft/fluentui-react-native",
9
11
  "directory": "packages/framework/use-styling"
10
12
  },
11
- "license": "MIT",
12
- "author": "",
13
+ "main": "lib-commonjs/index.js",
14
+ "module": "lib/index.js",
15
+ "types": "lib/index.d.ts",
13
16
  "exports": {
14
17
  ".": {
15
18
  "types": "./lib/index.d.ts",
16
19
  "import": "./lib/index.js",
17
- "require": "./lib-commonjs/index.js"
20
+ "require": "./lib-commonjs/index.js",
21
+ "default": "./src/index.ts"
18
22
  }
19
23
  },
20
- "main": "lib-commonjs/index.js",
21
- "module": "lib/index.js",
22
- "types": "lib/index.d.ts",
23
24
  "scripts": {
24
25
  "build": "fluentui-scripts build",
25
26
  "build-cjs": "tsgo --outDir lib-commonjs",
26
- "build-esm": "tsgo --outDir lib --module esnext --moduleResolution bundler",
27
+ "build-core": "tsgo --outDir lib --module esnext --moduleResolution bundler",
27
28
  "clean": "fluentui-scripts clean",
28
29
  "depcheck": "fluentui-scripts depcheck",
29
30
  "lint": "fluentui-scripts eslint",
30
31
  "lint-package": "fluentui-scripts lint-package",
31
- "prettier": "fluentui-scripts prettier",
32
+ "format": "fluentui-scripts format",
32
33
  "start": "fluentui-scripts dev",
33
34
  "start-test": "fluentui-scripts jest-watch",
34
35
  "test": "fluentui-scripts jest",
35
36
  "update-snapshots": "fluentui-scripts jest -u"
36
37
  },
37
38
  "dependencies": {
38
- "@fluentui-react-native/framework-base": "0.3.0",
39
- "@fluentui-react-native/use-tokens": "0.7.0"
39
+ "@fluentui-react-native/framework-base": "0.3.1",
40
+ "@fluentui-react-native/use-tokens": "0.7.1"
40
41
  },
41
42
  "devDependencies": {
42
43
  "@babel/core": "^7.20.0",
43
44
  "@fluentui-react-native/babel-config": "0.1.1",
44
- "@fluentui-react-native/eslint-config-rules": "0.1.1",
45
- "@fluentui-react-native/jest-config": "0.1.1",
45
+ "@fluentui-react-native/eslint-config-rules": "0.1.2",
46
+ "@fluentui-react-native/jest-config": "0.1.0",
46
47
  "@fluentui-react-native/kit-config": "0.1.2",
47
- "@fluentui-react-native/scripts": "0.1.2",
48
+ "@fluentui-react-native/scripts": "0.1.0",
48
49
  "@react-native-community/cli": "^20.0.0",
49
50
  "@react-native-community/cli-platform-android": "^20.0.0",
50
51
  "@react-native-community/cli-platform-ios": "^20.0.0",
package/src/buildProps.ts CHANGED
@@ -57,8 +57,8 @@ function refineKeys<TTokens>(keys: (keyof TTokens)[], mask?: TokensThatAreAlsoPr
57
57
  return typeof mask === 'object' && Array.isArray(mask)
58
58
  ? keys.filter((key) => mask.findIndex((val) => val === key) !== -1)
59
59
  : mask
60
- ? keys
61
- : [];
60
+ ? keys
61
+ : [];
62
62
  }
63
63
 
64
64
  /**