Skip to content

Commit

Permalink
Feature: Add shape divider position options
Browse files Browse the repository at this point in the history
  • Loading branch information
tomusborne committed Nov 29, 2024
1 parent e593438 commit ce3eb7c
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/blocks/shape/components/BlockSettings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { moreDesignOptions } from '@utils';
import { useBlockStyles } from '@hooks/useBlockStyles';
import generalSvgs from '@components/icon-picker/svgs-general';
import socialSvgs from '@components/icon-picker/svgs-social';
import { ShapeDividerControls } from './ShapeDividerControls';

export const shapeColorControls = [
{
Expand Down Expand Up @@ -97,6 +98,11 @@ export function BlockSettings( {
openLabel={ __( 'Open Library', 'generateblocks' ) }
modalTitle={ __( 'Shape Library', 'generateblocks' ) }
/>

<ShapeDividerControls
onStyleChange={ onStyleChange }
getStyleValue={ getStyleValue }
/>
</OpenPanel>

<OpenPanel
Expand Down
122 changes: 122 additions & 0 deletions src/blocks/shape/components/ShapeDividerControls.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { useMemo } from '@wordpress/element';
import { SelectControl, ToggleControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

import { useBlockStyles } from '@hooks/useBlockStyles';

function splitSpaces( value ) {
return value.split( /\s+(?![^()]*\))/ );
}

export function ShapeDividerControls( { getStyleValue, onStyleChange } ) {
const {
currentAtRule,
} = useBlockStyles();
const currentPosition = getStyleValue( 'position', currentAtRule );
const currentTopValue = getStyleValue( 'top', currentAtRule );
const currentBottomValue = getStyleValue( 'bottom', currentAtRule );
const currentTransformsValue = getStyleValue( 'transform', currentAtRule );
const locationValue = useMemo( () => {
if ( 'absolute' !== currentPosition ) {
return '';
}

if ( '0' === currentTopValue && '' === currentBottomValue ) {
return 'top';
}

if ( '0' === currentBottomValue && '' === currentTopValue ) {
return 'bottom';
}

return '';
}, [ currentPosition, currentTopValue, currentBottomValue ] );
const flippedHorizontallyValue = useMemo( () => {
if ( ! currentTransformsValue ) {
return false;
}

const existingTransforms = currentTransformsValue ? splitSpaces( currentTransformsValue ) : [];
const scaleValue = existingTransforms.find( ( transform ) => transform.startsWith( 'scale' ) ) ?? '';

if ( 'top' === locationValue ) {
return scaleValue.includes( '-1, -1' );
}

return scaleValue.includes( '-1, 1' );
}, [ currentTransformsValue, locationValue ] );

return (
<>
<SelectControl
label={ __( 'Location', 'generateblocks' ) }
value={ locationValue }
options={ [
{ label: __( 'Top', 'generateblocks' ), value: 'top' },
{ label: __( 'Bottom', 'generateblocks' ), value: 'bottom' },
] }
onChange={ ( value ) => {
const newValues = {};
const existingTransformsValue = getStyleValue( 'transform', currentAtRule );
const existingTransforms = existingTransformsValue ? splitSpaces( existingTransformsValue ) : [];
const scaleValue = existingTransforms.find( ( transform ) => transform.startsWith( 'scale' ) );
let newScaleValue = 'top' === value ? 'scale(1, -1)' : '';

if ( flippedHorizontallyValue ) {
if ( 'top' === value ) {
newScaleValue = 'scale(-1, -1)';
} else {
newScaleValue = 'scale(-1, 1)';
}
}

if ( 'top' === value ) {
newValues.top = '0';
newValues.bottom = '';
} else {
newValues.top = '';
newValues.bottom = '0';
}

if ( scaleValue ) {
newValues.transform = existingTransformsValue.replace( scaleValue, newScaleValue ).trim();
} else {
newValues.transform = existingTransformsValue ? `${ existingTransformsValue } ${ newScaleValue }` : newScaleValue;
newValues.transform = newValues.transform.trim();
}

onStyleChange( newValues, currentAtRule );
} }
/>

<ToggleControl
label={ __( 'Flip Horizontal', 'generateblocks' ) }
checked={ flippedHorizontallyValue }
onChange={ ( value ) => {
const newValues = {};
const existingTransformsValue = getStyleValue( 'transform', currentAtRule );
const existingTransforms = existingTransformsValue ? splitSpaces( existingTransformsValue ) : [];
const scaleValue = existingTransforms.find( ( transform ) => transform.startsWith( 'scale' ) );
let newScaleValue = value ? 'scale(-1, 1)' : '';

if ( 'top' === locationValue ) {
if ( value ) {
newScaleValue = 'scale(-1, -1)';
} else {
newScaleValue = 'scale(1, -1)';
}
}

if ( scaleValue ) {
newValues.transform = existingTransformsValue.replace( scaleValue, newScaleValue ).trim();
} else {
newValues.transform = existingTransformsValue ? `${ existingTransformsValue } ${ newScaleValue }` : newScaleValue;
newValues.transform = newValues.transform.trim();
}

onStyleChange( newValues, currentAtRule );
} }
/>
</>
);
}

0 comments on commit ce3eb7c

Please sign in to comment.