Skip to content

Commit

Permalink
Correct android font weight (#2141)
Browse files Browse the repository at this point in the history
Co-Authored-By: Drapich Piotr <drapich.piotr@gmail.com>
  • Loading branch information
lukewalczak and dratwas authored Apr 30, 2020
1 parent d1d5583 commit 69a2a06
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import './globals';
import { getTranslation } from '../i18n-cache';
import initialHtml from './initial-html';
import setupApiFetch from './api-fetch-setup';
import correctTextFontWeight from './text-font-weight-correct';
import setupJetpackEditor from './jetpack-editor-setup';

const gutenbergSetup = () => {
Expand Down Expand Up @@ -56,6 +57,7 @@ export class RootComponent extends React.Component {
super( props );
setupLocale( props.locale, props.translations );
setupApiFetch();
correctTextFontWeight();
setupJetpackEditor( props.jetpackState || { blogId: 1, isJetpackActive: true } );
require( '@wordpress/edit-post' ).initializeEditor();

Expand Down
77 changes: 77 additions & 0 deletions src/text-font-weight-correct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* External dependencies
*
* @format
*/

/**
* External dependencies
*/
import { Platform, Text } from 'react-native';
import { merge } from 'lodash';
/**
* WordPress dependencies
*/
import { cloneElement } from '@wordpress/element';

const textRender = Text.render;

const getCorrectFontWeight = ( fontWeight ) => {
switch ( fontWeight ) {
case '100':
case '200':
case '300':
return {
fontFamily: 'sans-serif-thin',
fontWeight: 'normal',
};
case '400':
return {
fontFamily: 'sans-serif-light',
fontWeight: 'normal',
};
case '500':
case 'normal':
return {
fontFamily: 'sans-serif',
fontWeight: 'normal',
};
case '600':
return {
fontFamily: 'sans-serif-medium',
fontWeight: 'normal',
};
case '700':
case 'bold':
return {
fontFamily: 'sans-serif',
fontWeight: 'bold',
};
case '800':
case '900':
return {
fontFamily: 'sans-serif-medium',
fontWeight: 'bold',
};
}
};

const correctTextFontWeight = ( args ) => {
const baseText = textRender.call( this, args );
const { style } = baseText.props;

const flatStyle = Array.isArray( style ) ? merge( {}, ...style ) : style;
const shouldCorrectFontWeight = flatStyle && flatStyle.fontWeight;

return shouldCorrectFontWeight ?
cloneElement( baseText, {
style: [ flatStyle, getCorrectFontWeight( flatStyle.fontWeight ) ],
} ) :
baseText;
};

export default () => {
if ( Platform.OS === 'android' ) {
Text.render = correctTextFontWeight;
}
};

0 comments on commit 69a2a06

Please sign in to comment.