Skip to content

Commit

Permalink
Block Image: Lightbox - Hide animation selector if behavior is Defaul…
Browse files Browse the repository at this point in the history
…t or None (#51748)

* Hide animation selector if default or none

* Small refactor, fix on translation

* Add zoom out icon

* Fix selector, but still dont like the solution

* Moved to own function

* Add useMemo
  • Loading branch information
cbravobernal committed Jun 24, 2023
1 parent a3af2c7 commit 302a420
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 18 deletions.
5 changes: 3 additions & 2 deletions lib/block-supports/behaviors.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ function gutenberg_render_behaviors_support_lightbox( $block_content, $block ) {
}
$content = $processor->get_updated_html();

$lightbox_animation = '';
if ( isset( $lightbox_settings['animation'] ) ) {
// If we don't set a default, it won't work if Lightbox is set to enabled by default.
$lightbox_animation = 'zoom';
if ( isset( $lightbox_settings['animation'] ) && '' !== $lightbox_settings['animation'] ) {
$lightbox_animation = $lightbox_settings['animation'];
}

Expand Down
41 changes: 25 additions & 16 deletions packages/block-editor/src/hooks/behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { __ } from '@wordpress/i18n';
import { hasBlockSupport } from '@wordpress/blocks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -65,43 +66,48 @@ function BehaviorsControl( {
...behaviorsOptions,
];

const { behaviors, behaviorsValue } = useMemo( () => {
const mergedBehaviors = {
...themeBehaviors,
...( blockBehaviors || {} ),
};

let value = '';
if ( blockBehaviors === undefined ) {
value = 'default';
}
if ( blockBehaviors?.lightbox.enabled ) {
value = 'lightbox';
}
return {
behaviors: mergedBehaviors,
behaviorsValue: value,
};
}, [ blockBehaviors, themeBehaviors ] );
// If every behavior is disabled, do not show the behaviors inspector control.
if ( behaviorsOptions.length === 0 ) {
return null;
}
// Block behaviors take precedence over theme behaviors.
const behaviors = { ...themeBehaviors, ...( blockBehaviors || {} ) };

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

const value = () => {
if ( blockBehaviors === undefined ) {
return 'default';
}
if ( behaviors?.lightbox.enabled ) {
return 'lightbox';
}
return '';
};

return (
<InspectorControls group="advanced">
{ /* This div is needed to prevent a margin bottom between the dropdown and the button. */ }
<div>
<SelectControl
label={ __( 'Behaviors' ) }
// At the moment we are only supporting one behavior (Lightbox)
value={ value() }
value={ behaviorsValue }
options={ options }
onChange={ onChangeBehavior }
hideCancelButton={ true }
help={ helpText }
size="__unstable-large"
disabled={ disabled }
/>
{ behaviors?.lightbox.enabled && (
{ behaviorsValue === 'lightbox' && (
<SelectControl
label={ __( 'Animation' ) }
// At the moment we are only supporting one behavior (Lightbox)
Expand All @@ -115,7 +121,10 @@ function BehaviorsControl( {
value: 'zoom',
label: __( 'Zoom' ),
},
{ value: 'fade', label: 'Fade' },
{
value: 'fade',
label: __( 'Fade' ),
},
] }
onChange={ onChangeAnimation }
hideCancelButton={ false }
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/image/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@
transform-origin: top left;
width: var(--lightbox-image-max-width);
height: var(--lightbox-image-max-height);
cursor: zoom-out;
}

&.active {
Expand Down
45 changes: 45 additions & 0 deletions test/e2e/specs/editor/blocks/image.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,51 @@ test.describe( 'Image - interactivity', () => {
).not.toBeInViewport();
} );

test.describe( 'Animation Select visibility', () => {
test( 'Animation selector should appear if Behavior is Lightbox', async ( {
page,
} ) => {
await page.getByRole( 'button', { name: 'Advanced' } ).click();
const behaviorSelect = page.getByRole( 'combobox', {
name: 'Behaviors',
} );
await behaviorSelect.selectOption( 'lightbox' );
await expect(
page.getByRole( 'combobox', {
name: 'Animation',
} )
).toBeVisible();
} );
test( 'Animation selector should NOT appear if Behavior is None', async ( {
page,
} ) => {
await page.getByRole( 'button', { name: 'Advanced' } ).click();
const behaviorSelect = page.getByRole( 'combobox', {
name: 'Behaviors',
} );
await behaviorSelect.selectOption( '' );
await expect(
page.getByRole( 'combobox', {
name: 'Animation',
} )
).not.toBeVisible();
} );
test( 'Animation selector should NOT appear if Behavior is Default', async ( {
page,
} ) => {
await page.getByRole( 'button', { name: 'Advanced' } ).click();
const behaviorSelect = page.getByRole( 'combobox', {
name: 'Behaviors',
} );
await behaviorSelect.selectOption( 'default' );
await expect(
page.getByRole( 'combobox', {
name: 'Animation',
} )
).not.toBeVisible();
} );
} );

test.describe( 'keyboard navigation', () => {
let openLightboxButton;
let lightbox;
Expand Down

1 comment on commit 302a420

@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 302a420.
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/5364849620
📝 Reported issues:

Please sign in to comment.