Giter VIP home page Giter VIP logo

reach-router-scroll-top's Introduction

reach-router-scroll-top

Want to have the accessibility features of Reach Router and scroll to top after navigating? Get it working with just this one component!

Install

npm install --save reach-router-scroll-top

Quickstart

// in your App.js / entry point
import { OnRouteChange } from "reach-router-scroll-top";
// ...
function App() {
	return (
		<div>
			<Router>
				<HomePage path="/" />
			</Router>
			{/* add it after the router! */}
			<OnRouteChange />
		</div>
	);
}
// ...

What it solves

When using Reach Router, the accessibility features are turned on by default. After scrolling to the bottom at one page and navigating to another page, the newly loaded page will be scrolled to the same distance as the previous. This can be disabled by setting primary={false} at the Router component of Reach, but this would also disable the accessibility features.

What it does

It watches the current location path and if it changed, it will scroll the view to the top.

Usage

Just add the <OnRouteChange /> component of this package to your App.js component. Important: The component must be rendered outside and after of the <Router> component! Otherwise the Reach Router will call focus() on the matched route afterwards and will undo the action.

// App.js - main entry point of react application
import React from "react";
import "./App.css";
import { Router } from "@reach/router";
import { OnRouteChange } from "reach-router-scroll-top";
// ...
import HomePage from "./pages/index";

function App() {
	return (
		<div>
			<Router>
				<HomePage path="/" />
			</Router>
			<OnRouteChange />
		</div>
	);
}

export default App;

You can also provide a custom action instead:

<OnRouteChange
	action={() => {
		window.scrollTo(0, 0);
	}}
/>

Example

Checkout the Example code for a working example with create-react-app.

Documentation

Component: OnRouteChange Parameters:

Parameter Default Description
action () => window.scrollTo(0, 0) The action that should be executed after the location changed.

reach-router-scroll-top's People

Contributors

darksmile92 avatar

Watchers

James Cloos avatar  avatar

reach-router-scroll-top's Issues

Edit Docs with working code

Doc contains this line
import { OnRouteChange } from "reach-router-scroll-top";
which does not works
it should be corrected to
import OnRouteChange from "reach-router-scroll-top";
which works perfectly.
Thanks for the workaround package :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.