Skip to content

Commit

Permalink
CustomGradientPicker: improve initial state UI (#49146)
Browse files Browse the repository at this point in the history
* CustomGradientPicker: better default value handling

* CustomGradientPicker: better opacity handling

* CHANGELOG
  • Loading branch information
ciampo authored Mar 17, 2023
1 parent 9e1bc03 commit d4d8612
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
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

1 comment on commit d4d8612

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in d4d8612.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4449587331
📝 Reported issues:

Please sign in to comment.