index.js
385 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { useContext, useRef } from '@wordpress/element'; |
| 3 | import { SelectControl } from '@wordpress/components'; |
| 4 | import { applyFilters } from '@wordpress/hooks'; |
| 5 | |
| 6 | import PanelArea from '../../../../components/panel-area'; |
| 7 | import getIcon from '../../../../utils/get-icon'; |
| 8 | import ControlsContext from '../../../../block-context'; |
| 9 | import LayoutControl from './components/LayoutControl'; |
| 10 | import isFlexItem from '../../../../utils/is-flex-item'; |
| 11 | import getAttribute from '../../../../utils/get-attribute'; |
| 12 | import getResponsivePlaceholder from '../../../../utils/get-responsive-placeholder'; |
| 13 | import FlexDirection from './components/FlexDirection'; |
| 14 | import LegacyLayoutControls from '../../../../blocks/container/components/LegacyLayoutControls'; |
| 15 | import ZIndex from './components/ZIndex'; |
| 16 | import FlexChild from '../flex-child-panel'; |
| 17 | import MigrateInnerContainer from '../../../../components/migrate-inner-container'; |
| 18 | import UnitControl from '../../../../components/unit-control'; |
| 19 | import { positionOptions, overflowOptions } from './options'; |
| 20 | import FlexControl from '../../../../components/flex-control'; |
| 21 | import getDeviceType from '../../../../utils/get-device-type'; |
| 22 | import ThemeWidth from './components/ThemeWidth'; |
| 23 | import { useStyleIndicator, useDeviceAttributes } from '../../../../hooks'; |
| 24 | import { getContentAttribute } from '../../../../utils/get-content-attribute'; |
| 25 | |
| 26 | export default function Layout( { attributes, setAttributes, computedStyles } ) { |
| 27 | const device = getDeviceType(); |
| 28 | const { id, blockName, supports: { layout, flexChildPanel } } = useContext( ControlsContext ); |
| 29 | const contentValue = getContentAttribute( attributes, blockName ); |
| 30 | const [ deviceAttributes ] = useDeviceAttributes( attributes, setAttributes ); |
| 31 | const panelControls = { |
| 32 | alignItems: false, |
| 33 | columnGap: false, |
| 34 | display: false, |
| 35 | flexDirection: false, |
| 36 | flexWrap: false, |
| 37 | justifyContent: false, |
| 38 | overflowX: false, |
| 39 | overflowY: false, |
| 40 | position: false, |
| 41 | rowGap: false, |
| 42 | zIndex: false, |
| 43 | }; |
| 44 | const { |
| 45 | dispatchControlGlobalStyle, |
| 46 | styleSources, |
| 47 | hasGlobalStyle, |
| 48 | contentWasUpdated, |
| 49 | } = useStyleIndicator( computedStyles, panelControls, contentValue, deviceAttributes ); |
| 50 | const panelRef = useRef( null ); |
| 51 | |
| 52 | const componentProps = { |
| 53 | attributes, |
| 54 | deviceType: device, |
| 55 | }; |
| 56 | |
| 57 | const { |
| 58 | display, |
| 59 | displayTablet, |
| 60 | displayMobile, |
| 61 | useInnerContainer, |
| 62 | zindex, |
| 63 | innerZindex, |
| 64 | align, |
| 65 | } = attributes; |
| 66 | |
| 67 | const directionValue = getAttribute( 'flexDirection', componentProps ) || getResponsivePlaceholder( 'flexDirection', attributes, device, 'row' ); |
| 68 | |
| 69 | function getLabel( defaultLabel, rules ) { |
| 70 | return applyFilters( |
| 71 | 'generateblocks.editor.control.label', |
| 72 | defaultLabel, |
| 73 | rules, |
| 74 | styleSources, |
| 75 | dispatchControlGlobalStyle, |
| 76 | contentWasUpdated, |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | const labels = { |
| 81 | display: getLabel( |
| 82 | __( 'Display', 'generateblocks' ), |
| 83 | { |
| 84 | display: getAttribute( 'display', componentProps ), |
| 85 | }, |
| 86 | ), |
| 87 | flexDirection: getLabel( |
| 88 | __( 'Direction', 'generateblocks' ), |
| 89 | { |
| 90 | flexDirection: directionValue, |
| 91 | } |
| 92 | ), |
| 93 | alignItems: getLabel( |
| 94 | __( 'Align Items', 'generateblocks' ), |
| 95 | { |
| 96 | alignItems: getAttribute( 'alignItems', componentProps ), |
| 97 | }, |
| 98 | ), |
| 99 | justifyContent: getLabel( |
| 100 | __( 'Justify Content', 'generateblocks' ), |
| 101 | { |
| 102 | justifyContent: getAttribute( 'justifyContent', componentProps ), |
| 103 | }, |
| 104 | ), |
| 105 | flexWrap: getLabel( |
| 106 | __( 'Wrap', 'generateblocks' ), |
| 107 | { |
| 108 | flexWrap: getAttribute( 'flexWrap', componentProps ), |
| 109 | }, |
| 110 | ), |
| 111 | columnGap: getLabel( |
| 112 | __( 'Column Gap', 'generateblocks' ), |
| 113 | { |
| 114 | columnGap: getAttribute( 'columnGap', componentProps ), |
| 115 | }, |
| 116 | ), |
| 117 | rowGap: getLabel( |
| 118 | __( 'Row Gap', 'generateblocks' ), |
| 119 | { |
| 120 | rowGap: getAttribute( 'rowGap', componentProps ), |
| 121 | }, |
| 122 | ), |
| 123 | position: getLabel( |
| 124 | __( 'Position', 'generateblocks' ), |
| 125 | { |
| 126 | position: getAttribute( 'position', componentProps ), |
| 127 | }, |
| 128 | ), |
| 129 | zIndex: getLabel( |
| 130 | __( 'z-index', 'generateblocks' ), |
| 131 | { |
| 132 | zIndex: getAttribute( 'zindex', componentProps ), |
| 133 | }, |
| 134 | ), |
| 135 | overflowX: getLabel( |
| 136 | __( 'Oveflow-x', 'generateblocks' ), |
| 137 | { |
| 138 | overflowX: getAttribute( 'overflowX', componentProps ), |
| 139 | }, |
| 140 | ), |
| 141 | overflowY: getLabel( |
| 142 | __( 'Oveflow-y', 'generateblocks' ), |
| 143 | { |
| 144 | overflowY: getAttribute( 'overflowY', componentProps ), |
| 145 | }, |
| 146 | ), |
| 147 | }; |
| 148 | |
| 149 | return ( |
| 150 | <PanelArea |
| 151 | title={ __( 'Layout', 'generateblocks' ) } |
| 152 | initialOpen={ false } |
| 153 | icon={ getIcon( 'layout' ) } |
| 154 | className="gblocks-panel-label" |
| 155 | id={ `${ id }Layout` } |
| 156 | ref={ panelRef } |
| 157 | hasGlobalStyle={ hasGlobalStyle } |
| 158 | > |
| 159 | { !! useInnerContainer && |
| 160 | <LegacyLayoutControls |
| 161 | attributes={ attributes } |
| 162 | setAttributes={ setAttributes } |
| 163 | deviceType={ device } |
| 164 | blockDefaults={ generateBlocksDefaults.container } |
| 165 | /> |
| 166 | } |
| 167 | |
| 168 | { layout.display && ! useInnerContainer && |
| 169 | <SelectControl |
| 170 | label={ labels.display } |
| 171 | options={ [ |
| 172 | { label: __( 'Default', 'generateblocks' ), value: '' }, |
| 173 | { label: 'Block', value: 'block' }, |
| 174 | { label: 'Inline Block', value: 'inline-block' }, |
| 175 | { label: 'Flex', value: 'flex' }, |
| 176 | { label: 'Inline Flex', value: 'inline-flex' }, |
| 177 | { label: 'Inline', value: 'inline' }, |
| 178 | ] } |
| 179 | value={ getAttribute( 'display', componentProps ) } |
| 180 | onChange={ ( nextDisplay ) => setAttributes( { |
| 181 | [ getAttribute( 'display', componentProps, true ) ]: nextDisplay, |
| 182 | } ) } |
| 183 | /> |
| 184 | } |
| 185 | |
| 186 | { isFlexItem( { device, display, displayTablet, displayMobile, computedStyles } ) && ! useInnerContainer && |
| 187 | <> |
| 188 | { layout.flexDirection && |
| 189 | <FlexDirection |
| 190 | value={ getAttribute( 'flexDirection', componentProps ) } |
| 191 | onChange={ ( value ) => { |
| 192 | const currentDirection = getAttribute( 'flexDirection', componentProps ); |
| 193 | value = currentDirection.includes( 'reverse' ) ? value + '-reverse' : value; |
| 194 | |
| 195 | setAttributes( { |
| 196 | [ getAttribute( 'flexDirection', componentProps, true ) ]: value !== getAttribute( 'flexDirection', componentProps ) ? value : '', |
| 197 | } ); |
| 198 | } } |
| 199 | onReverse={ ( value ) => { |
| 200 | if ( '' === value ) { |
| 201 | value = 'row'; |
| 202 | } |
| 203 | |
| 204 | value = value.includes( 'reverse' ) ? value.replace( '-reverse', '' ) : value + '-reverse'; |
| 205 | |
| 206 | setAttributes( { |
| 207 | [ getAttribute( 'flexDirection', componentProps, true ) ]: value, |
| 208 | } ); |
| 209 | } } |
| 210 | label={ labels.flexDirection } |
| 211 | directionValue={ directionValue } |
| 212 | fallback={ getResponsivePlaceholder( 'flexDirection', attributes, device, 'row' ) } |
| 213 | /> |
| 214 | } |
| 215 | |
| 216 | { layout.alignItems && |
| 217 | <LayoutControl |
| 218 | value={ getAttribute( 'alignItems', componentProps ) } |
| 219 | onChange={ ( value ) => setAttributes( { |
| 220 | [ getAttribute( 'alignItems', componentProps, true ) ]: value !== getAttribute( 'alignItems', componentProps ) ? value : '', |
| 221 | } ) } |
| 222 | label={ labels.alignItems } |
| 223 | attributeName="alignItems" |
| 224 | directionValue={ directionValue } |
| 225 | fallback={ getResponsivePlaceholder( 'alignItems', attributes, device, '' ) } |
| 226 | /> |
| 227 | } |
| 228 | |
| 229 | { layout.justifyContent && |
| 230 | <LayoutControl |
| 231 | value={ getAttribute( 'justifyContent', componentProps ) } |
| 232 | onChange={ ( value ) => setAttributes( { |
| 233 | [ getAttribute( 'justifyContent', componentProps, true ) ]: value !== getAttribute( 'justifyContent', componentProps ) ? value : '', |
| 234 | } ) } |
| 235 | label={ labels.justifyContent } |
| 236 | attributeName="justifyContent" |
| 237 | directionValue={ directionValue } |
| 238 | fallback={ getResponsivePlaceholder( 'justifyContent', attributes, device, '' ) } |
| 239 | /> |
| 240 | } |
| 241 | |
| 242 | { layout.flexWrap && |
| 243 | <LayoutControl |
| 244 | value={ getAttribute( 'flexWrap', componentProps ) } |
| 245 | onChange={ ( value ) => setAttributes( { |
| 246 | [ getAttribute( 'flexWrap', componentProps, true ) ]: value !== getAttribute( 'flexWrap', componentProps ) ? value : '', |
| 247 | } ) } |
| 248 | label={ labels.flexWrap } |
| 249 | attributeName="flexWrap" |
| 250 | directionValue={ directionValue } |
| 251 | fallback={ getResponsivePlaceholder( 'flexWrap', attributes, device, '' ) } |
| 252 | /> |
| 253 | } |
| 254 | |
| 255 | { ( layout.columnGap || layout.rowGap ) && |
| 256 | <FlexControl> |
| 257 | { layout.columnGap && |
| 258 | <UnitControl |
| 259 | label={ labels.columnGap } |
| 260 | id="gblocks-column-gap" |
| 261 | value={ getAttribute( 'columnGap', componentProps ) } |
| 262 | placeholder={ getResponsivePlaceholder( 'columnGap', attributes, device ) } |
| 263 | onChange={ ( value ) => setAttributes( { |
| 264 | [ getAttribute( 'columnGap', componentProps, true ) ]: value, |
| 265 | } ) } |
| 266 | /> |
| 267 | } |
| 268 | |
| 269 | { layout.rowGap && |
| 270 | <UnitControl |
| 271 | label={ labels.rowGap } |
| 272 | id="gblocks-row-gap" |
| 273 | value={ getAttribute( 'rowGap', componentProps ) } |
| 274 | placeholder={ getResponsivePlaceholder( 'rowGap', attributes, device ) } |
| 275 | onChange={ ( value ) => setAttributes( { |
| 276 | [ getAttribute( 'rowGap', componentProps, true ) ]: value, |
| 277 | } ) } |
| 278 | /> |
| 279 | } |
| 280 | </FlexControl> |
| 281 | } |
| 282 | </> |
| 283 | } |
| 284 | |
| 285 | { ! useInnerContainer && |
| 286 | <> |
| 287 | { layout.position && |
| 288 | <SelectControl |
| 289 | label={ labels.position } |
| 290 | value={ getAttribute( 'position', componentProps ) } |
| 291 | options={ positionOptions } |
| 292 | onChange={ ( value ) => setAttributes( { |
| 293 | [ getAttribute( 'position', componentProps, true ) ]: value, |
| 294 | } ) } |
| 295 | /> |
| 296 | } |
| 297 | |
| 298 | { layout.overflow && |
| 299 | <FlexControl> |
| 300 | <SelectControl |
| 301 | label={ labels.overflowX } |
| 302 | value={ getAttribute( 'overflowX', componentProps ) } |
| 303 | options={ overflowOptions } |
| 304 | onChange={ ( value ) => setAttributes( { |
| 305 | [ getAttribute( 'overflowX', componentProps, true ) ]: value, |
| 306 | } ) } |
| 307 | /> |
| 308 | |
| 309 | <SelectControl |
| 310 | label={ labels.overflowY } |
| 311 | value={ getAttribute( 'overflowY', componentProps ) } |
| 312 | options={ overflowOptions } |
| 313 | onChange={ ( value ) => setAttributes( { |
| 314 | [ getAttribute( 'overflowY', componentProps, true ) ]: value, |
| 315 | } ) } |
| 316 | /> |
| 317 | </FlexControl> |
| 318 | } |
| 319 | </> |
| 320 | } |
| 321 | |
| 322 | { layout.zIndex && |
| 323 | <> |
| 324 | { !! useInnerContainer && 'Desktop' === device && |
| 325 | <> |
| 326 | <ZIndex |
| 327 | label={ __( 'Outer z-index', 'generateblocks' ) } |
| 328 | value={ zindex } |
| 329 | onChange={ ( value ) => setAttributes( { zindex: value } ) } |
| 330 | /> |
| 331 | |
| 332 | <ZIndex |
| 333 | label={ __( 'Inner z-index', 'generateblocks' ) } |
| 334 | value={ innerZindex } |
| 335 | onChange={ ( value ) => setAttributes( { innerZindex: value } ) } |
| 336 | /> |
| 337 | </> |
| 338 | } |
| 339 | |
| 340 | { ! useInnerContainer && |
| 341 | <ZIndex |
| 342 | label={ labels.zIndex } |
| 343 | value={ getAttribute( 'zindex', componentProps ) } |
| 344 | placeholder={ getResponsivePlaceholder( 'zindex', attributes, device ) } |
| 345 | onChange={ ( value ) => setAttributes( { |
| 346 | [ getAttribute( 'zindex', componentProps, true ) ]: value, |
| 347 | [ getAttribute( 'position', componentProps, true ) ]: ! getAttribute( 'position', componentProps ) |
| 348 | ? 'relative' |
| 349 | : getAttribute( 'position', componentProps ), |
| 350 | } ) } |
| 351 | /> |
| 352 | } |
| 353 | </> |
| 354 | } |
| 355 | |
| 356 | { layout.themeWidth && |
| 357 | <> |
| 358 | <ThemeWidth |
| 359 | value={ align } |
| 360 | onChange={ ( value ) => { |
| 361 | setAttributes( { |
| 362 | align: value, |
| 363 | } ); |
| 364 | } } |
| 365 | /> |
| 366 | </> |
| 367 | } |
| 368 | |
| 369 | { !! useInnerContainer && |
| 370 | <MigrateInnerContainer |
| 371 | attributes={ attributes } |
| 372 | setAttributes={ setAttributes } |
| 373 | /> |
| 374 | } |
| 375 | |
| 376 | { flexChildPanel.enabled && |
| 377 | <FlexChild |
| 378 | attributes={ attributes } |
| 379 | setAttributes={ setAttributes } |
| 380 | /> |
| 381 | } |
| 382 | </PanelArea> |
| 383 | ); |
| 384 | } |
| 385 |