Skip to content

Commit

Permalink
Disable Lightbox UI if link has an image (WordPress#51180)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbravobernal authored and artemiomorales committed Jun 2, 2023
1 parent ddc8dd6 commit c4681a2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
19 changes: 16 additions & 3 deletions packages/block-editor/src/hooks/behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ import { store as blockEditorStore } from '../store';
*/
import merge from 'deepmerge';

function BehaviorsControl( { blockName, blockBehaviors, onChange } ) {
function BehaviorsControl( {
blockName,
blockBehaviors,
onChange,
disabled = false,
} ) {
const { settings, themeBehaviors } = useSelect(
( select ) => {
const { getBehaviors, getSettings } = select( blockEditorStore );
Expand Down Expand Up @@ -61,6 +66,10 @@ function BehaviorsControl( { blockName, blockBehaviors, onChange } ) {

const options = [ noBehaviorsOption, ...behaviorsOptions ];

const helpText = disabled
? __( 'The lightbox behavior is disabled for linked images.' )
: __( 'Add behaviors.' );

return (
<InspectorControls group="advanced">
<SelectControl
Expand All @@ -71,8 +80,9 @@ function BehaviorsControl( { blockName, blockBehaviors, onChange } ) {
options={ options }
onChange={ onChange }
hideCancelButton={ true }
help={ __( 'Add behaviors.' ) }
help={ helpText }
size="__unstable-large"
disabled={ disabled }
/>
</InspectorControls>
);
Expand All @@ -95,7 +105,9 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => {
if ( props.name !== 'core/image' ) {
return blockEdit;
}

const blockHasLink =
typeof props.attributes?.linkDestination !== 'undefined' &&
props.attributes?.linkDestination !== 'none';
return (
<>
{ blockEdit }
Expand All @@ -111,6 +123,7 @@ export const withBehaviors = createHigherOrderComponent( ( BlockEdit ) => {
},
} );
} }
disabled={ blockHasLink }
/>
</>
);
Expand Down
37 changes: 37 additions & 0 deletions test/e2e/specs/editor/various/behaviors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,43 @@ test.describe( 'Testing behaviors functionality', () => {
await select.selectOption( { label: 'No behaviors' } );
await expect( select ).toHaveValue( '' );
} );

test( 'Lightbox behavior is disabled if the Image has a link', async ( {
admin,
editor,
requestUtils,
page,
behaviorUtils,
} ) => {
// In this theme, the default value for settings.behaviors.blocks.core/image.lightbox is `true`.
await requestUtils.activateTheme( 'behaviors-enabled' );
await admin.createNewPost();
const media = await behaviorUtils.createMedia();

await editor.insertBlock( {
name: 'core/image',
attributes: {
alt: filename,
id: media.id,
url: media.source_url,
linkDestination: 'custom',
},
} );

await editor.openDocumentSettingsSidebar();
const editorSettings = page.getByRole( 'region', {
name: 'Editor settings',
} );
await editorSettings
.getByRole( 'button', { name: 'Advanced' } )
.click();
const select = editorSettings.getByRole( 'combobox', {
name: 'Behavior',
} );

// The behaviors dropdown should be present but disabled.
await expect( select ).toBeDisabled();
} );
} );

class BehaviorUtils {
Expand Down

0 comments on commit c4681a2

Please sign in to comment.