diff --git a/packages/components/src/custom-gradient-bar/constants.js b/packages/components/src/custom-gradient-bar/constants.js new file mode 100644 index 00000000000000..d4236f1f4d817a --- /dev/null +++ b/packages/components/src/custom-gradient-bar/constants.js @@ -0,0 +1,15 @@ +export const COLOR_POPOVER_PROPS = { + className: 'components-custom-gradient-picker__color-picker-popover', + position: 'top', +}; + +export const GRADIENT_MARKERS_WIDTH = 18; +export const INSERT_POINT_WIDTH = 23; +export const MINIMUM_ABSOLUTE_LEFT_POSITION = 5; +export const MINIMUM_DISTANCE_BETWEEN_INSERTER_AND_POINT = 10; +export const MINIMUM_DISTANCE_BETWEEN_POINTS = 0; +export const MINIMUM_SIGNIFICANT_MOVE = 5; + +export const KEYBOARD_CONTROL_POINT_VARIATION = MINIMUM_DISTANCE_BETWEEN_INSERTER_AND_POINT; +export const MINIMUM_DISTANCE_BETWEEN_INSERTER_AND_MARKER = + ( INSERT_POINT_WIDTH + GRADIENT_MARKERS_WIDTH ) / 2; diff --git a/packages/components/src/custom-gradient-picker/control-points.js b/packages/components/src/custom-gradient-bar/control-points.js similarity index 53% rename from packages/components/src/custom-gradient-picker/control-points.js rename to packages/components/src/custom-gradient-bar/control-points.js index 20f0ce1e18721b..d8d5ec16c2fa71 100644 --- a/packages/components/src/custom-gradient-picker/control-points.js +++ b/packages/components/src/custom-gradient-bar/control-points.js @@ -6,9 +6,10 @@ import classnames from 'classnames'; /** * WordPress dependencies */ -import { Component, useEffect, useRef } from '@wordpress/element'; -import { __, sprintf } from '@wordpress/i18n'; import { useInstanceId } from '@wordpress/compose'; +import { useEffect, useRef, useState } from '@wordpress/element'; +import { __, sprintf } from '@wordpress/i18n'; +import { plus } from '@wordpress/icons'; /** * Internal dependencies @@ -16,66 +17,52 @@ import { useInstanceId } from '@wordpress/compose'; import Button from '../button'; import ColorPicker from '../color-picker'; import Dropdown from '../dropdown'; +import KeyboardShortcuts from '../keyboard-shortcuts'; import VisuallyHidden from '../visually-hidden'; + import { - getGradientWithColorAtIndexChanged, - getGradientWithControlPointRemoved, - getGradientWithPositionAtIndexChanged, - getGradientWithPositionAtIndexDecreased, - getGradientWithPositionAtIndexIncreased, + addControlPoint, + clampPercent, + removeControlPoint, + updateControlPointColor, + updateControlPointColorByPosition, + updateControlPointPosition, getHorizontalRelativeGradientPosition, - isControlPointOverlapping, } from './utils'; import { COLOR_POPOVER_PROPS, GRADIENT_MARKERS_WIDTH, MINIMUM_SIGNIFICANT_MOVE, + KEYBOARD_CONTROL_POINT_VARIATION, } from './constants'; -import KeyboardShortcuts from '../keyboard-shortcuts'; -class ControlPointKeyboardMove extends Component { - constructor() { - super( ...arguments ); - this.increase = this.increase.bind( this ); - this.decrease = this.decrease.bind( this ); - this.shortcuts = { - right: this.increase, - left: this.decrease, - }; - } - increase( event ) { - // Stop propagation of the key press event to avoid focus moving - // to another editor area. - event.stopPropagation(); - const { gradientIndex, onChange, gradientAST } = this.props; - onChange( - getGradientWithPositionAtIndexIncreased( - gradientAST, - gradientIndex - ) - ); - } +function ControlPointKeyboardMove( { value: position, onChange, children } ) { + const shortcuts = { + right( event ) { + // Stop propagation of the key press event to avoid focus moving + // to another editor area. + event.stopPropagation(); + const newPosition = clampPercent( + position + KEYBOARD_CONTROL_POINT_VARIATION + ); + onChange( newPosition ); + }, + left( event ) { + // Stop propagation of the key press event to avoid focus moving + // to another editor area. + event.stopPropagation(); + const newPosition = clampPercent( + position - KEYBOARD_CONTROL_POINT_VARIATION + ); + onChange( newPosition ); + }, + }; - decrease( event ) { - // Stop propagation of the key press event to avoid focus moving - // to another editor area. - event.stopPropagation(); - const { gradientIndex, onChange, gradientAST } = this.props; - onChange( - getGradientWithPositionAtIndexDecreased( - gradientAST, - gradientIndex - ) - ); - } - render() { - const { children } = this.props; - return ( - - { children } - - ); - } + return ( + + { children } + + ); } function ControlPointButton( { @@ -83,23 +70,17 @@ function ControlPointButton( { position, color, onChange, - gradientIndex, - gradientAST, ...additionalProps } ) { const instanceId = useInstanceId( ControlPointButton ); const descriptionId = `components-custom-gradient-picker__control-point-button-description-${ instanceId }`; return ( - +