Choo: handling both hash routing & anchor links

Created on 23 Mar 2017  ยท  11Comments  ยท  Source: choojs/choo

As I'm porting minidocs to choo v5 I'm working on making choo v5 support both hash routing & navigating to anchor links if they are present on the page.

The tricky part is that when the router is first called, the element isn't available on the page yet: https://github.com/yoshuawuyts/choo/blob/master/index.js#L50

So there's not a way to check if an id is available to scroll to before the router is called.

Thoughts on how to implement this?

Enhancement

Most helpful comment

Totally agree!

Another thought off the top of my head.. ๐Ÿ’ญ
Having # behaving differently depending on page content feels a bit weird to me.
What about an option to turn off support for # in routes, i.e. do not turn /some#route into /some/route. And let the user make the choice of treating # as a path or anchor.

๐Ÿˆ

All 11 comments

Forgot to add:

Current behavior of createLocation function is to rewrite a url fragment like /about#install to /about/install before it is passed to the router.

The goal (I think) is to avoid this behavior when the #install id exists on the page, and only tell the router the pathname in that case.

Instead of checking if the id exists, perhaps we could prefix the hash if we want to use it as an anchor. Something like this: /route#anchor:id, just a thought.

Heya, thanks for the suggestion! - Personally I wouldn't want a framework
to tell me how to write my links, but just work as expected. Think we
should explore making that work first (think it might already? not sure,
not tested) and only if that's truly impossible look for different options.
Hope that makes sense โœจ

On Sat, Apr 8, 2017 at 1:36 AM Jens Lind notifications@github.com wrote:

Instead of checking if the id exists, perhaps we could prefix the hash if
we want to use it as an anchor. Something like this: /route#anchor:id,
just a thought.

โ€”
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/yoshuawuyts/choo/issues/440#issuecomment-292676011,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACWlej12aoysc71HoelYNBLGEnrJqeHvks5rtshqgaJpZM4MnbQp
.

Totally agree!

Another thought off the top of my head.. ๐Ÿ’ญ
Having # behaving differently depending on page content feels a bit weird to me.
What about an option to turn off support for # in routes, i.e. do not turn /some#route into /some/route. And let the user make the choice of treating # as a path or anchor.

๐Ÿˆ

@jenslind generally I'm not a big fan of options, but as per #489 by exposing the app._createRoute method the behavior can be changed if people wish to. Hope this good enough! :D

Do you have a hash routing example? I cannot get it to work.

As a fairly new user of choo (and so far overall super impressed by it ๐ŸŽ‰) Iโ€™m having a bit of trouble understanding the way anchor links are handled. How come you have decided to turn /some#route into /some/route?

It took awhile for me to wrap my head around what the section Hash routing in the README.md really meant (and how to get anchor links working like they usually do) โ€“ but that might just be me as a confused newbie. ๐Ÿค” โœจ

It seems like the always-there default to top (<a href="#">top</a>)is does not with by default with nanohref. It would be nice if that worked unless a route is set on #.

hash routing works 'out of the box' except when you are at the root page and have a link bringing somewhere to that page โ€” in which case a / is added before the #link.

how do you prevent that?

hash routing works 'out of the box' except when you are at the root page and have a link bringing somewhere to that page โ€” in which case a / is added before the #link.

how do you prevent that?

Any success on this?
@afincato

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wenkesj picture wenkesj  ยท  3Comments

pekeler picture pekeler  ยท  6Comments

crapthings picture crapthings  ยท  5Comments

yoshuawuyts picture yoshuawuyts  ยท  5Comments

jeffbski picture jeffbski  ยท  5Comments