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

[Pattern Overrides] Use a single checkbox to turn on pattern overrides for all allowed attributes #57009

Merged
merged 3 commits into from
Dec 21, 2023
Merged
Changes from 1 commit
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
72 changes: 31 additions & 41 deletions packages/patterns/src/components/partial-syncing-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@ import { PARTIAL_SYNCING_SUPPORTED_BLOCKS } from '../constants';
function PartialSyncingControls( { name, attributes, setAttributes } ) {
const syncedAttributes = PARTIAL_SYNCING_SUPPORTED_BLOCKS[ name ];

function updateConnections( attributeName, isChecked ) {
function updateConnections( isChecked ) {
let updatedConnections = {
...attributes.connections,
attributes: { ...attributes.connections?.attributes },
};

if ( ! isChecked ) {
let updatedConnections = {
...attributes.connections,
attributes: {
...attributes.connections?.attributes,
[ attributeName ]: undefined,
},
};
if ( Object.keys( updatedConnections.attributes ).length === 1 ) {
updatedConnections.attributes = undefined;
for ( const attributeName of Object.keys( syncedAttributes ) ) {
delete updatedConnections.attributes[ attributeName ];
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
}
if ( ! Object.keys( updatedConnections.attributes ).length ) {
delete updatedConnections.attributes;
}
if (
Object.keys( updatedConnections ).length === 1 &&
updateConnections.attributes === undefined
) {
if ( ! Object.keys( updatedConnections ).length ) {
updatedConnections = undefined;
}
setAttributes( {
Expand All @@ -42,15 +40,11 @@ function PartialSyncingControls( { name, attributes, setAttributes } ) {
return;
}

const updatedConnections = {
...attributes.connections,
attributes: {
...attributes.connections?.attributes,
[ attributeName ]: {
source: 'pattern_attributes',
},
},
};
for ( const attributeName of Object.keys( syncedAttributes ) ) {
updatedConnections.attributes[ attributeName ] = {
source: 'pattern_attributes',
};
}
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved

if ( typeof attributes.metadata?.id === 'string' ) {
setAttributes( { connections: updatedConnections } );
Expand All @@ -71,25 +65,21 @@ function PartialSyncingControls( { name, attributes, setAttributes } ) {
<InspectorControls group="advanced">
<BaseControl __nextHasNoMarginBottom>
<BaseControl.VisualLabel>
{ __( 'Synced attributes' ) }
{ __( 'Pattern overrides' ) }
</BaseControl.VisualLabel>
{ Object.entries( syncedAttributes ).map(
( [ attributeName, label ] ) => (
<CheckboxControl
key={ attributeName }
__nextHasNoMarginBottom
label={ label }
checked={
attributes.connections?.attributes?.[
attributeName
]?.source === 'pattern_attributes'
}
onChange={ ( isChecked ) => {
updateConnections( attributeName, isChecked );
} }
/>
)
) }
<CheckboxControl
__nextHasNoMarginBottom
label={ __( 'Allow instance overrides' ) }
checked={ Object.keys( syncedAttributes ).some(
( attributeName ) =>
attributes.connections?.attributes?.[
attributeName
]?.source === 'pattern_attributes'
) }
onChange={ ( isChecked ) => {
updateConnections( isChecked );
} }
/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think would also be good to disable the checkbox when the block's only connectable attributes are connected to something else (e.g. a custom field).

At the moment, if you try checking it nothing happens.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should just completely avoid rendering it if that happens. Disabling it might confuse the users too as we don't show any other helper text. Completely hiding it has the same issue but I think it's easier 😅. Given that both features (meta binding and pattern overrides) are not stable yet, I think it's best to keep it simple here for now. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wasn't sure, but I'm good with either 👍

I'll make sure to add it to the tracking issue as something to reconsider.

</BaseControl>
</InspectorControls>
);
Expand Down
Loading