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

Gatsby's Link overrides scrolling behavior with anchors #7450

Closed
alexkirsz opened this issue Aug 19, 2018 · 4 comments
Closed

Gatsby's Link overrides scrolling behavior with anchors #7450

alexkirsz opened this issue Aug 19, 2018 · 4 comments
Labels
stale? Issue that may be closed soon due to the original author not responding any more. type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@alexkirsz
Copy link
Contributor

Description

When clicking on an anchor <Link />, Gatsby will take it upon itself to scroll to the corresponding element if it exists, or to the top of the page if it doesn't. See

if (pathname === location.pathname || !pathname) {
const element = hash
? document.getElementById(hash.substr(1))
: null
if (element !== null) {
element.scrollIntoView()
} else {
// This is just a normal link to the current page so let's emulate default
// browser behavior by scrolling now to the top of the page.
window.scrollTo(0, 0)
}
}

This is inconsistent with react-router's <Link /> component which simply pushes a new history entry and leaves the scrolling up to the user.

This behavior prohibits me from using Gatsby's <Link /> for anchor links, as I want to override the scrolling part with a smooth scrolling behavior. Instead, I created my own <CustomLink /> component that uses Gatsby's push API.

Steps to reproduce

import { Link } from "gatsby";

ReactDOM.render(<Link to="/#about" />, document.getElementById("app"));

Expected result

Clicking the Link doesn't affect the page's scroll.

Actual result

Clicking the Link affects the page's scroll.

Environment

  System:
    OS: macOS High Sierra 10.13.6
    CPU: x64 Intel(R) Core(TM) i7-7567U CPU @ 3.50GHz
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 10.4.0 - /usr/local/bin/node
    Yarn: 1.7.0 - /usr/local/bin/yarn
    npm: 6.2.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 68.0.3440.106
    Firefox: 61.0.1
    Safari: 11.1.2
  npmPackages:
    gatsby: next => 2.0.0-beta.61
    gatsby-plugin-feed: next => 2.0.0-beta.4
    gatsby-plugin-google-analytics: next => 2.0.0-beta.3
    gatsby-plugin-jss: next => 2.0.2-beta.3
    gatsby-plugin-offline: next => 2.0.0-beta.5
    gatsby-plugin-react-helmet: next => 3.0.0-beta.4
    gatsby-plugin-sharp: next => 2.0.0-beta.7
    gatsby-remark-copy-linked-files: next => 2.0.0-beta.3
    gatsby-remark-images: next => 2.0.1-beta.9
    gatsby-remark-prismjs: next => 3.0.0-beta.5
    gatsby-remark-responsive-iframe: next => 2.0.0-beta.3
    gatsby-remark-smartypants: next => 2.0.0-beta.3
    gatsby-source-filesystem: next => 2.0.1-beta.10
    gatsby-transformer-remark: next => 2.1.1-beta.5
    gatsby-transformer-sharp: next => 2.1.1-beta.6

File contents (if changed)

gatsby-config.js: N/A
package.json: N/A
gatsby-node.js: N/A
gatsby-browser.js: N/A
gatsby-ssr.js: N/A

@Chuloo Chuloo added the status: needs reproduction This issue needs a simplified reproduction of the bug for further troubleshooting. label Aug 23, 2018
@JulianOrozcoC
Copy link

Please share the repo so I can reproduce the error!

@alexkirsz
Copy link
Contributor Author

This isn’t an error, this is more of a feature. I created this issue to discuss the relevancy of such a feature. I’ll make an example repo ASAP.

@Chuloo Chuloo added type: question or discussion Issue discussing or asking a question about Gatsby 🏷 type: feature and removed status: needs reproduction This issue needs a simplified reproduction of the bug for further troubleshooting. labels Aug 24, 2018
@gatsbot
Copy link

gatsbot bot commented Dec 28, 2018

Old issues will be closed after 30 days of inactivity. This issue has been quiet for 20 days and is being marked as stale. Reply here or add the label "not stale" to keep this issue open!

@gatsbot gatsbot bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Dec 28, 2018
@gatsbot
Copy link

gatsbot bot commented Jan 8, 2019

This issue is being closed due to inactivity. Is this a mistake? Please re-open this issue or create a new issue.

@gatsbot gatsbot bot closed this as completed Jan 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale? Issue that may be closed soon due to the original author not responding any more. type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

3 participants