-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-By: Drapich Piotr <drapich.piotr@gmail.com>
- Loading branch information
1 parent
d1d5583
commit 69a2a06
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |