| 1 |
import { RangeControl } from "@wordpress/components"; |
| 2 |
import "./editor.scss"; |
| 3 |
import { memo, useState, useEffect } from "@wordpress/element"; |
| 4 |
import { useDeviceType } from "../../controls/controls"; |
| 5 |
import Responsive from "../responsive/responsive"; |
| 6 |
import Units from "./units"; |
| 7 |
import ResetButton from "./resetBtn"; |
| 8 |
import { priceLink } from "../../blocks/shared/helpFn"; |
| 9 |
|
| 10 |
let setClearTimeOut = ""; |
| 11 |
const SPRangeControl = ({ |
| 12 |
attributes, |
| 13 |
attributesKey, |
| 14 |
setAttributes = () => {}, |
| 15 |
label, |
| 16 |
onValueChange = false, |
| 17 |
onUnitChange = false, |
| 18 |
units, |
| 19 |
resetIcon = true, |
| 20 |
min = 0, |
| 21 |
max = 200, |
| 22 |
step = 1, |
| 23 |
defaultValue = {}, |
| 24 |
className = "", |
| 25 |
helpText = "", |
| 26 |
typoLineHeight = false, |
| 27 |
customValue = "", |
| 28 |
setCustomReset = false, |
| 29 |
responsiveIcon = false, |
| 30 |
pro = false, |
| 31 |
}) => { |
| 32 |
const [eventLoad, setEventLoad] = useState(false); |
| 33 |
const deviceType = useDeviceType(); |
| 34 |
|
| 35 |
// Ranger single value and multiple device value. |
| 36 |
const value = attributes?.device |
| 37 |
? attributes?.device?.[deviceType] |
| 38 |
: "number" === typeof attributes?.value |
| 39 |
? attributes?.value |
| 40 |
: attributes; |
| 41 |
|
| 42 |
const [currentValue, setCurrentValue] = useState(value); |
| 43 |
|
| 44 |
useEffect(() => { |
| 45 |
setCurrentValue(value); |
| 46 |
}, [deviceType]); |
| 47 |
|
| 48 |
const setRangerAttributes = (newValue) => { |
| 49 |
// It's multiple device (desktop/tablet/mobile). |
| 50 |
if ( onValueChange ) { |
| 51 |
return onValueChange({ value: newValue, deviceType: deviceType }); |
| 52 |
} |
| 53 |
if (attributes.device) { |
| 54 |
setAttributes({ |
| 55 |
[attributesKey]: { |
| 56 |
...attributes, |
| 57 |
device: { |
| 58 |
...attributes.device, |
| 59 |
[deviceType]: newValue, |
| 60 |
}, |
| 61 |
}, |
| 62 |
}); |
| 63 |
} |
| 64 |
// It's single device (desktop) |
| 65 |
if ("object" === typeof attributes) { |
| 66 |
if ("number" === typeof attributes.value) { |
| 67 |
setAttributes({ |
| 68 |
[attributesKey]: { |
| 69 |
...attributes, |
| 70 |
value: newValue, |
| 71 |
}, |
| 72 |
}); |
| 73 |
} |
| 74 |
} else { |
| 75 |
setAttributes({ [attributesKey]: newValue }); |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
// Ranger value set function. |
| 80 |
|
| 81 |
// Fixed useEffect for state sync |
| 82 |
useEffect(() => { |
| 83 |
const newValue = attributes?.device ? attributes.device?.[deviceType] : (attributes?.value ?? attributes); |
| 84 |
setCurrentValue(newValue); |
| 85 |
}, [deviceType, attributes]); // Now watches both |
| 86 |
|
| 87 |
useEffect(() => { |
| 88 |
if (attributes?.unit === "%" || attributes?.unit?.[deviceType] === "%") { |
| 89 |
const updateValue = currentValue > 100 ? 100 : currentValue; |
| 90 |
setRangerAttributes(updateValue); |
| 91 |
setCurrentValue(updateValue); |
| 92 |
} |
| 93 |
}, [attributes?.unit?.[deviceType]]); |
| 94 |
|
| 95 |
const setValue = (newValue) => { |
| 96 |
setCurrentValue(newValue); |
| 97 |
|
| 98 |
clearTimeout(setClearTimeOut); |
| 99 |
setClearTimeOut = setTimeout(() => { |
| 100 |
setRangerAttributes(newValue); |
| 101 |
}, 100); |
| 102 |
}; |
| 103 |
|
| 104 |
// Active Label. |
| 105 |
const activeLabel = (e) => { |
| 106 |
const input = e.target.parentNode.parentNode.parentNode; |
| 107 |
const inputId = input.querySelector("input").getAttribute("id"); |
| 108 |
e.target.setAttribute("for", inputId); |
| 109 |
setEventLoad(eventLoad); |
| 110 |
}; |
| 111 |
|
| 112 |
return ( |
| 113 |
<> |
| 114 |
<div |
| 115 |
className={`sp-smart-post-range-control sp-smart-post-component-mb${ |
| 116 |
!attributes?.device && !units ? " sp-negative-space" : "" |
| 117 |
} ${className} ${pro ? "sp-pro-ranger" : ""}`} |
| 118 |
> |
| 119 |
<div className="sp-smart-post-header-control"> |
| 120 |
<div className="sp-smart-post-header-control-left"> |
| 121 |
<span onClick={(e) => activeLabel(e)} className="sp-smart-post-component-title"> |
| 122 |
{label} |
| 123 |
{pro && <a target="_blank" href={priceLink} className="sp-smart-pro-text"> (Pro) </a>} |
| 124 |
</span> |
| 125 |
{( attributes?.device || responsiveIcon ) && <Responsive />} |
| 126 |
</div> |
| 127 |
<div className="sp-smart-post-header-control-right"> |
| 128 |
{resetIcon && ( |
| 129 |
<ResetButton |
| 130 |
attributes={attributes} |
| 131 |
setAttributes={setAttributes} |
| 132 |
attributesKey={attributesKey} |
| 133 |
defaultValue={defaultValue} |
| 134 |
value={value} |
| 135 |
setCurrentValue={setCurrentValue} |
| 136 |
setCustomReset={setCustomReset} |
| 137 |
/> |
| 138 |
)} |
| 139 |
{units && ( |
| 140 |
<Units |
| 141 |
attributes={attributes} |
| 142 |
setAttributes={setAttributes} |
| 143 |
attributesKey={attributesKey} |
| 144 |
units={units} |
| 145 |
onUnitChange={onUnitChange ? onUnitChange : false} |
| 146 |
/> |
| 147 |
)} |
| 148 |
</div> |
| 149 |
</div> |
| 150 |
|
| 151 |
<RangeControl |
| 152 |
value={customValue ? customValue : currentValue} |
| 153 |
color="var(--sp-smart-primary-2-400)" |
| 154 |
onChange={(newValue) => |
| 155 |
onValueChange |
| 156 |
? onValueChange({ |
| 157 |
value: newValue, |
| 158 |
deviceType: deviceType, |
| 159 |
}) |
| 160 |
: setValue(newValue) |
| 161 |
} |
| 162 |
min={min} |
| 163 |
max={ |
| 164 |
"object" === typeof attributes?.unit && "%" === attributes?.unit?.[deviceType] |
| 165 |
? typoLineHeight |
| 166 |
? 200 |
| 167 |
: 100 |
| 168 |
: max |
| 169 |
} |
| 170 |
step={step} |
| 171 |
help={helpText} |
| 172 |
__nextHasNoMarginBottom={true} |
| 173 |
__next40pxDefaultSize |
| 174 |
/> |
| 175 |
</div> |
| 176 |
</> |
| 177 |
); |
| 178 |
}; |
| 179 |
|
| 180 |
export default memo(SPRangeControl); |
| 181 |
|