-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
Conversation
One thing I didn't understand from the description: Are there use cases for this when not using Exponent? Could the code live in Exponent only? Why add it to RN? |
@mkonicek - it's possible that some people might want to do this too, for example in our discussion around CRNA it has been brought up that we want the new project to support other clients. Other clients would likely need to do the same thing for their font apis. |
👍 Thanks for the explanation Brent! @facebook-github-bot shipit |
@mkonicek has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
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 facebook#11138 Differential Revision: D4245518 Pulled By: mkonicek fbshipit-source-id: bd2452b1129d6675aa7b88e41351f8bb61fa20a3
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 facebook#11138 Differential Revision: D4245518 Pulled By: mkonicek fbshipit-source-id: bd2452b1129d6675aa7b88e41351f8bb61fa20a3
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 facebook#11138 Differential Revision: D4245518 Pulled By: mkonicek fbshipit-source-id: bd2452b1129d6675aa7b88e41351f8bb61fa20a3
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 facebook#11138 Differential Revision: D4245518 Pulled By: mkonicek fbshipit-source-id: bd2452b1129d6675aa7b88e41351f8bb61fa20a3
Summary: from the original design of `StyleSheet.setStyleAttributePreprocessor()` in #11138, the overwriting warning shows when the existing preprocess is be overwritten. the behavior changes from 33b385825c72. This PR revises the logic back to original design. ## Changelog [Internal] [Fixed] - Show warning only when overwriting existing preprocessor in `StyleSheet.setStyleAttributePreprocessor()` Pull Request resolved: #34479 Test Plan: Unit Test ``` PASS Libraries/StyleSheet/__tests__/StyleSheet-test.js setStyleAttributePreprocessor ✓ should not show warning when set preprocessor first time (2 ms) ✓ should show warning when overwrite the preprocessor (1 ms) ``` Reviewed By: dmitryrykun Differential Revision: D38940676 Pulled By: cipolleschi fbshipit-source-id: 80cf30fff62f4a02c17f7f42b3260a6011d5fc82
Summary: from the original design of `StyleSheet.setStyleAttributePreprocessor()` in facebook#11138, the overwriting warning shows when the existing preprocess is be overwritten. the behavior changes from facebook@33b385825c72. This PR revises the logic back to original design. ## Changelog [Internal] [Fixed] - Show warning only when overwriting existing preprocessor in `StyleSheet.setStyleAttributePreprocessor()` Pull Request resolved: facebook#34479 Test Plan: Unit Test ``` PASS Libraries/StyleSheet/__tests__/StyleSheet-test.js setStyleAttributePreprocessor ✓ should not show warning when set preprocessor first time (2 ms) ✓ should show warning when overwrite the preprocessor (1 ms) ``` Reviewed By: dmitryrykun Differential Revision: D38940676 Pulled By: cipolleschi fbshipit-source-id: 80cf30fff62f4a02c17f7f42b3260a6011d5fc82
Summary: from the original design of `StyleSheet.setStyleAttributePreprocessor()` in #11138, the overwriting warning shows when the existing preprocess is be overwritten. the behavior changes from 33b385825c72. This PR revises the logic back to original design. ## Changelog [Internal] [Fixed] - Show warning only when overwriting existing preprocessor in `StyleSheet.setStyleAttributePreprocessor()` Pull Request resolved: #34479 Test Plan: Unit Test ``` PASS Libraries/StyleSheet/__tests__/StyleSheet-test.js setStyleAttributePreprocessor ✓ should not show warning when set preprocessor first time (2 ms) ✓ should show warning when overwrite the preprocessor (1 ms) ``` Reviewed By: dmitryrykun Differential Revision: D38940676 Pulled By: cipolleschi fbshipit-source-id: 80cf30fff62f4a02c17f7f42b3260a6011d5fc82
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-scopedfontFamily
.Example:
See the tests for another example which always converts fontFamily to Wingdings.
I marked this function as experimental because it seems possible that the attribute API will change, and because I built this specifically for the above use case and others may need to expand the API to work in their case -- I did not attempt to anticipate how others would use it.
Test plan
Run
npm test
. This does not impact any app that does not use the API.