0.0.15 • Published 1 year ago

react-scroll-router v0.0.15

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

A React package for routing between sections on a single page application

CodeSandbox demo

Quick Start

Setup Provider

import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";

import { ScrollProvider } from "react-scroll-router";

ReactDOM.createRoot(document.getElementById("root")!).render(
  <React.StrictMode>
    <ScrollProvider>
      <App />
    </ScrollProvider>
  </React.StrictMode>
);

Add routes

import { ScrollRoute } from "react-scroll-router";

function App() {
  return (
    <div>
      <ScrollRoute route="/" children={<ExampleHeaderComponent />} />
      <ScrollRoute
        route="/section"
        children={<ExampleSectionComponent />}
        pageTitle="Sample page title"
      />
      <ScrollRoute route="/footer" children={<ExampleFooterComponent />} />
    </div>
  );
}

Components

<ScrollProvider ... />

Props

PropTypeDescriptionRequired
defaultRoutestringDefault route - by default set to '/'
optionsScrollRouterOptionsConfiguration options for react-scroll-router. It partially overrides the default values
<ScrollRoute ... />

Props

PropTypeDescriptionRequired
routestringRoute name:white_check_mark:
childrenReactNodeRoute content:white_check_mark:
containerStyleReact.CSSPropertiesAdditional styles applied to the container

API

useScrollRouter()

export const useScrollRouter: () => {
  route: string;
  goToNextRoute: (nextRoute: string, pageTitle?: string) => void;
  options: ScrollRouterOptions;
};

Returns {route,goToNextRoute,options}

VariableTypeDescription
routestringCurrent route
goToNextRouteFunctionFunction to manually navigate to the next route. Replace or push a new route depending on the options.historyMethod
optionsScrollRouterOptionsOptions object provided in <ScrollProvider ... />

useScrollToRoute()

export const useScrollToRoute: () => ({route: string; scrollOptions?: ScrollIntoViewOptions}) => void;

Returns Function that can scroll your page to the selected route

  const scrollToRoute = useScrollToRoute()
  const scrollOptions = {behavior: "smooth"} // By default behavior value is set to "smooth"

  <button onClick={()=> scrollToRoute({route:'/section', scrollOptions})} />

LICENSE

MIT

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago