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

Block editor: scroll block into view on insert #61418

Merged
merged 6 commits into from
May 8, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { useEventHandlers } from './use-selected-block-event-handlers';
import { useNavModeExit } from './use-nav-mode-exit';
import { useBlockRefProvider } from './use-block-refs';
import { useIntersectionObserver } from './use-intersection-observer';
import { useScrollIntoView } from './use-scroll-into-view';
import { useFlashEditableBlocks } from '../../use-flash-editable-blocks';
import { canBindBlock } from '../../../hooks/use-bindings-attributes';

Expand Down Expand Up @@ -122,6 +123,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
clientId,
isEnabled: name === 'core/block' || templateLock === 'contentOnly',
} ),
useScrollIntoView( { isSelected } ),
] );

const blockEditContext = useBlockEditContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* WordPress dependencies
*/
import { useReducedMotion, useRefEffect } from '@wordpress/compose';

export function useScrollIntoView( { isSelected } ) {
const prefersReducedMotion = useReducedMotion();
return useRefEffect(
( node ) => {
if ( isSelected ) {
const { ownerDocument } = node;
const { defaultView } = ownerDocument;
if ( ! defaultView.IntersectionObserver ) {
return;
}
const observer = new defaultView.IntersectionObserver(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need an intersection observer here? Can't we just scroll into view directly? Or check the position at the moment of the block selection. An observer is something that runs constantly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this calls disconnect on first observe but still.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It disconnects immediately, the observer will always immediately call the callback with the initial intersecting state of the element. As for as I know this is the only way to check if an element is visible without using a library.

The Intersection Observer API allows you to configure a callback that is called when either of these circumstances occur:

  • A target element intersects either the device's viewport or a specified element. That specified element is called the root element or root for the purposes of the Intersection Observer API.
  • The first time the observer is initially asked to watch a target element.

https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#intersection_observer_concepts_and_usage

( entries ) => {
// Once observing starts, we always get an initial
// entry with the intersecting state.
if ( ! entries[ 0 ].isIntersecting ) {
node.scrollIntoView( {
behavior: prefersReducedMotion
? 'instant'
: 'smooth',
} );
}
observer.disconnect();
}
);
observer.observe( node );
return () => {
observer.disconnect();
};
}
},
[ isSelected ]
);
}
Loading