| 1 |
import { Dropdown, Tooltip } from "@wordpress/components"; |
| 2 |
import { useEffect, useRef, useState } from "@wordpress/element"; |
| 3 |
import icons from "./icon-lists"; |
| 4 |
import "./editor.scss"; |
| 5 |
import { |
| 6 |
ArrowRight, |
| 7 |
Square, |
| 8 |
Check, |
| 9 |
CheckCircle, |
| 10 |
ChevronRight, |
| 11 |
Dot, |
| 12 |
SquareBorder, |
| 13 |
RightArrow, |
| 14 |
Hand, |
| 15 |
FillStar, |
| 16 |
NpArrow, |
| 17 |
BulletPoint, |
| 18 |
CircleIcon, |
| 19 |
NumberIcon, |
| 20 |
} from "./icons"; |
| 21 |
const Icons = { |
| 22 |
CheckCircle, |
| 23 |
Dot, |
| 24 |
NumberIcon, |
| 25 |
Square, |
| 26 |
FillStar, |
| 27 |
NpArrow, |
| 28 |
BulletPoint, |
| 29 |
RightArrow, |
| 30 |
Check, |
| 31 |
CircleIcon, |
| 32 |
SquareBorder, |
| 33 |
Hand, |
| 34 |
ArrowRight, |
| 35 |
ChevronRight, |
| 36 |
}; |
| 37 |
|
| 38 |
const SpSVGIconPicker = ({ |
| 39 |
attributes, |
| 40 |
attributesKey, |
| 41 |
setAttributes, |
| 42 |
label, |
| 43 |
closeBtn = true, |
| 44 |
defaultCategory = "All", |
| 45 |
}) => { |
| 46 |
const [CurrentIcon, setCurrentIcon] = useState(attributes); |
| 47 |
const IconComponent = Icons[CurrentIcon]; |
| 48 |
|
| 49 |
const activeIconRef = useRef(null); |
| 50 |
const [searchQuery, setSearchQuery] = useState(""); |
| 51 |
const [selectedCategory, setSelectedCategory] = useState(defaultCategory); |
| 52 |
const [isDropdownOpen, setIsDropdownOpen] = useState(false); |
| 53 |
const handleChange = (newIcon) => { |
| 54 |
// const icon = newIcon.split( ' ' )[ 1 ]; |
| 55 |
setCurrentIcon(newIcon); |
| 56 |
setAttributes({ [attributesKey]: newIcon }); |
| 57 |
}; |
| 58 |
const handleClose = () => { |
| 59 |
setAttributes({ [attributesKey]: "" }); |
| 60 |
setCurrentIcon(null); |
| 61 |
}; |
| 62 |
const flatIcons = selectedCategory === "All" ? Object.values(icons).flat() : icons[selectedCategory]; |
| 63 |
// const activeIcon = 'fa ' + attributes; |
| 64 |
const activeIcon = attributes; |
| 65 |
const categories = Object.keys(icons); |
| 66 |
// Filter icons based on search query |
| 67 |
const filteredIcons = flatIcons.filter((icon) => icon.toLowerCase().includes(searchQuery.toLowerCase())); |
| 68 |
|
| 69 |
const iconNameHandle = (icon) => { |
| 70 |
const iconName = icon?.replace("fa fa-", "")?.replaceAll("-", " "); |
| 71 |
const capitalizedName = iconName?.replace(/\b\w/g, (char) => char.toUpperCase()); |
| 72 |
return capitalizedName; |
| 73 |
}; |
| 74 |
|
| 75 |
useEffect(() => { |
| 76 |
if (isDropdownOpen && activeIconRef.current) { |
| 77 |
setTimeout(() => { |
| 78 |
activeIconRef.current.scrollIntoView({ |
| 79 |
behavior: "smooth", |
| 80 |
block: "center", |
| 81 |
}); |
| 82 |
}, 100); |
| 83 |
} |
| 84 |
}, [isDropdownOpen]); |
| 85 |
|
| 86 |
return ( |
| 87 |
<div className="sp-smart-post-icon-picker-area sp-smart-post-component-mb"> |
| 88 |
<span className="sp-smart-post-component-title">{label}</span> |
| 89 |
{/* modal */} |
| 90 |
<Dropdown |
| 91 |
position="top right" |
| 92 |
renderToggle={({ isOpen, onToggle }) => ( |
| 93 |
<div |
| 94 |
onClick={() => { |
| 95 |
setIsDropdownOpen((prev) => !prev); |
| 96 |
onToggle(); |
| 97 |
}} |
| 98 |
aria-expanded={isOpen} |
| 99 |
> |
| 100 |
<div className="sp-smart-post-media-control__wrapper sp-smart-post-icon-picker"> |
| 101 |
{attributes ? ( |
| 102 |
<> |
| 103 |
<div className="sp-smart-post-icon-picker-active-icon"> |
| 104 |
<IconComponent /> |
| 105 |
</div> |
| 106 |
{closeBtn && ( |
| 107 |
<button |
| 108 |
className="sp-smart-post-icon-picker-close-btn sp-smart-post-icon-picker-btn" |
| 109 |
onClick={handleClose} |
| 110 |
> |
| 111 |
<i className="fa fa-times" /> |
| 112 |
</button> |
| 113 |
)} |
| 114 |
</> |
| 115 |
) : ( |
| 116 |
<button className="sp-smart-post-icon-picker-add-btn sp-smart-post-icon-picker-btn"> |
| 117 |
<i className="fa fa-plus" /> |
| 118 |
</button> |
| 119 |
)} |
| 120 |
<div className="sp-smart-post-media-picker-tooltip"> |
| 121 |
<span>{attributes ? "Change Icon" : "Choose Icon"}</span> |
| 122 |
</div> |
| 123 |
</div> |
| 124 |
</div> |
| 125 |
)} |
| 126 |
onClose={(event, isInside) => { |
| 127 |
setIsDropdownOpen(false); |
| 128 |
if (isInside) { |
| 129 |
event.stopPropagation(); |
| 130 |
return; |
| 131 |
} |
| 132 |
}} |
| 133 |
renderContent={() => ( |
| 134 |
<div |
| 135 |
onMouseDown={(event) => { |
| 136 |
event.stopPropagation(); |
| 137 |
}} |
| 138 |
className="sp-smart-post-icon-picker-popup" |
| 139 |
> |
| 140 |
<div className="sp-smart-post-icon-picker-popup-top-section"> |
| 141 |
<span className="sp-smart-post-icon-search-heading">Select an Icon</span> |
| 142 |
{/* <span className="sp-smart-post-icon-search-sub-heading"> |
| 143 |
Choose an icon for this collection. |
| 144 |
</span> */} |
| 145 |
<div className="sp-smart-post-icon-picker-search-and-category-wrapper"> |
| 146 |
{/* Search Input */} |
| 147 |
{/* <input |
| 148 |
type="text" |
| 149 |
placeholder="Search... sdf fsfsdfs" |
| 150 |
value={ searchQuery } |
| 151 |
onChange={ ( e ) => |
| 152 |
setSearchQuery( e.target.value ) |
| 153 |
} |
| 154 |
className="sp-smart-post-icon-picker-search" |
| 155 |
/> */} |
| 156 |
{/* Category Dropdown */} |
| 157 |
{/* <select |
| 158 |
className="sp-smart-post-icon-picker-category" |
| 159 |
value={ selectedCategory } |
| 160 |
onChange={ ( e ) => |
| 161 |
setSelectedCategory( e.target.value ) |
| 162 |
} |
| 163 |
> |
| 164 |
<option value="All">All</option> |
| 165 |
{ categories.map( ( category, index ) => ( |
| 166 |
<option |
| 167 |
key={ index } |
| 168 |
value={ category } |
| 169 |
> |
| 170 |
{ category } |
| 171 |
</option> |
| 172 |
) ) } |
| 173 |
</select> */} |
| 174 |
</div> |
| 175 |
</div> |
| 176 |
<div className="sp-smart-post-icon-picker-list"> |
| 177 |
{/* { filteredIcons?.map( ( icon, index ) => ( */} |
| 178 |
{Object.entries(Icons).map(([name, Icon], index) => ( |
| 179 |
<Tooltip |
| 180 |
key={name} |
| 181 |
text={iconNameHandle(name)} |
| 182 |
className={`sp-smart-post-component-tooltip`} |
| 183 |
position="top" |
| 184 |
> |
| 185 |
<span |
| 186 |
key={name} |
| 187 |
// ref={ |
| 188 |
// activeIcon === icon |
| 189 |
// ? activeIconRef |
| 190 |
// : null |
| 191 |
// } |
| 192 |
className={`sp-smart-post-icon-picker-item${ |
| 193 |
activeIcon === name ? " active" : "" |
| 194 |
}${index > 5 ? " sp-is-pro-icon" : ""}`} |
| 195 |
onClick={() => handleChange(name)} |
| 196 |
> |
| 197 |
{/* <i className={ icon }></i> */} |
| 198 |
{/* <ArrowRight /> */} |
| 199 |
|
| 200 |
<Icon active={activeIcon === name ? true : false} /> |
| 201 |
</span> |
| 202 |
</Tooltip> |
| 203 |
))} |
| 204 |
</div> |
| 205 |
</div> |
| 206 |
)} |
| 207 |
/> |
| 208 |
</div> |
| 209 |
); |
| 210 |
}; |
| 211 |
|
| 212 |
export default SpSVGIconPicker; |
| 213 |
|