|
1 |
| -import { useBlockProps } from '@wordpress/block-editor'; |
| 1 | +/* eslint-disable @wordpress/no-unsafe-wp-apis */ |
| 2 | +import { |
| 3 | + useBlockProps, |
| 4 | + InspectorControls, |
| 5 | + __experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown, |
| 6 | + __experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients, |
| 7 | +} from '@wordpress/block-editor'; |
| 8 | +/* eslint-enable @wordpress/no-unsafe-wp-apis */ |
| 9 | +import { SVG, Path } from '@wordpress/components'; |
| 10 | +import { __ } from '@wordpress/i18n'; |
| 11 | +import clsx from 'clsx'; |
2 | 12 | import StepControls from '../shared/components/form-step-controls';
|
3 | 13 | import useParentFormClientId from '../shared/hooks/use-parent-form-client-id';
|
4 |
| -import useStepNavigation from '../shared/hooks/use-step-navigation'; |
| 14 | +import { calculateProgressPercentage } from '../shared/util/progress-calculation'; |
5 | 15 |
|
6 |
| -import './editor.scss'; |
| 16 | +import './style.scss'; |
7 | 17 |
|
8 |
| -const FormProgressIndicatorEdit = ( { clientId } ) => { |
| 18 | +const FormProgressIndicatorEdit = ( { clientId, context, attributes, setAttributes } ) => { |
9 | 19 | const parentFormId = useParentFormClientId( clientId );
|
10 |
| - const { currentStepInfo, steps } = useStepNavigation( parentFormId ); |
11 | 20 |
|
12 |
| - let progress = steps.length ? ( ( currentStepInfo.index + 1 ) / steps.length ) * 100 : 10; |
13 |
| - if ( currentStepInfo.index === -1 && steps.length > 0 ) { |
14 |
| - progress = ( 1 / steps.length ) * 100; // Assume the first step is active |
15 |
| - } |
| 21 | + // Get data from context - provide mock data for previews when context is missing |
| 22 | + const steps = context?.[ 'jetpack/form-steps' ] || [ 1, 2, 3 ]; |
| 23 | + const currentStepInfo = context?.[ 'jetpack/form-current-step' ] || { index: 0 }; |
| 24 | + |
| 25 | + // Extract attributes |
| 26 | + const { variant = 'line', progressColor, progressBackgroundColor, textColor, style } = attributes; |
16 | 27 |
|
17 |
| - const blockProps = useBlockProps(); |
| 28 | + // Get WordPress color/gradient settings for the color panel |
| 29 | + const colorGradientSettings = useMultipleOriginColorsAndGradients(); |
18 | 30 |
|
19 |
| - // Only need to set width – colours come from core style engine variables. |
20 |
| - const progressBarStyle = { |
21 |
| - width: `${ progress }%`, |
| 31 | + // Build style object with CSS custom properties for colors |
| 32 | + const colorStyles = { |
| 33 | + '--jp-progress-active-color': progressColor, |
| 34 | + '--jp-progress-track-color': progressBackgroundColor, |
| 35 | + // Text color comes from standard color support |
| 36 | + '--jp-progress-text-color': textColor || style?.color?.text, |
22 | 37 | };
|
23 | 38 |
|
| 39 | + const blockProps = useBlockProps( { |
| 40 | + style: colorStyles, |
| 41 | + className: `is-variant-${ variant }`, |
| 42 | + } ); |
| 43 | + const isDotStyle = variant === 'dots'; |
| 44 | + |
| 45 | + // Use shared progress calculation logic |
| 46 | + const currentStep = currentStepInfo.index + 1; |
| 47 | + let progressPercentage = calculateProgressPercentage( currentStep, steps.length, isDotStyle ); |
| 48 | + |
| 49 | + // Show 25% progress in "All steps" view for line style to preview the bar |
| 50 | + if ( ! isDotStyle && currentStepInfo.index === -1 && steps.length > 0 ) { |
| 51 | + progressPercentage = 25; |
| 52 | + } |
| 53 | + |
24 | 54 | return (
|
25 | 55 | <>
|
26 |
| - <div className="jetpack-form-progress-indicator--wrapper"> |
27 |
| - <div { ...blockProps }> |
28 |
| - <div className="jetpack-form-progress-indicator-bar" style={ progressBarStyle }></div> |
| 56 | + <InspectorControls group="color"> |
| 57 | + <ColorGradientSettingsDropdown |
| 58 | + panelId={ clientId } |
| 59 | + settings={ [ |
| 60 | + { |
| 61 | + colorValue: progressColor, |
| 62 | + onColorChange: color => setAttributes( { progressColor: color } ), |
| 63 | + label: __( 'Progress color', 'jetpack-forms' ), |
| 64 | + }, |
| 65 | + { |
| 66 | + colorValue: progressBackgroundColor, |
| 67 | + onColorChange: color => setAttributes( { progressBackgroundColor: color } ), |
| 68 | + label: __( 'Track color', 'jetpack-forms' ), |
| 69 | + }, |
| 70 | + ] } |
| 71 | + { ...colorGradientSettings } |
| 72 | + /> |
| 73 | + </InspectorControls> |
| 74 | + <div { ...blockProps }> |
| 75 | + <div className="jetpack-form-progress-indicator-steps"> |
| 76 | + { steps.map( ( step, index ) => { |
| 77 | + const isActive = index === currentStepInfo.index; |
| 78 | + const isCompleted = index < currentStepInfo.index; |
| 79 | + |
| 80 | + return ( |
| 81 | + <div |
| 82 | + key={ index } |
| 83 | + className={ clsx( 'jetpack-form-progress-indicator-step', { |
| 84 | + 'is-active': isActive, |
| 85 | + 'is-completed': isCompleted, |
| 86 | + } ) } |
| 87 | + data-step-index={ index } |
| 88 | + > |
| 89 | + <div className="jetpack-form-progress-indicator-line"></div> |
| 90 | + { isDotStyle && ( |
| 91 | + <div className="jetpack-form-progress-indicator-dot"> |
| 92 | + <span className="jetpack-form-progress-indicator-step-number"> |
| 93 | + { isCompleted ? ( |
| 94 | + <SVG |
| 95 | + width="24" |
| 96 | + height="24" |
| 97 | + viewBox="0 0 24 24" |
| 98 | + xmlns="http://www.w3.org/2000/svg" |
| 99 | + > |
| 100 | + <Path |
| 101 | + d="M16.7 7.1l-6.3 8.5-3.3-2.5-.9 1.2 4.5 3.4L17.9 8z" |
| 102 | + fill="currentColor" |
| 103 | + /> |
| 104 | + </SVG> |
| 105 | + ) : ( |
| 106 | + index + 1 |
| 107 | + ) } |
| 108 | + </span> |
| 109 | + </div> |
| 110 | + ) } |
| 111 | + </div> |
| 112 | + ); |
| 113 | + } ) } |
| 114 | + <div |
| 115 | + className="jetpack-form-progress-indicator-progress" |
| 116 | + style={ { width: `${ progressPercentage }%` } } |
| 117 | + ></div> |
29 | 118 | </div>
|
30 | 119 | </div>
|
31 | 120 | <StepControls formClientId={ parentFormId } showToggle={ false } showNavigation={ true } />
|
|
0 commit comments