Skip to content

Commit

Permalink
Navigation Block: Add Color Options for Submenus
Browse files Browse the repository at this point in the history
Closes #29963
  • Loading branch information
George Hotelling committed Jul 12, 2021
1 parent dc1821f commit 15cdc90
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 12 deletions.
4 changes: 4 additions & 0 deletions packages/block-library/src/navigation-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
"customTextColor",
"backgroundColor",
"customBackgroundColor",
"overlayTextColor",
"customOverlayTextColor",
"overlayBackgroundColor",
"customOverlayBackgroundColor",
"fontSize",
"customFontSize",
"showSubmenuIcon",
Expand Down
49 changes: 44 additions & 5 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,14 @@ export default function NavigationLinkEdit( {
url,
opensInNewTab,
};
const { textColor, backgroundColor, style, showSubmenuIcon } = context;
const {
textColor,
backgroundColor,
overlayTextColor,
overlayBackgroundColor,
style,
showSubmenuIcon,
} = context;
const { saveEntityRecord } = useDispatch( coreStore );
const { insertBlock } = useDispatch( blockEditorStore );
const [ isLinkOpen, setIsLinkOpen ] = useState( false );
Expand All @@ -245,6 +252,7 @@ export default function NavigationLinkEdit( {

const {
isAtMaxNesting,
isTopLevelLink,
isParentOfSelectedBlock,
isImmediateParentOfSelectedBlock,
hasDescendants,
Expand All @@ -270,6 +278,8 @@ export default function NavigationLinkEdit( {
isAtMaxNesting:
getBlockParentsByBlockName( clientId, name ).length >=
MAX_NESTING,
isTopLevelLink:
getBlockParentsByBlockName( clientId, name ).length === 0,
isParentOfSelectedBlock: hasSelectedInnerBlock(
clientId,
true
Expand Down Expand Up @@ -381,17 +391,25 @@ export default function NavigationLinkEdit( {
};
}

const textColorForNesting = isTopLevelLink
? textColor
: overlayTextColor || textColor;
const bgColorForNesting = isTopLevelLink
? backgroundColor
: overlayBackgroundColor || backgroundColor;

const blockProps = useBlockProps( {
ref: listItemRef,
className: classnames( {
'is-editing': isSelected || isParentOfSelectedBlock,
'is-dragging-within': isDraggingWithin,
'has-link': !! url,
'has-child': hasDescendants,
'has-text-color': !! textColor || !! style?.color?.text,
[ `has-${ textColor }-color` ]: !! textColor,
'has-background': !! backgroundColor || !! style?.color?.background,
[ `has-${ backgroundColor }-background-color` ]: !! backgroundColor,
'has-text-color': !! textColorForNesting || !! style?.color?.text,
[ `has-${ textColorForNesting }-color` ]: !! textColorForNesting,
'has-background':
!! bgColorForNesting || !! style?.color?.background,
[ `has-${ bgColorForNesting }-background-color` ]: !! bgColorForNesting,
} ),
style: {
color: style?.color?.text,
Expand All @@ -407,7 +425,28 @@ export default function NavigationLinkEdit( {
{
className: classnames( 'wp-block-navigation-link__container', {
'is-parent-of-selected-block': isParentOfSelectedBlock,
'has-text-color': !! (
overlayTextColor ||
textColor ||
!! style?.color?.text
),
[ `has-${ overlayTextColor || textColor }-color` ]: !! (
overlayTextColor || textColor
),
'has-background':
!! overlayBackgroundColor ||
backgroundColor ||
!! style?.color?.background,
[ `has-${
overlayBackgroundColor || backgroundColor
}-background-color` ]: !! (
overlayBackgroundColor || backgroundColor
),
} ),
style: {
color: style?.color?.text,
backgroundColor: style?.color?.background,
},
},
{
allowedBlocks: ALLOWED_BLOCKS,
Expand Down
6 changes: 6 additions & 0 deletions packages/block-library/src/navigation-link/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
margin-bottom: $grid-unit-20;
margin-left: $grid-unit-20;
}

// Override the list reset for colored, nested menus
ol.has-background,
ul.has-background {
padding: 0;
}
}

.wp-block-navigation .block-editor-block-list__block[data-type="core/navigation-link"] {
Expand Down
19 changes: 17 additions & 2 deletions packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,29 @@
},
"__unstableLocation": {
"type": "string"
},
"overlayBackgroundColor": {
"type": "string"
},
"customOverlayBackgroundColor": {
"type": "string"
},
"overlayTextColor": {
"type": "string"
},
"customOverlayTextColor": {
"type": "string"
}
},
"providesContext": {
"textColor": "textColor",
"customTextColor": "customTextColor",
"backgroundColor": "backgroundColor",
"customBackgroundColor": "customBackgroundColor",
"overlayTextColor": "overlayTextColor",
"customOverlayTextColor": "customOverlayTextColor",
"overlayBackgroundColor": "overlayBackgroundColor",
"customOverlayBackgroundColor": "customOverlayBackgroundColor",
"fontSize": "fontSize",
"customFontSize": "customFontSize",
"showSubmenuIcon": "showSubmenuIcon",
Expand All @@ -67,8 +83,7 @@
"__experimentalTextTransform": true,
"__experimentalFontFamily": true,
"__experimentalTextDecoration": true
},
"color": true
}
},
"viewScript": "file:./view.min.js",
"editorStyle": "wp-block-navigation-editor",
Expand Down
63 changes: 58 additions & 5 deletions packages/block-library/src/navigation/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
BlockControls,
useBlockProps,
store as blockEditorStore,
withColors,
PanelColorSettings,
} from '@wordpress/block-editor';
import { useDispatch, withSelect, withDispatch } from '@wordpress/data';
import { PanelBody, ToggleControl, ToolbarGroup } from '@wordpress/components';
Expand Down Expand Up @@ -53,6 +55,14 @@ function Navigation( {
isSelected,
updateInnerBlocks,
className,
backgroundColor,
setBackgroundColor,
textColor,
setTextColor,
overlayBackgroundColor,
setOverlayBackgroundColor,
overlayTextColor,
setOverlayTextColor,
hasSubmenuIndicatorSetting = true,
hasItemJustificationControls = true,
} ) {
Expand All @@ -66,11 +76,23 @@ function Navigation( {
const { selectBlock } = useDispatch( blockEditorStore );

const blockProps = useBlockProps( {
className: classnames( className, {
[ `items-justified-${ attributes.itemsJustification }` ]: attributes.itemsJustification,
'is-vertical': attributes.orientation === 'vertical',
'is-responsive': attributes.isResponsive,
} ),
className: classnames(
className,
textColor.class,
backgroundColor.class,
{
[ `items-justified-${ attributes.itemsJustification }` ]: attributes.itemsJustification,
'is-vertical': attributes.orientation === 'vertical',
'is-responsive': attributes.isResponsive,
'has-text-color': !! textColor.color || !! textColor.class,
'has-background':
!! backgroundColor.color || !! backgroundColor.class,
}
),
style: {
color: textColor.color,
backgroundColor: backgroundColor.color,
},
} );

const { navigatorToolbarButton, navigatorModal } = useBlockNavigator(
Expand Down Expand Up @@ -166,6 +188,31 @@ function Navigation( {
/>
</PanelBody>
) }
<PanelColorSettings
title={ __( 'Color' ) }
colorSettings={ [
{
value: textColor.color,
onChange: setTextColor,
label: __( 'Text color' ),
},
{
value: backgroundColor.color,
onChange: setBackgroundColor,
label: __( 'Background text' ),
},
{
value: overlayTextColor.color,
onChange: setOverlayTextColor,
label: __( 'Overlay text color' ),
},
{
value: overlayBackgroundColor.color,
onChange: setOverlayBackgroundColor,
label: __( 'Overlay background color' ),
},
] }
/>
</InspectorControls>
<nav { ...blockProps }>
<ResponsiveWrapper
Expand Down Expand Up @@ -222,4 +269,10 @@ export default compose( [
},
};
} ),
withColors(
{ textColor: 'color' },
{ backgroundColor: 'color' },
{ overlayBackgroundColor: 'color' },
{ overlayTextColor: 'color' }
),
] )( Navigation );

0 comments on commit 15cdc90

Please sign in to comment.