Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Align classes when using theme.json #671

Merged
merged 2 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/blocks/container/components/ContainerContentRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import RootElement from '../../../components/root-element';
import GridItem from './GridItem';
import Element from '../../../components/element';
import { applyFilters } from '@wordpress/hooks';
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';
import { InnerBlocks, useBlockProps, store as blockEditorStore } from '@wordpress/block-editor';
import { Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import getIcon from '../../../utils/get-icon';
import ShapeDividers from './ShapeDividers';
import classnames from 'classnames';
import { useInnerBlocksCount } from '../../../hooks';
import { useDispatch } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import ComponentCSS from './ComponentCSS';
import getBackgroundImageUrl from '../../../utils/get-background-image-url';

Expand Down Expand Up @@ -39,6 +39,13 @@ export default function ContainerContentRenderer( props ) {
const { selectBlock } = useDispatch( 'core/block-editor' );
const innerBlocksCount = useInnerBlocksCount( clientId );
const hasChildBlocks = 0 < innerBlocksCount;
const supportsLayout = useSelect( ( select ) => {
const {
getSettings,
} = select( blockEditorStore );

return getSettings().supportsLayout || false;
}, [] );

let hasStyling = (
!! backgroundColor ||
Expand All @@ -57,9 +64,10 @@ export default function ContainerContentRenderer( props ) {
[ `${ className }` ]: undefined !== className,
'gb-container-empty': ! hasChildBlocks && ! isBlockPreview,
'gb-container-visual-guides': ! hasChildBlocks && ! hasStyling && ! props.isSelected && ! isBlockPreview,
[ `align${ align }` ]: supportsLayout,
} ),
id: anchor ? anchor : null,
'data-align': align ? align : null,
'data-align': align && ! supportsLayout ? align : null,
};

const backgroundUrl = getBackgroundImageUrl( props );
Expand Down
12 changes: 10 additions & 2 deletions src/blocks/image/components/ImageContentRenderer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ImagePlaceholder from './ImagePlaceholder';
import { useBlockProps } from '@wordpress/block-editor';
import { useBlockProps, store as blockEditorStore } from '@wordpress/block-editor';
import classnames from 'classnames';
import RootElement from '../../../components/root-element';
import Element from '../../../components/element';
Expand Down Expand Up @@ -79,14 +79,22 @@ export default function ImageContentRenderer( props ) {
const imageUrl = useDynamicData && dynamicContentType ? dynamicImageFallback : attributes.mediaUrl;
const altText = useDynamicData && dynamicContentType ? currentImage?.alt_text : attributes.alt;
const titleText = useDynamicData && dynamicContentType ? currentImage?.title?.rendered : attributes.title;
const supportsLayout = useSelect( ( select ) => {
const {
getSettings,
} = select( blockEditorStore );

return getSettings().supportsLayout || false;
}, [] );

const figureAttributes = useBlockProps( {
className: classnames( {
'gb-block-image': true,
[ `gb-block-image-${ uniqueId }` ]: true,
'is-applying': !! temporaryURL,
[ `align${ align }` ]: !! parentBlock && supportsLayout,
} ),
'data-align': !! parentBlock ? align : null,
'data-align': !! parentBlock && ! supportsLayout ? align : null,
ref: figureRef,
} );

Expand Down
12 changes: 11 additions & 1 deletion src/components/root-element/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import { createElement } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import classnames from 'classnames';
import { store as blockEditorStore } from '@wordpress/block-editor';

export default function RootElement( { name, clientId, align, children } ) {
const {
getBlockRootClientId,
} = useSelect( ( select ) => select( 'core/block-editor' ), [] );

const supportsLayout = useSelect( ( select ) => {
const {
getSettings,
} = select( blockEditorStore );

return getSettings().supportsLayout || false;
}, [] );

const blockName = name.toString().replace( '/', '-' );

const blockProps = {
className: classnames( {
'wp-block': true,
'gb-is-root-block': true,
[ `gb-root-block-${ blockName }` ]: true,
[ `align${ align }` ]: supportsLayout,
} ),
'data-align': align ? align : null,
'data-align': align && ! supportsLayout ? align : null,
'data-block': clientId,
};

Expand Down