| 1 |
import { __ } from "@wordpress/i18n"; |
| 2 |
import { Button, PanelBody, SelectControl, __experimentalInputControl as InputControl } from "@wordpress/components"; |
| 3 |
|
| 4 |
import "./editor.scss"; |
| 5 |
import { Popup, SpColorPicker } from "../../components"; |
| 6 |
import { useEffect, useState } from "@wordpress/element"; |
| 7 |
import { useDispatch, useSelect } from "@wordpress/data"; |
| 8 |
import { generateShadowRootCSS } from "../default-settings"; |
| 9 |
import { PlusIcon } from "../../icons/arrowIcons"; |
| 10 |
// import { PlusIcon } from "../../icons/icons"; |
| 11 |
|
| 12 |
const ShadowSettings = () => { |
| 13 |
const shadow = useSelect((select) => select("smartpost/global-settings").getCategory("shadow")); |
| 14 |
|
| 15 |
const { updateSetting } = useDispatch("smartpost/global-settings"); |
| 16 |
|
| 17 |
const [shadowList, setShadowList] = useState(shadow.shadowList); |
| 18 |
|
| 19 |
const addCustomShadow = () => { |
| 20 |
const customSizeId = shadowList?.length - 6 + 1; |
| 21 |
const randomKey = () => Math.random().toString(36).slice(2, 5); |
| 22 |
|
| 23 |
const newShadow = [ |
| 24 |
...shadowList, |
| 25 |
...[ |
| 26 |
{ |
| 27 |
...shadowList[0], |
| 28 |
custom: true, |
| 29 |
slug: `custom-shadow-${customSizeId}-${randomKey()}`, |
| 30 |
title: "New Shadow " + customSizeId, |
| 31 |
}, |
| 32 |
], |
| 33 |
]; |
| 34 |
|
| 35 |
setShadowList(newShadow); |
| 36 |
|
| 37 |
updateSetting("shadow", "shadowList", newShadow); |
| 38 |
}; |
| 39 |
|
| 40 |
const changeValue = (newValue, slug, key) => { |
| 41 |
const newData = shadowList.map((item) => (item.slug === slug ? { ...item, [key]: newValue } : item)); |
| 42 |
|
| 43 |
setShadowList(newData); |
| 44 |
|
| 45 |
updateSetting("shadow", "shadowList", newData); |
| 46 |
}; |
| 47 |
|
| 48 |
const deleteShadow = (slug) => { |
| 49 |
const newData = shadowList.filter((shadow) => shadow.slug !== slug); |
| 50 |
|
| 51 |
const confirmed = window.confirm("Are you sure you want to delete box shadow?"); |
| 52 |
if (confirmed) { |
| 53 |
setShadowList(newData); |
| 54 |
updateSetting("shadow", "shadowList", newData); |
| 55 |
} |
| 56 |
}; |
| 57 |
|
| 58 |
const shadowCss = (item) => { |
| 59 |
return `${"inset" === item["type"] ? "inset" : ""} ${item["x-offset"]}px ${item["y-offset"]}px ${ |
| 60 |
item["blur"] |
| 61 |
}px ${item["speared"]}px ${item["color"]}`; |
| 62 |
}; |
| 63 |
|
| 64 |
useEffect(() => { |
| 65 |
const shadowRootCSS = generateShadowRootCSS(shadowList); |
| 66 |
updateSetting("shadow", "shadowRootCSS", shadowRootCSS); |
| 67 |
}, [shadowList]); |
| 68 |
|
| 69 |
return ( |
| 70 |
<PanelBody title={__("Shadow", "post-carousel")} initialOpen={false}> |
| 71 |
<div className="sp-smart-global-shadow"> |
| 72 |
<div className="sp-smart-global-shadow-list"> |
| 73 |
<ul> |
| 74 |
{shadowList?.map((item) => ( |
| 75 |
<li key={item.slug}> |
| 76 |
<Popup label={item.title}> |
| 77 |
<div className="sp-smart-global-shadow-wrapper"> |
| 78 |
<div className="sp-smart-global-shadow-header"> |
| 79 |
<h4> |
| 80 |
{item.custom ? ( |
| 81 |
<> |
| 82 |
<span>Name</span> |
| 83 |
<input |
| 84 |
name={item.slug} |
| 85 |
value={item.title} |
| 86 |
onChange={(e) => |
| 87 |
changeValue(e.target.value, item.slug, "title") |
| 88 |
} |
| 89 |
/> |
| 90 |
</> |
| 91 |
) : ( |
| 92 |
item.title |
| 93 |
)} |
| 94 |
</h4> |
| 95 |
{item.custom && ( |
| 96 |
<Button |
| 97 |
className="sp-smart-delete-btn" |
| 98 |
onClick={() => deleteShadow(item.slug)} |
| 99 |
> |
| 100 |
<svg |
| 101 |
width="20" |
| 102 |
height="20" |
| 103 |
viewBox="0 0 20 20" |
| 104 |
fill="none" |
| 105 |
xmlns="http://www.w3.org/2000/svg" |
| 106 |
> |
| 107 |
<path |
| 108 |
d="M12 4H15C15.6 4 16 4.4 16 5V6H3V5C3 4.4 3.5 4 4 4H7C7.2 2.9 8.3 2 9.5 2C10.7 2 11.8 2.9 12 4ZM8 4H11C10.8 3.4 10.1 3 9.5 3C8.9 3 8.2 3.4 8 4ZM4 7H15L14.1 17.1C14.1 17.6 13.6 18 13.1 18H5.9C5.4 18 5 17.6 4.9 17.1L4 7Z" |
| 109 |
fill="currentColor" |
| 110 |
/> |
| 111 |
</svg> |
| 112 |
</Button> |
| 113 |
)} |
| 114 |
</div> |
| 115 |
<div className="sp-smart-global-shadow-body"> |
| 116 |
<div className="sp-smart-global-shadow-preset"> |
| 117 |
<div |
| 118 |
className="sp-smart-shadow-preset-box" |
| 119 |
style={{ |
| 120 |
boxShadow: shadowCss(item), |
| 121 |
}} |
| 122 |
></div> |
| 123 |
</div> |
| 124 |
<SelectControl |
| 125 |
options={[ |
| 126 |
{ |
| 127 |
label: "Outset", |
| 128 |
value: "outset", |
| 129 |
}, |
| 130 |
{ |
| 131 |
label: "Inset", |
| 132 |
value: "inset", |
| 133 |
}, |
| 134 |
]} |
| 135 |
value={item.type} |
| 136 |
onChange={(newValue) => changeValue(newValue, item.slug, "type")} |
| 137 |
/> |
| 138 |
<div className="sp-smart-global-shadow-input-field"> |
| 139 |
{["x-offset", "y-offset", "blur", "speared"].map((field, i) => ( |
| 140 |
<InputControl |
| 141 |
key={i} |
| 142 |
label={field} |
| 143 |
type="number" |
| 144 |
onChange={(newValue) => changeValue(newValue, item.slug, field)} |
| 145 |
value={item[field]} |
| 146 |
/> |
| 147 |
))} |
| 148 |
</div> |
| 149 |
<div className="sp-smart-global-shadow-color"> |
| 150 |
<SpColorPicker |
| 151 |
label={__("Shadow Color", "post-carousel")} |
| 152 |
value={item?.color} |
| 153 |
onChange={(newColor) => changeValue(newColor, item.slug, "color")} |
| 154 |
defaultColor="#333333" |
| 155 |
/> |
| 156 |
</div> |
| 157 |
</div> |
| 158 |
</div> |
| 159 |
</Popup> |
| 160 |
</li> |
| 161 |
))} |
| 162 |
</ul> |
| 163 |
</div> |
| 164 |
<div className="sp-global-add-custom"> |
| 165 |
<div className="sp-add-custom-sizes sp-d-flex sp-space-between"> |
| 166 |
<span>Custom</span> |
| 167 |
<span className="sp-btn" onClick={() => addCustomShadow()}> |
| 168 |
<PlusIcon /> |
| 169 |
</span> |
| 170 |
</div> |
| 171 |
</div> |
| 172 |
</div> |
| 173 |
</PanelBody> |
| 174 |
); |
| 175 |
}; |
| 176 |
|
| 177 |
export default ShadowSettings; |
| 178 |
|