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

Tree Grid: use framer motion for all animations #33765

Closed
wants to merge 7 commits into from
Closed
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
20 changes: 11 additions & 9 deletions packages/block-editor/src/components/list-view/appender.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { __experimentalTreeGridCell as TreeGridCell } from '@wordpress/components';
import {
__experimentalTreeGridCell as TreeGridCell,
__experimentalTreeGridRow as TreeGridRow,
} from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import { __, sprintf } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import ListViewLeaf from './leaf';
import Inserter from '../inserter';
import { store as blockEditorStore } from '../../store';

Expand All @@ -23,7 +25,6 @@ export default function ListViewAppender( {
position,
level,
rowCount,
path,
} ) {
const isDragging = useSelect(
( select ) => {
Expand All @@ -49,12 +50,13 @@ export default function ListViewAppender( {
);

return (
<ListViewLeaf
className={ classnames( { 'is-dragging': isDragging } ) }
<TreeGridRow
className={ classnames( 'block-editor-list-view-leaf', {
'is-dragging': isDragging,
} ) }
level={ level }
position={ position }
rowCount={ rowCount }
path={ path }
positionInSet={ position }
setSize={ rowCount }
>
<TreeGridCell
className="block-editor-list-view-appender__cell"
Expand All @@ -77,6 +79,6 @@ export default function ListViewAppender( {
</div>
) }
</TreeGridCell>
</ListViewLeaf>
</TreeGridRow>
);
}
17 changes: 10 additions & 7 deletions packages/block-editor/src/components/list-view/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import classnames from 'classnames';
import {
__experimentalTreeGridCell as TreeGridCell,
__experimentalTreeGridItem as TreeGridItem,
__experimentalTreeGridRow as TreeGridRow,
MenuGroup,
MenuItem,
} from '@wordpress/components';
Expand All @@ -20,7 +21,6 @@ import { useDispatch, useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import ListViewLeaf from './leaf';
import {
BlockMoverUpButton,
BlockMoverDownButton,
Expand All @@ -42,8 +42,8 @@ export default function ListViewBlock( {
rowCount,
siblingBlockCount,
showBlockMovers,
path,
isExpanded,
animateToggleOpen,
} ) {
const cellRef = useRef( null );
const [ isHovered, setIsHovered ] = useState( false );
Expand Down Expand Up @@ -81,6 +81,7 @@ export default function ListViewBlock( {
__experimentalFeatures: withExperimentalFeatures,
__experimentalPersistentListViewFeatures: withExperimentalPersistentListViewFeatures,
isTreeGridMounted,
animate,
} = useListViewContext();
const listViewBlockSettingsClassName = classnames(
'block-editor-list-view-block__menu-cell',
Expand Down Expand Up @@ -122,6 +123,7 @@ export default function ListViewBlock( {
};

const classes = classnames( {
'block-editor-list-view-leaf': true,
'is-selected': isSelected,
'is-branch-selected':
withExperimentalPersistentListViewFeatures && isBranchSelected,
Expand All @@ -132,19 +134,20 @@ export default function ListViewBlock( {
} );

return (
<ListViewLeaf
<TreeGridRow
className={ classes }
onMouseEnter={ onMouseEnter }
onMouseLeave={ onMouseLeave }
onFocus={ onMouseEnter }
onBlur={ onMouseLeave }
level={ level }
position={ position }
rowCount={ rowCount }
path={ path }
positionInSet={ position }
setSize={ rowCount }
id={ `list-view-block-${ clientId }` }
data-block={ clientId }
isExpanded={ isExpanded }
animate={ animate }
animateOnMount={ animateToggleOpen }
>
<TreeGridCell
className="block-editor-list-view-block__contents-cell"
Expand Down Expand Up @@ -243,6 +246,6 @@ export default function ListViewBlock( {
) }
</TreeGridCell>
) }
</ListViewLeaf>
</TreeGridRow>
);
}
23 changes: 17 additions & 6 deletions packages/block-editor/src/components/list-view/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export default function ListViewBranch( props ) {
parentBlockClientId,
level = 1,
terminatedLevels = [],
path = [],
isBranchSelected = false,
isLastOfBranch = false,
animateToggleOpen = false,
} = props;

const isTreeRoot = ! parentBlockClientId;
Expand All @@ -44,7 +44,13 @@ export default function ListViewBranch( props ) {
const rowCount = hasAppender ? blockCount + 1 : blockCount;
const appenderPosition = rowCount;

const { expandedState, expand, collapse } = useListViewContext();
const {
expandedState,
expand,
collapse,
isTreeGridMounted,
animate,
} = useListViewContext();

return (
<>
Expand All @@ -55,7 +61,6 @@ export default function ListViewBranch( props ) {
const updatedTerminatedLevels = isLastRowAtLevel
? [ ...terminatedLevels, level ]
: terminatedLevels;
const updatedPath = [ ...path, position ];
const hasNestedBlocks =
showNestedBlocks && !! innerBlocks && !! innerBlocks.length;
const hasNestedAppender = itemHasAppender( clientId );
Expand Down Expand Up @@ -93,6 +98,13 @@ export default function ListViewBranch( props ) {
}
};

const animateToggle =
animate &&
( animateToggleOpen ||
( isExpanded &&
isTreeGridMounted &&
expandedState[ clientId ] !== undefined ) );

return (
<Fragment key={ clientId }>
<ListViewBlock
Expand All @@ -108,8 +120,8 @@ export default function ListViewBranch( props ) {
siblingBlockCount={ blockCount }
showBlockMovers={ showBlockMovers }
terminatedLevels={ terminatedLevels }
path={ updatedPath }
isExpanded={ isExpanded }
animateToggleOpen={ animateToggle }
/>
{ hasNestedBranch && isExpanded && (
<ListViewBranch
Expand All @@ -126,7 +138,7 @@ export default function ListViewBranch( props ) {
parentBlockClientId={ clientId }
level={ level + 1 }
terminatedLevels={ updatedTerminatedLevels }
path={ updatedPath }
animateToggleOpen={ animateToggle }
/>
) }
</Fragment>
Expand All @@ -139,7 +151,6 @@ export default function ListViewBranch( props ) {
rowCount={ appenderPosition }
level={ level }
terminatedLevels={ terminatedLevels }
path={ [ ...path, appenderPosition ] }
/>
) }
</>
Expand Down
7 changes: 6 additions & 1 deletion packages/block-editor/src/components/list-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/

import { useMergeRefs } from '@wordpress/compose';
import { useMergeRefs, useReducedMotion } from '@wordpress/compose';
import { __experimentalTreeGrid as TreeGrid } from '@wordpress/components';
import { useDispatch } from '@wordpress/data';
import {
Expand Down Expand Up @@ -100,6 +100,8 @@ export default function ListView( {
collapse( row?.dataset?.block );
};

const animate = ! useReducedMotion();

const contextValue = useMemo(
() => ( {
__experimentalFeatures,
Expand All @@ -108,6 +110,7 @@ export default function ListView( {
expandedState,
expand,
collapse,
animate,
} ),
[
__experimentalFeatures,
Expand All @@ -116,6 +119,7 @@ export default function ListView( {
expandedState,
expand,
collapse,
animate,
]
);

Expand All @@ -131,6 +135,7 @@ export default function ListView( {
ref={ treeGridRef }
onCollapseRow={ collapseRow }
onExpandRow={ expandRow }
animate={ animate }
>
<ListViewContext.Provider value={ contextValue }>
<ListViewBranch
Expand Down
48 changes: 0 additions & 48 deletions packages/block-editor/src/components/list-view/leaf.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/block-editor/src/components/list-view/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@
width: $icon-size;
}

// First level of indentation is aria-level 2, max indent is 8.
// First level of indentation is aria-level 2, max indent is 7.
// Indent is a full icon size, plus 4px which optically aligns child icons to the text label above.
$block-navigation-max-indent: 8;
$block-navigation-max-indent: 7;
.block-editor-list-view-leaf[aria-level] .block-editor-list-view__expander {
margin-left: ( $icon-size ) * $block-navigation-max-indent + 4 * ( $block-navigation-max-indent - 1 );
}
Expand Down
19 changes: 17 additions & 2 deletions packages/components/src/tree-grid/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import { UP, DOWN, LEFT, RIGHT } from '@wordpress/keycodes';
* Internal dependencies
*/
import RovingTabIndexContainer from './roving-tab-index';
import {
__unstableMotion as motion,
__unstableAnimateSharedLayout as AnimateSharedLayout,
} from '../animation';

/**
* Return focusables in a row element, excluding those from other branches
Expand Down Expand Up @@ -43,10 +47,17 @@ function getRowFocusables( rowElement ) {
* @param {WPElement} props.children Children to be rendered.
* @param {Function} props.onExpandRow Callback to fire when row is expanded.
* @param {Function} props.onCollapseRow Callback to fire when row is collapsed.
* @param {boolean} props.animate Boolean layout animation is enabled when true.
* @param {Object} ref A ref to the underlying DOM table element.
*/
function TreeGrid(
{ children, onExpandRow = () => {}, onCollapseRow = () => {}, ...props },
{
children,
onExpandRow = () => {},
onCollapseRow = () => {},
animate = false,
...props
},
ref
) {
const onKeyDown = useCallback( ( event ) => {
Expand Down Expand Up @@ -201,7 +212,11 @@ function TreeGrid(
onKeyDown={ onKeyDown }
ref={ ref }
>
<tbody>{ children }</tbody>
<AnimateSharedLayout>
<motion.tbody layout={ animate ? 'position' : false }>
{ children }
</motion.tbody>
</AnimateSharedLayout>
</table>
</RovingTabIndexContainer>
);
Expand Down
Loading