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

Maintain scroll position post-drop #291

Closed
alexreardon opened this issue Jan 24, 2018 · 9 comments
Closed

Maintain scroll position post-drop #291

alexreardon opened this issue Jan 24, 2018 · 9 comments

Comments

@alexreardon
Copy link
Collaborator

When dropping an item it is up to a consumer to update their state syncronously. However, this change can result in a shift in the scroll of the container / viewport that the user is interacting with. Ideally there would be no shift after a drop.

I have avoiding this until we do #27. That feature makes this very obvious. I have been hesitant to do this until now as it involves doing some post-drop work (potentially correcting scroll's) which we are not doing at this point. However, I think it is time.

@alexreardon
Copy link
Collaborator Author

Duplicate: #290

@alexreardon
Copy link
Collaborator Author

I can confirm that the scroll is shifted by the size of the dropped item. I am currently attempting to create a reproducible example and then i'll explore ways of getting around it

@alexreardon
Copy link
Collaborator Author

I have set up a little independent demo that has a list with changing items. Unable to replicate issue. I have been digging into React internals to see if it is doing some scroll magic

@alexreardon
Copy link
Collaborator Author

It turns out the issue is only occurring in Chrome. This got me thinking that it is probably not react.

It turns out the issue is caused by Chrome trying to be clever with Scroll Anchoring. To get around this permanently we will add this style (probably to a droppable)

overflow-anchor: none;

@alexreardon
Copy link
Collaborator Author

As a work around you can add the style for yourself in the meantime

@alexreardon
Copy link
Collaborator Author

alexreardon commented Jan 24, 2018

Unfortunately to add this it will be a breaking change as we need to add a property to droppables 💥.

I will add a DroppableProps which will include a data attribute to be bound. This will allow us to control these type of non-visible style changes in the future (similar to how the Draggable works today)

@alexreardon
Copy link
Collaborator Author

Closed by #321

@mdodge-ecgrow
Copy link

mdodge-ecgrow commented Jun 24, 2020

FYI, for anyone coming here looking for answers. I have done something that doesn't look super great, it scrolls after that state is saved and scrolled to the top. And it is very hacky, but it does work to get the dropped item into focus at the top of the scroll container.
I stuck this code into my onDragEnd function.

// scroll the order into view
		const target = document.getElementById(theOrder.orderNumber);
		const whereToScroll =
			target.offsetTop -
			document.getElementById(destination.droppableId).offsetTop -
			55;
		setTimeout(() => {
			document.getElementById(
				destination.droppableId
			).scrollTop = whereToScroll;
		}, 5);

Edit: After doing some more research (aka googling), I have discovered a much simpler cleaner way to do this. Albeit still fairly hacky.
Toward the top of the onDragEnd function before I set the state causing the container to scroll to the top, I run this line saving the scroll position:

const savedScrollPos = document.getElementById(
	destination.droppableId
	).scrollTop;

Then toward the bottom right after saving the state, I do this:

setTimeout(() => {
			document.getElementById(
				destination.droppableId
			).scrollTop = savedScrollPos;
		}, 0);

So it is better, although, you can still see it go to the top, then go back to the original scroll position again. Very hacky, but it works!

@altmshfkgudtjr
Copy link

@mdodge-ecgrow
I have problem same as yours, and my solution is same too. Is there really any better way than this one? 😭😭😭

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants