import React, { Component, Fragment } from "react"; import PropTypes from "prop-types"; import { GRADIENT_TYPE, RADIAL_TYPES } from "./constants"; const { PanelColorSettings } = wp.editor; const { __ } = wp.i18n; const { RangeControl, BaseControl, Button, ButtonGroup } = wp.components; class GradientColorControl extends Component { state = { gradientType: "linear", colorOne: "transparent", colorOnePosition: 0, colorTwo: "transparent", colorTwoPosition: 100, angle: 0, radialShape: "ellipse", radialX: 50, radialY: 50, }; getColorString = () => { const { colorOne, colorOnePosition, colorTwo, colorTwoPosition, } = this.state; return `${colorOne} ${colorOnePosition}% , ${colorTwo} ${colorTwoPosition}%`; }; getRadialGradient = () => { const { radialShape, radialX, radialY } = this.state; return `radial-gradient(${radialShape} at ${radialX}% ${radialY}%, ${this.getColorString()})`; }; getLinearGradient = () => `linear-gradient(${this.state.angle}deg, ${this.getColorString()})`; generateString = () => { // Send linear or radial gradiant string to parent this.props.onChange( this.state.gradientType === "linear" ? this.getLinearGradient() : this.getRadialGradient(), ); }; onGradientChange = (attributeName, value) => { this.setState({ [attributeName]: value }, () => this.generateString()); }; render() { const { gradientType, colorOne, colorOnePosition, colorTwo, colorTwoPosition, angle, radialShape, radialX, radialY, } = this.state; return (
{GRADIENT_TYPE.map(item => ( ))} {gradientType === "radial" && ( {RADIAL_TYPES.map(item => ( ))} )} this.onGradientChange("colorOne", newColor), label: __("First Color"), }, ]} /> this.onGradientChange("colorOnePosition", newValue) } min={0} max={100} /> this.onGradientChange("colorTwo", newColor), label: __("Second Color"), }, ]} /> this.onGradientChange("colorTwoPosition", newValue) } min={0} max={100} /> {gradientType === "linear" && ( this.onGradientChange("angle", newValue)} min={0} max={360} /> )} {gradientType === "radial" && ( this.onGradientChange("radialX", newValue)} min={0} max={100} /> this.onGradientChange("radialY", newValue)} min={0} max={100} /> )}
); } } GradientColorControl.propTypes = { onChange: PropTypes.func.isRequired, }; export default GradientColorControl;