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

@ksbrooksjr/drag-resize

v0.4.0

Published

Draggable and resizable web component.

Downloads

5

Readme

Introduction

This is a zero dependency web component that provides a draggable and resizable div. This was mainly written as a way for me to explore web components and familiarize myself with the process of publishing to NPM and commercial CDNs. NOTE: this component was designed for desktop devices.

Demo

https://ksbrooksjr.github.io/drag-resize/

Installation

You can install this component from NPM:

npm install @ksbrooksjr/drag-resize

Or you can simply add a script tag that loads the component from a CDN:

<script src="https://cdn.jsdelivr.net/npm/@ksbrooksjr/[email protected]/dist/index.js"></script>

Usage

After installing simply add the component to your markup.

<drag-resize>
  <div>
    <p>Drag and Resize Me!</p>
  </div>
</drag-resize>

Html Attributes

The component can be customized using the following html attributes:

  • isResizable(boolean): whether or not the component can be resized (default: true)
  • isDraggable(boolean): whether or not the component can be dragged (default: true)
  • minwidth(number): the minimum width (in pixels) that the component can be resized to (default: 100)
  • minheight(number): the minimum height (in pixels) that the component can be resized to (default: 100)
  • maxwidth(number): the maximum width (in pixels) that the component can be resized to (default: Infinity)
  • maxheight(number): the minimum height (in pixels) that the component can be resized to (default: Infinity)

Example with all options:

<drag-resize
  isResizable="true"
  isDraggable="false"
  minwidth="100"
  minheight="100"
  maxwidth="500"
  maxheight="500">
  <div>
    <p>Drag and Resize Me!</p>
    <p><noscript>This demo requires JavaScript!</noscript></p>
  </div>
</drag-resize>

Styling

You can style the component using the ::part(wrapper) pseudoselector.

drag-resize::part(wrapper) {
  border-color: blue;
}

Don't change the width, height, top, left, or cursor properties using this method though. To set the initial dimensions and position use css variables. The component accepts the following four css variables:

:root {
  --dr-initial-width: 400px;
  --dr-initial-height: 400px;
  --dr-initial-top: 50px;
  --dr-initial-left: 50px;
}

The div rendered by the component has the following default styles:

drag-resize::part(wrapper) {
  margin: 5px;
  border: 1px solid black;
  background: white;
}

Flash of Unstyled Content

Before the component is registered (or if js is unavailable) the browser will render the children of the custom element. To prevent a flash of unstyled content you should use the drag-resize:not(:defined) selector to give the child node the same styles that will eventually be applied to the component's shadow dom div element.

<style>
  :root {
    --dr-initial-width: 400px;
    --dr-initial-height: 400px;
    --dr-initial-top: 50px;
    --dr-initial-left: 50px;
  }
  drag-resize:not(:defined) > * {
    position: absolute;
    width: var(--dr-initial-width);
    height: var(--dr-initial-height);
    top: var(--dr-initial-top);
    left: var(--dr-initial-left);

    margin: 5px;
    border: 1px solid black;
    background: white;
  }
  drag-resize::part(wrapper) {
    margin: 5px;
    border: 1px solid black;
    background: white;
  }
</style>
<body>
  <drag-resize>
    <div>
      <span>Drag and Resize Me!</span>
    </div>
  </drag-resize>
</body>

Manually Defining the Component

The component is registered by default. If you'd like to import the class and register it yourself then import it like this:

import DraggableResizable from '@ksbrooksjr/drag-resize/dist/component.js'
customElements.define('drag-resize', DraggableResizable)