Skip to content

Commit

Permalink
NavigationLink: improve colors handling (#20022)
Browse files Browse the repository at this point in the history
* block-editor: add getColorObjectByColorSlug()

* navigation-link: get color from slug or value

* navigation-link: rename prop to improve reading

* navigation: remove stuff about rgb block attrs

* navigation-link: replace getting rgb color values

* navigation: fixing linting errors

* minor doc improvements

* navigation-link: fix default colors value

* block-editor: add getColors() selector

* block-editor: add getColorObjectByColorSlug()

* block-editor: remove colors helper function

* navigation-edit: refact using selectors

* navigation-link: set nav colors as single props

* docs: add getColorObjectByColorSlug() selector doc

* navigation: handling deprecated attrs

* navigation: adding support to deprecated

* navigation: handling deprecated in server side

* navigation: fixing linting errors

* navigation-link: remove wrong comment

* navigation: fix typo in file name

* navigation: getting rbg color with local fn

* block-editor: remove color selectors

* navigation: simplify deprecated in server-side

* navigation: swtich commenting block using *

* navigation-link: avoid abbreviations in var name
  • Loading branch information
retrofox authored Feb 6, 2020
1 parent 088032b commit cbda33e
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 37 deletions.
54 changes: 40 additions & 14 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { escape, head } from 'lodash';
import { escape, get, head, find } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -45,20 +45,12 @@ function NavigationLinkEdit( {
setAttributes,
showSubmenuIcon,
insertLinkBlock,
navigationBlockAttributes,
textColor,
backgroundColor,
rgbTextColor,
rgbBackgroundColor,
} ) {
const { label, opensInNewTab, url, , description } = attributes;

/*
* Navigation Block attributes.
*/
const {
textColor,
rgbTextColor,
backgroundColor,
rgbBackgroundColor,
} = navigationBlockAttributes;

const link = {
url,
opensInNewTab,
Expand Down Expand Up @@ -284,19 +276,42 @@ function NavigationLinkEdit( {
);
}

/**
* Returns the color object matching the slug, or undefined.
*
* @param {Array} colors The editor settings colors array.
* @param {string} colorSlug A string containing the color slug.
* @param {string} customColor A string containing the custom color value.
*
* @return {Object} Color object included in the editor settings colors, or Undefined.
*/
const getColorObjectByColorSlug = ( colors, colorSlug, customColor ) => {
if ( customColor ) {
return customColor;
}

if ( ! colors || ! colors.length ) {
return;
}

return get( find( colors, { slug: colorSlug } ), 'color' );
};

export default compose( [
withSelect( ( select, ownProps ) => {
const {
getBlockAttributes,
getClientIdsOfDescendants,
hasSelectedInnerBlock,
getBlockParentsByBlockName,
getSettings,
} = select( 'core/block-editor' );
const { clientId } = ownProps;
const rootBlock = head(
getBlockParentsByBlockName( clientId, 'core/navigation' )
);
const navigationBlockAttributes = getBlockAttributes( rootBlock );
const colors = get( getSettings(), 'colors', [] );
const hasDescendants = !! getClientIdsOfDescendants( [ clientId ] )
.length;
const showSubmenuIcon =
Expand All @@ -307,7 +322,18 @@ export default compose( [
isParentOfSelectedBlock,
hasDescendants,
showSubmenuIcon,
navigationBlockAttributes,
textColor: navigationBlockAttributes.textColor,
backgroundColor: navigationBlockAttributes.backgroundColor,
rgbTextColor: getColorObjectByColorSlug(
colors,
navigationBlockAttributes.textColor,
navigationBlockAttributes.customTextColor
),
rgbBackgroundColor: getColorObjectByColorSlug(
colors,
navigationBlockAttributes.backgroundColor,
navigationBlockAttributes.customBackgroundColor
),
};
} ),
withDispatch( ( dispatch, ownProps, registry ) => {
Expand Down
66 changes: 66 additions & 0 deletions packages/block-library/src/navigation/deprecated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* External dependencies
*/
import { omit } from 'lodash';

/**
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';

export default [
{
attributes: {
className: {
type: 'string',
},
textColor: {
type: 'string',
},
rgbTextColor: {
type: 'string',
},
backgroundColor: {
type: 'string',
},
rgbBackgroundColor: {
type: 'string',
},
fontSize: {
type: 'string',
},
customFontSize: {
type: 'number',
},
itemsJustification: {
type: 'string',
},
showSubmenuIcon: {
type: 'boolean',
},
},
isEligible( attribute ) {
return attribute.rgbTextColor || attribute.rgbBackgroundColor;
},
supports: {
align: [ 'wide', 'full' ],
anchor: true,
html: false,
inserter: true,
},
migrate( attributes ) {
return {
...omit( attributes, [ 'rgbTextColor', 'rgbBackgroundColor' ] ),
customTextColor: attributes.textColor
? undefined
: attributes.rgbTextColor,
customBackgroundColor: attributes.backgroundColor
? undefined
: attributes.rgbBackgroundColor,
};
},
save() {
return <InnerBlocks.Content />;
},
},
];
10 changes: 1 addition & 9 deletions packages/block-library/src/navigation/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useMemo, Fragment, useRef, useEffect } from '@wordpress/element';
import { useMemo, Fragment, useRef } from '@wordpress/element';
import {
InnerBlocks,
InspectorControls,
Expand Down Expand Up @@ -86,14 +86,6 @@ function Navigation( {
[ fontSize.size ]
);

// Pickup and store text and background colors in grb format into attrs object.
useEffect( () => {
setAttributes( {
rgbTextColor: TextColor.color,
rgbBackgroundColor: BackgroundColor.color,
} );
}, [ TextColor.color, BackgroundColor.color ] );

/* eslint-enable @wordpress/no-unused-vars-before-return */
const { navigatorToolbarButton, navigatorModal } = useBlockNavigator(
clientId
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/navigation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { menu as icon } from '@wordpress/icons';
*/
import edit from './edit';
import save from './save';
import deprecated from './deprecated';

export const name = 'core/navigation';

Expand Down Expand Up @@ -38,4 +39,6 @@ export const settings = {
edit,

save,

deprecated,
};
53 changes: 39 additions & 14 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function core_block_navigation_build_css_colors( $attributes ) {

// Text color.
$has_named_text_color = array_key_exists( 'textColor', $attributes );
$has_custom_text_color = array_key_exists( 'rgbTextColor', $attributes );
$has_custom_text_color = array_key_exists( 'customTextColor', $attributes );

// If has text color.
if ( $has_custom_text_color || $has_named_text_color ) {
Expand All @@ -33,12 +33,12 @@ function core_block_navigation_build_css_colors( $attributes ) {
$colors['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] );
} elseif ( $has_custom_text_color ) {
// Add the custom color inline style.
$colors['inline_styles'] .= sprintf( 'color: %s;', $attributes['rgbTextColor'] );
$colors['inline_styles'] .= sprintf( 'color: %s;', $attributes['customTextColor'] );
}

// Background color.
$has_named_background_color = array_key_exists( 'backgroundColor', $attributes );
$has_custom_background_color = array_key_exists( 'rgbBackgroundColor', $attributes );
$has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes );

// If has background color.
if ( $has_custom_background_color || $has_named_background_color ) {
Expand All @@ -51,7 +51,7 @@ function core_block_navigation_build_css_colors( $attributes ) {
$colors['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] );
} elseif ( $has_custom_background_color ) {
// Add the custom background-color inline style.
$colors['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['rgbBackgroundColor'] );
$colors['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] );
}

return $colors;
Expand Down Expand Up @@ -133,8 +133,25 @@ function render_block_navigation( $content, $block ) {
return $content;
}

$attributes = $block['attrs'];
$block['innerBlocks'] = core_block_navigation_empty_navigation_links_recursive( $block['innerBlocks'] );
$attributes = $block['attrs'];

/**
* Deprecated:
* The rgbTextColor and rgbBackgroundColor attributes
* have been deprecated in favor of
* customTextColor and customBackgroundColor ones.
* Move the values from old attrs to the new ones.
*/
if ( isset( $attributes['rgbTextColor'] ) && empty( $attributes['textColor'] ) ) {
$attributes['customTextColor'] = $attributes['rgbTextColor'];
}

if ( isset( $attributes['rgbBackgroundColor'] ) && empty( $attributes['backgroundColor'] ) ) {
$attributes['customBackgroundColor'] = $attributes['rgbBackgroundColor'];
}

unset( $attributes['rgbTextColor'], $attributes['rgbBackgroundColor'] );

if ( empty( $block['innerBlocks'] ) ) {
return '';
Expand Down Expand Up @@ -267,31 +284,39 @@ function register_block_core_navigation() {
'core/navigation',
array(
'attributes' => array(
'className' => array(
'className' => array(
'type' => 'string',
),
'textColor' => array(
'type' => 'string',
),
'customTextColor' => array(
'type' => 'string',
),
'textColor' => array(
// deprecated.
'rgbTextColor' => array(
'type' => 'string',
),
'rgbTextColor' => array(
'backgroundColor' => array(
'type' => 'string',
),
'backgroundColor' => array(
'customBackgroundColor' => array(
'type' => 'string',
),
'rgbBackgroundColor' => array(
// deprecated.
'rgbBackgroundColor' => array(
'type' => 'string',
),
'fontSize' => array(
'fontSize' => array(
'type' => 'string',
),
'customFontSize' => array(
'customFontSize' => array(
'type' => 'number',
),
'itemsJustification' => array(
'itemsJustification' => array(
'type' => 'string',
),
'showSubmenuIcon' => array(
'showSubmenuIcon' => array(
'type' => 'boolean',
'default' => true,
),
Expand Down

0 comments on commit cbda33e

Please sign in to comment.