Skip to content

Commit

Permalink
Components: Remove Lodash usage from CustomGradientPicker (WordPress#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla authored Jun 24, 2022
1 parent eb6dd8e commit a972672
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 33 deletions.
16 changes: 5 additions & 11 deletions packages/components/src/custom-gradient-picker/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get, omit } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -34,11 +29,8 @@ import {
} from './styles/custom-gradient-picker-styles';

const GradientAnglePicker = ( { gradientAST, hasGradient, onChange } ) => {
const angle = get(
gradientAST,
[ 'orientation', 'value' ],
DEFAULT_LINEAR_GRADIENT_ANGLE
);
const angle =
gradientAST?.orientation?.value ?? DEFAULT_LINEAR_GRADIENT_ANGLE;
const onAngleChange = ( newAngle ) => {
onChange(
serializeGradient( {
Expand Down Expand Up @@ -74,9 +66,11 @@ const GradientTypePicker = ( { gradientAST, hasGradient, onChange } ) => {
};

const onSetRadialGradient = () => {
// eslint-disable-next-line no-unused-vars
const { orientation, ...restGradientAST } = gradientAST;
onChange(
serializeGradient( {
...omit( gradientAST, [ 'orientation' ] ),
...restGradientAST,
type: 'radial-gradient',
} )
);
Expand Down
14 changes: 3 additions & 11 deletions packages/components/src/custom-gradient-picker/index.native.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* External dependencies
*/
import { get, omit } from 'lodash';
/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -37,7 +33,7 @@ function CustomGradientPicker( { setColor, currentValue, isGradientColor } ) {
}

function getGradientColor( type ) {
const orientation = get( gradientAST, [ 'orientation' ] );
const { orientation, ...restGradientAST } = gradientAST;

if ( orientation ) {
setGradientOrientation( orientation );
Expand All @@ -55,7 +51,7 @@ function CustomGradientPicker( { setColor, currentValue, isGradientColor } ) {
type,
}
: {
...omit( gradientAST, [ 'orientation' ] ),
...restGradientAST,
type,
}
);
Expand Down Expand Up @@ -83,11 +79,7 @@ function CustomGradientPicker( { setColor, currentValue, isGradientColor } ) {
}

function getGradientAngle() {
return get(
gradientAST,
[ 'orientation', 'value' ],
DEFAULT_LINEAR_GRADIENT_ANGLE
);
return gradientAST?.orientation?.value ?? DEFAULT_LINEAR_GRADIENT_ANGLE;
}
return (
<>
Expand Down
16 changes: 5 additions & 11 deletions packages/components/src/custom-gradient-picker/serializer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { compact, get } from 'lodash';

export function serializeGradientColor( { type, value } ) {
if ( type === 'literal' ) {
return value;
Expand Down Expand Up @@ -40,13 +35,12 @@ export function serializeGradient( { type, orientation, colorStops } ) {
const serializedColorStops = colorStops
.sort( ( colorStop1, colorStop2 ) => {
return (
get( colorStop1, [ 'length', 'value' ], 0 ) -
get( colorStop2, [ 'length', 'value' ], 0 )
( colorStop1?.length?.value ?? 0 ) -
( colorStop2?.length?.value ?? 0 )
);
} )
.map( serializeGradientColorStop );
return `${ type }(${ compact( [
serializedOrientation,
...serializedColorStops,
] ).join( ',' ) })`;
return `${ type }(${ [ serializedOrientation, ...serializedColorStops ]
.filter( Boolean )
.join( ',' ) })`;
}

0 comments on commit a972672

Please sign in to comment.