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

[RNMobile] Refactor native gradient logic #27307

Merged
merged 7 commits into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions packages/components/src/color-palette/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ function ColorPalette( {
customIndicatorWrapperStyles,
} ) {
const customSwatchGradients = [
'linear-gradient(120deg, rgba(255,0,0,.8), 0%, rgba(255,255,255,1) 70.71%)',
'linear-gradient(240deg, rgba(0,255,0,.8), 0%, rgba(0,255,0,0) 70.71%)',
'linear-gradient(360deg, rgba(0,0,255,.8), 0%, rgba(0,0,255,0) 70.71%)',
'linear-gradient(120deg, rgba(255,0,0,.8) 0%, rgba(255,255,255,1) 70.71%)',
'linear-gradient(240deg, rgba(0,255,0,.8) 0%, rgba(0,255,0,0) 70.71%)',
'linear-gradient(360deg, rgba(0,0,255,.8) 0%, rgba(0,0,255,0) 70.71%)',
];

const scrollViewRef = useRef();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function serializeGradientColorStop( { type, value, length } ) {
return `${ serializeGradientColor( {
type,
value,
} ) } ${ serializeGradientPosition( length ) }`;
} ) } ${ length ? serializeGradientPosition( length ) : '' }`;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gradient color length (gradient color location - e.g. 50%) can be undefined e.g. in linear-gradient(to top , #3C8067, #FAFBF6) from the theme called Spearhead. On web nothing happens when trying to switch from linear to radial but on mobile there is a crash since we reuse logic within serializer.js.

Properties of undefined length are then used in function:

export function serializeGradientPosition( { type, value } ) {
	return `${ value }${ type }`;
}

which makes a crash.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me this seems ok to do, but this code is new to me as well. Maybe @jorgefilipecosta can have a quick look?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks good to me 👍

I guess as an enhancement we can add this logic inside the serializeGradientPosition function directly:

export function serializeGradientPosition( position ) {
	if ( ! position ) {
		return '';
	}
	const { value, type } = position;
	return `${ value }${ type }`;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, make sense 👍🏼

}

export function serializeGradientOrientation( orientation ) {
Expand Down
68 changes: 62 additions & 6 deletions packages/components/src/mobile/gradient/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { View, Platform } from 'react-native';
import RNLinearGradient from 'react-native-linear-gradient';
import gradientParser from 'gradient-parser';
/**
* WordPress dependencies
*/
Expand All @@ -16,17 +17,72 @@ import { useResizeObserver } from '@wordpress/compose';
import styles from './style.scss';

function getGradientAngle( gradientValue ) {
const matchDeg = /(\d+)deg/g;
const matchAngle = /\(((\d+deg)|(to\s[^,]+))/g;
const angle = matchAngle.exec( gradientValue )[ 1 ];
const angleBase = 45;

return Number( matchDeg.exec( gradientValue )[ 1 ] );
const angleType = angle.includes( 'deg' ) ? 'angle' : 'sideOrCorner';

if ( angleType === 'sideOrCorner' ) {
switch ( angle ) {
case 'to top':
return 0;
case 'to top right':
case 'to right top':
return angleBase;
case 'to right':
return 2 * angleBase;
case 'to bottom right':
case 'to right bottom':
return 3 * angleBase;
case 'to bottom':
return 4 * angleBase;
case 'to bottom left':
case 'to left bottom':
return 5 * angleBase;
case 'to left':
return 6 * angleBase;
case 'to top left':
case 'to left top':
return 7 * angleBase;
}
} else if ( angleType === 'angle' ) {
return parseFloat( angle );
} else return 180;
}

function getGradientColorGroup( gradientValue ) {
const matchColorGroup = /(rgba|rgb|#)(.+?)[\%]/g;
const colorNeedParenthesis = [ 'rgb', 'rgba' ];

const excludeSideOrCorner = /linear-gradient\(to\s+([a-z\s]+,)/;

// Parser has some difficulties with angle defined as a side or corner (e.g. `to left`)
// so it's going to be excluded in order to matching color groups
const modifiedGradientValue = gradientValue.replace(
excludeSideOrCorner,
'linear-gradient('
);

return [].concat(
...gradientParser.parse( modifiedGradientValue )?.map( ( gradient ) =>
gradient.colorStops?.map( ( color, index ) => {
const { type, value, length } = color;
const fallbackLength = `${
100 * ( index / ( gradient.colorStops.length - 1 ) )
}%`;
const colorLength = length
? `${ length.value }${ length.type }`
: fallbackLength;

return gradientValue
.match( matchColorGroup )
.map( ( color ) => color.split( ' ' ) );
if ( colorNeedParenthesis.includes( type ) ) {
return [ `${ type }(${ value.join( ',' ) })`, colorLength ];
} else if ( type === 'literal' ) {
return [ value, colorLength ];
}
return [ `#${ value }`, colorLength ];
} )
)
);
}

function getGradientBaseColors( gradientValue ) {
Expand Down