| 1 |
import { __ } from "@wordpress/i18n"; |
| 2 |
import { useState, useEffect } from "@wordpress/element"; |
| 3 |
import SpColorPicker from "../color/color.js"; |
| 4 |
import SPToggleGroupControl from "../toggleGroupControl/toggleGroupControl.js"; |
| 5 |
|
| 6 |
import { Tooltip } from "@wordpress/components"; |
| 7 |
import { BorderIcon, CheckMark, GloveIcon, ResetIcon } from "../../icons/icons.js"; |
| 8 |
import { BoxSpacing } from "./boxSpacing.js"; |
| 9 |
import "./editor.scss"; |
| 10 |
import { dispatch, useSelect, select, hasStore } from "@wordpress/data"; |
| 11 |
|
| 12 |
const BoxShadow = ({ |
| 13 |
shadowColorBtn = false, |
| 14 |
attributes, |
| 15 |
attributesKey, |
| 16 |
setAttributes, |
| 17 |
defaultColor = "#4E4F521A", |
| 18 |
onChange = false, |
| 19 |
}) => { |
| 20 |
const [buttonTab, setButtonTab] = useState("default"); |
| 21 |
const [globalStyle, setGlobalStyle] = useState(null); |
| 22 |
const [selectedShadow, setSelectedShadow] = useState("var(--smart-post-shadow-medium-4dp)"); |
| 23 |
const [customShadow, setCustomShadow] = useState(false); |
| 24 |
const [update, setUpdate] = useState(0); |
| 25 |
const [sidebarState, setSidebarState] = useState(false); |
| 26 |
|
| 27 |
// Initialize selected shadow from attributes |
| 28 |
useEffect(() => { |
| 29 |
if (attributes?.selectDefault && attributes.selectDefault !== "custom") { |
| 30 |
setSelectedShadow(attributes.selectDefault); |
| 31 |
} |
| 32 |
if (attributes?.selectDefault === "custom") { |
| 33 |
setCustomShadow(true); |
| 34 |
} |
| 35 |
}, [attributes]); |
| 36 |
|
| 37 |
// Get global shadow styles |
| 38 |
const getGlobalShadow = useSelect((_select) => _select("smartpost/global-settings")?.getCategory("shadow"), []); |
| 39 |
useEffect(() => { |
| 40 |
if (getGlobalShadow) { |
| 41 |
setGlobalStyle(getGlobalShadow?.shadowList); |
| 42 |
} |
| 43 |
}, [getGlobalShadow]); |
| 44 |
|
| 45 |
useEffect( () => { |
| 46 |
const isPostEditor = select("core/edit-post"); |
| 47 |
const isSiteEditor = select("core/edit-site"); |
| 48 |
const isWidgetEditor = select("core/customize-widgets"); |
| 49 |
|
| 50 |
if (isPostEditor || isSiteEditor || isWidgetEditor) { |
| 51 |
setSidebarState(true); |
| 52 |
} |
| 53 |
}, []) |
| 54 |
|
| 55 |
const shadowColor = (newColor) => { |
| 56 |
if (onChange) { |
| 57 |
onChange(attributesKey, { ...attributes, color: newColor }); |
| 58 |
} else { |
| 59 |
setAttributes({ |
| 60 |
[attributesKey]: { ...attributes, color: newColor }, |
| 61 |
}); |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
const shadowHoverColor = (newColor) => { |
| 66 |
setAttributes({ |
| 67 |
[attributesKey]: { ...attributes, hoverColor: newColor }, |
| 68 |
}); |
| 69 |
}; |
| 70 |
|
| 71 |
const tabButton = (newValue) => { |
| 72 |
setButtonTab(newValue); |
| 73 |
}; |
| 74 |
|
| 75 |
const shadowHandler = (newValue) => { |
| 76 |
setAttributes({ |
| 77 |
[attributesKey]: { ...attributes, selectDefault: newValue }, |
| 78 |
}); |
| 79 |
}; |
| 80 |
|
| 81 |
const handleShadowType = () => { |
| 82 |
const newCustomShadow = !customShadow; |
| 83 |
setCustomShadow(newCustomShadow); |
| 84 |
|
| 85 |
if (newCustomShadow) { |
| 86 |
// Switching to custom shadow |
| 87 |
if (onChange) { |
| 88 |
onChange(attributesKey, { ...attributes, selectDefault: "custom" }); |
| 89 |
} else { |
| 90 |
setAttributes({ |
| 91 |
[attributesKey]: { ...attributes, selectDefault: "custom" }, |
| 92 |
}); |
| 93 |
} |
| 94 |
} else { |
| 95 |
// Switching back to preset shadows |
| 96 |
if (selectedShadow && selectedShadow !== "custom") { |
| 97 |
shadowHandler(selectedShadow); |
| 98 |
} |
| 99 |
} |
| 100 |
}; |
| 101 |
|
| 102 |
const handleShadowSelect = (newValue) => { |
| 103 |
setSelectedShadow(newValue); |
| 104 |
if (onChange) { |
| 105 |
onChange(attributesKey, { ...attributes, selectDefault: newValue }); |
| 106 |
} else { |
| 107 |
shadowHandler(newValue); |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
const handleReset = () => { |
| 112 |
// Reset to default medium shadow |
| 113 |
setSelectedShadow("var(--smart-post-shadow-medium-4dp)"); |
| 114 |
shadowHandler("var(--smart-post-shadow-medium-4dp)"); |
| 115 |
}; |
| 116 |
|
| 117 |
const handleGlobalSettings = () => { |
| 118 |
const sidebarName = "smart-post-show-pro-global-settings/sidebar"; |
| 119 |
if (select("core/edit-post")) { |
| 120 |
dispatch("core/edit-post").openGeneralSidebar(sidebarName); |
| 121 |
} else if (select("core/edit-site")) { |
| 122 |
dispatch("core/edit-site").openGeneralSidebar(sidebarName); |
| 123 |
} else if (select("core/customize-widgets")) { |
| 124 |
dispatch("core/customize-widgets").openGeneralSidebar(sidebarName); |
| 125 |
} else { |
| 126 |
console.log("Could not determine current editor type"); |
| 127 |
} |
| 128 |
}; |
| 129 |
const updateGlobalStyleHandler = (slugValue) => { |
| 130 |
if (onChange) { |
| 131 |
onChange(attributesKey, `var(--smart-post-shadow-${slugValue})`); |
| 132 |
} else { |
| 133 |
handleShadowSelect(`var(--smart-post-shadow-${slugValue})`); |
| 134 |
} |
| 135 |
}; |
| 136 |
|
| 137 |
return ( |
| 138 |
<> |
| 139 |
<div className="shadow-selector-container"> |
| 140 |
{/* Header */} |
| 141 |
<div className="shadow-selector-header"> |
| 142 |
<p className="shadow-selector-title">Shadow Type</p> |
| 143 |
<div className="shadow-selector-actions"> |
| 144 |
{!customShadow && ( |
| 145 |
<> |
| 146 |
<Tooltip text="Reset to default"> |
| 147 |
<button className="action-button action-refresh" onClick={handleReset}> |
| 148 |
<ResetIcon /> |
| 149 |
</button> |
| 150 |
</Tooltip> |
| 151 |
{ sidebarState && ( |
| 152 |
<Tooltip text="Global Settings"> |
| 153 |
<button className="action-button" onClick={handleGlobalSettings}> |
| 154 |
<GloveIcon /> |
| 155 |
</button> |
| 156 |
</Tooltip> |
| 157 |
)} |
| 158 |
</> |
| 159 |
)} |
| 160 |
|
| 161 |
<Tooltip text={customShadow ? "Switch to Presets" : "Switch to Custom"}> |
| 162 |
<button onClick={handleShadowType} className="action-button"> |
| 163 |
<BorderIcon isActive={customShadow} /> |
| 164 |
</button> |
| 165 |
</Tooltip> |
| 166 |
</div> |
| 167 |
</div> |
| 168 |
|
| 169 |
{customShadow ? ( |
| 170 |
<BoxSpacing |
| 171 |
key={update} |
| 172 |
label={__("Box Shadow", "post-carousel")} |
| 173 |
customClass={"sp-box-shadow"} |
| 174 |
attributes={attributes} |
| 175 |
attributesKey={attributesKey} |
| 176 |
setAttributes={setAttributes} |
| 177 |
boxUnits={true} |
| 178 |
units={["Outset", "Inset"]} |
| 179 |
labelItem={{ |
| 180 |
top: __("X Offset", "post-carousel"), |
| 181 |
right: __("Y Offset", "post-carousel"), |
| 182 |
bottom: __("Blur", "post-carousel"), |
| 183 |
left: __("Speared", "post-carousel"), |
| 184 |
}} |
| 185 |
handleReset={handleReset} |
| 186 |
onChange={onChange} |
| 187 |
/> |
| 188 |
) : ( |
| 189 |
<div className="shadow-options-grid"> |
| 190 |
{/* { shadowTypes.map( ( shadow ) => ( |
| 191 |
<Tooltip |
| 192 |
key={ shadow.id } |
| 193 |
text={ shadow.name } |
| 194 |
position="top" |
| 195 |
className="custom-tooltip" |
| 196 |
> |
| 197 |
<button |
| 198 |
onClick={ () => |
| 199 |
handleShadowSelect( shadow.id ) |
| 200 |
} |
| 201 |
className={ `shadow-option ${ |
| 202 |
selectedShadow === shadow.id |
| 203 |
? 'selected' |
| 204 |
: '' |
| 205 |
}` } |
| 206 |
style={ shadow.style } |
| 207 |
> |
| 208 |
{ selectedShadow === shadow.id && ( |
| 209 |
<div className="checkmark"> |
| 210 |
<CheckMark /> |
| 211 |
</div> |
| 212 |
) } |
| 213 |
</button> |
| 214 |
</Tooltip> |
| 215 |
) ) } */} |
| 216 |
{globalStyle?.map((shadow, i) => ( |
| 217 |
<Tooltip |
| 218 |
key={i} |
| 219 |
text={shadow.title} |
| 220 |
// position="top" |
| 221 |
className="custom-tooltip" |
| 222 |
> |
| 223 |
<button |
| 224 |
onClick={() => handleShadowSelect(`var(--smart-post-shadow-${shadow.slug})`)} |
| 225 |
className={`shadow-option ${ |
| 226 |
selectedShadow === "var(--smart-post-shadow-" + shadow.slug + ")" |
| 227 |
? "selected" |
| 228 |
: "" |
| 229 |
}`} |
| 230 |
style={{ |
| 231 |
boxShadow: `var(--smart-post-shadow-${shadow.slug})`, |
| 232 |
}} |
| 233 |
> |
| 234 |
{selectedShadow === `var(--smart-post-shadow-${shadow.slug})` && ( |
| 235 |
<div className="checkmark"> |
| 236 |
<CheckMark /> |
| 237 |
</div> |
| 238 |
)} |
| 239 |
</button> |
| 240 |
</Tooltip> |
| 241 |
))} |
| 242 |
</div> |
| 243 |
)} |
| 244 |
</div> |
| 245 |
|
| 246 |
{customShadow && ( |
| 247 |
<> |
| 248 |
{shadowColorBtn && ( |
| 249 |
<SPToggleGroupControl |
| 250 |
label={__("Shadow Color", "post-carousel")} |
| 251 |
attributes={buttonTab} |
| 252 |
items={[ |
| 253 |
{ label: "Default", value: "default" }, |
| 254 |
{ label: "Hover", value: "hover" }, |
| 255 |
]} |
| 256 |
onClick={tabButton} |
| 257 |
/> |
| 258 |
)} |
| 259 |
{"default" === buttonTab && ( |
| 260 |
<SpColorPicker |
| 261 |
label={__("Shadow Color", "post-carousel")} |
| 262 |
value={attributes.color} |
| 263 |
onChange={shadowColor} |
| 264 |
defaultColor={defaultColor} |
| 265 |
/> |
| 266 |
)} |
| 267 |
{"hover" === buttonTab && ( |
| 268 |
<> |
| 269 |
<SpColorPicker |
| 270 |
label={__("Hover Color", "post-carousel")} |
| 271 |
value={attributes.hoverColor} |
| 272 |
onChange={shadowHoverColor} |
| 273 |
defaultColor={defaultColor} |
| 274 |
/> |
| 275 |
</> |
| 276 |
)} |
| 277 |
</> |
| 278 |
)} |
| 279 |
</> |
| 280 |
); |
| 281 |
}; |
| 282 |
|
| 283 |
export default BoxShadow; |
| 284 |
|