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

Linking to anchors does not scroll to point on page #228

Closed
CodingItWrong opened this issue Aug 22, 2018 · 4 comments · Fixed by #403
Closed

Linking to anchors does not scroll to point on page #228

CodingItWrong opened this issue Aug 22, 2018 · 4 comments · Fixed by #403
Labels

Comments

@CodingItWrong
Copy link

Say you copy a link to a specific anchor on a page, such as:

https://ember-decorators.github.io/ember-decorators/latest/docs/api/modules/@ember-decorators/service#service

The user would expect that loading this URL in a new tab would automatically scroll you to the #service anchor on the page. But it doesn't seem to; it seems to keep me at the top of the page. I assume this is because of the way the page loads, how that anchor is not present at first.

If possible, it could be nice to do some JS magic to scroll down to that appropriate point on the page once the page is loaded!

@pzuraq
Copy link
Contributor

pzuraq commented Aug 24, 2018

Related to #202, in general linking is limited this way. I think support for anchors may need to be added to the Ember router itself.

@samselikoff
Copy link
Contributor

I consider this a bug as long as we append # to the URL, since the affordance is that will work as a sharable link. We should either fix this (strong prefer) or not change the URL history.

@makepanic
Copy link
Contributor

makepanic commented Sep 10, 2019

If anyone is looking for a cheap fix until this is properly solved, you can reopen the route class in an initializer and implement scrolling the anchor into view:

  Route.reopen({
    afterModel() {
      this._super(...arguments);

      scheduleOnce('afterRender', null, () => {
        if (location.hash.length) {
          const anchor = document.querySelector(`a[href="${location.hash}"`);
          if (anchor) {
            anchor.scrollIntoView();
          }
        }
      });
    }
  })

It's not pretty but works for me

@samselikoff
Copy link
Contributor

@makepanic if you want to PR this I'd accept it, I think it's better than what we have now!

makepanic added a commit to makepanic/ember-cli-addon-docs that referenced this issue Sep 15, 2019
makepanic added a commit to makepanic/ember-cli-addon-docs that referenced this issue Sep 15, 2019
makepanic added a commit to makepanic/ember-cli-addon-docs that referenced this issue Sep 15, 2019
RobbieTheWagner pushed a commit that referenced this issue Sep 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants