BlockControls.js
4 years ago
ComponentCSS.js
1 year ago
InspectorAdvancedControls.js
2 years ago
InspectorControls.js
2 years ago
LayoutSelector.js
3 years ago
WidthControls.js
2 years ago
LayoutSelector.js
91 lines
| 1 | import { Placeholder } from '@wordpress/components'; |
| 2 | import { __ } from '@wordpress/i18n'; |
| 3 | import classnames from 'classnames'; |
| 4 | |
| 5 | /** |
| 6 | * Get columns sizes array from layout string |
| 7 | * |
| 8 | * @param {string} layout The layout string. Example: `3-6-3` |
| 9 | * @param {string} uniqueId The grid block uniqueId attribute |
| 10 | * @return {Array} The columns attributes |
| 11 | */ |
| 12 | export const getColumnsFromLayout = ( layout, uniqueId ) => { |
| 13 | const { |
| 14 | gridItemPaddingTop, |
| 15 | gridItemPaddingRight, |
| 16 | gridItemPaddingBottom, |
| 17 | gridItemPaddingLeft, |
| 18 | } = generateBlocksStyling.container; |
| 19 | |
| 20 | const columnsData = layout.split( '-' ) || []; |
| 21 | |
| 22 | return columnsData.map( ( col ) => ( { |
| 23 | isGrid: true, |
| 24 | gridId: uniqueId, |
| 25 | paddingTop: gridItemPaddingTop || '', |
| 26 | paddingRight: gridItemPaddingRight || '', |
| 27 | paddingBottom: gridItemPaddingBottom || '', |
| 28 | paddingLeft: gridItemPaddingLeft || '', |
| 29 | sizing: { |
| 30 | width: `${ col }%`, |
| 31 | widthMobile: '100%', |
| 32 | }, |
| 33 | } ) ); |
| 34 | }; |
| 35 | |
| 36 | export default ( { uniqueId, onClick, isDisabled = false } ) => { |
| 37 | if ( isDisabled ) { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | const layouts = [ |
| 42 | '100', |
| 43 | '50-50', |
| 44 | '33.33-33.33-33.33', |
| 45 | '25-25-25-25', |
| 46 | |
| 47 | '25-75', |
| 48 | '75-25', |
| 49 | '25-25-50', |
| 50 | '25-50-25', |
| 51 | |
| 52 | '50-25-25', |
| 53 | '20-60-20', |
| 54 | '20-20-20-20-20', |
| 55 | '16-16-16-16-16-16', |
| 56 | ]; |
| 57 | |
| 58 | return ( |
| 59 | <Placeholder |
| 60 | label={ __( 'Grid', 'generateblocks' ) } |
| 61 | instructions={ __( 'Choose how many Containers to start with.', 'generateblocks' ) } |
| 62 | className="gb-select-layout" |
| 63 | > |
| 64 | <div className="gb-grid-wrapper-layout-preview"> |
| 65 | { layouts.map( ( layout ) => { |
| 66 | const columnsData = getColumnsFromLayout( layout, uniqueId ); |
| 67 | |
| 68 | return ( |
| 69 | <button |
| 70 | key={ `layout-${ layout }` } |
| 71 | className="gb-grid-wrapper-layout-preview-btn" |
| 72 | onClick={ () => onClick( layout ) } |
| 73 | > |
| 74 | { columnsData.map( ( colAttrs, idx ) => { |
| 75 | const colWidth = colAttrs.sizing.width.replace( '%', '' ).replace( '.', '-' ); |
| 76 | |
| 77 | return ( |
| 78 | <div |
| 79 | key={ `layout-${ layout }-col-${ idx }` } |
| 80 | className={ classnames( 'gb-col', `gb-col-${ colWidth }` ) } |
| 81 | /> |
| 82 | ); |
| 83 | } ) } |
| 84 | </button> |
| 85 | ); |
| 86 | } ) } |
| 87 | </div> |
| 88 | </Placeholder> |
| 89 | ); |
| 90 | }; |
| 91 |