Skip to content

Commit

Permalink
Add section styles switch button in block toolbar in zoom out mode (#…
Browse files Browse the repository at this point in the history
…67140)

* Add section styles switch button in zoom out toolbar

* add icon background color

* tweak label and strokeWidth

* 1.25 > 1.5

---------

Co-authored-by: matiasbenedetto <[email protected]>
Co-authored-by: richtabor <[email protected]>
Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
Co-authored-by: mtias <[email protected]>
Co-authored-by: jasmussen <[email protected]>
  • Loading branch information
7 people authored Nov 22, 2024
1 parent 4337e35 commit 3b5bf63
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/block-editor/src/components/block-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import __unstableBlockNameContext from './block-name-context';
import NavigableToolbar from '../navigable-toolbar';
import { useHasBlockToolbar } from './use-has-block-toolbar';
import ChangeDesign from './change-design';
import SwitchSectionStyle from './switch-section-style';
import { unlock } from '../../lock-unlock';

/**
Expand Down Expand Up @@ -72,6 +73,7 @@ export function PrivateBlockToolbar( {
showSlots,
showGroupButtons,
showLockButtons,
showSwitchSectionStyleButton,
} = useSelect( ( select ) => {
const {
getBlockName,
Expand Down Expand Up @@ -141,6 +143,7 @@ export function PrivateBlockToolbar( {
showSlots: ! isZoomOut(),
showGroupButtons: ! isZoomOut(),
showLockButtons: ! isZoomOut(),
showSwitchSectionStyleButton: isZoomOut(),
};
}, [] );

Expand Down Expand Up @@ -222,6 +225,9 @@ export function PrivateBlockToolbar( {
{ showShuffleButton && (
<ChangeDesign clientId={ blockClientIds[ 0 ] } />
) }
{ showSwitchSectionStyleButton && (
<SwitchSectionStyle clientId={ blockClientIds[ 0 ] } />
) }
{ shouldShowVisualToolbar && showSlots && (
<>
<BlockControls.Slot
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* WordPress dependencies
*/
import {
ToolbarButton,
ToolbarGroup,
Icon,
Path,
SVG,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
import { useContext } from '@wordpress/element';

/**
* Internal dependencies
*/
import useStylesForBlocks from '../block-styles/use-styles-for-block';
import { replaceActiveStyle } from '../block-styles/utils';
import { store as blockEditorStore } from '../../store';
import { GlobalStylesContext } from '../global-styles';
import { globalStylesDataKey } from '../../store/private-keys';
import { getVariationStylesWithRefValues } from '../../hooks/block-style-variation';

const styleIcon = (
<SVG
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
aria-hidden="true"
focusable="false"
>
<Path d="M17.2 10.9c-.5-1-1.2-2.1-2.1-3.2-.6-.9-1.3-1.7-2.1-2.6L12 4l-1 1.1c-.6.9-1.3 1.7-2 2.6-.8 1.2-1.5 2.3-2 3.2-.6 1.2-1 2.2-1 3 0 3.4 2.7 6.1 6.1 6.1s6.1-2.7 6.1-6.1c0-.8-.3-1.8-1-3z" />
<Path
stroke="currentColor"
strokeWidth="1.5"
d="M17.2 10.9c-.5-1-1.2-2.1-2.1-3.2-.6-.9-1.3-1.7-2.1-2.6L12 4l-1 1.1c-.6.9-1.3 1.7-2 2.6-.8 1.2-1.5 2.3-2 3.2-.6 1.2-1 2.2-1 3 0 3.4 2.7 6.1 6.1 6.1s6.1-2.7 6.1-6.1c0-.8-.3-1.8-1-3z"
/>
</SVG>
);

function SwitchSectionStyle( { clientId } ) {
const { stylesToRender, activeStyle, className } = useStylesForBlocks( {
clientId,
} );
const { updateBlockAttributes } = useDispatch( blockEditorStore );

// Get global styles data
const { merged: mergedConfig } = useContext( GlobalStylesContext );
const { globalSettings, globalStyles, blockName } = useSelect(
( select ) => {
const settings = select( blockEditorStore ).getSettings();
return {
globalSettings: settings.__experimentalFeatures,
globalStyles: settings[ globalStylesDataKey ],
blockName: select( blockEditorStore ).getBlockName( clientId ),
};
},
[ clientId ]
);

// Get the background color for the active style
const activeStyleBackground = activeStyle?.name
? getVariationStylesWithRefValues(
{
settings: mergedConfig?.settings ?? globalSettings,
styles: mergedConfig?.styles ?? globalStyles,
},
blockName,
activeStyle.name
)?.color?.background
: undefined;

if ( ! stylesToRender || stylesToRender.length === 0 ) {
return null;
}

const handleStyleSwitch = () => {
const currentIndex = stylesToRender.findIndex(
( style ) => style.name === activeStyle.name
);

const nextIndex = ( currentIndex + 1 ) % stylesToRender.length;
const nextStyle = stylesToRender[ nextIndex ];

const styleClassName = replaceActiveStyle(
className,
activeStyle,
nextStyle
);

updateBlockAttributes( clientId, {
className: styleClassName,
} );
};

return (
<ToolbarGroup>
<ToolbarButton
onClick={ handleStyleSwitch }
label={ __( 'Shuffle styles' ) }
>
<Icon
icon={ styleIcon }
style={ {
fill: activeStyleBackground || 'transparent',
} }
/>
</ToolbarButton>
</ToolbarGroup>
);
}

export default SwitchSectionStyle;

0 comments on commit 3b5bf63

Please sign in to comment.