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

Disable Lightbox UI if link has an image #51180

Merged
merged 1 commit into from
Jun 2, 2023
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
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