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

@jfm/vue-infinite-scroll

v0.2.1

Published

A bidirectional infinite scroll component for Vue.js

Downloads

256

Readme

@jfm/vue-infinite-scroll

A bidirectional infinite scroll component for Vue.js.

Installation

npm i @jfm/vue-infinite-scroll

Demo

Requirements:

Usage

<template>
  <main>
    <button class="btn-reset" @click="reset">reset</button>

    <!-- infinite scroll up -->
    <InfiniteScroll
      v-if="items.length"
      direction="up"
      auto="in-viewport"
      :head="items[0] && items[0].id"
      :next="prev"
      :handler="loadPrevPage"
    />

    <ul>
      <!--
        If you use upward infinite scroll, `data-inf-id` should be set.
        If you use downward infinite scroll, it is not needed.
      -->
      <li
        v-for="item in items"
        :key="item.id"
        :data-inf-id="item.id"
        :style="{ background: `hsl(${item.color.join()})` }"
      >
        {{ item.id }}
      </li>
    </ul>

    <!-- infinite scroll down -->
    <InfiniteScroll
      :head="items.length"
      :next="next"
      :handler="loadNextPage"
    />
  </main>
</template>

<script>
import InfiniteScroll from '@jfm/vue-infinite-scroll'

const limit = 10

export default {
  components: { InfiniteScroll },

  data: () => ({
    items: [],
    prev: 4,
    next: 5
  }),

  methods: {
    async loadNextPage() {
      console.log(Date.now(), 'loadNextPage')
      const page = this.next
      const { items, totalCount } = await this.getMockData({ limit, page })

      if (page === this.next) {
        this.items = this.items.concat(items)
        this.next = totalCount / limit > page ? page + 1 : null
        console.log('next:', this.next)
      }
    },

    async loadPrevPage() {
      console.log(Date.now(), 'loadPrevPage')
      const page = this.prev
      const { items } = await this.getMockData({ limit, page })

      if (page === this.prev) {
        this.items = items.concat(this.items)
        this.prev = page - 1 || null
        console.log('prev:', this.prev)
      }
    },

    async getMockData({ limit = 10, page = 1 } = {}) {
      console.log('page:', page)
      await new Promise(resolve => setTimeout(resolve, Math.random() * 2000))

      // if (Math.random() > 0.7) {
      //   throw new Error('server error')
      // }

      const totalCount = 100
      const start = (page - 1) * limit
      const end = Math.min(start + limit, totalCount)
      const items = []

      for (let i = start; i < end; i++) {
        const color = [360 / (totalCount - 1) * i, '67%', '67%']
        items.push({ id: i + 1, color })
      }

      return {
        items,
        totalCount
      }
    },

    reset() {
      this.items = []
      this.prev = 4
      this.next = 5
    }
  }
}
</script>

<style scoped>
main {
  max-width: 600px;
  margin: 0 auto;
}

ul {
  padding: 0;
}

li {
  margin: 10px;
  list-style: none;
  height: 128px;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 18px
}

.btn-reset {
  position: fixed;
  right: 0;
  top: 0;
}
</style>

Props

auto: false, in-advance or in-viewport. Whether and when to call the handler function. Defaults to in-advance.

  • false: Needs user to click the component to call the handler function manually.
  • in-advance: The handler function will be called automatically in advance of one screen height distance.
  • in-viewport The handler function will be called automatically when the component is fully in viewport.

direction: up or down. Defaults to down. If direction is up, every item element should set a data-inf-id attribute to a unique value. (See usage section for example). It is used to track and adjust the scroll position after loading.

head:

  • If the items array is empty, set it to a falsey false, such as null.
  • If direction is up, set it to the first item's data-inf-id attribute value. It will be used to track and adjust the scroll position. If item's id is number 0, convert it to string.
<InfiniteScroll :head="items[0] && items[0].id">
  • If direction is down, you can simply set it to items.length if the items array is not empty. So you don't need to care about the item's unique id.
<InfiniteScroll :head="items.length">

next: The next page number or cursor. At initial state, if you don't know the cursor value, you could set it to an empty string. If there's no more data, set it to null.

handler: The resource load function. It should return a promise. It should prepend/append results to the list, and set next cursor. If loading error, the promise should be rejected, then the component will show error state.

Slot

<InfiniteScroll ...>
  <template v-slot="{ state, error, auto, direction }">
    <template v-if="state === 'loading'">Loading...</template>
    <template v-else-if="state === 'empty'">Empty</template>
    <template v-else-if="state === 'end'">End</template>
    <template v-else-if="state === 'error'">Error: {{ error.message }}. Click to retry</template>

    <template v-else-if="state === 'standby' && auto !== 'in-advance'">
      {{ auto ? `Scroll ${direction} to load more` : 'Click to load more' }}
    </template>
  </template>
</InfiniteScroll>

Slot props:

  • state: Includes loading, empty, end, error, standby.
  • error: The rejected value of handler function.
  • auto: false, in-advance or in-viewport.
  • direction: up or down.

License

MIT