-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Gradient Type and Angle Picker to the custom gradient UI
- Loading branch information
1 parent
b33c959
commit 8827cd5
Showing
8 changed files
with
297 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { withInstanceId } from '@wordpress/compose'; | ||
import { | ||
SVG, | ||
G, | ||
Rect, | ||
Defs, | ||
RadialGradient, | ||
LinearGradient, | ||
Stop, | ||
} from '@wordpress/primitives'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
export const LinearGradientIcon = withInstanceId( ( { instanceId } ) => { | ||
const linerGradientId = `linear-gradient-${ instanceId }`; | ||
return ( | ||
<SVG width="20" height="20" xmlns="http://www.w3.org/2000/svg"> | ||
<Defs> | ||
<LinearGradient | ||
y2="0" | ||
x2="0.5" | ||
y1="1" | ||
x1="0.5" | ||
id={ linerGradientId } | ||
> | ||
<Stop offset="0" stopColor="#000000" /> | ||
<Stop offset="1" stopColor="#fff" /> | ||
</LinearGradient> | ||
</Defs> | ||
<G> | ||
<Rect | ||
fill={ `url(#${ linerGradientId })` } | ||
height="20" | ||
width="20" | ||
y="-1" | ||
x="-1" | ||
/> | ||
</G> | ||
</SVG> | ||
); | ||
} ); | ||
|
||
export const RadialGradientIcon = withInstanceId( ( { instanceId } ) => { | ||
const radialGradientId = `radial-gradient-${ instanceId }`; | ||
return ( | ||
<SVG width="20" height="20" xmlns="http://www.w3.org/2000/svg"> | ||
<Defs> | ||
<RadialGradient | ||
cy="0.5" | ||
cx="0.5" | ||
spreadMethod="pad" | ||
id={ radialGradientId } | ||
> | ||
<Stop offset="0" stopColor="#fff" /> | ||
<Stop offset="1" stopColor="#000000" /> | ||
</RadialGradient> | ||
</Defs> | ||
<G> | ||
<Rect | ||
fill={ `url(#${ radialGradientId })` } | ||
height="20" | ||
width="20" | ||
y="-1" | ||
x="-1" | ||
/> | ||
</G> | ||
</SVG> | ||
); | ||
} ); |
119 changes: 119 additions & 0 deletions
119
packages/components/src/custom-gradient-picker/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get, omit } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import AnglePicker from '../angle-picker'; | ||
import { LinearGradientIcon, RadialGradientIcon } from './icons'; | ||
import CustomGradientBar from './custom-gradient-bar'; | ||
import BaseControl from '../base-control'; | ||
import { getGradientParsed } from './utils'; | ||
import { serializeGradient } from './serializer'; | ||
import ToolbarGroup from '../toolbar-group'; | ||
import { | ||
DEFAULT_LINEAR_GRADIENT_ANGLE, | ||
HORIZONTAL_GRADIENT_ORIENTATION, | ||
} from './constants'; | ||
|
||
const GradientAnglePicker = ( { gradientAST, hasGradient, onChange } ) => { | ||
const angle = get( | ||
gradientAST, | ||
[ 'orientation', 'value' ], | ||
DEFAULT_LINEAR_GRADIENT_ANGLE | ||
); | ||
const onAngleChange = ( newAngle ) => { | ||
onChange( | ||
serializeGradient( { | ||
...gradientAST, | ||
orientation: { | ||
type: 'angular', | ||
value: newAngle, | ||
}, | ||
} ) | ||
); | ||
}; | ||
return ( | ||
<AnglePicker | ||
value={ hasGradient ? angle : '' } | ||
onChange={ onAngleChange } | ||
/> | ||
); | ||
}; | ||
|
||
const GradientTypePicker = ( { gradientAST, hasGradient, onChange } ) => { | ||
const { type } = gradientAST; | ||
const onSetLinearGradient = () => { | ||
onChange( | ||
serializeGradient( { | ||
...gradientAST, | ||
...( gradientAST.orientation | ||
? {} | ||
: { orientation: HORIZONTAL_GRADIENT_ORIENTATION } ), | ||
type: 'linear-gradient', | ||
} ) | ||
); | ||
}; | ||
|
||
const onSetRadialGradient = () => { | ||
onChange( | ||
serializeGradient( { | ||
...omit( gradientAST, [ 'orientation' ] ), | ||
type: 'radial-gradient', | ||
} ) | ||
); | ||
}; | ||
|
||
return ( | ||
<BaseControl className="components-custom-gradient-picker__type-picker"> | ||
<BaseControl.VisualLabel>{ __( 'Type' ) }</BaseControl.VisualLabel> | ||
<ToolbarGroup | ||
controls={ [ | ||
{ | ||
icon: <LinearGradientIcon />, | ||
title: 'Linear Gradient', | ||
isActive: hasGradient && type === 'linear-gradient', | ||
onClick: onSetLinearGradient, | ||
}, | ||
{ | ||
icon: <RadialGradientIcon />, | ||
title: 'Radial Gradient', | ||
isActive: hasGradient && type === 'radial-gradient', | ||
onClick: onSetRadialGradient, | ||
}, | ||
] } | ||
/> | ||
</BaseControl> | ||
); | ||
}; | ||
|
||
export default function CustomGradientPicker( { value, onChange } ) { | ||
const { gradientAST, hasGradient } = getGradientParsed( value ); | ||
const { type } = gradientAST; | ||
return ( | ||
<div className="components-custom-gradient-picker"> | ||
<CustomGradientBar value={ value } onChange={ onChange } /> | ||
<div className="components-custom-gradient-picker__ui-line"> | ||
<GradientTypePicker | ||
gradientAST={ gradientAST } | ||
hasGradient={ hasGradient } | ||
onChange={ onChange } | ||
/> | ||
{ type === 'linear-gradient' && ( | ||
<GradientAnglePicker | ||
gradientAST={ gradientAST } | ||
hasGradient={ hasGradient } | ||
onChange={ onChange } | ||
/> | ||
) } | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.