css
6 years ago
attributes.js
6 years ago
block.js
6 years ago
edit.js
6 years ago
editor.scss
6 years ago
save.js
6 years ago
edit.js
886 lines
| 1 | /** |
| 2 | * Block: Grid |
| 3 | */ |
| 4 | |
| 5 | import classnames from 'classnames'; |
| 6 | import getIcon from '../../utils/get-icon'; |
| 7 | import ResponsiveTabs from '../../components/responsive-tabs'; |
| 8 | import DesktopCSS from './css/desktop.js'; |
| 9 | import PanelArea from '../../components/panel-area/'; |
| 10 | |
| 11 | const { __ } = wp.i18n; |
| 12 | |
| 13 | const { |
| 14 | TextControl, |
| 15 | SelectControl, |
| 16 | Tooltip, |
| 17 | Placeholder, |
| 18 | Button, |
| 19 | Toolbar, |
| 20 | } = wp.components; |
| 21 | |
| 22 | const { |
| 23 | Fragment, |
| 24 | Component, |
| 25 | } = wp.element; |
| 26 | |
| 27 | const { |
| 28 | InspectorControls, |
| 29 | InnerBlocks, |
| 30 | BlockControls, |
| 31 | } = wp.blockEditor; |
| 32 | |
| 33 | const { |
| 34 | createBlock, |
| 35 | } = wp.blocks; |
| 36 | |
| 37 | const { |
| 38 | applyFilters, |
| 39 | } = wp.hooks; |
| 40 | |
| 41 | const ELEMENT_ID_REGEX = /[\s#]/g; |
| 42 | const gbGridIds = []; |
| 43 | |
| 44 | class GenerateBlockGridContainer extends Component { |
| 45 | constructor() { |
| 46 | super( ...arguments ); |
| 47 | |
| 48 | this.state = { |
| 49 | selectedLayout: false, |
| 50 | selectedDevice: 'desktop', |
| 51 | }; |
| 52 | |
| 53 | this.onLayoutSelect = this.onLayoutSelect.bind( this ); |
| 54 | this.getColumnsFromLayout = this.getColumnsFromLayout.bind( this ); |
| 55 | this.getLayoutsSelector = this.getLayoutsSelector.bind( this ); |
| 56 | } |
| 57 | |
| 58 | componentDidMount() { |
| 59 | const id = this.props.clientId.substr( 2, 9 ).replace( '-', '' ); |
| 60 | |
| 61 | if ( ! this.props.attributes.uniqueId ) { |
| 62 | this.props.setAttributes( { |
| 63 | uniqueId: id, |
| 64 | } ); |
| 65 | |
| 66 | gbGridIds.push( id ); |
| 67 | } else if ( gbGridIds.includes( this.props.attributes.uniqueId ) ) { |
| 68 | this.props.setAttributes( { |
| 69 | uniqueId: id, |
| 70 | } ); |
| 71 | |
| 72 | gbGridIds.push( id ); |
| 73 | } else { |
| 74 | gbGridIds.push( this.props.attributes.uniqueId ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | componentDidUpdate() { |
| 79 | const { |
| 80 | attributes, |
| 81 | setAttributes, |
| 82 | clientId, |
| 83 | } = this.props; |
| 84 | |
| 85 | let { |
| 86 | columns, |
| 87 | } = attributes; |
| 88 | |
| 89 | if ( this.state.selectedLayout ) { |
| 90 | const columnsData = this.getColumnsFromLayout( this.state.selectedLayout ); |
| 91 | |
| 92 | columnsData.forEach( ( colAttrs ) => { |
| 93 | wp.data.dispatch( 'core/block-editor' ).insertBlocks( createBlock( 'generateblocks/container', colAttrs ), undefined, clientId, false ); |
| 94 | } ); |
| 95 | |
| 96 | columns = columnsData.length; |
| 97 | |
| 98 | setAttributes( { |
| 99 | columns, |
| 100 | } ); |
| 101 | |
| 102 | this.setState( { |
| 103 | selectedLayout: false, |
| 104 | } ); |
| 105 | } else { |
| 106 | const parentBlock = wp.data.select( 'core/block-editor' ).getBlocksByClientId( clientId )[ 0 ]; |
| 107 | |
| 108 | if ( parentBlock ) { |
| 109 | const childBlocks = parentBlock.innerBlocks; |
| 110 | columns = childBlocks.length; |
| 111 | |
| 112 | setAttributes( { |
| 113 | columns, |
| 114 | } ); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get columns sizes array from layout string |
| 121 | * |
| 122 | * @param {string} layout - layout data. Example: `3-6-3` |
| 123 | * |
| 124 | * @return {array}. |
| 125 | */ |
| 126 | getColumnsFromLayout( layout ) { |
| 127 | const result = []; |
| 128 | const columnsData = layout.split( '-' ); |
| 129 | |
| 130 | let i = 0; |
| 131 | columnsData.forEach( () => { |
| 132 | const colAttrs = { |
| 133 | isGrid: true, |
| 134 | gridId: this.props.attributes.uniqueId, |
| 135 | paddingTop: generateBlocksStyling.container.gridItemPaddingTop || '0', |
| 136 | paddingRight: generateBlocksStyling.container.gridItemPaddingRight || '0', |
| 137 | paddingBottom: generateBlocksStyling.container.gridItemPaddingBottom || '0', |
| 138 | paddingLeft: generateBlocksStyling.container.gridItemPaddingLeft || '0', |
| 139 | }; |
| 140 | |
| 141 | colAttrs.width = Number( columnsData[ i ] ); |
| 142 | i++; |
| 143 | |
| 144 | result.push( colAttrs ); |
| 145 | } ); |
| 146 | |
| 147 | return result; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Layouts selector when no columns selected. |
| 152 | * |
| 153 | * @return {jsx}. |
| 154 | */ |
| 155 | getLayoutsSelector() { |
| 156 | const layouts = [ |
| 157 | '100', |
| 158 | '50-50', |
| 159 | '33.33-33.33-33.33', |
| 160 | '25-25-25-25', |
| 161 | |
| 162 | '25-75', |
| 163 | '75-25', |
| 164 | '25-25-50', |
| 165 | '25-50-25', |
| 166 | |
| 167 | '50-25-25', |
| 168 | '20-60-20', |
| 169 | '20-20-20-20-20', |
| 170 | '16.66-16.66-16.66-16.66-16.66-16.66', |
| 171 | ]; |
| 172 | |
| 173 | return ( |
| 174 | <Placeholder |
| 175 | label={ __( 'Grid', 'generateblocks' ) } |
| 176 | instructions={ __( 'Select one layout to get started.', 'generateblocks' ) } |
| 177 | className="gb-select-layout" |
| 178 | > |
| 179 | <div className="gb-grid-wrapper-layout-preview"> |
| 180 | { layouts.map( ( layout ) => { |
| 181 | const columnsData = this.getColumnsFromLayout( layout ); |
| 182 | |
| 183 | return ( |
| 184 | <button |
| 185 | key={ `layout-${ layout }` } |
| 186 | className="gb-grid-wrapper-layout-preview-btn" |
| 187 | onClick={ () => this.onLayoutSelect( layout ) } |
| 188 | > |
| 189 | { columnsData.map( ( colAttrs, i ) => { |
| 190 | return ( |
| 191 | <div |
| 192 | key={ `layout-${ layout }-col-${ i }` } |
| 193 | className={ classnames( 'gb-col', `gb-col-${ colAttrs.width }` ) } |
| 194 | /> |
| 195 | ); |
| 196 | } ) } |
| 197 | </button> |
| 198 | ); |
| 199 | } ) } |
| 200 | </div> |
| 201 | </Placeholder> |
| 202 | ); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Select predefined layout. |
| 207 | * |
| 208 | * @param {String} layout layout string. |
| 209 | */ |
| 210 | onLayoutSelect( layout ) { |
| 211 | this.setState( { |
| 212 | selectedLayout: layout, |
| 213 | } ); |
| 214 | } |
| 215 | |
| 216 | render() { |
| 217 | const { |
| 218 | attributes, |
| 219 | setAttributes, |
| 220 | clientId, |
| 221 | } = this.props; |
| 222 | |
| 223 | const { |
| 224 | selectedDevice, |
| 225 | } = this.state; |
| 226 | |
| 227 | const { |
| 228 | uniqueId, |
| 229 | elementId, |
| 230 | cssClasses, |
| 231 | columns, |
| 232 | horizontalGap, |
| 233 | verticalGap, |
| 234 | verticalAlignment, |
| 235 | horizontalGapTablet, |
| 236 | verticalGapTablet, |
| 237 | verticalAlignmentTablet, |
| 238 | horizontalGapMobile, |
| 239 | verticalGapMobile, |
| 240 | verticalAlignmentMobile, |
| 241 | horizontalAlignment, |
| 242 | horizontalAlignmentTablet, |
| 243 | horizontalAlignmentMobile, |
| 244 | } = attributes; |
| 245 | |
| 246 | const horizontalGapPlaceholderTablet = horizontalGap || 0 === horizontalGap ? horizontalGap : '', |
| 247 | verticalGapPlaceholderTablet = verticalGap || 0 === verticalGap ? verticalGap : ''; |
| 248 | |
| 249 | let horizontalGapPlaceholderMobile = horizontalGap || 0 === horizontalGap ? horizontalGap : '', |
| 250 | verticalGapPlaceholderMobile = verticalGap || 0 === verticalGap ? verticalGap : ''; |
| 251 | |
| 252 | if ( horizontalGapTablet ) { |
| 253 | horizontalGapPlaceholderMobile = horizontalGapTablet; |
| 254 | } |
| 255 | |
| 256 | if ( verticalGapTablet ) { |
| 257 | verticalGapPlaceholderMobile = verticalGapTablet; |
| 258 | } |
| 259 | |
| 260 | return ( |
| 261 | <Fragment> |
| 262 | <BlockControls> |
| 263 | <Toolbar> |
| 264 | <Tooltip text={ __( 'Add Grid Item', 'generateblocks' ) }> |
| 265 | <Button |
| 266 | className="gblocks-block-control-icon gblocks-add-grid-item" |
| 267 | icon={ getIcon( 'addContainer' ) } |
| 268 | onClick={ () => { |
| 269 | wp.data.dispatch( 'core/block-editor' ).insertBlocks( |
| 270 | createBlock( 'generateblocks/container', { |
| 271 | isGrid: true, |
| 272 | gridId: uniqueId, |
| 273 | paddingTop: generateBlocksStyling.container.gridItemPaddingTop || '0', |
| 274 | paddingRight: generateBlocksStyling.container.gridItemPaddingRight || '0', |
| 275 | paddingBottom: generateBlocksStyling.container.gridItemPaddingBottom || '0', |
| 276 | paddingLeft: generateBlocksStyling.container.gridItemPaddingLeft || '0', |
| 277 | } ), |
| 278 | undefined, |
| 279 | clientId |
| 280 | ); |
| 281 | } } |
| 282 | /> |
| 283 | </Tooltip> |
| 284 | </Toolbar> |
| 285 | </BlockControls> |
| 286 | <InspectorControls> |
| 287 | <ResponsiveTabs { ...this.props } |
| 288 | selectedDevice={ selectedDevice } |
| 289 | onClick={ ( device ) => { |
| 290 | this.setState( { |
| 291 | selectedDevice: device, |
| 292 | } ); |
| 293 | } } |
| 294 | /> |
| 295 | |
| 296 | <PanelArea { ...this.props } |
| 297 | id={ 'gridLayout' } |
| 298 | state={ this.state } |
| 299 | > |
| 300 | { 'desktop' === selectedDevice && ( |
| 301 | <Fragment> |
| 302 | <div className="components-gblocks-control__header"> |
| 303 | <div className="components-gblocks-control__label"> |
| 304 | { __( 'Horizontal Gap', 'generateblocks' ) } |
| 305 | </div> |
| 306 | |
| 307 | <div className="components-gblocks-control__units"> |
| 308 | <Tooltip text={ __( 'Pixel Units' ) } key={ 'h-gap-unit' }> |
| 309 | <Button |
| 310 | key={ 'h-gap-unit' } |
| 311 | isSmall |
| 312 | isPrimary={ true } |
| 313 | /* translators: %s: values associated with CSS syntax, 'Pixel', 'Em', 'Percentage' */ |
| 314 | aria-label={ __( 'Pixel Units' ) } |
| 315 | > |
| 316 | px |
| 317 | </Button> |
| 318 | </Tooltip> |
| 319 | </div> |
| 320 | </div> |
| 321 | |
| 322 | <div className="components-base-control components-gblocks-typography-control__inputs"> |
| 323 | <TextControl |
| 324 | type={ 'number' } |
| 325 | value={ horizontalGap || 0 === horizontalGap ? horizontalGap : '' } |
| 326 | min="0" |
| 327 | onChange={ ( value ) => { |
| 328 | // No hyphens allowed here. |
| 329 | value = value.toString().replace( /-/g, '' ); |
| 330 | |
| 331 | setAttributes( { |
| 332 | horizontalGap: value, |
| 333 | } ); |
| 334 | } } |
| 335 | onBlur={ () => { |
| 336 | if ( ! horizontalGap && generateBlocksDefaults.gridContainer.horizontalGap ) { |
| 337 | // If we have no value and a default exists, set to 0 to prevent default from coming back. |
| 338 | setAttributes( { |
| 339 | horizontalGap: 0, |
| 340 | } ); |
| 341 | } else { |
| 342 | setAttributes( { |
| 343 | horizontalGap: parseFloat( horizontalGap ), |
| 344 | } ); |
| 345 | } |
| 346 | } } |
| 347 | onClick={ ( e ) => { |
| 348 | // Make sure onBlur fires in Firefox. |
| 349 | e.currentTarget.focus(); |
| 350 | } } |
| 351 | /> |
| 352 | |
| 353 | <Button |
| 354 | isSmall |
| 355 | isSecondary |
| 356 | className="components-gblocks-default-number" |
| 357 | onClick={ () => { |
| 358 | setAttributes( { |
| 359 | horizontalGap: generateBlocksDefaults.gridContainer.horizontalGap, |
| 360 | } ); |
| 361 | } } |
| 362 | > |
| 363 | { __( 'Reset', 'generateblocks' ) } |
| 364 | </Button> |
| 365 | </div> |
| 366 | |
| 367 | <div className="components-gblocks-control__header"> |
| 368 | <div className="components-gblocks-control__label"> |
| 369 | { __( 'Vertical Gap', 'generateblocks' ) } |
| 370 | </div> |
| 371 | |
| 372 | <div className="components-gblocks-control__units"> |
| 373 | <Tooltip text={ __( 'Pixel Units' ) } key={ 'v-gap-unit' }> |
| 374 | <Button |
| 375 | key={ 'v-gap-unit' } |
| 376 | isSmall |
| 377 | isPrimary={ true } |
| 378 | /* translators: %s: values associated with CSS syntax, 'Pixel', 'Em', 'Percentage' */ |
| 379 | aria-label={ __( 'Pixel Units' ) } |
| 380 | > |
| 381 | px |
| 382 | </Button> |
| 383 | </Tooltip> |
| 384 | </div> |
| 385 | </div> |
| 386 | |
| 387 | <div className="components-base-control components-gblocks-typography-control__inputs"> |
| 388 | <TextControl |
| 389 | type={ 'number' } |
| 390 | value={ verticalGap || 0 === verticalGap ? verticalGap : '' } |
| 391 | min="0" |
| 392 | onChange={ ( value ) => { |
| 393 | // No negative values allowed here. |
| 394 | value = value.toString().replace( /-/g, '' ); |
| 395 | |
| 396 | setAttributes( { |
| 397 | verticalGap: value, |
| 398 | } ); |
| 399 | } } |
| 400 | onBlur={ () => { |
| 401 | if ( ! verticalGap && generateBlocksDefaults.gridContainer.verticalGap ) { |
| 402 | // If we have no value and a default exists, set to 0 to prevent default from coming back. |
| 403 | setAttributes( { |
| 404 | verticalGap: 0, |
| 405 | } ); |
| 406 | } else { |
| 407 | setAttributes( { |
| 408 | verticalGap: parseFloat( verticalGap ), |
| 409 | } ); |
| 410 | } |
| 411 | } } |
| 412 | onClick={ ( e ) => { |
| 413 | // Make sure onBlur fires in Firefox. |
| 414 | e.currentTarget.focus(); |
| 415 | } } |
| 416 | /> |
| 417 | |
| 418 | <Button |
| 419 | isSmall |
| 420 | isSecondary |
| 421 | className="components-gblocks-default-number" |
| 422 | onClick={ () => { |
| 423 | setAttributes( { |
| 424 | verticalGap: generateBlocksDefaults.gridContainer.verticalGap, |
| 425 | } ); |
| 426 | } } |
| 427 | > |
| 428 | { __( 'Reset', 'generateblocks' ) } |
| 429 | </Button> |
| 430 | </div> |
| 431 | |
| 432 | <SelectControl |
| 433 | label={ __( 'Vertical Alignment', 'generateblocks' ) } |
| 434 | value={ verticalAlignment } |
| 435 | help={ __( 'Align grid items. Removes same height columns and overrides grid item content alignment.', 'generateblocks' ) } |
| 436 | options={ [ |
| 437 | { label: __( 'Default', 'generateblocks' ), value: '' }, |
| 438 | { label: __( 'Top', 'generateblocks' ), value: 'flex-start' }, |
| 439 | { label: __( 'Center', 'generateblocks' ), value: 'center' }, |
| 440 | { label: __( 'Bottom', 'generateblocks' ), value: 'flex-end' }, |
| 441 | ] } |
| 442 | onChange={ ( value ) => { |
| 443 | setAttributes( { |
| 444 | verticalAlignment: value, |
| 445 | } ); |
| 446 | } } |
| 447 | /> |
| 448 | |
| 449 | <SelectControl |
| 450 | label={ __( 'Horizontal Alignment', 'generateblocks' ) } |
| 451 | value={ horizontalAlignment } |
| 452 | options={ [ |
| 453 | { label: __( 'Default', 'generateblocks' ), value: '' }, |
| 454 | { label: __( 'Left', 'generateblocks' ), value: 'flex-start' }, |
| 455 | { label: __( 'Center', 'generateblocks' ), value: 'center' }, |
| 456 | { label: __( 'Right', 'generateblocks' ), value: 'flex-end' }, |
| 457 | ] } |
| 458 | onChange={ ( value ) => { |
| 459 | setAttributes( { |
| 460 | horizontalAlignment: value, |
| 461 | } ); |
| 462 | } } |
| 463 | /> |
| 464 | </Fragment> |
| 465 | ) } |
| 466 | |
| 467 | { 'tablet' === selectedDevice && ( |
| 468 | <Fragment> |
| 469 | <div className="components-gblocks-control__header"> |
| 470 | <div className="components-gblocks-control__label"> |
| 471 | { __( 'Horizontal Gap', 'generateblocks' ) } |
| 472 | </div> |
| 473 | |
| 474 | <div className="components-gblocks-control__units"> |
| 475 | <Tooltip text={ __( 'Pixel Units' ) } key={ 'h-gap-tablet-unit' }> |
| 476 | <Button |
| 477 | key={ 'h-gap-tablet-unit' } |
| 478 | isSmall |
| 479 | isPrimary={ true } |
| 480 | /* translators: %s: values associated with CSS syntax, 'Pixel', 'Em', 'Percentage' */ |
| 481 | aria-label={ __( 'Pixel Units' ) } |
| 482 | > |
| 483 | px |
| 484 | </Button> |
| 485 | </Tooltip> |
| 486 | </div> |
| 487 | </div> |
| 488 | |
| 489 | <div className="components-base-control components-gblocks-typography-control__inputs"> |
| 490 | <TextControl |
| 491 | type={ 'number' } |
| 492 | value={ horizontalGapTablet || 0 === horizontalGapTablet ? horizontalGapTablet : '' } |
| 493 | min="0" |
| 494 | placeholder={ horizontalGapPlaceholderTablet } |
| 495 | onChange={ ( value ) => { |
| 496 | // No negative values allowed here. |
| 497 | value = value.toString().replace( /-/g, '' ); |
| 498 | |
| 499 | setAttributes( { |
| 500 | horizontalGapTablet: value, |
| 501 | } ); |
| 502 | } } |
| 503 | onBlur={ () => { |
| 504 | if ( ! horizontalGapTablet && generateBlocksDefaults.gridContainer.horizontalGapTablet ) { |
| 505 | // If we have no value and a default exists, set to 0 to prevent default from coming back. |
| 506 | setAttributes( { |
| 507 | horizontalGapTablet: 0, |
| 508 | } ); |
| 509 | } else { |
| 510 | setAttributes( { |
| 511 | horizontalGapTablet: parseFloat( horizontalGapTablet ), |
| 512 | } ); |
| 513 | } |
| 514 | } } |
| 515 | onClick={ ( e ) => { |
| 516 | // Make sure onBlur fires in Firefox. |
| 517 | e.currentTarget.focus(); |
| 518 | } } |
| 519 | /> |
| 520 | |
| 521 | <Button |
| 522 | isSmall |
| 523 | isSecondary |
| 524 | className="components-gblocks-default-number" |
| 525 | onClick={ () => { |
| 526 | setAttributes( { |
| 527 | horizontalGapTablet: generateBlocksDefaults.gridContainer.horizontalGapTablet, |
| 528 | } ); |
| 529 | } } |
| 530 | > |
| 531 | { __( 'Reset', 'generateblocks' ) } |
| 532 | </Button> |
| 533 | </div> |
| 534 | |
| 535 | <div className="components-gblocks-control__header"> |
| 536 | <div className="components-gblocks-control__label"> |
| 537 | { __( 'Vertical Gap', 'generateblocks' ) } |
| 538 | </div> |
| 539 | |
| 540 | <div className="components-gblocks-control__units"> |
| 541 | <Tooltip text={ __( 'Pixel Units' ) } key={ 'v-gap-tablet-unit' }> |
| 542 | <Button |
| 543 | key={ 'v-gap-tablet-unit' } |
| 544 | isSmall |
| 545 | isPrimary={ true } |
| 546 | /* translators: %s: values associated with CSS syntax, 'Pixel', 'Em', 'Percentage' */ |
| 547 | aria-label={ __( 'Pixel Units' ) } |
| 548 | > |
| 549 | px |
| 550 | </Button> |
| 551 | </Tooltip> |
| 552 | </div> |
| 553 | </div> |
| 554 | |
| 555 | <div className="components-base-control components-gblocks-typography-control__inputs"> |
| 556 | <TextControl |
| 557 | type={ 'number' } |
| 558 | value={ verticalGapTablet || 0 === verticalGapTablet ? verticalGapTablet : '' } |
| 559 | min="0" |
| 560 | placeholder={ verticalGapPlaceholderTablet } |
| 561 | onChange={ ( value ) => { |
| 562 | // No negative values allowed here. |
| 563 | value = value.toString().replace( /-/g, '' ); |
| 564 | |
| 565 | setAttributes( { |
| 566 | verticalGapTablet: value, |
| 567 | } ); |
| 568 | } } |
| 569 | onBlur={ () => { |
| 570 | if ( ! verticalGapTablet && generateBlocksDefaults.gridContainer.verticalGapTablet ) { |
| 571 | // If we have no value and a default exists, set to 0 to prevent default from coming back. |
| 572 | setAttributes( { |
| 573 | verticalGapTablet: 0, |
| 574 | } ); |
| 575 | } else { |
| 576 | setAttributes( { |
| 577 | verticalGapTablet: parseFloat( verticalGapTablet ), |
| 578 | } ); |
| 579 | } |
| 580 | } } |
| 581 | onClick={ ( e ) => { |
| 582 | // Make sure onBlur fires in Firefox. |
| 583 | e.currentTarget.focus(); |
| 584 | } } |
| 585 | /> |
| 586 | |
| 587 | <Button |
| 588 | isSmall |
| 589 | isSecondary |
| 590 | className="components-gblocks-default-number" |
| 591 | onClick={ () => { |
| 592 | setAttributes( { |
| 593 | verticalGapTablet: generateBlocksDefaults.gridContainer.verticalGapTablet, |
| 594 | } ); |
| 595 | } } |
| 596 | > |
| 597 | { __( 'Reset', 'generateblocks' ) } |
| 598 | </Button> |
| 599 | </div> |
| 600 | |
| 601 | <SelectControl |
| 602 | label={ __( 'Vertical Alignment', 'generateblocks' ) } |
| 603 | help={ __( 'Align grid items. Removes same height columns and overrides grid item content alignment.', 'generateblocks' ) } |
| 604 | value={ verticalAlignmentTablet } |
| 605 | options={ [ |
| 606 | { label: __( 'Inherit', 'generateblocks' ), value: 'inherit' }, |
| 607 | { label: __( 'Default', 'generateblocks' ), value: '' }, |
| 608 | { label: __( 'Top', 'generateblocks' ), value: 'flex-start' }, |
| 609 | { label: __( 'Center', 'generateblocks' ), value: 'center' }, |
| 610 | { label: __( 'Bottom', 'generateblocks' ), value: 'flex-end' }, |
| 611 | ] } |
| 612 | onChange={ ( value ) => { |
| 613 | setAttributes( { |
| 614 | verticalAlignmentTablet: value, |
| 615 | } ); |
| 616 | } } |
| 617 | /> |
| 618 | |
| 619 | <SelectControl |
| 620 | label={ __( 'Horizontal Alignment', 'generateblocks' ) } |
| 621 | value={ horizontalAlignmentTablet } |
| 622 | options={ [ |
| 623 | { label: __( 'Inherit', 'generateblocks' ), value: 'inherit' }, |
| 624 | { label: __( 'Default', 'generateblocks' ), value: '' }, |
| 625 | { label: __( 'Left', 'generateblocks' ), value: 'flex-start' }, |
| 626 | { label: __( 'Center', 'generateblocks' ), value: 'center' }, |
| 627 | { label: __( 'Right', 'generateblocks' ), value: 'flex-end' }, |
| 628 | ] } |
| 629 | onChange={ ( value ) => { |
| 630 | setAttributes( { |
| 631 | horizontalAlignmentTablet: value, |
| 632 | } ); |
| 633 | } } |
| 634 | /> |
| 635 | </Fragment> |
| 636 | ) } |
| 637 | |
| 638 | { 'mobile' === selectedDevice && ( |
| 639 | <Fragment> |
| 640 | <div className="components-gblocks-control__header"> |
| 641 | <div className="components-gblocks-control__label"> |
| 642 | { __( 'Horizontal Gap', 'generateblocks' ) } |
| 643 | </div> |
| 644 | |
| 645 | <div className="components-gblocks-control__units"> |
| 646 | <Tooltip text={ __( 'Pixel Units' ) } key={ 'h-gap-mobile-unit' }> |
| 647 | <Button |
| 648 | key={ 'h-gap-mobile-unit' } |
| 649 | isSmall |
| 650 | isPrimary={ true } |
| 651 | /* translators: %s: values associated with CSS syntax, 'Pixel', 'Em', 'Percentage' */ |
| 652 | aria-label={ __( 'Pixel Units' ) } |
| 653 | > |
| 654 | px |
| 655 | </Button> |
| 656 | </Tooltip> |
| 657 | </div> |
| 658 | </div> |
| 659 | |
| 660 | <div className="components-base-control components-gblocks-typography-control__inputs"> |
| 661 | <TextControl |
| 662 | type={ 'number' } |
| 663 | value={ horizontalGapMobile || 0 === horizontalGapMobile ? horizontalGapMobile : '' } |
| 664 | min="0" |
| 665 | placeholder={ horizontalGapPlaceholderMobile } |
| 666 | onChange={ ( value ) => { |
| 667 | // No negative values allowed here. |
| 668 | value = value.toString().replace( /-/g, '' ); |
| 669 | |
| 670 | setAttributes( { |
| 671 | horizontalGapMobile: value, |
| 672 | } ); |
| 673 | } } |
| 674 | onBlur={ () => { |
| 675 | if ( ! horizontalGapMobile && generateBlocksDefaults.gridContainer.horizontalGapMobile ) { |
| 676 | // If we have no value and a default exists, set to 0 to prevent default from coming back. |
| 677 | setAttributes( { |
| 678 | horizontalGapMobile: 0, |
| 679 | } ); |
| 680 | } else { |
| 681 | setAttributes( { |
| 682 | horizontalGapMobile: parseFloat( horizontalGapMobile ), |
| 683 | } ); |
| 684 | } |
| 685 | } } |
| 686 | onClick={ ( e ) => { |
| 687 | // Make sure onBlur fires in Firefox. |
| 688 | e.currentTarget.focus(); |
| 689 | } } |
| 690 | /> |
| 691 | |
| 692 | <Button |
| 693 | isSmall |
| 694 | isSecondary |
| 695 | className="components-gblocks-default-number" |
| 696 | onClick={ () => { |
| 697 | setAttributes( { |
| 698 | horizontalGapMobile: generateBlocksDefaults.gridContainer.horizontalGapMobile, |
| 699 | } ); |
| 700 | } } |
| 701 | > |
| 702 | { __( 'Reset', 'generateblocks' ) } |
| 703 | </Button> |
| 704 | </div> |
| 705 | |
| 706 | <div className="components-gblocks-control__header"> |
| 707 | <div className="components-gblocks-control__label"> |
| 708 | { __( 'Vertical Gap', 'generateblocks' ) } |
| 709 | </div> |
| 710 | |
| 711 | <div className="components-gblocks-control__units"> |
| 712 | <Tooltip text={ __( 'Pixel Units' ) } key={ 'v-gap-mobile-unit' }> |
| 713 | <Button |
| 714 | key={ 'v-gap-mobile-unit' } |
| 715 | isSmall |
| 716 | isPrimary={ true } |
| 717 | /* translators: %s: values associated with CSS syntax, 'Pixel', 'Em', 'Percentage' */ |
| 718 | aria-label={ __( 'Pixel Units' ) } |
| 719 | > |
| 720 | px |
| 721 | </Button> |
| 722 | </Tooltip> |
| 723 | </div> |
| 724 | </div> |
| 725 | |
| 726 | <div className="components-base-control components-gblocks-typography-control__inputs"> |
| 727 | <TextControl |
| 728 | type={ 'number' } |
| 729 | value={ verticalGapMobile || 0 === verticalGapMobile ? verticalGapMobile : '' } |
| 730 | min="0" |
| 731 | placeholder={ verticalGapPlaceholderMobile } |
| 732 | onChange={ ( value ) => { |
| 733 | // No negative values allowed here. |
| 734 | value = value.toString().replace( /-/g, '' ); |
| 735 | |
| 736 | setAttributes( { |
| 737 | verticalGapMobile: value, |
| 738 | } ); |
| 739 | } } |
| 740 | onBlur={ () => { |
| 741 | if ( ! verticalGapMobile && generateBlocksDefaults.gridContainer.verticalGapMobile ) { |
| 742 | // If we have no value and a default exists, set to 0 to prevent default from coming back. |
| 743 | setAttributes( { |
| 744 | verticalGapMobile: 0, |
| 745 | } ); |
| 746 | } else { |
| 747 | setAttributes( { |
| 748 | verticalGapMobile: parseFloat( verticalGapMobile ), |
| 749 | } ); |
| 750 | } |
| 751 | } } |
| 752 | onClick={ ( e ) => { |
| 753 | // Make sure onBlur fires in Firefox. |
| 754 | e.currentTarget.focus(); |
| 755 | } } |
| 756 | /> |
| 757 | |
| 758 | <Button |
| 759 | isSmall |
| 760 | isSecondary |
| 761 | className="components-gblocks-default-number" |
| 762 | onClick={ () => { |
| 763 | setAttributes( { |
| 764 | verticalGapMobile: generateBlocksDefaults.gridContainer.verticalGapMobile, |
| 765 | } ); |
| 766 | } } |
| 767 | > |
| 768 | { __( 'Reset', 'generateblocks' ) } |
| 769 | </Button> |
| 770 | </div> |
| 771 | |
| 772 | <SelectControl |
| 773 | label={ __( 'Vertical Alignment', 'generateblocks' ) } |
| 774 | help={ __( 'Align grid items. Removes same height columns and overrides grid item content alignment.', 'generateblocks' ) } |
| 775 | value={ verticalAlignmentMobile } |
| 776 | options={ [ |
| 777 | { label: __( 'Inherit', 'generateblocks' ), value: 'inherit' }, |
| 778 | { label: __( 'Default', 'generateblocks' ), value: '' }, |
| 779 | { label: __( 'Top', 'generateblocks' ), value: 'flex-start' }, |
| 780 | { label: __( 'Center', 'generateblocks' ), value: 'center' }, |
| 781 | { label: __( 'Bottom', 'generateblocks' ), value: 'flex-end' }, |
| 782 | ] } |
| 783 | onChange={ ( value ) => { |
| 784 | setAttributes( { |
| 785 | verticalAlignmentMobile: value, |
| 786 | } ); |
| 787 | } } |
| 788 | /> |
| 789 | |
| 790 | <SelectControl |
| 791 | label={ __( 'Horizontal Alignment', 'generateblocks' ) } |
| 792 | value={ horizontalAlignmentMobile } |
| 793 | options={ [ |
| 794 | { label: __( 'Inherit', 'generateblocks' ), value: 'inherit' }, |
| 795 | { label: __( 'Default', 'generateblocks' ), value: '' }, |
| 796 | { label: __( 'Left', 'generateblocks' ), value: 'flex-start' }, |
| 797 | { label: __( 'Center', 'generateblocks' ), value: 'center' }, |
| 798 | { label: __( 'Right', 'generateblocks' ), value: 'flex-end' }, |
| 799 | ] } |
| 800 | onChange={ ( value ) => { |
| 801 | setAttributes( { |
| 802 | horizontalAlignmentMobile: value, |
| 803 | } ); |
| 804 | } } |
| 805 | /> |
| 806 | </Fragment> |
| 807 | ) } |
| 808 | |
| 809 | { applyFilters( 'generateblocks.editor.controls', '', 'gridLayout', this.props, this.state ) } |
| 810 | </PanelArea> |
| 811 | |
| 812 | <PanelArea { ...this.props } |
| 813 | title={ __( 'Advanced', 'generateblocks' ) } |
| 814 | initialOpen={ false } |
| 815 | icon={ getIcon( 'advanced' ) } |
| 816 | className={ 'gblocks-panel-label' } |
| 817 | id={ 'gridAdvanced' } |
| 818 | state={ this.state } |
| 819 | showPanel={ 'desktop' === selectedDevice || false } |
| 820 | > |
| 821 | <TextControl |
| 822 | label={ __( 'Element ID', 'generateblocks' ) } |
| 823 | value={ elementId } |
| 824 | onChange={ ( value ) => { |
| 825 | const newElementId = value.replace( ELEMENT_ID_REGEX, '-' ); |
| 826 | |
| 827 | setAttributes( { |
| 828 | elementId: newElementId, |
| 829 | } ); |
| 830 | } } |
| 831 | /> |
| 832 | |
| 833 | <TextControl |
| 834 | label={ __( 'CSS Classes', 'generateblocks' ) } |
| 835 | value={ cssClasses } |
| 836 | onChange={ ( value ) => { |
| 837 | setAttributes( { |
| 838 | cssClasses: value, |
| 839 | } ); |
| 840 | } } |
| 841 | /> |
| 842 | |
| 843 | { applyFilters( 'generateblocks.editor.controls', '', 'gridAdvanced', this.props, this.state ) } |
| 844 | </PanelArea> |
| 845 | |
| 846 | <PanelArea { ...this.props } |
| 847 | title={ __( 'Documentation', 'generateblocks' ) } |
| 848 | icon={ getIcon( 'documentation' ) } |
| 849 | initialOpen={ false } |
| 850 | className={ 'gblocks-panel-label' } |
| 851 | id={ 'gridDocumentation' } |
| 852 | state={ this.state } |
| 853 | > |
| 854 | <p>{ __( 'Need help with this block?', 'generateblocks' ) }</p> |
| 855 | <a href="https://docs.generateblocks.com/collection/grid/" target="_blank" rel="noreferrer noopener">{ __( 'Visit our documentation', 'generateblocks' ) }</a> |
| 856 | |
| 857 | { applyFilters( 'generateblocks.editor.controls', '', 'gridDocumentation', this.props, this.state ) } |
| 858 | </PanelArea> |
| 859 | </InspectorControls> |
| 860 | |
| 861 | <DesktopCSS { ...this.props } /> |
| 862 | |
| 863 | <div |
| 864 | id={ !! elementId ? elementId : undefined } |
| 865 | className={ classnames( { |
| 866 | 'gb-grid-wrapper': true, |
| 867 | [ `gb-grid-wrapper-${ uniqueId }` ]: true, |
| 868 | [ `${ cssClasses }` ]: '' !== cssClasses, |
| 869 | } ) } |
| 870 | > |
| 871 | { columns > 0 || this.state.selectedLayout ? ( |
| 872 | <Fragment> |
| 873 | <InnerBlocks |
| 874 | allowedBlocks={ [ 'generateblocks/container' ] } |
| 875 | renderAppender={ false } |
| 876 | /> |
| 877 | </Fragment> |
| 878 | ) : this.getLayoutsSelector() } |
| 879 | </div> |
| 880 | </Fragment> |
| 881 | ); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | export default ( GenerateBlockGridContainer ); |
| 886 |