| 1 |
import { Button, RangeControl } from "@wordpress/components"; |
| 2 |
import { __ } from "@wordpress/i18n"; |
| 3 |
import { ResetButton, SpLinkedButton, Units } from "../utility"; |
| 4 |
import Responsive from "../responsive/responsive"; |
| 5 |
import { BorderIcon, LinkedIcon } from "../../icons/icons"; |
| 6 |
import "./editor.scss"; |
| 7 |
import { sideAxisIcons } from "../../controls/constants"; |
| 8 |
import { useDeviceType } from "../../controls/controls"; |
| 9 |
import { memo, useEffect, useState } from "@wordpress/element"; |
| 10 |
|
| 11 |
const SpCustomRanger = ({ |
| 12 |
attr, |
| 13 |
sideKey = "all", |
| 14 |
onChangeHandler, |
| 15 |
rangeStep, |
| 16 |
min, |
| 17 |
max, |
| 18 |
step, |
| 19 |
rangeMax, |
| 20 |
indicator = "", |
| 21 |
selectUnit = "", |
| 22 |
}) => { |
| 23 |
const [showInput, setShowInput] = useState(true); // when user clicks presetControl |
| 24 |
const rangerStep = showInput ? rangeStep : step; |
| 25 |
const iconType = indicator ? `${indicator}-${sideKey}` : sideKey; |
| 26 |
const rangerValue = () => { |
| 27 |
if ( sideKey === "all" ) { |
| 28 |
return Number(attr.top) || ""; |
| 29 |
} |
| 30 |
return Number( attr[sideKey] || "" ); |
| 31 |
}; |
| 32 |
const rangeValue = rangerValue(); |
| 33 |
useEffect(() => { |
| 34 |
if (rangeValue % rangeStep !== 0) { |
| 35 |
setShowInput(false); |
| 36 |
} |
| 37 |
if (rangeMax && rangeValue > rangeMax) { |
| 38 |
setShowInput(false); |
| 39 |
} |
| 40 |
if (typeof rangeValue === "undefined" || rangeValue === "") { |
| 41 |
setShowInput(true); |
| 42 |
} |
| 43 |
}, [step, sideKey, attr]); |
| 44 |
|
| 45 |
return ( |
| 46 |
<div className="sp-smart-range-control-ranger-row"> |
| 47 |
<div className="sp-smart-range-control sp-side-demo-icon"> |
| 48 |
<span className="sp-link-side-icon">{sideAxisIcons[iconType]}</span> |
| 49 |
</div> |
| 50 |
<div className="sp-smart-range-control sp-ranger"> |
| 51 |
<RangeControl |
| 52 |
value={rangeValue} |
| 53 |
onChange={(val) => onChangeHandler(sideKey, val)} |
| 54 |
step={rangerStep} |
| 55 |
min={min} |
| 56 |
max={showInput && rangeMax ? rangeMax : max} |
| 57 |
withInputField={showInput ? false : true} |
| 58 |
marks={showInput ? true : false} |
| 59 |
__nextHasNoMarginBottom |
| 60 |
__next40pxDefaultSize |
| 61 |
renderTooltipContent={(value) => `${value}${selectUnit}`} |
| 62 |
/> |
| 63 |
</div> |
| 64 |
<div className="sp-smart-range-control sp-preset-btn" onClick={() => setShowInput(!showInput)}> |
| 65 |
<BorderIcon isActive={!showInput} /> |
| 66 |
</div> |
| 67 |
</div> |
| 68 |
); |
| 69 |
}; |
| 70 |
|
| 71 |
const Spacing = ({ |
| 72 |
label, |
| 73 |
attributes, |
| 74 |
attributesKey, |
| 75 |
setAttributes = () => {}, |
| 76 |
onChange, |
| 77 |
units = false, |
| 78 |
rangeStep = 8, |
| 79 |
step = 1, |
| 80 |
min = 0, |
| 81 |
max = 300, |
| 82 |
rangeMax = 50, |
| 83 |
resetIcon = true, |
| 84 |
onUnitChange = false, |
| 85 |
// sidesAxis = false, // array [ 'horizontal', 'vertical'] |
| 86 |
indicator = "", |
| 87 |
defaultValue = {unit: "px", value: { top: 0, right: 0, bottom: 0, left: 0 } }, |
| 88 |
customClass = "", |
| 89 |
updateAllChange, |
| 90 |
}) => { |
| 91 |
const deviceType = useDeviceType(); |
| 92 |
const attrValue = () => { |
| 93 |
let attributesValue = {}; |
| 94 |
if ("object" === typeof attributes?.device) { |
| 95 |
attributesValue = attributes?.device?.[deviceType]; |
| 96 |
} else if ("object" === typeof attributes?.value) { |
| 97 |
attributesValue = attributes?.value; |
| 98 |
} else { |
| 99 |
attributesValue = attributes; |
| 100 |
} |
| 101 |
return attributesValue; |
| 102 |
}; |
| 103 |
|
| 104 |
const [simpleAttr, setSimpleAttr] = useState(attrValue()); |
| 105 |
|
| 106 |
const linked = attributes?.allChange || false; |
| 107 |
|
| 108 |
const onChangeHandler = (side, newVal) => { |
| 109 |
let updatedValue = { ...simpleAttr }; |
| 110 |
|
| 111 |
switch (side) { |
| 112 |
case "all": |
| 113 |
updatedValue = { |
| 114 |
top: newVal, |
| 115 |
right: newVal, |
| 116 |
bottom: newVal, |
| 117 |
left: newVal, |
| 118 |
}; |
| 119 |
break; |
| 120 |
default: |
| 121 |
updatedValue[side] = newVal; |
| 122 |
break; |
| 123 |
} |
| 124 |
|
| 125 |
const newAttributes = |
| 126 |
attributes?.device && typeof attributes.device === "object" |
| 127 |
? { |
| 128 |
...attributes, |
| 129 |
device: { |
| 130 |
...attributes.device, |
| 131 |
[deviceType]: updatedValue, |
| 132 |
}, |
| 133 |
} |
| 134 |
: { |
| 135 |
...attributes, |
| 136 |
value: updatedValue, |
| 137 |
}; |
| 138 |
if (onChange) { |
| 139 |
onChange(newAttributes); |
| 140 |
} else { |
| 141 |
setAttributes({ [attributesKey]: newAttributes }); |
| 142 |
} |
| 143 |
setSimpleAttr(updatedValue); |
| 144 |
}; |
| 145 |
const onValueReset = () => { |
| 146 |
const newAttributes = |
| 147 |
attributes?.device && typeof attributes.device === "object" |
| 148 |
? { |
| 149 |
...attributes, |
| 150 |
device: { |
| 151 |
...attributes.device, |
| 152 |
[deviceType]: defaultValue.value, |
| 153 |
}, |
| 154 |
} |
| 155 |
: { |
| 156 |
...attributes, |
| 157 |
value: defaultValue.value, |
| 158 |
}; |
| 159 |
setAttributes({ [attributesKey]: newAttributes }); |
| 160 |
setSimpleAttr(defaultValue.value); |
| 161 |
}; |
| 162 |
useEffect(() => { |
| 163 |
const deviceValue = attributes?.device?.[deviceType]; |
| 164 |
if (deviceValue) { |
| 165 |
if (Object.values(deviceValue).every((side) => side === deviceValue.top)) { |
| 166 |
setAttributes({ |
| 167 |
[attributesKey]: { ...attributes, allChange: true }, |
| 168 |
}); |
| 169 |
} |
| 170 |
} |
| 171 |
}, []); |
| 172 |
|
| 173 |
return ( |
| 174 |
<div |
| 175 |
className={`sp-smart-post-spacing-range-control sp-smart-post-range-control sp-smart-post-component-mb${ |
| 176 |
customClass ? " " + customClass : "" |
| 177 |
}`} |
| 178 |
> |
| 179 |
<div className="sp-smart-post-header-control"> |
| 180 |
<div className="sp-smart-post-header-control-left"> |
| 181 |
<span onClick={(e) => activeLabel(e)} className="sp-smart-post-component-title"> |
| 182 |
{label} |
| 183 |
</span> |
| 184 |
{attributes?.device && <Responsive />} |
| 185 |
</div> |
| 186 |
<div className="sp-smart-post-header-control-right"> |
| 187 |
{resetIcon && <ResetButton onClick={() => onValueReset()} />} |
| 188 |
<SpLinkedButton |
| 189 |
attributes={attributes} |
| 190 |
attributesKey={attributesKey} |
| 191 |
setAttributes={setAttributes} |
| 192 |
Icon={indicator ? sideAxisIcons["radius-all"] : sideAxisIcons.all} |
| 193 |
updateAllChange={updateAllChange ? updateAllChange : false} |
| 194 |
/> |
| 195 |
{units && ( |
| 196 |
<Units |
| 197 |
attributes={attributes} |
| 198 |
setAttributes={setAttributes} |
| 199 |
attributesKey={attributesKey} |
| 200 |
units={units} |
| 201 |
onUnitChange={onUnitChange ? onUnitChange : false} |
| 202 |
/> |
| 203 |
)} |
| 204 |
</div> |
| 205 |
</div> |
| 206 |
{linked && ( |
| 207 |
|
| 208 |
<SpCustomRanger |
| 209 |
key={linked} |
| 210 |
attr={simpleAttr} |
| 211 |
sideKey={"all"} |
| 212 |
onChangeHandler={onChangeHandler} |
| 213 |
min={min} |
| 214 |
max={max} |
| 215 |
rangeStep={rangeStep} |
| 216 |
step={step} |
| 217 |
rangeMax={rangeMax} |
| 218 |
indicator={indicator} |
| 219 |
selectUnit={attributes.unit?.[deviceType]} |
| 220 |
/> |
| 221 |
) } |
| 222 |
{!linked && |
| 223 |
["top", "right", "bottom", "left"].map((side) => ( |
| 224 |
<SpCustomRanger |
| 225 |
key={`${side}${linked}`} |
| 226 |
attr={simpleAttr} |
| 227 |
sideKey={side} |
| 228 |
onChangeHandler={onChangeHandler} |
| 229 |
min={min} |
| 230 |
max={max} |
| 231 |
rangeStep={rangeStep} |
| 232 |
step={step} |
| 233 |
rangeMax={rangeMax} |
| 234 |
indicator={indicator} |
| 235 |
selectUnit={attributes?.unit?.[deviceType] || attributes?.unit} |
| 236 |
/> |
| 237 |
))} |
| 238 |
</div> |
| 239 |
); |
| 240 |
}; |
| 241 |
|
| 242 |
export default memo(Spacing); |
| 243 |
|