| 1 |
import { |
| 2 |
ButtonGroup, |
| 3 |
Button, |
| 4 |
Tooltip, |
| 5 |
RangeControl, |
| 6 |
__experimentalNumberControl as NumberControl, |
| 7 |
} from '@wordpress/components'; |
| 8 |
import ResponsiveToggle from '../responsive-toggle'; |
| 9 |
import { __, sprintf } from '@wordpress/i18n'; |
| 10 |
import styles from './editor.lazy.scss'; |
| 11 |
import React, { useLayoutEffect } from 'react'; |
| 12 |
import { limitMax, limitMin } from '@Controls/unitWiseMinMaxOption'; |
| 13 |
import UAGReset from '../reset'; |
| 14 |
|
| 15 |
const isNumberControlSupported = !! NumberControl; |
| 16 |
|
| 17 |
const Range = ( props ) => { |
| 18 |
// Add and remove the CSS on the drop and remove of the component. |
| 19 |
useLayoutEffect( () => { |
| 20 |
styles.use(); |
| 21 |
return () => { |
| 22 |
styles.unuse(); |
| 23 |
}; |
| 24 |
}, [] ); |
| 25 |
|
| 26 |
const { withInputField, isShiftStepEnabled } = props; |
| 27 |
|
| 28 |
let max = limitMax( props.unit?.value, props ); |
| 29 |
let min = limitMin( props.unit?.value, props ); |
| 30 |
const inputValue = isNaN( props?.value ) ? '' : props?.value; |
| 31 |
|
| 32 |
let unitSizes = [ |
| 33 |
{ |
| 34 |
name: __( 'Pixel', 'ultimate-addons-for-gutenberg' ), |
| 35 |
unitValue: 'px', |
| 36 |
}, |
| 37 |
{ |
| 38 |
name: __( 'Em', 'ultimate-addons-for-gutenberg' ), |
| 39 |
unitValue: 'em', |
| 40 |
}, |
| 41 |
]; |
| 42 |
|
| 43 |
if ( props.units ) { |
| 44 |
unitSizes = props.units; |
| 45 |
} |
| 46 |
|
| 47 |
const handleOnChange = ( newValue ) => { |
| 48 |
const parsedValue = parseFloat( newValue ); |
| 49 |
if ( props.setAttributes ) { |
| 50 |
props.setAttributes( { |
| 51 |
[ props.data.label ]: parsedValue, |
| 52 |
} ) |
| 53 |
} |
| 54 |
if ( props?.onChange ) { |
| 55 |
props.onChange( parsedValue ); |
| 56 |
} |
| 57 |
}; |
| 58 |
|
| 59 |
const resetValues = ( defaultValues ) => { |
| 60 |
|
| 61 |
if ( props?.onChange ) { |
| 62 |
props?.onChange( defaultValues[props?.data?.label] ) |
| 63 |
} |
| 64 |
if ( props.displayUnit ) { |
| 65 |
onChangeUnits( defaultValues[props?.unit?.label] ) |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
const onChangeUnits = ( newValue ) => { |
| 70 |
|
| 71 |
props.setAttributes( { [ props.unit.label ]: newValue } ); |
| 72 |
|
| 73 |
max = limitMax( newValue, props ); |
| 74 |
min = limitMin( newValue, props ); |
| 75 |
|
| 76 |
if ( props.value > max ) { |
| 77 |
handleOnChange( max ); |
| 78 |
} |
| 79 |
if ( props.value < min ) { |
| 80 |
handleOnChange( min ); |
| 81 |
} |
| 82 |
|
| 83 |
}; |
| 84 |
|
| 85 |
const onUnitSizeClick = ( uSizes ) => { |
| 86 |
const items = []; |
| 87 |
uSizes.map( ( key ) => |
| 88 |
items.push( |
| 89 |
<Tooltip |
| 90 |
text={ sprintf( |
| 91 |
/* translators: abbreviation for units */ |
| 92 |
__( '%s units', 'ultimate-addons-for-gutenberg' ), |
| 93 |
key.name |
| 94 |
) } |
| 95 |
key={key.name} |
| 96 |
> |
| 97 |
<Button |
| 98 |
key={ key.unitValue } |
| 99 |
className={ 'uagb-range-control__units--' + key.name } |
| 100 |
isSmall |
| 101 |
isPrimary={ props.unit.value === key.unitValue } |
| 102 |
isSecondary={ props.unit.value !== key.unitValue } |
| 103 |
aria-pressed={ props.unit.value === key.unitValue } |
| 104 |
aria-label={ sprintf( |
| 105 |
/* translators: abbreviation for units */ |
| 106 |
__( '%s units', 'ultimate-addons-for-gutenberg' ), |
| 107 |
key.name |
| 108 |
) } |
| 109 |
onClick={ () => onChangeUnits( key.unitValue ) } |
| 110 |
> |
| 111 |
{ key.unitValue } |
| 112 |
</Button> |
| 113 |
</Tooltip> |
| 114 |
) |
| 115 |
); |
| 116 |
|
| 117 |
return items; |
| 118 |
}; |
| 119 |
|
| 120 |
return ( |
| 121 |
<div className="components-base-control uag-range-control uagb-size-type-field-tabs"> |
| 122 |
<div className="uagb-control__header"> |
| 123 |
<ResponsiveToggle |
| 124 |
label= { props.label } |
| 125 |
responsive= { props.responsive } |
| 126 |
/> |
| 127 |
<div className="uagb-range-control__actions uagb-control__actions"> |
| 128 |
<UAGReset |
| 129 |
onReset={resetValues} |
| 130 |
attributeNames = {[ |
| 131 |
props.data.label, |
| 132 |
props.displayUnit ? props.unit.label : false |
| 133 |
]} |
| 134 |
setAttributes={ props.setAttributes } |
| 135 |
/> |
| 136 |
{ props.displayUnit && ( |
| 137 |
<ButtonGroup |
| 138 |
className="uagb-control__units" |
| 139 |
aria-label={ __( |
| 140 |
'Select Units', |
| 141 |
'ultimate-addons-for-gutenberg' |
| 142 |
) } |
| 143 |
> |
| 144 |
{ onUnitSizeClick( unitSizes ) } |
| 145 |
</ButtonGroup> |
| 146 |
) } |
| 147 |
</div> |
| 148 |
</div> |
| 149 |
<div className="uagb-range-control__mobile-controls"> |
| 150 |
<RangeControl |
| 151 |
value={ inputValue } |
| 152 |
onChange={ handleOnChange } |
| 153 |
withInputField={ false } |
| 154 |
allowReset={ false } |
| 155 |
max={ max } |
| 156 |
min={ min } |
| 157 |
step={ props?.step || 1 } |
| 158 |
initialPosition = {inputValue} |
| 159 |
/> |
| 160 |
{ withInputField && isNumberControlSupported && ( |
| 161 |
<NumberControl |
| 162 |
disabled={ props.disabled } |
| 163 |
isShiftStepEnabled={ isShiftStepEnabled } |
| 164 |
max={ max } |
| 165 |
min={ min } |
| 166 |
onChange={ handleOnChange } |
| 167 |
value={ inputValue } |
| 168 |
step={ props?.step || 1 } |
| 169 |
/> |
| 170 |
) } |
| 171 |
</div> |
| 172 |
{ props.help && ( |
| 173 |
<p className="uag-control-help-notice">{ props.help }</p> |
| 174 |
) } |
| 175 |
</div> |
| 176 |
); |
| 177 |
}; |
| 178 |
|
| 179 |
Range.defaultProps = { |
| 180 |
label: __( 'Margin', 'ultimate-addons-for-gutenberg' ), |
| 181 |
className: '', |
| 182 |
allowReset: true, |
| 183 |
withInputField: true, |
| 184 |
isShiftStepEnabled: true, |
| 185 |
max: Infinity, |
| 186 |
min: -Infinity, |
| 187 |
resetFallbackValue: '', |
| 188 |
placeholder: null, |
| 189 |
unit: [ 'px', 'em' ], |
| 190 |
displayUnit: true, |
| 191 |
responsive: false, |
| 192 |
}; |
| 193 |
|
| 194 |
export default Range; |
| 195 |
|