add css class name helper functions for blocks
This commit is contained in:
39
web/app/themes/badegg/resources/js/lib/blocks/classNames.js
Normal file
39
web/app/themes/badegg/resources/js/lib/blocks/classNames.js
Normal file
@@ -0,0 +1,39 @@
|
||||
export function containerClassNames(attributes, bgProps)
|
||||
{
|
||||
|
||||
let classNames = [
|
||||
'container',
|
||||
];
|
||||
|
||||
if('container_width' in attributes)
|
||||
classNames.push(`container-${attributes.container_width}`);
|
||||
|
||||
if('alignment' in attributes)
|
||||
classNames.push(`align-${attributes.alignment}`);
|
||||
|
||||
return classNames;
|
||||
}
|
||||
|
||||
export function sectionClassNames(attributes, defaultClasses, extraClasses = [])
|
||||
{
|
||||
defaultClasses = defaultClasses.split(' ');
|
||||
|
||||
let classNames = [
|
||||
'section',
|
||||
];
|
||||
|
||||
if('padding_top'in attributes && !attributes.padding_top)
|
||||
classNames.push('section-zero-top');
|
||||
|
||||
if('padding_bottom'in attributes && !attributes.padding_bottom)
|
||||
classNames.push('section-zero-bottom');
|
||||
|
||||
|
||||
// combine arrays
|
||||
classNames = classNames.concat(defaultClasses).concat(extraClasses);
|
||||
|
||||
// remove duplicate items
|
||||
classNames = [ ...new Set(classNames) ];
|
||||
|
||||
return classNames;
|
||||
}
|
||||
@@ -18,10 +18,12 @@ import {
|
||||
SelectControl,
|
||||
ToggleControl,
|
||||
} from '@wordpress/components';
|
||||
|
||||
import { useState, useEffect } from '@wordpress/element';
|
||||
import apiFetch from '@wordpress/api-fetch';
|
||||
import metadata from './block.json';
|
||||
import allowedBlocks from '../../../json/core-block-whitelist.json';
|
||||
import { containerClassNames, sectionClassNames } from '../../../js/lib/blocks/classNames';
|
||||
|
||||
registerBlockType(metadata.name, {
|
||||
edit({ attributes, setAttributes }) {
|
||||
@@ -40,7 +42,7 @@ registerBlockType(metadata.name, {
|
||||
] = useState( [] );
|
||||
|
||||
useEffect( () => {
|
||||
apiFetch( { path: '/badegg/v1/blocks/container_width' } )
|
||||
apiFetch( { path: '/badegg/v1/blocks/container-widths' } )
|
||||
.then( ( data ) => {
|
||||
setContainerWidthOptions( data );
|
||||
setIsLoading( false );
|
||||
@@ -51,7 +53,14 @@ registerBlockType(metadata.name, {
|
||||
} );
|
||||
}, [] );
|
||||
|
||||
console.log(attributes);
|
||||
blockProps.className = sectionClassNames(
|
||||
attributes,
|
||||
blockProps.className,
|
||||
[
|
||||
'wysiwyg'
|
||||
]).join(' ');
|
||||
|
||||
console.log(blockProps.className);
|
||||
|
||||
return (
|
||||
<div { ...blockProps }>
|
||||
@@ -87,7 +96,7 @@ registerBlockType(metadata.name, {
|
||||
</PanelBody>
|
||||
</Panel>
|
||||
</InspectorControls>
|
||||
<div className={`container container-${ attributes.container_width } align-${ attributes.alignment }`}>
|
||||
<div className={ containerClassNames(attributes).join(' ') }>
|
||||
<InnerBlocks
|
||||
allowedBlocks={ allowedBlocks }
|
||||
defaultBlock={
|
||||
@@ -106,7 +115,7 @@ registerBlockType(metadata.name, {
|
||||
save({ attributes }) {
|
||||
return (
|
||||
<div { ...useBlockProps.save() }>
|
||||
<div className={`container container-${attributes.container_width} align-${ attributes.alignment }`}>
|
||||
<div className={ containerClassNames(attributes).join() }>
|
||||
<InnerBlocks.Content />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user