Skip to content

Commit

Permalink
#110 - Only show innerblocks with content
Browse files Browse the repository at this point in the history
  • Loading branch information
khleomix committed Jun 30, 2021
1 parent 44e3ddd commit 951df19
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/blocks/carousel-slide/edit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { InnerBlocks, withColors } from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { compose } from '@wordpress/compose';
import Settings from './components/Settings';
import Slide from './components/Slide';
Expand Down Expand Up @@ -27,8 +29,19 @@ function Edit( props ) {
setBackgroundColor,
overlayColor,
setOverlayColor,
clientId,
} = props;

const { innerBlocksLength } = useSelect( ( select ) => ( {
innerBlocksLength: select( 'core/block-editor' ).getBlockCount(
clientId
),
} ) );

useEffect( () => {
setAttributes( { innerBlocksLength } );
}, [ innerBlocksLength ] );

// Define props relating to block background settings.
const backgroundProps = {
...attributes,
Expand Down
4 changes: 4 additions & 0 deletions src/blocks/carousel-slide/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ registerBlockType( `${ PREFIX }/carousel-slide`, {
category: 'wds-blocks',
keywords: [ __( 'carousel, slide', 'wdsblocks' ) ],
attributes: {
innerBlocksLength: {
type: 'number',
default: 0,
},
fontColor: {
type: 'string',
default: undefined,
Expand Down
9 changes: 6 additions & 3 deletions src/blocks/carousel-slide/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { PREFIX } from '../../utils/config';
*/
export default function Save( props ) {
const { attributes } = props;
const { innerBlocksLength } = props.attributes;

// Define props relating to slide settings.
const slideProps = {
Expand All @@ -28,9 +29,11 @@ export default function Save( props ) {

return (
<Slide { ...slideProps }>
<div className="slide-content">
<InnerBlocks.Content />
</div>
{ innerBlocksLength > 0 && (
<div className="slide-content">
<InnerBlocks.Content />
</div>
) }
</Slide>
);
}

0 comments on commit 951df19

Please sign in to comment.