npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-remember-scroll-cache

v1.1.0

Published

Vue plugin that provides functionality for remembering scroll position and loaded items in a catalog page after navigating back and forth to a single item page, with customisable options.

Downloads

9

Readme

vue-remember-scroll-cache

Vue plugin that provides functionality for remembering scroll position and loaded items in a catalog page after navigating back and forth to a single item page, with customisable options.

Use case

This plugin was created as a usecase for an ecommerce website. Our category view page has an infinity loader (loads more products as we scroll down). We want to retain the loaded products and scroll position after clicking on a product and redirecting to the product page and then navigating back to the category view page. We want to create a seamless transition going between category view listing and product view page so that the user can continue exploring the available products without losing their progress.

Demo GIF

How it works

  1. Adds click event listeners to the elements (using the developer specified selector) living inside the element that uses the v-remember-scroll-cache directive
  2. When a user clicks on the aforementioned elements, the browser's scroll position is retained as well as the content of the variable that contains all items listed (eg. on an ecommerge category view), the name of the variable can also be configured.
  3. User goes to item page, the one they clicked on.
  4. User goes back to the catalog page
  5. Previously loaded items are inserted by the plugin into the vue component using the directive, thus retaining all items previously loaded
  6. Previous scroll position is restored.
  7. User is at the same place they started and can continue scrolling down without losing their previous position in the category view page 🙂

Installation

npm i --save vue-remember-scroll-cache

Usage

In main.js or any other vue entry point:

import Vue from 'vue'
import VueRememberScrollCache from 'vue-remember-scroll-cache'

Vue.use(VueRememberScrollCache)

In vue component template:

<div v-remember-scroll-cache="{ itemsName: 'products', selector: 'a' }">
Catalog view
.....
</div>

itemsName should contain the name of the internal state variable that holds all items that are listed. In this example our vue component has an internal state variable called products that is an array of all products in our ecommerce category view. this.products will be repopulated with the products loaded earlier when scrolling down the category view of our app.

selector should contain the css element selector to use for applying the click event listeners. In this example we want our plugin to run when a link (a tag) inside our div container is clicked

In component script section:

const prevPage = localStorage.getItem('v-remember-scroll-page')
if (this.$products && this.$products.length > 0 && prevPage === window.location.hostname) {
      this.products = this.$products
}

^^ The plugin creates a global variable in this case ($products) and then deletes it after a short time period (3s), so on mounted of our component, we check if the global var is there and update our internal state. (instead of fetching from the API like normal, this time we want to recover previously loaded content after user navigation to product page and back)