• Stars
    star
    116
  • Rank 302,098 (Top 6 %)
  • Language
    TypeScript
  • Created almost 6 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React library to provide a declarative scroll to between sections

react-scroll-section

Master npm version Netlify Status

React Scroll Section logo

A declarative solution to vertically navigate your React App

Live demo โœจ

Why you should use it? ๐Ÿค”

When you think about implementing a scrollTo functionality in a website the first thing it will your mind will be jQuery, using the following line of code:

$('#target').scroll();

I won't deny that actually works, but the problem comes when you start working with frameworks based on component, like React.

Manage the navigation to another section and which section can be hard task without a state management library.

This library will help you to avoid thinking all the possible edges cases and focus on the final result, which is just provide an easy navigation inside your app.

Built with ๐Ÿ”ง

Just React! Using the following API:

  • Context: using the of <Consumer /> and <Provider />.
  • Ref: new createRef API
  • Hooks: useScrollSection and useScrollSections to interact with the registered sections.

Installation

# npm
> npm install react-scroll-section

# yarn
> yarn add react-scroll-section

Usage

The library provides the following components:

  • ScrollingProvider: responsible to link Section and SectionLink and know which Section is selected.
  • Section: renders a <section /> tag that receives an ID and register itself in ScrollingProvider.
  • useScrollSection: React Hook given the id of the section returns if the section is selected and a callback to scroll to it.
  • useScrollSection: returns an array of all the sections with id, selected and scrollTo.

Examples

Using SectionLink (Static Menu)

import React from 'react';
import {
  ScrollingProvider,
  useScrollSection,
  Section,
} from 'react-scroll-section';

const StaticMenu = () => {
  const homeSection = useScrollSection('home');
  const aboutSection = useScrollSection('about');

  return (
    <ul>
      <li onClick={homeSection.onClick} selected={homeSection.selected}>
        Home
      </li>
      <li onClick={aboutSection.onClick} selected={aboutSection.selected}>
        About
      </li>
    </ul>
  );
};

const App = () => (
  <ScrollingProvider>
    <StaticMenu />
    <Section id="home">MY HOME</Section>
    <Section id="about">ABOUT ME</Section>
  </ScrollingProvider>
);

Using SectionLinks (Dynamic Menu)

import React from 'react';
import {
  ScrollingProvider,
  useScrollSections,
  Section,
} from 'react-scroll-section';

export const DynamicMenu = () => {
  const sections = useScrollSections();

  return (
    <ul>
      {sections.map(({ id, onClick, selected }) => (
        <li key={id} onClick={onClick} selected={selected}>
          {id.toUpperCase()}
        </li>
      ))}
    </ul>
  );
};

const App = () => (
  <ScrollingProvider>
    <DynamicMenu />
    <Section id="home">Home section</Section>
    <Section id="about">About section</Section>
  </ScrollingProvider>
);

Props

ScrollingProvider

Property Type Required Default Description
debounceDelay number false 50 time to wait until the calculation of the current section
scrollBehavior string false "smooth" scrolling style
children ReactNode false null React component
offset number false null Vertical offset the modifies the final scrolling position

Section

Property Type Required Default Description
children ReactNode false null Section content
id string true - Section ID

Contributing ๐Ÿง‘โ€๐Ÿ’ผ

I'm always open for suggestions and improvements, so don't hesitate to open an Issue or new Pull Request.

Setup project

This project is using Yarn workspace, therefore check that you have Yarn as your package manager.

# Check if you have you Yarn install
> yarn -v
1.22.18

# Install dependencies
> yarn

Commands

# Builds library
> yarn build

# Starts demo app locally
> yarn start

# Run E2E tests
> yarn test

# Build library + demo app
> yarn build:demo

License ๐Ÿ”–

MIT.

More Repositories

1

gatsby-starter-mate

An accessible and fast portfolio starter for Gatsby integrated with Contentful CMS
TypeScript
545
star
2

astro-art-portfolio

Minimalistic Art showcase built with Astro and connected with Notion API
Astro
103
star
3

gatsby-starter-event-calendar

A customisable calendar event page built with Gatsby integrated with Google Spreadsheet
TypeScript
76
star
4

astro-resume

Resume builder written in Markdown using Astro with Tailwind
Astro
71
star
5

react-hotkey-tooltip

A global Hotkey provider with built in tooltip for React
TypeScript
34
star
6

gatsby-theme-jsonresume

A Gatsby Theme with JSONResume to host your Resume in HTML and PDF
JavaScript
24
star
7

weather-styled-icon

โ›…๏ธโ˜€๏ธ๐ŸŒง๐ŸŒจin โš›๏ธ
JavaScript
23
star
8

astro-multi-framework-dashboard

Multi framework dashboard built with Astro
TypeScript
21
star
9

e2e-react-native-detox

Demo of react-native project using Detox
JavaScript
14
star
10

maintainable-icon-system-react

Implementation of a maintainable icon system for React and React Native
JavaScript
14
star
11

react-radio-group-context

Radio Group Component for React written with the new Context API
JavaScript
10
star
12

jsonresume-theme-standard-resume

Standard Resume JSONResume theme
Handlebars
7
star
13

gatsby-starter-linkedin-resume

Generate your resume (HTML, PDF) from your Linked information with Gatsby ๐Ÿ’ช
TypeScript
6
star
14

particle-js-background

Created with CodeSandbox
JavaScript
6
star
15

astro-mate

New version of Gatsby Mate, built in Astro
Astro
6
star
16

test-workshop-fastify

Test project using fastify for the workshop of Nodeconf EU 2019
JavaScript
4
star
17

oss-project-dashboard

Dashboard to provide a quick overview of Open Source projects in Github
TypeScript
4
star
18

gatsby-theme-mate

Gatsby theme for gatsby-starter-mate
TypeScript
4
star
19

react-spring-parallax-demo

Demo of parallax effect of react-spring
JavaScript
3
star
20

custom-icons-react-native

This is an example of how to load custom icons into react native without Fontello or Icomoon
JavaScript
3
star
21

portfolio

My portfolio where I show information about myself, my projects and articles.
Astro
3
star
22

brastlewark-finder

Web application that will allow you to find all the gnomes inside Brastlewark (Gnome town) and see their characteristics
JavaScript
2
star
23

30DaysOfReactNative

30 RN Projects in 30 Days
JavaScript
1
star
24

react-scrollreveal

React wrapper for the library scrollreveal
JavaScript
1
star
25

weather-app-integration-test-cosmos

This project is a test of cosmos framework to do integration test
JavaScript
1
star
26

astro-client-directives-test

Created with StackBlitz โšก๏ธ
Astro
1
star
27

Felix-fix-it-multiplayer

Remake of the retro game: Felix Fix It, with the ability to create rooms dynamically in order to play with another person via HTTPS.
C
1
star
28

gatsby-followers

Gatsby page that shows all the followers of an user in different platform
CSS
1
star
29

error-handling-workshop

Workshop of error Handling - NodeConf 2019
JavaScript
1
star
30

from-CRA-to-CAA

Talk given at Byteconf React 2020
JavaScript
1
star
31

visual-testing-storybook

Example of how to do visual testing with storybook
JavaScript
1
star
32

typescript-eslint-test

Testing new typescript-eslint with prettier and airbnb config
JavaScript
1
star
33

exchange-app

Exchange app built with React and Recoil
TypeScript
1
star