frontblocks-columns-same-height-option.js
2 months ago
frontblocks-columns-same-height-option.jsx
2 months ago
frontblocks-columns-same-height.css
2 months ago
frontblocks-columns-same-height-option.jsx
92 lines
| 1 | const { createHigherOrderComponent } = wp.compose; |
| 2 | const { Fragment } = wp.element; |
| 3 | const { InspectorControls } = wp.blockEditor; |
| 4 | const { PanelBody, ToggleControl } = wp.components; |
| 5 | const { __ } = wp.i18n; |
| 6 | |
| 7 | const COLUMNS_BLOCK = 'core/columns'; |
| 8 | |
| 9 | // Register same-height attribute server-side. |
| 10 | wp.hooks.addFilter( |
| 11 | 'blocks.registerBlockType', |
| 12 | 'frontblocks/add-same-height-attribute', |
| 13 | ( settings, name ) => { |
| 14 | if ( COLUMNS_BLOCK !== name ) { |
| 15 | return settings; |
| 16 | } |
| 17 | |
| 18 | settings.attributes = Object.assign( settings.attributes || {}, { |
| 19 | frblSameHeight: { |
| 20 | type: 'boolean', |
| 21 | default: false, |
| 22 | }, |
| 23 | } ); |
| 24 | |
| 25 | return settings; |
| 26 | } |
| 27 | ); |
| 28 | |
| 29 | // Add data attribute to the block wrapper in the editor for CSS targeting. |
| 30 | const withSameHeightWrapper = createHigherOrderComponent( ( BlockListBlock ) => { |
| 31 | return ( props ) => { |
| 32 | if ( COLUMNS_BLOCK !== props.name || ! props.attributes.frblSameHeight ) { |
| 33 | return <BlockListBlock { ...props } />; |
| 34 | } |
| 35 | |
| 36 | const wrapperProps = { |
| 37 | ...( props.wrapperProps || {} ), |
| 38 | 'data-frbl-same-height': 'true', |
| 39 | }; |
| 40 | |
| 41 | return <BlockListBlock { ...props } wrapperProps={ wrapperProps } />; |
| 42 | }; |
| 43 | }, 'withSameHeightWrapper' ); |
| 44 | |
| 45 | wp.hooks.addFilter( |
| 46 | 'editor.BlockListBlock', |
| 47 | 'frontblocks/columns-same-height-wrapper', |
| 48 | withSameHeightWrapper |
| 49 | ); |
| 50 | |
| 51 | // Add the inspector toggle. |
| 52 | const withSameHeightControl = createHigherOrderComponent( ( BlockEdit ) => { |
| 53 | return ( props ) => { |
| 54 | if ( COLUMNS_BLOCK !== props.name ) { |
| 55 | return <BlockEdit { ...props } />; |
| 56 | } |
| 57 | |
| 58 | const { attributes, setAttributes } = props; |
| 59 | const isSameHeight = !! attributes.frblSameHeight; |
| 60 | |
| 61 | return ( |
| 62 | <Fragment> |
| 63 | <BlockEdit { ...props } /> |
| 64 | |
| 65 | <InspectorControls> |
| 66 | <PanelBody |
| 67 | title={ __( 'FrontBlocks - Layout', 'frontblocks' ) } |
| 68 | initialOpen={ false } |
| 69 | > |
| 70 | <ToggleControl |
| 71 | label={ __( 'Same height columns', 'frontblocks' ) } |
| 72 | checked={ isSameHeight } |
| 73 | onChange={ ( value ) => setAttributes( { frblSameHeight: value } ) } |
| 74 | help={ |
| 75 | isSameHeight ? |
| 76 | __( 'All columns stretch to match the tallest one.', 'frontblocks' ) : |
| 77 | __( 'Enable to force all columns to the same height.', 'frontblocks' ) |
| 78 | } |
| 79 | /> |
| 80 | </PanelBody> |
| 81 | </InspectorControls> |
| 82 | </Fragment> |
| 83 | ); |
| 84 | }; |
| 85 | }, 'withSameHeightControl' ); |
| 86 | |
| 87 | wp.hooks.addFilter( |
| 88 | 'editor.BlockEdit', |
| 89 | 'frontblocks/columns-same-height-control', |
| 90 | withSameHeightControl |
| 91 | ); |
| 92 |