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

CustomGradientPicker: improve initial state UI #49146

Merged
merged 3 commits into from
Mar 17, 2023
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
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- `CustomGradientPicker`: Convert to TypeScript ([#48929](https://github.com/WordPress/gutenberg/pull/48929)).

### Enhancements

- `CustomGradientPicker`: improve initial state UI ([#49146](https://github.com/WordPress/gutenberg/pull/49146)).

## 23.6.0 (2023-03-15)

### Enhancements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,15 @@ export default function CustomGradientBar( {
) }
onMouseEnter={ onMouseEnterAndMove }
onMouseMove={ onMouseEnterAndMove }
style={ { background } }
onMouseLeave={ onMouseLeave }
>
<div
className="components-custom-gradient-picker__gradient-bar-background"
style={ {
background,
opacity: hasGradient ? 1 : 0.4,
} }
/>
<div
ref={ gradientMarkersContainerDomRef }
className="components-custom-gradient-picker__markers-container"
Expand Down
7 changes: 3 additions & 4 deletions packages/components/src/custom-gradient-picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
DEFAULT_LINEAR_GRADIENT_ANGLE,
HORIZONTAL_GRADIENT_ORIENTATION,
GRADIENT_OPTIONS,
DEFAULT_GRADIENT,
} from './constants';
import {
AccessoryWrapper,
Expand Down Expand Up @@ -148,13 +147,13 @@ export function CustomGradientPicker( {
onChange,
__experimentalIsRenderedInSidebar = false,
}: CustomGradientPickerProps ) {
const { gradientAST, gradientAstValue } =
getGradientAstWithDefault( value );
const { gradientAST, hasGradient } = getGradientAstWithDefault( value );

// On radial gradients the bar should display a linear gradient.
// On radial gradients the bar represents a slice of the gradient from the center until the outside.
// On liner gradients the bar represents the color stops from left to right independently of the angle.
const background = getLinearGradientRepresentation( gradientAST );
const hasGradient = gradientAstValue !== DEFAULT_GRADIENT;

// Control points color option may be hex from presets, custom colors will be rgb.
// The position should always be a percentage.
const controlPoints = gradientAST.colorStops.map( ( colorStop ) => {
Expand Down
29 changes: 25 additions & 4 deletions packages/components/src/custom-gradient-picker/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,30 @@ $components-custom-gradient-picker__padding: $grid-unit-20; // 48px container, 1
}
}

.components-custom-gradient-picker__gradient-bar:not(.has-gradient) {
opacity: 0.4;
}

.components-custom-gradient-picker__gradient-bar {
border-radius: $radius-block-ui;
width: 100%;
height: $grid-unit-60;
position: relative;
z-index: 1;

&.has-gradient {
// The background image creates a checkerboard pattern. Ignore rtlcss to
// make it work both in LTR and RTL.
// See https://github.com/WordPress/gutenberg/pull/42510
/*rtl:begin:ignore*/
background-image:
repeating-linear-gradient(45deg, $gray-200 25%, transparent 25%, transparent 75%, $gray-200 75%, $gray-200),
repeating-linear-gradient(45deg, $gray-200 25%, transparent 25%, transparent 75%, $gray-200 75%, $gray-200);
background-position: 0 0, 12px 12px;
/*rtl:end:ignore*/
background-size: calc(2 * 12px) calc(2 * 12px);
}

.components-custom-gradient-picker__gradient-bar-background {
position: absolute;
inset: 0;
}

.components-custom-gradient-picker__markers-container {
position: relative;
Expand Down Expand Up @@ -109,3 +125,8 @@ $components-custom-gradient-picker__padding: $grid-unit-20; // 48px container, 1
}
}
}

.components-custom-gradient-picker__ui-line {
position: relative;
z-index: 0;
}
16 changes: 10 additions & 6 deletions packages/components/src/custom-gradient-picker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,22 @@ function hasUnsupportedLength( item: gradientParser.ColorStop ) {
export function getGradientAstWithDefault( value?: string | null ) {
// gradientAST will contain the gradient AST as parsed by gradient-parser npm module.
// More information of its structure available at https://www.npmjs.com/package/gradient-parser#ast.
let gradientAST: gradientParser.GradientNode;
let gradientAstValue: string | undefined;
let gradientAST: gradientParser.GradientNode | undefined;
let hasGradient = !! value;

const valueToParse = value ?? DEFAULT_GRADIENT;

try {
gradientAST = gradientParser.parse( valueToParse )[ 0 ];
gradientAstValue = valueToParse;
} catch ( error ) {
// eslint-disable-next-line no-console
console.warn(
'wp.components.CustomGradientPicker failed to parse the gradient with error',
error
);

gradientAST = gradientParser.parse( DEFAULT_GRADIENT )[ 0 ];
gradientAstValue = DEFAULT_GRADIENT;
hasGradient = false;
}

if (
Expand All @@ -69,10 +74,9 @@ export function getGradientAstWithDefault( value?: string | null ) {
type: '%',
};
} );
gradientAstValue = serializeGradient( gradientAST );
}

return { gradientAST, gradientAstValue };
return { gradientAST, hasGradient };
}

export function getGradientAstWithControlPoints(
Expand Down