Skip to content

Commit 9972a2c

Browse files
artemiomoralesdmsnell
authored andcommitted
Add rough animation to navigation and links (#46342)
* Add rough animation to navigation and links * Add support for configuring animation in block settings; revise animation implementation * Fix animation direction; do code cleanup * Add framer dependency and disable import restriction * Fix linter errors * Limit scope to fix tests * Add wrapper for animation logic, update useSelect dependencies * Fix dependency declaration that was causing tests to break
1 parent fb788fa commit 9972a2c

File tree

5 files changed

+180
-5
lines changed

5 files changed

+180
-5
lines changed

lib/experimental/class-wp-rest-block-editor-settings-controller.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ public function get_item_schema() {
174174
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
175175
),
176176

177+
'__experimentalBlockInspectorAnimation' => array(
178+
'description' => __( 'Whether to enable animation when showing and hiding the block inspector.', 'gutenberg' ),
179+
'type' => 'object',
180+
'context' => array( 'site-editor' ),
181+
),
182+
177183
'alignWide' => array(
178184
'description' => __( 'Enable/Disable Wide/Full Alignments.', 'gutenberg' ),
179185
'type' => 'boolean',

packages/block-editor/src/components/block-inspector/index.js

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import {
1414
__experimentalHStack as HStack,
1515
__experimentalVStack as VStack,
1616
Button,
17+
__unstableMotion as motion,
1718
} from '@wordpress/components';
1819
import { useSelect, useDispatch } from '@wordpress/data';
19-
import { useMemo, useCallback } from '@wordpress/element';
20+
import { useMemo, useCallback, Fragment } from '@wordpress/element';
2021

2122
/**
2223
* Internal dependencies
@@ -171,6 +172,22 @@ const BlockInspector = ( { showNoBlockSelectedMessage = true } ) => {
171172
const availableTabs = useInspectorControlsTabs( blockType?.name );
172173
const showTabs = availableTabs?.length > 1;
173174

175+
const isOffCanvasNavigationEditorEnabled =
176+
window?.__experimentalEnableOffCanvasNavigationEditor === true;
177+
178+
const blockInspectorAnimationSettings = useSelect(
179+
( select ) => {
180+
if ( isOffCanvasNavigationEditorEnabled ) {
181+
const globalBlockInspectorAnimationSettings =
182+
select( blockEditorStore ).getSettings()
183+
.__experimentalBlockInspectorAnimation;
184+
return globalBlockInspectorAnimationSettings[ blockType.name ];
185+
}
186+
return null;
187+
},
188+
[ selectedBlockClientId, isOffCanvasNavigationEditorEnabled, blockType ]
189+
);
190+
174191
if ( count > 1 ) {
175192
return (
176193
<div className="block-editor-block-inspector">
@@ -231,11 +248,67 @@ const BlockInspector = ( { showNoBlockSelectedMessage = true } ) => {
231248
/>
232249
);
233250
}
251+
234252
return (
235-
<BlockInspectorSingleBlock
236-
clientId={ selectedBlockClientId }
237-
blockName={ blockType.name }
238-
/>
253+
<BlockInspectorSingleBlockWrapper
254+
animate={
255+
isOffCanvasNavigationEditorEnabled &&
256+
blockInspectorAnimationSettings
257+
}
258+
wrapper={ ( children ) => (
259+
<AnimatedContainer
260+
blockInspectorAnimationSettings={
261+
blockInspectorAnimationSettings
262+
}
263+
selectedBlockClientId={ selectedBlockClientId }
264+
>
265+
{ children }
266+
</AnimatedContainer>
267+
) }
268+
>
269+
<Fragment>
270+
<BlockInspectorSingleBlock
271+
clientId={ selectedBlockClientId }
272+
blockName={ blockType.name }
273+
/>
274+
</Fragment>
275+
</BlockInspectorSingleBlockWrapper>
276+
);
277+
};
278+
279+
const BlockInspectorSingleBlockWrapper = ( { animate, wrapper, children } ) => {
280+
return animate ? wrapper( children ) : children;
281+
};
282+
283+
const AnimatedContainer = ( {
284+
blockInspectorAnimationSettings,
285+
selectedBlockClientId,
286+
children,
287+
} ) => {
288+
const animationOrigin =
289+
blockInspectorAnimationSettings &&
290+
blockInspectorAnimationSettings.enterDirection === 'leftToRight'
291+
? -50
292+
: 50;
293+
294+
return (
295+
<motion.div
296+
animate={ {
297+
x: 0,
298+
opacity: 1,
299+
transition: {
300+
ease: 'easeInOut',
301+
duration: 0.14,
302+
},
303+
} }
304+
initial={ {
305+
x: animationOrigin,
306+
opacity: 0,
307+
} }
308+
key={ selectedBlockClientId }
309+
>
310+
{ children }
311+
</motion.div>
239312
);
240313
};
241314

packages/block-library/src/navigation-link/index.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,35 @@ function gutenberg_disable_tabs_for_navigation_link_block( $settings ) {
376376
}
377377

378378
add_filter( 'block_editor_settings_all', 'gutenberg_disable_tabs_for_navigation_link_block' );
379+
380+
/**
381+
* Enables animation of the block inspector for the Navigation Link block.
382+
*
383+
* See:
384+
* - https://github.com/WordPress/gutenberg/pull/46342
385+
* - https://github.com/WordPress/gutenberg/issues/45884
386+
*
387+
* @param array $settings Default editor settings.
388+
* @return array Filtered editor settings.
389+
*/
390+
function gutenberg_enable_animation_for_navigation_link_inspector( $settings ) {
391+
$current_animation_settings = _wp_array_get(
392+
$settings,
393+
array( '__experimentalBlockInspectorAnimation' ),
394+
array()
395+
);
396+
397+
$settings['__experimentalBlockInspectorAnimation'] = array_merge(
398+
$current_animation_settings,
399+
array(
400+
'core/navigation-link' =>
401+
array(
402+
'enterDirection' => 'rightToLeft',
403+
),
404+
)
405+
);
406+
407+
return $settings;
408+
}
409+
410+
add_filter( 'block_editor_settings_all', 'gutenberg_enable_animation_for_navigation_link_inspector' );

packages/block-library/src/navigation-submenu/index.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,35 @@ function gutenberg_disable_tabs_for_navigation_submenu_block( $settings ) {
321321
}
322322

323323
add_filter( 'block_editor_settings_all', 'gutenberg_disable_tabs_for_navigation_submenu_block' );
324+
325+
/**
326+
* Enables animation of the block inspector for the Navigation Submenu block.
327+
*
328+
* See:
329+
* - https://github.com/WordPress/gutenberg/pull/46342
330+
* - https://github.com/WordPress/gutenberg/issues/45884
331+
*
332+
* @param array $settings Default editor settings.
333+
* @return array Filtered editor settings.
334+
*/
335+
function gutenberg_enable_animation_for_navigation_submenu_inspector( $settings ) {
336+
$current_animation_settings = _wp_array_get(
337+
$settings,
338+
array( '__experimentalBlockInspectorAnimation' ),
339+
array()
340+
);
341+
342+
$settings['__experimentalBlockInspectorAnimation'] = array_merge(
343+
$current_animation_settings,
344+
array(
345+
'core/navigation-submenu' =>
346+
array(
347+
'enterDirection' => 'rightToLeft',
348+
),
349+
)
350+
);
351+
352+
return $settings;
353+
}
354+
355+
add_filter( 'block_editor_settings_all', 'gutenberg_enable_animation_for_navigation_submenu_inspector' );

packages/block-library/src/navigation/index.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,3 +856,35 @@ function block_core_navigation_typographic_presets_backcompatibility( $parsed_bl
856856
}
857857

858858
add_filter( 'render_block_data', 'block_core_navigation_typographic_presets_backcompatibility' );
859+
860+
/**
861+
* Enables animation of the block inspector for the Navigation block.
862+
*
863+
* See:
864+
* - https://github.com/WordPress/gutenberg/pull/46342
865+
* - https://github.com/WordPress/gutenberg/issues/45884
866+
*
867+
* @param array $settings Default editor settings.
868+
* @return array Filtered editor settings.
869+
*/
870+
function gutenberg_enable_animation_for_navigation_inspector( $settings ) {
871+
$current_animation_settings = _wp_array_get(
872+
$settings,
873+
array( '__experimentalBlockInspectorAnimation' ),
874+
array()
875+
);
876+
877+
$settings['__experimentalBlockInspectorAnimation'] = array_merge(
878+
$current_animation_settings,
879+
array(
880+
'core/navigation' =>
881+
array(
882+
'enterDirection' => 'leftToRight',
883+
),
884+
)
885+
);
886+
887+
return $settings;
888+
}
889+
890+
add_filter( 'block_editor_settings_all', 'gutenberg_enable_animation_for_navigation_inspector' );

0 commit comments

Comments
 (0)