slate: Scroll-to-cursor is broken inside scrollable containers

Do you want to request a feature or report a bug?

bug

What’s the current behavior?

Set the editor to a fixed height and overflow-y: auto (standard procedure for scrolling content). Press enter several times to exceed this height and the and cursor disappears; a scrollbar appears but the editor does not scroll down. However, if you type a few characters (instead of just creating new lines with 0-1 characters each), the editor suddenly snaps down as expected.

Demo (direct from example page): slate scroll gif

Slate JSFiddle with same issue: https://jsfiddle.net/f4150bbq/

This is not reproduceable in a simple JSFiddle with a contenteditable div: https://jsfiddle.net/gz2wrkxz/1/

What’s the expected behavior?

When content exceeds available height, scroll editor down to cursor (i.e., the behavior that happens when you type several characters in the demo above).

I’ve tried my best to figure out the cause of this, but I’m coming up short- could there be a problem with scrollToSelection? Seeing the expected behavior when typing several characters is also interesting; are we accidentally overriding some default browser behavior?

Mac OS 10.12.3, Chrome 60.0.3112.101, Slate current

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 18 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Either way, this is still something that would be nice to fix in core. Since the current behavior seems obviously wrong. If someone wants to make a PR I’d be happy to have this fixed. Thanks!

I’d like to add my 2¢ by saying that scrolling logic should probably be a separate plugin.

Not sure if this is possible to implement with the way Slate operates now, however, scrolling behaviour is not universal for all types of applications. For example, focusing on void blocks (i.e. an image) may make sense in some cases to have the view scroll to the top edge of the block, and in others, to the bottom edge; and in some cases no scrolling is desired at all. Another case is a textfield with a limited input that requires no scrolling, which could make Slate component a lot lighter if it isn’t requiring additional logic. Finally, there may be apps that prefer smooth scrolling or other clever methods.

@ianstormtaylor @jennaplusplus @conorcussell Looking at this more, I think I see some potential issues.

screen shot 2017-09-05 at 10 59 38 am

No matter what ends up happening here, we do a window.scrollTo, everything in this function is assuming that the scrolling container is the window, i’d wager a guess that for most people experiencing this issue, the window isn’t there scrolling container. I know that even for myself I use a custom scrolling container, because my editor can be positioned around the screen using position fixed.

So now the interesting part, what do we do about this? Should we try and figure out every time we run this function what the true scrolling container is?

I believe that this may be fixed by https://github.com/ianstormtaylor/slate/pull/3093, which has changed a lot of the logic in Slate and slate-react especially. I’m going to close this out, but as always, feel free to open a new issue if it persists for you. Thanks for understanding.

Just thought I’d add my experience, hopefully it’ll help someone else.

I ran into this issue, where the editor wouldn’t scroll correctly on pressing enter in a scrollable container. However, after setting some breakpoints around the relevant logic, the issue I had was that findScrollContainer() was finding the “wrong” container. I had another container with auto overflow around the editor, which, however, wasn’t doing the scrolling as its parent also had auto scroll. findScrollContainer() was finding the inner one.

I fixed it/worked around it by just setting overflow: visible on the container that was incorrectly identified by slate as the scroller by findScrollContainer().