| 1 |
import { useEffect, useMemo } from "@wordpress/element"; |
| 2 |
import { useBlockProps, RichText } from "@wordpress/block-editor"; |
| 3 |
import InspectorControl from "../../components/inspectorControls/inspectorControls"; |
| 4 |
import { panelBodyRightIcon, panelBodyTitleIcon } from "../../icons/icons"; |
| 5 |
import { jsonStringify } from "../shared/helpFn"; |
| 6 |
import dynamicCssFn from "./dynamicCss"; |
| 7 |
import Inspector from "./inspect"; |
| 8 |
import { googleFonts, useDeviceType } from "../../controls/controls"; |
| 9 |
import { arrowIcons } from "../../controls/constants"; |
| 10 |
import classNames from "classnames"; |
| 11 |
import { compose } from "@wordpress/compose"; |
| 12 |
import addInitialAttr from "../shared/addInitialAttr"; |
| 13 |
|
| 14 |
const ButtonEdit = (props) => { |
| 15 |
const { attributes, setAttributes } = props; |
| 16 |
const currentDevice = useDeviceType(); |
| 17 |
const { |
| 18 |
uniqueId, |
| 19 |
buttonLabel, |
| 20 |
iconSource, |
| 21 |
buttonStyle, |
| 22 |
hoverEffects, |
| 23 |
enableIcon, |
| 24 |
buttonLabelTypography, |
| 25 |
additionalCssClass, |
| 26 |
customCss, |
| 27 |
fontListsEditPage, |
| 28 |
buttonLabelEnable, |
| 29 |
buttonLabelGlobalTypography, |
| 30 |
} = attributes; |
| 31 |
|
| 32 |
const googleFontLists = [buttonLabelTypography]; |
| 33 |
|
| 34 |
useEffect(() => { |
| 35 |
setAttributes({ |
| 36 |
fontLists: jsonStringify(googleFonts(googleFontLists, "frontend")), |
| 37 |
fontListsEditPage: googleFonts(googleFontLists), |
| 38 |
}); |
| 39 |
}, googleFontLists); |
| 40 |
|
| 41 |
const blockProps = useBlockProps({ |
| 42 |
className: additionalCssClass, |
| 43 |
}); |
| 44 |
|
| 45 |
const blockStyling = useMemo(() => { |
| 46 |
return dynamicCssFn(attributes, "frontend", currentDevice); |
| 47 |
}, [attributes, currentDevice]); |
| 48 |
|
| 49 |
useEffect(() => { |
| 50 |
if (buttonStyle !== "default" && hoverEffects === "flip") { |
| 51 |
setAttributes({ hoverEffects: "ghostDefault" }); |
| 52 |
} |
| 53 |
}, [buttonStyle]); |
| 54 |
|
| 55 |
const BtnIcon = arrowIcons[iconSource]; |
| 56 |
|
| 57 |
return ( |
| 58 |
<div {...blockProps}> |
| 59 |
<style> |
| 60 |
{fontListsEditPage} |
| 61 |
{blockStyling} |
| 62 |
{customCss} |
| 63 |
</style> |
| 64 |
|
| 65 |
<InspectorControl |
| 66 |
TitleIcon={panelBodyTitleIcon} |
| 67 |
RightIcon={panelBodyRightIcon} |
| 68 |
Inspector={Inspector} |
| 69 |
attributes={attributes} |
| 70 |
setAttributes={setAttributes} |
| 71 |
/> |
| 72 |
|
| 73 |
<div |
| 74 |
className={classNames( |
| 75 |
"sp-smart-post-wrapper", |
| 76 |
"sp-smart-post-button-editor-page", |
| 77 |
"sp-smart-post-button-wrapper" |
| 78 |
)} |
| 79 |
id={uniqueId} |
| 80 |
> |
| 81 |
<button className={`sp-smart-post-btn btn-${buttonStyle} ${hoverEffects}`}> |
| 82 |
<div className="button-label"> |
| 83 |
<div className="front"> |
| 84 |
{enableIcon && ( |
| 85 |
<span |
| 86 |
aria-label="arrow icon" |
| 87 |
className="icon" |
| 88 |
style={{ |
| 89 |
fontSize: "14px", |
| 90 |
lineHeight: "1", |
| 91 |
display: "inline-block", |
| 92 |
}} |
| 93 |
> |
| 94 |
<i |
| 95 |
aria-hidden={true} |
| 96 |
className={`sp-icon-${iconSource}`} |
| 97 |
// style={ { |
| 98 |
// display: 'block', |
| 99 |
// width: '1em', |
| 100 |
// height: '1em', |
| 101 |
// } } |
| 102 |
> |
| 103 |
<BtnIcon /> |
| 104 |
</i> |
| 105 |
</span> |
| 106 |
)} |
| 107 |
{buttonLabelEnable && ( |
| 108 |
<RichText |
| 109 |
tagName="span" |
| 110 |
className={classNames( |
| 111 |
"sp-rich-text", |
| 112 |
"sp-btn-label", |
| 113 |
buttonLabelGlobalTypography?.class ? buttonLabelGlobalTypography.class : "" |
| 114 |
)} |
| 115 |
value={buttonLabel} |
| 116 |
onChange={(newVal) => setAttributes({ buttonLabel: newVal })} |
| 117 |
allowedFormats={[]} // optional: no bold/italic/etc. |
| 118 |
placeholder="Enter label" |
| 119 |
/> |
| 120 |
)} |
| 121 |
</div> |
| 122 |
|
| 123 |
{hoverEffects === "flip" && ( |
| 124 |
<div className="back"> |
| 125 |
{enableIcon && ( |
| 126 |
<span |
| 127 |
aria-label="button flip on hover arrow icon" |
| 128 |
className="icon" |
| 129 |
style={{ |
| 130 |
fontSize: "14px", |
| 131 |
lineHeight: "1", |
| 132 |
display: "inline-block", |
| 133 |
}} |
| 134 |
> |
| 135 |
<i |
| 136 |
aria-hidden={true} |
| 137 |
className={`sp-icon-${iconSource}`} |
| 138 |
// style={ { |
| 139 |
// display: 'block', |
| 140 |
// width: '1em', |
| 141 |
// height: '1em', |
| 142 |
// } } |
| 143 |
> |
| 144 |
<BtnIcon /> |
| 145 |
</i> |
| 146 |
</span> |
| 147 |
)} |
| 148 |
{buttonLabelEnable && ( |
| 149 |
<RichText |
| 150 |
tagName="span" |
| 151 |
className={classNames( |
| 152 |
"sp-rich-text", |
| 153 |
"sp-btn-label", |
| 154 |
buttonLabelGlobalTypography?.class ? buttonLabelGlobalTypography.class : "" |
| 155 |
)} |
| 156 |
value={buttonLabel} |
| 157 |
onChange={(newVal) => |
| 158 |
setAttributes({ |
| 159 |
buttonLabel: newVal, |
| 160 |
}) |
| 161 |
} |
| 162 |
allowedFormats={[]} // optional: no bold/italic/etc. |
| 163 |
placeholder="Enter label" |
| 164 |
/> |
| 165 |
)} |
| 166 |
</div> |
| 167 |
)} |
| 168 |
</div> |
| 169 |
</button> |
| 170 |
</div> |
| 171 |
</div> |
| 172 |
); |
| 173 |
}; |
| 174 |
|
| 175 |
export default compose(addInitialAttr)(ButtonEdit); |
| 176 |
|