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

Add support for named orientations and literal colors on custom gradients #22239

Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions packages/components/src/custom-gradient-picker/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import { compact, get } from 'lodash';

export function serializeGradientColor( { type, value } ) {
if ( type === 'literal' ) {
return value;
}
return `${ type }(${ value.join( ',' ) })`;
}

Expand Down
24 changes: 24 additions & 0 deletions packages/components/src/custom-gradient-picker/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,21 @@ export function getLinearGradientRepresentationOfARadial( gradientAST ) {
} );
}

const DIRECTIONAL_ORIENTATION_ANGLE_MAP = {
top: 0,
'top right': 45,
'right top': 45,
right: 90,
'right bottom': 135,
'bottom right': 135,
bottom: 180,
'bottom left': 225,
'left bottom': 225,
left: 270,
'top left': 315,
'left top': 315,
};

export function getGradientParsed( value ) {
let hasGradient = !! value;
// gradientAST will contain the gradient AST as parsed by gradient-parser npm module.
Expand All @@ -255,6 +270,15 @@ export function getGradientParsed( value ) {
gradientValue = DEFAULT_GRADIENT;
}

if (
gradientAST.orientation &&
gradientAST.orientation.type === 'directional'
) {
gradientAST.orientation.type = 'angular';
gradientAST.orientation.value = DIRECTIONAL_ORIENTATION_ANGLE_MAP[
gradientAST.orientation.value
].toString();
}
return {
hasGradient,
gradientAST,
Expand Down