admin管理员组文章数量:1336107
I've got a custom Gutenberg block that is using two custom components. Each of the components has a toggle in the Inspector Controls panel. This is just a simplified scenario, in reality, there will be more than just two components.
// My GB block
import PreTitle from '../components/PreTitle';
import Title from '../components/Title';
const { registerBlockType } = wp.blocks;
const blockConfig = {
title: 'My Block',
category: 'my-blocks',
attributes: {
// Some attributes
},
edit: ( props ) => {
return (
<div className={`controls`}>
<PreTitle { ...props } />
<Title { ...props } />
</div>
);
},
save: ({attributes: { preTitleContent, TitleContent }}) => {
return(
<div className={`controls`}>
<PreTitle.Content { ...props } />
<Title.Content { ...props } />
</div>
);
}
};
export const headings = registerBlockType(
`cv-wp-lib-gutenberg-blocks/headings`,
blockConfig
);
My Title component:
const Title = ( props ) => {
// ... some code
return (
<>
<InspectorControls>
<PanelBody>
<ToggleControl
label={ __(`Toggle title`, `cv-wp-lib-gutenberg-blocks`) }
checked={ showTitle }
onChange={ onChangeToggleField }
/>
</PanelBody>
</InspectorControls>
// ... some JSX
</>
);
};
My PreTitle component:
const PreTitle = ( props ) => {
// ... some code
return (
<>
<InspectorControls>
<PanelBody>
<ToggleControl
label={ __(`Toggle preTitle`, `cv-wp-lib-gutenberg-blocks`) }
checked={ showPreTitle }
onChange={ onChangeToggleField }
/>
</PanelBody>
</InspectorControls>
// ... some JSX
</>
);
};
Question: Is there a way for the components sharing the same panel?
I've got a custom Gutenberg block that is using two custom components. Each of the components has a toggle in the Inspector Controls panel. This is just a simplified scenario, in reality, there will be more than just two components.
// My GB block
import PreTitle from '../components/PreTitle';
import Title from '../components/Title';
const { registerBlockType } = wp.blocks;
const blockConfig = {
title: 'My Block',
category: 'my-blocks',
attributes: {
// Some attributes
},
edit: ( props ) => {
return (
<div className={`controls`}>
<PreTitle { ...props } />
<Title { ...props } />
</div>
);
},
save: ({attributes: { preTitleContent, TitleContent }}) => {
return(
<div className={`controls`}>
<PreTitle.Content { ...props } />
<Title.Content { ...props } />
</div>
);
}
};
export const headings = registerBlockType(
`cv-wp-lib-gutenberg-blocks/headings`,
blockConfig
);
My Title component:
const Title = ( props ) => {
// ... some code
return (
<>
<InspectorControls>
<PanelBody>
<ToggleControl
label={ __(`Toggle title`, `cv-wp-lib-gutenberg-blocks`) }
checked={ showTitle }
onChange={ onChangeToggleField }
/>
</PanelBody>
</InspectorControls>
// ... some JSX
</>
);
};
My PreTitle component:
const PreTitle = ( props ) => {
// ... some code
return (
<>
<InspectorControls>
<PanelBody>
<ToggleControl
label={ __(`Toggle preTitle`, `cv-wp-lib-gutenberg-blocks`) }
checked={ showPreTitle }
onChange={ onChangeToggleField }
/>
</PanelBody>
</InspectorControls>
// ... some JSX
</>
);
};
Question: Is there a way for the components sharing the same panel?
Share Improve this question asked Jun 25, 2020 at 12:44 crs1138crs1138 2911 gold badge2 silver badges11 bronze badges1 Answer
Reset to default 1You have two options:
- You write only one InspectorControls and put it in your main component/block and remove the InpertorControls from your Title and PreTitle js files. Don't forget to change the onChange function name 'onChangeToggleField' cause they both share the same function name. Whatever props you change now, should be now passed to Title and PreTitle
// My GB block
import PreTitle from '../components/PreTitle';
import Title from '../components/Title';
const { registerBlockType } = wp.blocks;
const blockConfig = {
title: 'My Block',
category: 'my-blocks',
attributes: {
// Some attributes
},
edit: ( props ) => {
return (
<>
<InspectorControls>
<PanelBody>
<ToggleControl
label={ __(`Toggle title`, `cv-wp-lib-gutenberg-blocks`) }
checked={ showTitle }
onChange={ onChangeToggleFieldTitle }
/>
<ToggleControl
label={ __(`Toggle preTitle`, `cv-wp-lib-gutenberg-
blocks`) }
checked={ showPreTitle }
onChange={ onChangeToggleFieldPreTitle }
/>
</PanelBody>
</InspectorControls>
<div className={`controls`}>
<PreTitle { ...props } />
<Title { ...props } />
</div>
</>
);
},
save: ({attributes: { preTitleContent, TitleContent }}) => {
return(
<div className={`controls`}>
<PreTitle.Content { ...props } />
<Title.Content { ...props } />
</div>
);
}
};
export const headings = registerBlockType(
`cv-wp-lib-gutenberg-blocks/headings`,
blockConfig
);
- You write an extra component (for example MyGbBlockInspector.js and import it in your block.
// My GB block
import PreTitle from '../components/PreTitle';
import Title from '../components/Title';
import MyGbBlockInspector from '../components/MyGbBlockInspector';
I prefer option 2, although the problem is, that Title and PreTitle are now not global anymore and can't be simply used in other Blocks without rewriting their InspectorControls functions.
本文标签: block editorTwo Gutenberg components sharing the same Panel Body in Inspector Controls
版权声明:本文标题:block editor - Two Gutenberg components sharing the same Panel Body in Inspector Controls 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742317365a2452088.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论