Skip to content

Commit

Permalink
Add rough animation to navigation and links (#46342)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
artemiomorales authored Dec 14, 2022
1 parent 271f650 commit b856a3f
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ public function get_item_schema() {
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),

'__experimentalBlockInspectorAnimation' => array(
'description' => __( 'Whether to enable animation when showing and hiding the block inspector.', 'gutenberg' ),
'type' => 'object',
'context' => array( 'site-editor' ),
),

'alignWide' => array(
'description' => __( 'Enable/Disable Wide/Full Alignments.', 'gutenberg' ),
'type' => 'boolean',
Expand Down
83 changes: 78 additions & 5 deletions packages/block-editor/src/components/block-inspector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import {
__experimentalHStack as HStack,
__experimentalVStack as VStack,
Button,
__unstableMotion as motion,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { useMemo, useCallback } from '@wordpress/element';
import { useMemo, useCallback, Fragment } from '@wordpress/element';

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

const isOffCanvasNavigationEditorEnabled =
window?.__experimentalEnableOffCanvasNavigationEditor === true;

const blockInspectorAnimationSettings = useSelect(
( select ) => {
if ( isOffCanvasNavigationEditorEnabled ) {
const globalBlockInspectorAnimationSettings =
select( blockEditorStore ).getSettings()
.__experimentalBlockInspectorAnimation;
return globalBlockInspectorAnimationSettings[ blockType.name ];
}
return null;
},
[ selectedBlockClientId, isOffCanvasNavigationEditorEnabled, blockType ]
);

if ( count > 1 ) {
return (
<div className="block-editor-block-inspector">
Expand Down Expand Up @@ -231,11 +248,67 @@ const BlockInspector = ( { showNoBlockSelectedMessage = true } ) => {
/>
);
}

return (
<BlockInspectorSingleBlock
clientId={ selectedBlockClientId }
blockName={ blockType.name }
/>
<BlockInspectorSingleBlockWrapper
animate={
isOffCanvasNavigationEditorEnabled &&
blockInspectorAnimationSettings
}
wrapper={ ( children ) => (
<AnimatedContainer
blockInspectorAnimationSettings={
blockInspectorAnimationSettings
}
selectedBlockClientId={ selectedBlockClientId }
>
{ children }
</AnimatedContainer>
) }
>
<Fragment>
<BlockInspectorSingleBlock
clientId={ selectedBlockClientId }
blockName={ blockType.name }
/>
</Fragment>
</BlockInspectorSingleBlockWrapper>
);
};

const BlockInspectorSingleBlockWrapper = ( { animate, wrapper, children } ) => {
return animate ? wrapper( children ) : children;
};

const AnimatedContainer = ( {
blockInspectorAnimationSettings,
selectedBlockClientId,
children,
} ) => {
const animationOrigin =
blockInspectorAnimationSettings &&
blockInspectorAnimationSettings.enterDirection === 'leftToRight'
? -50
: 50;

return (
<motion.div
animate={ {
x: 0,
opacity: 1,
transition: {
ease: 'easeInOut',
duration: 0.14,
},
} }
initial={ {
x: animationOrigin,
opacity: 0,
} }
key={ selectedBlockClientId }
>
{ children }
</motion.div>
);
};

Expand Down
32 changes: 32 additions & 0 deletions packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,35 @@ function gutenberg_disable_tabs_for_navigation_link_block( $settings ) {
}

add_filter( 'block_editor_settings_all', 'gutenberg_disable_tabs_for_navigation_link_block' );

/**
* Enables animation of the block inspector for the Navigation Link block.
*
* See:
* - https://github.com/WordPress/gutenberg/pull/46342
* - https://github.com/WordPress/gutenberg/issues/45884
*
* @param array $settings Default editor settings.
* @return array Filtered editor settings.
*/
function gutenberg_enable_animation_for_navigation_link_inspector( $settings ) {
$current_animation_settings = _wp_array_get(
$settings,
array( '__experimentalBlockInspectorAnimation' ),
array()
);

$settings['__experimentalBlockInspectorAnimation'] = array_merge(
$current_animation_settings,
array(
'core/navigation-link' =>
array(
'enterDirection' => 'rightToLeft',
),
)
);

return $settings;
}

add_filter( 'block_editor_settings_all', 'gutenberg_enable_animation_for_navigation_link_inspector' );
32 changes: 32 additions & 0 deletions packages/block-library/src/navigation-submenu/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,35 @@ function gutenberg_disable_tabs_for_navigation_submenu_block( $settings ) {
}

add_filter( 'block_editor_settings_all', 'gutenberg_disable_tabs_for_navigation_submenu_block' );

/**
* Enables animation of the block inspector for the Navigation Submenu block.
*
* See:
* - https://github.com/WordPress/gutenberg/pull/46342
* - https://github.com/WordPress/gutenberg/issues/45884
*
* @param array $settings Default editor settings.
* @return array Filtered editor settings.
*/
function gutenberg_enable_animation_for_navigation_submenu_inspector( $settings ) {
$current_animation_settings = _wp_array_get(
$settings,
array( '__experimentalBlockInspectorAnimation' ),
array()
);

$settings['__experimentalBlockInspectorAnimation'] = array_merge(
$current_animation_settings,
array(
'core/navigation-submenu' =>
array(
'enterDirection' => 'rightToLeft',
),
)
);

return $settings;
}

add_filter( 'block_editor_settings_all', 'gutenberg_enable_animation_for_navigation_submenu_inspector' );
32 changes: 32 additions & 0 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -856,3 +856,35 @@ function block_core_navigation_typographic_presets_backcompatibility( $parsed_bl
}

add_filter( 'render_block_data', 'block_core_navigation_typographic_presets_backcompatibility' );

/**
* Enables animation of the block inspector for the Navigation block.
*
* See:
* - https://github.com/WordPress/gutenberg/pull/46342
* - https://github.com/WordPress/gutenberg/issues/45884
*
* @param array $settings Default editor settings.
* @return array Filtered editor settings.
*/
function gutenberg_enable_animation_for_navigation_inspector( $settings ) {
$current_animation_settings = _wp_array_get(
$settings,
array( '__experimentalBlockInspectorAnimation' ),
array()
);

$settings['__experimentalBlockInspectorAnimation'] = array_merge(
$current_animation_settings,
array(
'core/navigation' =>
array(
'enterDirection' => 'leftToRight',
),
)
);

return $settings;
}

add_filter( 'block_editor_settings_all', 'gutenberg_enable_animation_for_navigation_inspector' );

0 comments on commit b856a3f

Please sign in to comment.