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

@alextana/dragster

v1.1.3

Published

A drag and drop library

Downloads

5

Readme

Dragster Vue

Drag and drop composable for Vue 3

The useDragster composable is a Vue.js composition function that allows easy drag and drop functionality for arrays of items. It requires items and dropZoneClass parameters to be passed in for it to work.

Installation

npm install @alextana/dragster

Then in your component you can import the vue composable by doing: import { useDragster } from '@alextana/dragster/vue'

Usage

Once installed and imported in your file you can use the composable like so:

const { lists, onDragEnd, onDragStart } = useDragster({ items: [array1, array2],
dropZoneClass: 'dragster-dropzone', itemClass: 'dragster' })

Parameters

The useDragster composable requires a parameters object with the following properties:

  • items (required): an array of objects representing the items to be dragged and dropped. Each object should have an id property that uniquely identifies the item.
  • dropZoneClass (required): a string representing the class to be used for the drop zone elements.
  • itemClass (required): a string representing the class to be used for the draggable item elements.
  • animationDuration (optional): if you're using an animation with TransitionGroup then you'll need to pass the value (in ms) as a Number.

Returned object

The useDragster function returns an object with the following properties:

  • lists: a reactive ref containing the updated lists after drag and drop operations.

  • onDragEnd: a function that accepts a callback to be executed when the dragging operation ends. Returns an object with an off function that can be used to remove the callback. And it's used like so: onDragEnd(() => { // do something after drag ends })

  • onDragStart: a function that accepts a callback to be executed when the dragging operation starts. Returns an object with an off function that can be used to remove the callback. And it's used like so: onDragStart(() => { // do something after drag starts })

Template

To build your template with draggable items you will need to loop through the returned list from the composable to have reactivity.

A quick example of two lists with draggable items would look like this

  <script setup>
    import { reactive, ref } from 'vue'
    import { useDragster } from '@alextana/dragster/vue'

    // First array
    const array1 = reactive([
      { id: 4453, name: 'Connie 🧸' },
      { id: 456, name: 'Bradley 🧸' },
      { id: 789, name: 'Marvin 🧸' },
      { id: 321, name: 'Simon 🦧' },
      { id: 654, name: 'Klaus 🧸' }
    ])

    // Second array
    const array2 = reactive([
      { id: 9874545, name: 'Emma 🤪' },
      { id: 6590, name: 'Bailey 🧸' },
      { id: 322345, name: 'Peanut 🧸' },
      { id: 785659, name: 'Bluebell 🐳' },
      { id: 246766, name: 'Mabel 🧸' }
    ])

    const { lists, onDragEnd, onDragStart } = useDragster({
      items: [array1, array2],
      dropZoneClass: 'dragster-dropzone',
      itemClass: 'dragster',
      animationDuration: 200 // based on the transition declared in css
    })

    onDragStart(() => {
      // do something when dragging starts
    })

    onDragEnd(() => {
      // Do something when drag ends
    })
  </script>

  <div class="dragster-dropzone">
    <div v-for="item in lists[0]" :key="item.id">
      <div
        :id="item.id.toString()" class="dragster"
      >
        <h3>
          {{ item.name }}
        </h3>
      </div>
    </div>
  </div>
  <div class="dragster-dropzone">
    <div v-for="item in lists[1]" :key="item.id">
      <div
        :id="item.id.toString()" class="dragster"
      >
        <h3>
          {{ item.name }}
        </h3>
      </div>
    </div>
  </div>

Animations

Animations are not baked in but Vue makes it really easy to do your own, you could do something like wrapping each list in a TransitionGroup component and adding some styles:

<TransitionGroup name="fade" tag="div">
    <div v-for="item in lists[1]" :key="item.id">
    <!-- ..... -->
    </div>
  </TransitionGroup>
.fade-move,
.fade-enter-active,
.fade-leave-active {
  transition: all 0.2s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.fade-leave-active {
  position: absolute;
  transition: 0s; /* important so the element doesn't stick when the transition starts */
}