Skip to content

Commit

Permalink
Button Block: Add parent-child syncing mechanism (#15931)
Browse files Browse the repository at this point in the history
  • Loading branch information
Copons authored Jun 11, 2020
1 parent 266889b commit 72a9014
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions extensions/blocks/button/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export default {
uniqueId: {
type: 'string',
},
passthroughAttributes: {
type: 'object',
},
text: {
type: 'string',
},
Expand Down
4 changes: 4 additions & 0 deletions extensions/blocks/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import applyFallbackStyles from './apply-fallback-styles';
import ButtonBorderPanel from './button-border-panel';
import ButtonColorsPanel from './button-colors-panel';
import { IS_GRADIENT_AVAILABLE } from './constants';
import usePassthroughAttributes from './use-passthrough-attributes';
import './editor.scss';

function ButtonEdit( {
attributes,
backgroundColor,
className,
clientId,
fallbackBackgroundColor,
fallbackTextColor,
setAttributes,
Expand All @@ -37,6 +39,8 @@ function ButtonEdit( {
} ) {
const { borderRadius, element, placeholder, text } = attributes;

usePassthroughAttributes( { attributes, clientId, setAttributes } );

const onChange = value => {
// TODO: Remove `replace` once minimum Gutenberg version is 8.0 (to fully support `disableLineBreaks`)
const newValue = 'input' === element ? value.replace( /<br>/gim, ' ' ) : value;
Expand Down
40 changes: 40 additions & 0 deletions extensions/blocks/button/use-passthrough-attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* External dependencies
*/
import { isEmpty, mapValues, pickBy } from 'lodash';

/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';

export default function usePassthroughAttributes( { attributes, clientId, setAttributes } ) {
// `passthroughAttributes` is a map of child/parent attribute names,
// to indicate which parent attribute values will be injected into which child attributes.
// E.g. { childAttribute: 'parentAttribute' }
const { passthroughAttributes } = attributes;

const { attributesToSync } = useSelect( select => {
const { getBlockAttributes, getBlockHierarchyRootClientId } = select( 'core/block-editor' );
const parentAttributes = getBlockAttributes( getBlockHierarchyRootClientId( clientId ) );

// Here we actually map the parent attribute value to the child attribute.
// E.g. { childAttribute: 'foobar' }
const mappedAttributes = mapValues( passthroughAttributes, key => parentAttributes[ key ] );

// Discard all equals attributes
const validAttributes = pickBy(
mappedAttributes,
( value, key ) => value !== attributes[ key ]
);

return { attributesToSync: validAttributes };
} );

useEffect( () => {
if ( ! isEmpty( attributesToSync ) ) {
setAttributes( attributesToSync );
}
}, [ attributesToSync, setAttributes ] );
}

0 comments on commit 72a9014

Please sign in to comment.