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

[List] Scroll to a specific item in the list #11261

Closed
deepfriedbrain opened this issue May 7, 2018 · 9 comments
Closed

[List] Scroll to a specific item in the list #11261

deepfriedbrain opened this issue May 7, 2018 · 9 comments
Labels
support: Stack Overflow Please ask the community on Stack Overflow

Comments

@deepfriedbrain
Copy link

Do we have an API to scroll to a specific item in the list (either in 0.x or 1.x version)? It's particularly tricky to achieve this when the list has sub headers and nested items.

My use case is that user clicks on an item outside of the list. System finds that item in the list and brings it to the center (or top) of the list.

If it's not available, can this be an enhancement request for future?

@oliviertassinari oliviertassinari added the support: Stack Overflow Please ask the community on Stack Overflow label May 7, 2018
@support
Copy link

support bot commented May 7, 2018

👋 Thanks for using Material-UI!

We use the issue tracker exclusively for bug reports and feature requests, however,
this issue appears to be a support request or question. Please ask on StackOverflow where the
community will do their best to help. There is a "material-ui" tag that you can use to tag your
question.

If you would like to link from here to your question on SO, it will help others find it.
If your issues is confirmed as a bug, you are welcome to reopen the issue using the issue template.

@support support bot closed this as completed May 7, 2018
@oliviertassinari
Copy link
Member

oliviertassinari commented May 7, 2018

@deepfriedbrain I believe it's a generic DOM question, unrelated to Material-UI.

@deepfriedbrain
Copy link
Author

@oliviertassinari I agree. Let me clarify that I'm not asking for a "how to" here.

I've already built the code to achieve this for a simple lists (without sub headers or nesting) in my application. But adding headers and nested lists to the mix make it very tricky. Now I have to take into consideration the differences in heights of different elements, and whether the nested lists are expanded or not, and the type of device etc.

This is a common feature found on Lists elements in other UI libraries that I've used in the past (not referring particularly to React Libraries). I noticed that even React Native has an API for this (https://facebook.github.io/react-native/docs/flatlist.html#scrolltoindex).

Today each application that uses this library has to build its own custom solution for scrollTo. If there was an API provided in the library, it would make things so much easier for everyone who needs this feature.

Therefore, I came here to suggest this as an "enhancement" request.

@oliviertassinari
Copy link
Member

oliviertassinari commented May 7, 2018

Why don't you use the .scrollIntoView() API?

@deepfriedbrain
Copy link
Author

Sorry for the late response, but which .scrollIntoView() API are you referring to? I didn't find it in MUI.

Is it this by any chance: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView ? If so, it's an experimental technology and not supported on IE and Safari.

@oliviertassinari
Copy link
Member

oliviertassinari commented May 15, 2018

@deepfriedbrain I was referring to the web API. I'm sure you can find some polyfills, like https://github.com/Treora/scroll-into-view.

@jankalfus
Copy link

jankalfus commented Jan 23, 2019

You can use RootRef to wrap your component https://material-ui.com/api/root-ref/. Then use rootRef on that to access the underlying DOM node and use the standard DOM .scrollIntoView() API.

@RonanFelipe
Copy link

@deepfriedbrain Could I ask you how you have done it?

@deepfriedbrain
Copy link
Author

deepfriedbrain commented Jun 14, 2019

@RonanFelipe I get the DOM nodes, and keep track of node top, node height, window height, etc. using HTML APIs. Following are some random snippets of the code to give you an idea.

        let node = this.refs.listName;
        let childNodes = node.childNodes;

        //...

        let totalNodeHeight = node.scrollHeight;
        let nodeTop = node.scrollTop;
        let nodeHeight = node.offsetHeight;
        let windowHeight = window.innerHeight;

        //...
        
        selItemTop = origPos;
        selItemBottom = selItemTop + childNodes[0].offsetHeight;
        
        //...
        
        window.scrollTo(0, origPos);

        //...

        node.scrollTop = origPos;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support: Stack Overflow Please ask the community on Stack Overflow
Projects
None yet
Development

No branches or pull requests

4 participants