| 1 |
/** |
| 2 |
* The Quick Access Sidebar. |
| 3 |
*/ |
| 4 |
import { useLayoutEffect, useState, useEffect } from '@wordpress/element'; |
| 5 |
import style from '../editor.lazy.scss'; |
| 6 |
import Blocks from './blocks'; |
| 7 |
import PopoverModal from './Modal'; |
| 8 |
import getApiData from '@Controls/getApiData'; |
| 9 |
import { __ } from '@wordpress/i18n'; |
| 10 |
import { Popover } from '@wordpress/components'; |
| 11 |
|
| 12 |
const Sidebar = () => { |
| 13 |
const [ |
| 14 |
defaultAllowedQuickSidebarBlocks, |
| 15 |
setDefaultAllowedQuickSidebarBlocks, |
| 16 |
] = useState( srfm_quick_sidebar_blocks.allowed_blocks ); |
| 17 |
const [ isPopoverVisible, setPopoverVisible ] = useState( false ); |
| 18 |
|
| 19 |
// State to track the sorting status |
| 20 |
const [ isSorting, setSorting ] = useState( false ); |
| 21 |
const [ hovering, setHovering ] = useState( null ); |
| 22 |
// State to track the active tab |
| 23 |
const [ activeTab, setActiveTab ] = useState( 0 ); |
| 24 |
const handleMouseOver = ( button ) => setHovering( button ); |
| 25 |
|
| 26 |
const handleMouseOut = () => setHovering( false ); |
| 27 |
// Function to handle outside click |
| 28 |
useEffect( () => { |
| 29 |
const handleOutsideClick = ( event ) => { |
| 30 |
// Check if the click is outside the QAB sidebar and popover |
| 31 |
if ( |
| 32 |
document.querySelector( '.srfm-ee-quick-access' ) && |
| 33 |
! document |
| 34 |
.querySelector( '.srfm-ee-quick-access' ) |
| 35 |
.contains( event.target ) && |
| 36 |
! document.querySelector( '.popover' )?.contains( event.target ) |
| 37 |
) { |
| 38 |
setPopoverVisible( false ); |
| 39 |
setSorting( false ); |
| 40 |
|
| 41 |
setActiveTab( 0 ); |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
// Add event listener when component mounts |
| 46 |
document.body.addEventListener( 'click', handleOutsideClick ); |
| 47 |
|
| 48 |
// Remove event listener when component unmounts |
| 49 |
return () => { |
| 50 |
document.body.removeEventListener( 'click', handleOutsideClick ); |
| 51 |
}; |
| 52 |
}, [] ); // Empty array ensures this effect runs only once when the component mounts |
| 53 |
|
| 54 |
// Function to handle tab click |
| 55 |
const handleTabClick = ( index ) => { |
| 56 |
setActiveTab( index ); |
| 57 |
}; |
| 58 |
|
| 59 |
useLayoutEffect( () => { |
| 60 |
style.use(); |
| 61 |
return () => { |
| 62 |
style.unuse(); |
| 63 |
}; |
| 64 |
}, [] ); |
| 65 |
// Function to open the popover |
| 66 |
const openPopover = () => { |
| 67 |
setPopoverVisible( true ); |
| 68 |
setSorting( false ); |
| 69 |
}; |
| 70 |
// Function to close the popover |
| 71 |
const closePopover = () => { |
| 72 |
setPopoverVisible( false ); |
| 73 |
setSorting( false ); |
| 74 |
}; |
| 75 |
// Function to enable re-arrange |
| 76 |
const enableRearrange = () => { |
| 77 |
setSorting( true ); |
| 78 |
}; |
| 79 |
|
| 80 |
function updateDefaultAllowedQuickSidebarBlocks( allowedBlocks ) { |
| 81 |
setDefaultAllowedQuickSidebarBlocks( allowedBlocks ); |
| 82 |
} |
| 83 |
// Saving the allowed blocks to the database. |
| 84 |
const saveOptionToDatabase = ( allowedBlocks ) => { |
| 85 |
// update allowedBlocks. |
| 86 |
updateDefaultAllowedQuickSidebarBlocks( allowedBlocks ); |
| 87 |
// Create an object with the srfm_ajax_nonce and confirmation properties. |
| 88 |
const data = { |
| 89 |
security: srfm_quick_sidebar_blocks.srfm_ajax_nonce, |
| 90 |
defaultAllowedQuickSidebarBlocks: JSON.stringify( allowedBlocks ), |
| 91 |
}; |
| 92 |
// Call the getApiData function with the specified parameters. |
| 93 |
getApiData( { |
| 94 |
url: srfm_quick_sidebar_blocks.srfm_ajax_url, |
| 95 |
action: 'srfm_global_update_allowed_block', |
| 96 |
data, |
| 97 |
} ); |
| 98 |
}; |
| 99 |
return ( |
| 100 |
<> |
| 101 |
<div className="srfm-ee-quick-access"> |
| 102 |
<div className="srfm-ee-quick-access__sidebar"> |
| 103 |
{ /* The block selection buttons will come here. */ } |
| 104 |
<div className="srfm-ee-quick-access__sidebar--blocks"> |
| 105 |
<Blocks |
| 106 |
defaultAllowedQuickSidebarBlocks={ |
| 107 |
defaultAllowedQuickSidebarBlocks |
| 108 |
} |
| 109 |
updateDefaultAllowedQuickSidebarBlocks={ |
| 110 |
updateDefaultAllowedQuickSidebarBlocks |
| 111 |
} |
| 112 |
saveOptionToDatabase={ saveOptionToDatabase } |
| 113 |
enableRearrange={ isSorting } |
| 114 |
/> |
| 115 |
</div> |
| 116 |
</div> |
| 117 |
{ /* The sidebar actions will come here - like the plus sign. */ } |
| 118 |
<div className="srfm-ee-quick-access__sidebar--tabs-container"> |
| 119 |
<div className="srfm-ee-quick-access__sidebar--tabs"> |
| 120 |
{ /* Map over your tab items */ } |
| 121 |
{ [ 'add', 'sort' ].map( ( tab, index ) => ( |
| 122 |
<div |
| 123 |
key={ index } |
| 124 |
className={ `tab ${ |
| 125 |
activeTab === index ? 'active' : '' |
| 126 |
}` } |
| 127 |
onClick={ () => handleTabClick( index ) } |
| 128 |
> |
| 129 |
{ 'add' === tab && ( |
| 130 |
<div className="srfm-ee-quick-access__sidebar--actions"> |
| 131 |
<div className="srfm-ee-quick-access__sidebar--actions--plus"> |
| 132 |
<div className="srfm-quick-action-sidebar-wrap"> |
| 133 |
<div |
| 134 |
id="plus-icon" |
| 135 |
onClick={ openPopover } |
| 136 |
onMouseOver={ () => |
| 137 |
handleMouseOver( 'add' ) |
| 138 |
} |
| 139 |
onMouseOut={ |
| 140 |
handleMouseOut |
| 141 |
} |
| 142 |
onFocus={ () => |
| 143 |
handleMouseOver( 'add' ) |
| 144 |
} |
| 145 |
onBlur={ handleMouseOut } |
| 146 |
> |
| 147 |
<svg |
| 148 |
xmlns="http://www.w3.org/2000/svg" |
| 149 |
viewBox="0 0 24 24" |
| 150 |
width="24" |
| 151 |
height="24" |
| 152 |
aria-hidden="true" |
| 153 |
fill="#fff" |
| 154 |
focusable="false" |
| 155 |
> |
| 156 |
<path d="M18 11.2h-5.2V6h-1.6v5.2H6v1.6h5.2V18h1.6v-5.2H18z"></path> |
| 157 |
</svg> |
| 158 |
</div> |
| 159 |
{ isPopoverVisible && ( |
| 160 |
<PopoverModal |
| 161 |
closePopover={ |
| 162 |
closePopover |
| 163 |
} |
| 164 |
updateDefaultAllowedQuickSidebarBlocks={ |
| 165 |
updateDefaultAllowedQuickSidebarBlocks |
| 166 |
} |
| 167 |
defaultAllowedQuickSidebarBlocks={ |
| 168 |
defaultAllowedQuickSidebarBlocks |
| 169 |
} |
| 170 |
saveOptionToDatabase={ |
| 171 |
saveOptionToDatabase |
| 172 |
} |
| 173 |
/> |
| 174 |
) } |
| 175 |
{ hovering === 'add' && ( |
| 176 |
<Popover |
| 177 |
placement="right" |
| 178 |
className="srfm-ee-quick-access__sidebar--blocks--block--icon--name" |
| 179 |
> |
| 180 |
<div className="block-title"> |
| 181 |
{ __( |
| 182 |
'Add blocks to Quick Action Bar', |
| 183 |
'sureforms' |
| 184 |
) } |
| 185 |
</div> |
| 186 |
</Popover> |
| 187 |
) } |
| 188 |
</div> |
| 189 |
</div> |
| 190 |
</div> |
| 191 |
) } |
| 192 |
{ 'sort' === tab && ( |
| 193 |
<div className="srfm-ee-quick-access__sidebar--actions"> |
| 194 |
<div className="srfm-ee-quick-access__sidebar--actions--plus rearrange"> |
| 195 |
<div className="srfm-quick-action-sidebar-wrap"> |
| 196 |
<div |
| 197 |
id="rearrange-icon" |
| 198 |
onClick={ enableRearrange } |
| 199 |
onMouseOver={ () => |
| 200 |
handleMouseOver( |
| 201 |
'sort' |
| 202 |
) |
| 203 |
} |
| 204 |
onMouseOut={ |
| 205 |
handleMouseOut |
| 206 |
} |
| 207 |
onFocus={ () => |
| 208 |
handleMouseOver( |
| 209 |
'sort' |
| 210 |
) |
| 211 |
} |
| 212 |
onBlur={ handleMouseOut } |
| 213 |
> |
| 214 |
<svg |
| 215 |
xmlns="http://www.w3.org/2000/svg" |
| 216 |
viewBox="0 0 24 24" |
| 217 |
width="24" |
| 218 |
height="24" |
| 219 |
aria-hidden="true" |
| 220 |
fill="#fff" |
| 221 |
focusable="false" |
| 222 |
> |
| 223 |
<path d="m19 7-3-3-8.5 8.5-1 4 4-1L19 7Zm-7 11.5H5V20h7v-1.5Z"></path> |
| 224 |
</svg> |
| 225 |
{ hovering === 'sort' && ( |
| 226 |
<Popover |
| 227 |
placement="right" |
| 228 |
className="srfm-ee-quick-access__sidebar--blocks--block--icon--name" |
| 229 |
> |
| 230 |
<div className="block-title"> |
| 231 |
{ __( |
| 232 |
'Re-arrange block inside Quick Action Bar', |
| 233 |
'sureforms' |
| 234 |
) } |
| 235 |
</div> |
| 236 |
</Popover> |
| 237 |
) } |
| 238 |
</div> |
| 239 |
</div> |
| 240 |
</div> |
| 241 |
</div> |
| 242 |
) } |
| 243 |
</div> |
| 244 |
) ) } |
| 245 |
</div> |
| 246 |
</div> |
| 247 |
</div> |
| 248 |
</> |
| 249 |
); |
| 250 |
}; |
| 251 |
|
| 252 |
export default Sidebar; |
| 253 |
|