| 1 |
import { useEffect, useRef, useState } from "@wordpress/element"; |
| 2 |
import { Button, Popover } from "@wordpress/components"; |
| 3 |
import "./editor.scss"; |
| 4 |
import { BorderIcon } from "../../icons/icons"; |
| 5 |
import { jsonParse } from "../../blocks/shared/helpFn"; |
| 6 |
|
| 7 |
const SelectField = ({ popupRef, childElement, onClose, anchor = "", divClassName = "" }) => { |
| 8 |
return ( |
| 9 |
<> |
| 10 |
<Popover |
| 11 |
ref={popupRef} |
| 12 |
shift={true} |
| 13 |
className={`sp-border-popover sp-smart-post-tab-panel ${divClassName}`} |
| 14 |
anchor={anchor} |
| 15 |
// position={ 'bottom left' } |
| 16 |
offset={10} |
| 17 |
// placement="right" |
| 18 |
focusOnMount={false} |
| 19 |
> |
| 20 |
<div className="sp-smart-post-popup-content"> |
| 21 |
<div className="sp-smart-post-popup-wrapper"> |
| 22 |
<div className="sp-smart-post-popup-content-area"> |
| 23 |
{React?.cloneElement(childElement, { onClose })} |
| 24 |
</div> |
| 25 |
</div> |
| 26 |
</div> |
| 27 |
</Popover> |
| 28 |
</> |
| 29 |
); |
| 30 |
}; |
| 31 |
|
| 32 |
const Popup = (props) => { |
| 33 |
const { |
| 34 |
label, |
| 35 |
children, |
| 36 |
toggleButton, |
| 37 |
type = "", |
| 38 |
activeLabel = "", |
| 39 |
divClassName = "", |
| 40 |
popupClose = true, |
| 41 |
setPopupClose = () => {}, |
| 42 |
} = props; |
| 43 |
const [isContentsVisible, setIsContentsVisible] = useState(false); |
| 44 |
const [popoverAnchor, setPopoverAnchor] = useState(null); |
| 45 |
const popupRef = useRef(null); |
| 46 |
const buttonRef = useRef(null); |
| 47 |
|
| 48 |
const handleButtonClick = () => { |
| 49 |
setIsContentsVisible((prev) => !prev); // Toggle content visibility |
| 50 |
setPopupClose((prev) => !prev); |
| 51 |
}; |
| 52 |
|
| 53 |
// Close the dropdown when clicking outside of it |
| 54 |
useEffect(() => { |
| 55 |
const handleClickOutside = (event) => { |
| 56 |
const typographyElement = event.target.closest(".sp-smart-post-typography-fonts"); |
| 57 |
const popupElement = event.target.closest(".sp-smart-post-popup-content"); |
| 58 |
const colorPopupElement = event.target.closest(".sp-smart-post-picker-pallet-wrapper"); |
| 59 |
|
| 60 |
if ( |
| 61 |
popupRef.current && |
| 62 |
!popupRef.current.contains(event.target) && |
| 63 |
!buttonRef.current.contains(event.target) && |
| 64 |
!typographyElement && |
| 65 |
!popupElement && |
| 66 |
!colorPopupElement |
| 67 |
) { |
| 68 |
setIsContentsVisible(false); |
| 69 |
setPopupClose(false); |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
document.addEventListener("mousedown", handleClickOutside); |
| 74 |
return () => { |
| 75 |
document.removeEventListener("mousedown", handleClickOutside); |
| 76 |
}; |
| 77 |
}, [popupRef, buttonRef]); |
| 78 |
|
| 79 |
const value = toggleButton?.props?.attributes || null; |
| 80 |
const isActivePopUpButton = value ? jsonParse(value) : true; |
| 81 |
|
| 82 |
return ( |
| 83 |
<> |
| 84 |
<div className="sp-smart-post-button sp-smart-post-component-mb"> |
| 85 |
{type !== "select" && ( |
| 86 |
<> |
| 87 |
<div className={`sp-smart-post-header-left ${toggleButton && "wide-area"}`}> |
| 88 |
{label && ( |
| 89 |
<span className="sp-smart-post-component-title"> |
| 90 |
{label} |
| 91 |
</span> |
| 92 |
)} |
| 93 |
{toggleButton && toggleButton} |
| 94 |
</div> |
| 95 |
<div className="sp-smart-post-header-right" ref={setPopoverAnchor}> |
| 96 |
<Button |
| 97 |
disabled={!isActivePopUpButton} |
| 98 |
className={`sp-smart-post-border-icon-button ${ |
| 99 |
isActivePopUpButton ? "active" : "" |
| 100 |
} ${isContentsVisible ? "button-clicked" : ""}`} |
| 101 |
icon={<BorderIcon isActive={isContentsVisible} />} |
| 102 |
ref={buttonRef} |
| 103 |
onClick={handleButtonClick} |
| 104 |
/> |
| 105 |
</div> |
| 106 |
</> |
| 107 |
)} |
| 108 |
{type === "select" && ( |
| 109 |
<> |
| 110 |
<div |
| 111 |
ref={buttonRef} |
| 112 |
className={`sp-smart-post-dropdown-select ${divClassName}`} |
| 113 |
onClick={handleButtonClick} |
| 114 |
> |
| 115 |
{activeLabel?.replaceAll("-", " ")} |
| 116 |
{isContentsVisible && isActivePopUpButton ? ( |
| 117 |
<i className="sp-icon-angle-up" /> |
| 118 |
) : ( |
| 119 |
<i className="sp-icon-angle-down" /> |
| 120 |
)} |
| 121 |
</div> |
| 122 |
</> |
| 123 |
)} |
| 124 |
</div> |
| 125 |
{type !== "select" && isContentsVisible && isActivePopUpButton && ( |
| 126 |
<SelectField |
| 127 |
popupRef={popupRef} |
| 128 |
childElement={children} |
| 129 |
anchor={popoverAnchor} |
| 130 |
divClassName={ divClassName } |
| 131 |
onClose={() => { |
| 132 |
setIsContentsVisible(false); |
| 133 |
setPopupClose(false); |
| 134 |
}} |
| 135 |
/> |
| 136 |
)} |
| 137 |
{type === "select" && popupClose && ( |
| 138 |
<SelectField |
| 139 |
popupRef={popupRef} |
| 140 |
childElement={children} |
| 141 |
divClassName={ divClassName } |
| 142 |
// onClose={() => { |
| 143 |
// setIsContentsVisible(false); |
| 144 |
// setPopupClose(false); |
| 145 |
// }} |
| 146 |
/> |
| 147 |
)} |
| 148 |
</> |
| 149 |
); |
| 150 |
}; |
| 151 |
|
| 152 |
export default Popup; |
| 153 |
|