Skip to content

Commit 5279781

Browse files
committed
Add layout
1 parent f4b426b commit 5279781

File tree

4 files changed

+55
-174
lines changed

4 files changed

+55
-174
lines changed

packages/block-editor/src/hooks/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import fontSize from './font-size';
1818
import border from './border';
1919
import position from './position';
2020
import layout from './layout';
21+
import childLayout from './layout-child';
2122
import './content-lock-ui';
2223
import './metadata';
2324
import customFields from './custom-fields';
@@ -45,6 +46,7 @@ createBlockListBlockFilter( [
4546
fontSize,
4647
border,
4748
position,
49+
childLayout,
4850
] );
4951

5052
export { useCustomSides } from './dimensions';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { useInstanceId } from '@wordpress/compose';
5+
import { useSelect } from '@wordpress/data';
6+
7+
/**
8+
* Internal dependencies
9+
*/
10+
import { store as blockEditorStore } from '../store';
11+
import { useStyleOverride } from './utils';
12+
13+
function useBlockPropsChildLayoutStyles( { style } ) {
14+
const shouldRenderChildLayoutStyles = useSelect( ( select ) => {
15+
return ! select( blockEditorStore ).getSettings().disableLayoutStyles;
16+
} );
17+
const layout = style?.layout ?? {};
18+
const { selfStretch, flexSize } = layout;
19+
const id = useInstanceId( useBlockPropsChildLayoutStyles );
20+
const selector = `.wp-container-content-${ id }`;
21+
22+
let css = '';
23+
if ( shouldRenderChildLayoutStyles ) {
24+
if ( selfStretch === 'fixed' && flexSize ) {
25+
css = `${ selector } {
26+
flex-basis: ${ flexSize };
27+
box-sizing: border-box;
28+
}`;
29+
} else if ( selfStretch === 'fill' ) {
30+
css = `${ selector } {
31+
flex-grow: 1;
32+
}`;
33+
}
34+
}
35+
36+
useStyleOverride( { css } );
37+
38+
// Only attach a container class if there is generated CSS to be attached.
39+
if ( ! css ) {
40+
return;
41+
}
42+
43+
// Attach a `wp-container-content` id-based classname.
44+
return { className: `wp-container-content-${ id }` };
45+
}
46+
47+
export default {
48+
useBlockProps: useBlockPropsChildLayoutStyles,
49+
attributeKeys: [ 'style' ],
50+
hasSupport() {
51+
return true;
52+
},
53+
};

packages/block-editor/src/hooks/layout.js

-62
Original file line numberDiff line numberDiff line change
@@ -417,63 +417,6 @@ export const withLayoutStyles = createHigherOrderComponent(
417417
'withLayoutStyles'
418418
);
419419

420-
function BlockWithChildLayoutStyles( { block: BlockListBlock, props } ) {
421-
const layout = props.attributes.style?.layout ?? {};
422-
const { selfStretch, flexSize } = layout;
423-
424-
const id = useInstanceId( BlockListBlock );
425-
const selector = `.wp-container-content-${ id }`;
426-
427-
let css = '';
428-
if ( selfStretch === 'fixed' && flexSize ) {
429-
css = `${ selector } {
430-
flex-basis: ${ flexSize };
431-
box-sizing: border-box;
432-
}`;
433-
} else if ( selfStretch === 'fill' ) {
434-
css = `${ selector } {
435-
flex-grow: 1;
436-
}`;
437-
}
438-
439-
// Attach a `wp-container-content` id-based classname.
440-
const className = classnames( props.className, {
441-
[ `wp-container-content-${ id }` ]: !! css, // Only attach a container class if there is generated CSS to be attached.
442-
} );
443-
444-
useStyleOverride( { css } );
445-
446-
return <BlockListBlock { ...props } className={ className } />;
447-
}
448-
449-
/**
450-
* Override the default block element to add the child layout styles.
451-
*
452-
* @param {Function} BlockListBlock Original component.
453-
*
454-
* @return {Function} Wrapped component.
455-
*/
456-
export const withChildLayoutStyles = createHigherOrderComponent(
457-
( BlockListBlock ) => ( props ) => {
458-
const shouldRenderChildLayoutStyles = useSelect( ( select ) => {
459-
return ! select( blockEditorStore ).getSettings()
460-
.disableLayoutStyles;
461-
} );
462-
463-
if ( ! shouldRenderChildLayoutStyles ) {
464-
return <BlockListBlock { ...props } />;
465-
}
466-
467-
return (
468-
<BlockWithChildLayoutStyles
469-
block={ BlockListBlock }
470-
props={ props }
471-
/>
472-
);
473-
},
474-
'withChildLayoutStyles'
475-
);
476-
477420
addFilter(
478421
'blocks.registerBlockType',
479422
'core/layout/addAttribute',
@@ -484,8 +427,3 @@ addFilter(
484427
'core/editor/layout/with-layout-styles',
485428
withLayoutStyles
486429
);
487-
addFilter(
488-
'editor.BlockListBlock',
489-
'core/editor/layout/with-child-layout-styles',
490-
withChildLayoutStyles
491-
);

packages/block-editor/src/hooks/test/color.js

-112
This file was deleted.

0 commit comments

Comments
 (0)