1.3.3 ā€¢ Published 9 months ago

gatsby-plugin-scroll-position v1.3.3

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Gatsby Plugin for saving overflow scroll position

  • Automatic persist CSS overflow scroll position
  • Restores position before React has hydrated
  • No flickering
  • Performant ā€“ no event listener
  • Uses localStorage by default

Live Demo šŸš€ provided by the example code.

How to use and Install

npm install gatsby-plugin-scroll-position
# or
yarn add gatsby-plugin-scroll-position

ā€¦ and add it to your gatsby-config.js file:

exports.plugins = [
  {
    resolve: 'gatsby-plugin-scroll-position',
    options: {
      elements: [
        '.class-selector',
        '#id-selector',
        '[data-testid="test-selector"]',
        {
          selector: '#fallback-position',
          ensureInView: '#fallback-position ul li.scroll-to-me', // ensure this element is in view
        },
      ],
    },
  },
]

ensureInView smartness

When nothing is stored in the localStorage, the plugin tries to fulfill the scroll position of the defined selector given in ensureInView.

But when the user did scroll before, it will persist its position, as long as it is actually visible.

How it works

It puts a script at the bottom of every page body in order to restore scroll position before React has hydrated. It stores and restores scroll position on every page transition.

1.3.3

9 months ago

1.3.2

9 months ago

1.3.1

9 months ago

1.3.0

9 months ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago