| 1 |
import { __ } from "@wordpress/i18n"; |
| 2 |
import { useState, memo } from "@wordpress/element"; |
| 3 |
import SpColorPicker from "../color/color"; |
| 4 |
import SPToggleGroupControl from "../toggleGroupControl/toggleGroupControl"; |
| 5 |
import { DashedStyle, DottedStyle, DoubleStyle, SolidStyle } from "../../icons/icons"; |
| 6 |
import "./editor.scss"; |
| 7 |
import Spacing from "../spacing/spacing"; |
| 8 |
import { useDeviceType } from "../../controls/controls"; |
| 9 |
|
| 10 |
const Border = ({ |
| 11 |
label = "Border", |
| 12 |
attributes, |
| 13 |
setAttributes, |
| 14 |
attributesKey, |
| 15 |
units = ["px", "%", "em"], |
| 16 |
btnType = false, |
| 17 |
defaultValue = {unit: "px", value: { top: 0, right: 0, bottom: 0, left: 0 } }, |
| 18 |
defaultColor = "#fff", |
| 19 |
onStateUpdate = false, |
| 20 |
}) => { |
| 21 |
const { border, borderWidth } = attributes; |
| 22 |
const [buttonTab, setButtonTab] = useState("normal"); |
| 23 |
const deviceType = useDeviceType(); |
| 24 |
|
| 25 |
const borderColorOptions = [ |
| 26 |
{ label: "Normal", value: "normal" }, |
| 27 |
...(attributes.border.hoverColor || |
| 28 |
attributes.border.hoverColor === "" || |
| 29 |
attributes.border.hover || |
| 30 |
attributes.border.hover === "" |
| 31 |
? [{ label: "Hover", value: "hover" }] |
| 32 |
: []), |
| 33 |
...(attributes.border.activeColor || |
| 34 |
attributes.border.activeColor === "" || |
| 35 |
attributes.border.active || |
| 36 |
attributes.border.active === "" |
| 37 |
? [{ label: "Active", value: "activeColor" }] |
| 38 |
: []), |
| 39 |
]; |
| 40 |
|
| 41 |
const hoverColor = () => { |
| 42 |
if (border?.hoverColor !== undefined) { |
| 43 |
return "hoverColor"; |
| 44 |
} |
| 45 |
if (border?.hover !== undefined) { |
| 46 |
return "hover"; |
| 47 |
} |
| 48 |
if (border?.activeColor !== undefined) { |
| 49 |
return "activeColor"; |
| 50 |
} |
| 51 |
return "active"; |
| 52 |
}; |
| 53 |
|
| 54 |
const borderStyleString = () => { |
| 55 |
if (btnType && btnType !== "normal") { |
| 56 |
return "hoverStyle"; |
| 57 |
} |
| 58 |
return "style"; |
| 59 |
}; |
| 60 |
const updateBorderWithHandler = (newValue) => { |
| 61 |
if (onStateUpdate) { |
| 62 |
onStateUpdate(attributesKey.borderWidth, newValue); |
| 63 |
} |
| 64 |
return false; |
| 65 |
}; |
| 66 |
const updateSpacingUtilityHandler = (type, newUtility) => { |
| 67 |
if (onStateUpdate) { |
| 68 |
onStateUpdate(attributesKey.borderWidth, { |
| 69 |
...attributes.borderWidth, |
| 70 |
[type]: newUtility, |
| 71 |
}); |
| 72 |
} else { |
| 73 |
const updateAttr = |
| 74 |
type === "allChange" |
| 75 |
? { ...attributes.borderWidth, [type]: newUtility } |
| 76 |
: { |
| 77 |
...attributes.borderWidth, |
| 78 |
[type]: { ...attributes?.borderWidth?.unit, [deviceType]: newUtility }, |
| 79 |
}; |
| 80 |
setAttributes({ [attributesKey.borderWidth]: updateAttr }); |
| 81 |
} |
| 82 |
}; |
| 83 |
|
| 84 |
return ( |
| 85 |
<> |
| 86 |
<div className="sp-smart-post-border-control sp-smart-post-component-mb "> |
| 87 |
<div className="sp-smart-post-header-control sp-mb-8px"> |
| 88 |
<span className="sp-smart-post-component-title">{label}</span> |
| 89 |
</div> |
| 90 |
<div |
| 91 |
className={`sp-smart-post-border-style-wrapper${ |
| 92 |
!["none", ""].includes(border?.[borderStyleString()]) ? " sp-smart-post-component-mb" : "" |
| 93 |
}`} |
| 94 |
> |
| 95 |
<SPToggleGroupControl |
| 96 |
attributes={border?.[borderStyleString()]} |
| 97 |
onClick={(newValue) => |
| 98 |
onStateUpdate |
| 99 |
? onStateUpdate(attributesKey.border, { ...border, [borderStyleString()]: newValue }) |
| 100 |
: setAttributes({ |
| 101 |
[attributesKey.border]: { |
| 102 |
...border, |
| 103 |
[borderStyleString()]: newValue, |
| 104 |
}, |
| 105 |
}) |
| 106 |
} |
| 107 |
items={[ |
| 108 |
{ label: "None", value: "none" }, |
| 109 |
{ label: <SolidStyle />, value: "solid" }, |
| 110 |
{ label: <DashedStyle />, value: "dashed" }, |
| 111 |
{ label: <DottedStyle />, value: "dotted" }, |
| 112 |
{ label: <DoubleStyle />, value: "double" }, |
| 113 |
]} |
| 114 |
/> |
| 115 |
</div> |
| 116 |
{!["none", ""].includes(border?.[borderStyleString()]) && ( |
| 117 |
<> |
| 118 |
<div className="sp-smart-post-component-mb"> |
| 119 |
<Spacing |
| 120 |
key={borderStyleString()} |
| 121 |
label={"Border Width"} |
| 122 |
attributes={borderWidth} |
| 123 |
attributesKey={attributesKey.borderWidth} |
| 124 |
setAttributes={setAttributes} |
| 125 |
units={units} |
| 126 |
rangeStep={0.5} |
| 127 |
min={0} |
| 128 |
max={10} |
| 129 |
rangeMax={2} |
| 130 |
defaultValue={defaultValue} |
| 131 |
onChange={onStateUpdate ? updateBorderWithHandler : false} |
| 132 |
onUnitChange={(newValue) => updateSpacingUtilityHandler("unit", newValue)} |
| 133 |
updateAllChange={(newValue) => updateSpacingUtilityHandler("allChange", newValue)} |
| 134 |
/> |
| 135 |
</div> |
| 136 |
{!btnType && ( |
| 137 |
<SPToggleGroupControl |
| 138 |
attributes={buttonTab} |
| 139 |
items={borderColorOptions} |
| 140 |
onClick={(newValue) => setButtonTab(newValue)} |
| 141 |
/> |
| 142 |
)} |
| 143 |
{/* {[btnType, buttonTab].includes("normal") && ( */} |
| 144 |
{((btnType && btnType === "normal") || (!btnType && buttonTab === "normal")) && ( |
| 145 |
<SpColorPicker |
| 146 |
label={__("Border Color", "post-carousel")} |
| 147 |
value={border?.color} |
| 148 |
onChange={(newValue) => |
| 149 |
onStateUpdate |
| 150 |
? onStateUpdate(attributesKey.border, { ...border, color: newValue }) |
| 151 |
: setAttributes({ |
| 152 |
[attributesKey.border]: { |
| 153 |
...border, |
| 154 |
color: newValue, |
| 155 |
}, |
| 156 |
}) |
| 157 |
} |
| 158 |
defaultColor={typeof defaultColor === "object" ? defaultColor.color : defaultColor} |
| 159 |
/> |
| 160 |
)} |
| 161 |
{([btnType, buttonTab].includes("hover") || [btnType, buttonTab].includes("activeColor")) && ( |
| 162 |
<SpColorPicker |
| 163 |
label={__("Border Color Hover", "post-carousel")} |
| 164 |
value={border?.[hoverColor()]} |
| 165 |
onChange={(newValue) => |
| 166 |
onStateUpdate |
| 167 |
? onStateUpdate(attributesKey.border, { ...border, hoverColor: newValue }) |
| 168 |
: setAttributes({ |
| 169 |
[attributesKey.border]: { |
| 170 |
...border, |
| 171 |
[hoverColor()]: newValue, |
| 172 |
}, |
| 173 |
}) |
| 174 |
} |
| 175 |
defaultColor={typeof defaultColor === "object" ? defaultColor.hover : defaultColor} |
| 176 |
/> |
| 177 |
)} |
| 178 |
</> |
| 179 |
)} |
| 180 |
</div> |
| 181 |
</> |
| 182 |
); |
| 183 |
}; |
| 184 |
|
| 185 |
export default memo(Border); |
| 186 |
|