| 1 |
const { __ } = wp.i18n; |
| 2 |
const { RangeControl } = wp.components; |
| 3 |
|
| 4 |
import WithResBtns from "./responsive-btn"; |
| 5 |
import UnitControl from "../unit-control"; |
| 6 |
|
| 7 |
const ResponsiveRangeController = ({ |
| 8 |
baseLabel, |
| 9 |
controlName, |
| 10 |
resRequiredProps, |
| 11 |
units, |
| 12 |
min, |
| 13 |
max, |
| 14 |
step, |
| 15 |
noUnits, |
| 16 |
}) => { |
| 17 |
const { attributes, setAttributes, resOption } = resRequiredProps; |
| 18 |
|
| 19 |
let desktopSizeUnit; |
| 20 |
let TABsizeUnit; |
| 21 |
let MOBsizeUnit; |
| 22 |
let defaultUnits; |
| 23 |
|
| 24 |
const { |
| 25 |
[`${controlName}Range`]: desktopRange, |
| 26 |
[`TAB${controlName}Range`]: TABrange, |
| 27 |
[`MOB${controlName}Range`]: MOBrange, |
| 28 |
} = attributes; |
| 29 |
|
| 30 |
if (!noUnits) { |
| 31 |
desktopSizeUnit = attributes[`${controlName}Unit`]; |
| 32 |
TABsizeUnit = attributes[`TAB${controlName}Unit`]; |
| 33 |
MOBsizeUnit = attributes[`MOB${controlName}Unit`]; |
| 34 |
defaultUnits = [ |
| 35 |
{ label: "px", value: "px" }, |
| 36 |
{ label: "em", value: "em" }, |
| 37 |
{ label: "%", value: "%" }, |
| 38 |
]; |
| 39 |
} |
| 40 |
|
| 41 |
return ( |
| 42 |
<div className="responsiveRangeControllerWrapper"> |
| 43 |
{noUnits ? ( |
| 44 |
<> |
| 45 |
{resOption == "Desktop" && ( |
| 46 |
<WithResBtns |
| 47 |
noUnits={noUnits} |
| 48 |
label={baseLabel} |
| 49 |
resRequiredProps={resRequiredProps} |
| 50 |
controlName={controlName} |
| 51 |
> |
| 52 |
<RangeControl |
| 53 |
value={desktopRange} |
| 54 |
onChange={(desktopRange) => |
| 55 |
setAttributes({ |
| 56 |
[`${controlName}Range`]: desktopRange, |
| 57 |
}) |
| 58 |
} |
| 59 |
step={step || 1} |
| 60 |
min={min || 0} |
| 61 |
max={max || 100} |
| 62 |
/> |
| 63 |
</WithResBtns> |
| 64 |
)} |
| 65 |
{resOption == "Tablet" && ( |
| 66 |
<WithResBtns |
| 67 |
noUnits={noUnits} |
| 68 |
label={baseLabel} |
| 69 |
resRequiredProps={resRequiredProps} |
| 70 |
controlName={controlName} |
| 71 |
> |
| 72 |
<RangeControl |
| 73 |
value={TABrange} |
| 74 |
onChange={(TABrange) => |
| 75 |
setAttributes({ |
| 76 |
[`TAB${controlName}Range`]: TABrange, |
| 77 |
}) |
| 78 |
} |
| 79 |
step={step || 1} |
| 80 |
min={min || 0} |
| 81 |
max={max || 100} |
| 82 |
/> |
| 83 |
</WithResBtns> |
| 84 |
)} |
| 85 |
{resOption == "Mobile" && ( |
| 86 |
<WithResBtns |
| 87 |
noUnits={noUnits} |
| 88 |
label={baseLabel} |
| 89 |
resRequiredProps={resRequiredProps} |
| 90 |
controlName={controlName} |
| 91 |
> |
| 92 |
<RangeControl |
| 93 |
value={MOBrange} |
| 94 |
onChange={(MOBrange) => |
| 95 |
setAttributes({ |
| 96 |
[`MOB${controlName}Range`]: MOBrange, |
| 97 |
}) |
| 98 |
} |
| 99 |
step={step || 1} |
| 100 |
min={min || 0} |
| 101 |
max={max || 100} |
| 102 |
/> |
| 103 |
</WithResBtns> |
| 104 |
)} |
| 105 |
</> |
| 106 |
) : ( |
| 107 |
<> |
| 108 |
{resOption == "Desktop" && ( |
| 109 |
<> |
| 110 |
<UnitControl |
| 111 |
selectedUnit={desktopSizeUnit} |
| 112 |
unitTypes={units || defaultUnits} |
| 113 |
onClick={(desktopSizeUnit) => |
| 114 |
setAttributes({ [`${controlName}Unit`]: desktopSizeUnit }) |
| 115 |
} |
| 116 |
/> |
| 117 |
<WithResBtns |
| 118 |
label={baseLabel} |
| 119 |
resRequiredProps={resRequiredProps} |
| 120 |
controlName={controlName} |
| 121 |
> |
| 122 |
<RangeControl |
| 123 |
value={desktopRange} |
| 124 |
onChange={(desktopRange) => |
| 125 |
setAttributes({ |
| 126 |
[`${controlName}Range`]: desktopRange, |
| 127 |
}) |
| 128 |
} |
| 129 |
step={desktopSizeUnit === "em" ? 0.1 : step} |
| 130 |
min={desktopSizeUnit === "px" ? min : 0} |
| 131 |
max={desktopSizeUnit === "px" ? max : 100} |
| 132 |
/> |
| 133 |
</WithResBtns> |
| 134 |
</> |
| 135 |
)} |
| 136 |
{resOption == "Tablet" && ( |
| 137 |
<> |
| 138 |
<UnitControl |
| 139 |
selectedUnit={TABsizeUnit} |
| 140 |
unitTypes={units || defaultUnits} |
| 141 |
onClick={(TABsizeUnit) => |
| 142 |
setAttributes({ [`TAB${controlName}Unit`]: TABsizeUnit }) |
| 143 |
} |
| 144 |
/> |
| 145 |
<WithResBtns |
| 146 |
label={baseLabel} |
| 147 |
resRequiredProps={resRequiredProps} |
| 148 |
controlName={controlName} |
| 149 |
> |
| 150 |
<RangeControl |
| 151 |
value={TABrange} |
| 152 |
onChange={(TABrange) => |
| 153 |
setAttributes({ |
| 154 |
[`TAB${controlName}Range`]: TABrange, |
| 155 |
}) |
| 156 |
} |
| 157 |
step={TABsizeUnit === "em" ? 0.1 : step} |
| 158 |
min={TABsizeUnit === "px" ? min : 0} |
| 159 |
max={TABsizeUnit === "px" ? max : 100} |
| 160 |
/> |
| 161 |
</WithResBtns> |
| 162 |
</> |
| 163 |
)} |
| 164 |
{resOption == "Mobile" && ( |
| 165 |
<> |
| 166 |
<UnitControl |
| 167 |
selectedUnit={MOBsizeUnit} |
| 168 |
unitTypes={units || defaultUnits} |
| 169 |
onClick={(MOBsizeUnit) => |
| 170 |
setAttributes({ [`MOB${controlName}Unit`]: MOBsizeUnit }) |
| 171 |
} |
| 172 |
/> |
| 173 |
<WithResBtns |
| 174 |
label={baseLabel} |
| 175 |
resRequiredProps={resRequiredProps} |
| 176 |
controlName={controlName} |
| 177 |
> |
| 178 |
<RangeControl |
| 179 |
value={MOBrange} |
| 180 |
onChange={(MOBrange) => |
| 181 |
setAttributes({ |
| 182 |
[`MOB${controlName}Range`]: MOBrange, |
| 183 |
}) |
| 184 |
} |
| 185 |
step={MOBsizeUnit === "em" ? 0.1 : step} |
| 186 |
min={MOBsizeUnit === "px" ? min : 0} |
| 187 |
max={MOBsizeUnit === "px" ? max : 100} |
| 188 |
/> |
| 189 |
</WithResBtns> |
| 190 |
</> |
| 191 |
)} |
| 192 |
</> |
| 193 |
)} |
| 194 |
</div> |
| 195 |
); |
| 196 |
}; |
| 197 |
|
| 198 |
export default ResponsiveRangeController; |
| 199 |
|