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

simplydrag-js

v1.2.1

Published

A library for making html objects dragable

Downloads

19

Readme

see the npm package at link

Usage

Install the package:

npm install simplydrag-js

Import and use:

import { makeDraggable } from 'simplydrag-js'

//get the element that you want to make draggable
var draggable = document.querySelector("#toDrag")

//call the makeDraggable function with the element passed as a parameter
makeDraggable(draggable)

And that's it! Now your element should be completely draggable!

Note: This makes your HTML element's position 'absolute'. So you should be aware of this when using this package, just in case you need your element's position attribute to be different.

Additional Note: For more advanced use cases where you might need to get the absolute position relative to a parent element, there are options to allow this position to be saved on the element in dat attributes. This is great for when you might need to get the absolute position relative to a portion of the screen set aside for printing (where the print css and positioning might need to be different to the general webpage positioning). This allows you to repostion the parent element, but still keep the draaggable element absolute position relative to the parent element and add this back to the parent elements position to get the right spot for the draggable element before printing.

Here's an example of

import { makeDraggable } from 'simplydrag-js'
import { Printer } from 'simplyprint-js'
var scrapbook = document.querySelector('#scrapbook') as HTMLElement
const options = {dataAttributeName:'scrapbook', parentElement:scrapbook} as options
//object draggable by default
var draggableArr = document.querySelectorAll(".draggable") ;
draggableArr.forEach((draggable)=> makeDraggable(draggable as HTMLElement, options))

//enable print button
const btnPrint = document.querySelector('#btn-print') as HTMLDivElement
btnPrint.onclick = () => {
    //for each draggable element...
    var draggableArr = document.querySelectorAll('.draggable') as NodeListOf<HTMLElement>
    draggableArr.forEach((draggableElement) => 
    {
        //set x and y as relative to parent element (when a x,y = 0,0 in my use case)
        draggableElement.style.left = draggableElement.getAttribute('data-scrapbook-x') as string + (0) + 'px'
        draggableElement.style.top = draggableElement.getAttribute('data-scrapbook-y') as string + (0) +'px'
    })

    //print
    var cssArr = [printCss]
    var printer = new Printer('#scrapbook', cssArr)
    printer.print()

    //set images in scrapbook back to the original positions
    var scrapbook = document.querySelector('#scrapbook') as HTMLElement
    var sbX = scrapbook.getBoundingClientRect().x
    var sbY = scrapbook.getBoundingClientRect().y
    draggableArr.forEach((draggableElement) => 
    {
        var xRelSB = Number(draggableElement.getAttribute('data-scrapbook-x'))
        var yRelSB = Number(draggableElement.getAttribute('data-scrapbook-y'))

        var orignialX:number = sbX + xRelSB
        var originalY:number = sbY + yRelSB

        //set x as relative to parent element when a x,y = 0,0
        draggableElement.style.left = String(orignialX) + 'px'
        draggableElement.style.top = String(originalY) + 'px'
    })
}

Please feel free to take a look at the underling package code, here or on github to get an idea of what is going on.