Skip to content

Commit

Permalink
Disable Lightbox UI if link has an image (#51180)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbravobernal authored Jun 2, 2023
1 parent 362475d commit 8f40804
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

1 comment on commit 8f40804

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 8f40804.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5153105552
📝 Reported issues:

Please sign in to comment.