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

How to override scroll-to-top on route transition? #567

Closed
tobscure opened this Issue Apr 17, 2015 · 11 comments

Comments

Projects
None yet
3 participants
@tobscure
Contributor

tobscure commented Apr 17, 2015

Mithril automatically scrolls to the top when m.route is called (via computePostRedrawHook). I would like to prevent this from happening and immediately scroll down to a certain point as soon as the page is rendered.

I would think I could do this in a node's config function, but it seems that computePostRedrawHook is called after the config functions are run. The only other way I can think of is to use setTimeout, but that results in a slight delay which isn't nice.

@barneycarroll

This comment has been minimized.

Member

barneycarroll commented Apr 17, 2015

@tobscure reading through the source, Mithril has a special exception for this when you're using a routing mode other than hash (path or query), and you have a hash specified. This sounds ideal – you could use the hash either to refer to the ID of the element you're scrolling to or some abstract that represents it…?

@tobscure

This comment has been minimized.

Contributor

tobscure commented Apr 17, 2015

@barneycarroll Problem is, I'm using the hash route mode, at least for the time being!

I think there needs to be a proper way to do this. Would running the configs after the postRedrawHook create any problems?

@barneycarroll

This comment has been minimized.

Member

barneycarroll commented Apr 17, 2015

Don't think so — believe config execution is tail-called by build, so these secure before the scroll reset :/

@lhorie

This comment has been minimized.

Member

lhorie commented Apr 18, 2015

@tobscure it's a somewhat contrived edge case, but technically, yes, running configs after postRedrawHook would mean that document.title changes in a config would not be reflected in the browser history

I'm assuming this issue only really exists because of the lack of support for #559?

@tobscure

This comment has been minimized.

Contributor

tobscure commented Apr 18, 2015

@lhorie No this is a separate issue for me. #559 is regarding updating the URL when the user scrolls; this one is regarding setting the scrolling position when routing into a URL that specifies a scroll position.

Yeah I took a look and I guess it would need to be render -> scrollTo(0, 0) -> run configs -> pushState. Could we add a preConfigHook or something similar?

@barneycarroll

This comment has been minimized.

Member

barneycarroll commented Apr 18, 2015

document.title changes in a config

@lhorie hang on – are you saying the current execution order exists to preserve that feature?

@lhorie

This comment has been minimized.

Member

lhorie commented Apr 18, 2015

@tobscure yeah I think that makes more sense

@tobscure

This comment has been minimized.

Contributor

tobscure commented Apr 19, 2015

@lhorie if you think a preConfigHook sounds like a good solution, I can do a PR?

lhorie added a commit that referenced this issue Apr 19, 2015

@lhorie

This comment has been minimized.

Member

lhorie commented Apr 19, 2015

Actually, just pushed a change for it. It's in the components branch

@lhorie lhorie closed this Apr 19, 2015

@tobscure

This comment has been minimized.

Contributor

tobscure commented Apr 19, 2015

Fantastic, thanks!

@barneycarroll

This comment has been minimized.

Member

barneycarroll commented Apr 19, 2015

Can we just rename components to master? ;)

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