Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button Block: Add parent-child syncing mechanism #15931

Merged
merged 1 commit into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ] );
}