| 1 |
import { __ } from "@wordpress/i18n"; |
| 2 |
import { Button, GradientPicker } from "@wordpress/components"; |
| 3 |
import "./editor.scss"; |
| 4 |
import SpColorPicker from "../color/color"; |
| 5 |
import SPRangeControl from "../rangeControl/rangeControl"; |
| 6 |
import MediaPicker from "../mediaUpload/image"; |
| 7 |
import { useSelect } from "@wordpress/data"; |
| 8 |
import { priceLink } from "../../blocks/shared/helpFn"; |
| 9 |
|
| 10 |
const Background = ({ |
| 11 |
attributes, |
| 12 |
attributesKey, |
| 13 |
setAttributes, |
| 14 |
label, |
| 15 |
items, |
| 16 |
transition, |
| 17 |
colorLabel = "Background Color", |
| 18 |
defaultColor, |
| 19 |
colorType, |
| 20 |
imageObj = { imageKey: "", backgroundImage: "" } || {}, |
| 21 |
videoObj = { imageKey: "", backgroundImage: "" } || {}, |
| 22 |
onStateUpdate = false, |
| 23 |
pro = false, |
| 24 |
}) => { |
| 25 |
const { defaultGradients, customGradients } = useSelect((select) => { |
| 26 |
const settings = select("smartpost/global-settings"); |
| 27 |
return { |
| 28 |
customGradients: settings?.getCustomGradients() || [], |
| 29 |
defaultGradients: settings?.getDefaultGradient() || [], |
| 30 |
}; |
| 31 |
}, []); |
| 32 |
|
| 33 |
// Set background type |
| 34 |
const setBgType = (newValue) => { |
| 35 |
if (onStateUpdate && "color" === colorType) { |
| 36 |
onStateUpdate(attributesKey, { ...attributes, color: { ...attributes.color, style: newValue } }); |
| 37 |
} |
| 38 |
if (onStateUpdate && "hover" === colorType) { |
| 39 |
onStateUpdate(attributesKey, { ...attributes, hover: { ...attributes.hover, style: newValue } }); |
| 40 |
} |
| 41 |
if (!onStateUpdate && "color" === colorType) { |
| 42 |
setAttributes({ |
| 43 |
[attributesKey]: { |
| 44 |
...attributes, |
| 45 |
color: { ...attributes.color, style: newValue }, |
| 46 |
}, |
| 47 |
}); |
| 48 |
} |
| 49 |
if (!onStateUpdate && "hover" === colorType) { |
| 50 |
setAttributes({ |
| 51 |
[attributesKey]: { |
| 52 |
...attributes, |
| 53 |
hover: { ...attributes.hover, style: newValue }, |
| 54 |
}, |
| 55 |
}); |
| 56 |
} |
| 57 |
}; |
| 58 |
|
| 59 |
const gradientValue = |
| 60 |
attributes[colorType]?.gradient || |
| 61 |
"linear-gradient(90deg, rgba(0, 0, 0, 0.68) 0.09%, rgba(0, 0, 0, 0.07) 99.95%)"; |
| 62 |
|
| 63 |
return ( |
| 64 |
<> |
| 65 |
<div className={`sp-smart-post-background sp-smart-post-component-mb${pro ? " sp-is-pro" : ""}`}> |
| 66 |
{/* Background type */} |
| 67 |
<div className={`sp-smart-post-background-control sp-smart-post-d-flex sp-smart-post-component-mb`}> |
| 68 |
<span className="sp-smart-post-component-title"> |
| 69 |
{label} |
| 70 |
{pro && <a target="_blank" href={priceLink} className="sp-smart-pro-text">(Pro)</a>} |
| 71 |
</span> |
| 72 |
<div className={`sp-smart-post-background-left`}> |
| 73 |
{items?.map((item, i) => ( |
| 74 |
<Button |
| 75 |
className={attributes[colorType]?.style === item.value ? "active" : ""} |
| 76 |
key={i} |
| 77 |
value={item.value} |
| 78 |
onClick={(e) => setBgType(e.target.closest("button").value)} |
| 79 |
> |
| 80 |
<span>{item.label}</span> |
| 81 |
{<p>{item.tooltip}</p>} |
| 82 |
</Button> |
| 83 |
))} |
| 84 |
</div> |
| 85 |
</div> |
| 86 |
<> |
| 87 |
{"bgColor" === attributes[colorType]?.style && ( |
| 88 |
<> |
| 89 |
<SpColorPicker |
| 90 |
label={colorLabel} |
| 91 |
value={attributes[colorType].solidColor} |
| 92 |
onChange={(newColor) => |
| 93 |
onStateUpdate |
| 94 |
? onStateUpdate(attributesKey, { |
| 95 |
...attributes, |
| 96 |
[colorType]: { |
| 97 |
...attributes[colorType], |
| 98 |
solidColor: newColor, |
| 99 |
}, |
| 100 |
}) |
| 101 |
: setAttributes({ |
| 102 |
[attributesKey]: { |
| 103 |
...attributes, |
| 104 |
[colorType]: { |
| 105 |
...attributes[colorType], |
| 106 |
solidColor: newColor, |
| 107 |
}, |
| 108 |
}, |
| 109 |
}) |
| 110 |
} |
| 111 |
defaultColor={defaultColor} |
| 112 |
/> |
| 113 |
{"hover" === colorType && transition && ( |
| 114 |
<SPRangeControl |
| 115 |
label={__("Transition", "post-carousel")} |
| 116 |
setAttributes={setAttributes} |
| 117 |
attributes={transition.value} |
| 118 |
units={false} |
| 119 |
step={0.1} |
| 120 |
min={0} |
| 121 |
max={30} |
| 122 |
defaultValue={{ unit: "px", value: 0.3 }} |
| 123 |
attributesKey={transition.key} |
| 124 |
/> |
| 125 |
)} |
| 126 |
</> |
| 127 |
)} |
| 128 |
{"gradient" === attributes[colorType]?.style && ( |
| 129 |
<GradientPicker |
| 130 |
// value={ attributes[ colorType ].gradient } |
| 131 |
value={gradientValue} |
| 132 |
gradients={[...(defaultGradients ?? []), ...(customGradients ?? [])]} |
| 133 |
onChange={(newValue) => |
| 134 |
onStateUpdate |
| 135 |
? onStateUpdate(attributesKey, { |
| 136 |
...attributes, |
| 137 |
[colorType]: { |
| 138 |
...attributes[colorType], |
| 139 |
gradient: newValue, |
| 140 |
}, |
| 141 |
}) |
| 142 |
: setAttributes({ |
| 143 |
[attributesKey]: { |
| 144 |
...attributes, |
| 145 |
[colorType]: { |
| 146 |
...attributes[colorType], |
| 147 |
gradient: newValue, |
| 148 |
}, |
| 149 |
}, |
| 150 |
}) |
| 151 |
} |
| 152 |
/> |
| 153 |
)} |
| 154 |
|
| 155 |
{"image" === attributes[colorType]?.style && ( |
| 156 |
<MediaPicker |
| 157 |
label={__("Image", "post-carousel")} |
| 158 |
imageKey={imageObj?.imageKey} |
| 159 |
enableImageSize={false} |
| 160 |
setAttributes={setAttributes} |
| 161 |
backgroundImage={imageObj?.backgroundImage} |
| 162 |
/> |
| 163 |
)} |
| 164 |
{"video" === attributes[colorType]?.style && ( |
| 165 |
<MediaPicker |
| 166 |
label={__("Video", "post-carousel")} |
| 167 |
imageKey={videoObj.imageKey} |
| 168 |
mediaType="video" |
| 169 |
slug="video" |
| 170 |
enableImageSize={false} |
| 171 |
setAttributes={setAttributes} |
| 172 |
backgroundImage={videoObj.backgroundImage} |
| 173 |
/> |
| 174 |
)} |
| 175 |
</> |
| 176 |
</div> |
| 177 |
</> |
| 178 |
); |
| 179 |
}; |
| 180 |
|
| 181 |
export default Background; |
| 182 |
|