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,
|
SelectControl,
|
||||||
ToggleControl,
|
ToggleControl,
|
||||||
} from '@wordpress/components';
|
} from '@wordpress/components';
|
||||||
|
|
||||||
import { useState, useEffect } from '@wordpress/element';
|
import { useState, useEffect } from '@wordpress/element';
|
||||||
import apiFetch from '@wordpress/api-fetch';
|
import apiFetch from '@wordpress/api-fetch';
|
||||||
import metadata from './block.json';
|
import metadata from './block.json';
|
||||||
import allowedBlocks from '../../../json/core-block-whitelist.json';
|
import allowedBlocks from '../../../json/core-block-whitelist.json';
|
||||||
|
import { containerClassNames, sectionClassNames } from '../../../js/lib/blocks/classNames';
|
||||||
|
|
||||||
registerBlockType(metadata.name, {
|
registerBlockType(metadata.name, {
|
||||||
edit({ attributes, setAttributes }) {
|
edit({ attributes, setAttributes }) {
|
||||||
@@ -40,7 +42,7 @@ registerBlockType(metadata.name, {
|
|||||||
] = useState( [] );
|
] = useState( [] );
|
||||||
|
|
||||||
useEffect( () => {
|
useEffect( () => {
|
||||||
apiFetch( { path: '/badegg/v1/blocks/container_width' } )
|
apiFetch( { path: '/badegg/v1/blocks/container-widths' } )
|
||||||
.then( ( data ) => {
|
.then( ( data ) => {
|
||||||
setContainerWidthOptions( data );
|
setContainerWidthOptions( data );
|
||||||
setIsLoading( false );
|
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 (
|
return (
|
||||||
<div { ...blockProps }>
|
<div { ...blockProps }>
|
||||||
@@ -87,7 +96,7 @@ registerBlockType(metadata.name, {
|
|||||||
</PanelBody>
|
</PanelBody>
|
||||||
</Panel>
|
</Panel>
|
||||||
</InspectorControls>
|
</InspectorControls>
|
||||||
<div className={`container container-${ attributes.container_width } align-${ attributes.alignment }`}>
|
<div className={ containerClassNames(attributes).join(' ') }>
|
||||||
<InnerBlocks
|
<InnerBlocks
|
||||||
allowedBlocks={ allowedBlocks }
|
allowedBlocks={ allowedBlocks }
|
||||||
defaultBlock={
|
defaultBlock={
|
||||||
@@ -106,7 +115,7 @@ registerBlockType(metadata.name, {
|
|||||||
save({ attributes }) {
|
save({ attributes }) {
|
||||||
return (
|
return (
|
||||||
<div { ...useBlockProps.save() }>
|
<div { ...useBlockProps.save() }>
|
||||||
<div className={`container container-${attributes.container_width} align-${ attributes.alignment }`}>
|
<div className={ containerClassNames(attributes).join() }>
|
||||||
<InnerBlocks.Content />
|
<InnerBlocks.Content />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user