Skip to content

Commit 346858a

Browse files
brentvatneFacebook Github Bot
authored and
Facebook Github Bot
committed
Add StyleSheet.setStyleAttributePreprocessor
Summary: **Motivation** On Exponent we load fonts dynamically and assign their native names by appending a session id, so that fonts from one Exponent "experience" do not clash with each other. So, before sending the `fontFamily` to native, we want to change it to the Exponent-scoped `fontFamily`. Example: ```js // Before rendering your app StyleSheet.setStyleAttributePreprocessor('fontFamily', _processFontFamily); function _processFontFamily(name) { // Pass system fonts through if (!name || Constants.systemFonts.indexOf(name) >= 0) { return name; } if (!Font.isLoaded(name)) { if (__DEV__) { console.error(`${name} is not a system font and has not been loaded through Exponent.Font.loadAsync. If you intended to use a system font, make sure you typed the name correctly and that it is supported by the current operating system. If this is a custom font, be sure to load it with Exponent.Font.loadAsync`); } else { return 'system'; } } return `ExponentFont- Closes #11138 Differential Revision: D4245518 Pulled By: mkonicek fbshipit-source-id: bd2452b1129d6675aa7b88e41351f8bb61fa20a3
1 parent 00f888a commit 346858a

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
jest.disableAutomock();
2+
3+
const ReactNativeAttributePayload = require('ReactNativeAttributePayload');
4+
const ReactNativeStyleAttributes = require('ReactNativeStyleAttributes');
5+
const StyleSheet = require('StyleSheet');
6+
7+
describe('ReactNativeAttributePayload', () => {
8+
9+
describe('create', () => {
10+
it('works with custom style processors', () => {
11+
StyleSheet.setStyleAttributePreprocessor('fontFamily', (nextValue) => 'Wingdings');
12+
13+
const updatePayload = ReactNativeAttributePayload.create(
14+
{style: {fontFamily: 'Comic Sans'}},
15+
{style: ReactNativeStyleAttributes},
16+
);
17+
18+
expect(updatePayload.fontFamily).toEqual('Wingdings');
19+
});
20+
});
21+
22+
});

Libraries/StyleSheet/StyleSheet.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
var PixelRatio = require('PixelRatio');
1515
var ReactNativePropRegistry = require('ReactNativePropRegistry');
16+
var ReactNativeStyleAttributes = require('ReactNativeStyleAttributes');
1617
var StyleSheetValidation = require('StyleSheetValidation');
1718

1819
var flatten = require('flattenStyle');
@@ -162,6 +163,34 @@ module.exports = {
162163
*/
163164
flatten,
164165

166+
/**
167+
* WARNING: EXPERIMENTAL. Breaking changes will probably happen a lot and will
168+
* not be reliably announced. The whole thing might be deleted, who knows? Use
169+
* at your own risk.
170+
*
171+
* Sets a function to use to pre-process a style property value. This is used
172+
* internally to process color and transform values. You should not use this
173+
* unless you really know what you are doing and have exhausted other options.
174+
*/
175+
setStyleAttributePreprocessor(property: string, process: (nextProp: mixed) => mixed) {
176+
let value;
177+
178+
if (typeof ReactNativeStyleAttributes[property] === 'string') {
179+
value = {};
180+
} else if (typeof ReactNativeStyleAttributes[property] === 'object') {
181+
value = ReactNativeStyleAttributes[property];
182+
} else {
183+
console.error(`${property} is not a valid style attribute`);
184+
return;
185+
}
186+
187+
if (__DEV__ && typeof value.process === 'function') {
188+
console.warn(`Overwriting ${property} style attribute preprocessor`);
189+
}
190+
191+
ReactNativeStyleAttributes[property] = { ...value, process };
192+
},
193+
165194
/**
166195
* Creates a StyleSheet style reference from the given object.
167196
*/
@@ -172,5 +201,5 @@ module.exports = {
172201
result[key] = ReactNativePropRegistry.register(obj[key]);
173202
}
174203
return result;
175-
}
204+
},
176205
};

0 commit comments

Comments
 (0)