Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StyleSheet.setStyleAttributePreprocessor #11138

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
jest.disableAutomock();

const ReactNativeAttributePayload = require('ReactNativeAttributePayload');
const ReactNativeStyleAttributes = require('ReactNativeStyleAttributes');
const StyleSheet = require('StyleSheet');

describe('ReactNativeAttributePayload', () => {

describe('create', () => {
it('works with custom style processors', () => {
StyleSheet.setStyleAttributePreprocessor('fontFamily', (nextValue) => 'Wingdings');

const updatePayload = ReactNativeAttributePayload.create(
{style: {fontFamily: 'Comic Sans'}},
{style: ReactNativeStyleAttributes},
);

expect(updatePayload.fontFamily).toEqual('Wingdings');
});
});

});
31 changes: 30 additions & 1 deletion Libraries/StyleSheet/StyleSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

var PixelRatio = require('PixelRatio');
var ReactNativePropRegistry = require('ReactNativePropRegistry');
var ReactNativeStyleAttributes = require('ReactNativeStyleAttributes');
var StyleSheetValidation = require('StyleSheetValidation');

var flatten = require('flattenStyle');
Expand Down Expand Up @@ -162,6 +163,34 @@ module.exports = {
*/
flatten,

/**
* WARNING: EXPERIMENTAL. Breaking changes will probably happen a lot and will
* not be reliably announced. The whole thing might be deleted, who knows? Use
* at your own risk.
*
* Sets a function to use to pre-process a style property value. This is used
* internally to process color and transform values. You should not use this
* unless you really know what you are doing and have exhausted other options.
*/
setStyleAttributePreprocessor(property: string, process: (nextProp: mixed) => mixed) {
let value;

if (typeof ReactNativeStyleAttributes[property] === 'string') {
value = {};
} else if (typeof ReactNativeStyleAttributes[property] === 'object') {
value = ReactNativeStyleAttributes[property];
} else {
console.error(`${property} is not a valid style attribute`);
return;
}

if (__DEV__ && typeof value.process === 'function') {
console.warn(`Overwriting ${property} style attribute preprocessor`);
}

ReactNativeStyleAttributes[property] = { ...value, process };
},

/**
* Creates a StyleSheet style reference from the given object.
*/
Expand All @@ -172,5 +201,5 @@ module.exports = {
result[key] = ReactNativePropRegistry.register(obj[key]);
}
return result;
}
},
};