Giter VIP home page Giter VIP logo

svelte-agnostic-draggable's Introduction

svelte-agnostic-draggable

jQuery UI interactions draggable/droppable/sortable for Svelte

Important Notice: because of not yet merged (but important) bug fixes in the original agnostic-draggable, this package currently uses a newer copy, namely agnostic-draggable-bug-fixed. Apart from these bug fixes, however, the original description still applies.

NPM users: please consider the Github README for the latest description of this package (as updating the docs would otherwise always require a new NPM package version)

Just a small note: if you like this module and plan to use it, consider "starring" this repository (you will find the "Star" button on the top right of this page), so that I know which of my repositories to take most care of.

Installation

npm install svelte-agnostic-draggable

Usage

It is recommended to import the package within a module context:

<script context="module">
  import { draggable, droppable, sortable } from 'svelte-agnostic-draggable'
</script>

<script>
  function EventHandler (Event) { ... } // just as an example
</script>

<div use:draggable={{ options }}
  on:draggable:init={EventHandler} on:draggable:destroy={EventHandler}
  on:drag:start={EventHandler} on:drag:move={EventHandler} on:drag:stop={EventHandler}
>...</div>

<div use:droppable={{ options }}
  on:droppable:init={EventHandler} on:droppable:destroy={EventHandler}
  on:droppable:activate={EventHandler} on:droppable:deactivate={EventHandler}
  on:droppable:over={EventHandler} on:droppable:out={EventHandler}
  on:droppable:drop={EventHandler}
>...</div>

<div use:sortable={{ options }}
  on:sortable:init={EventHandler} on:sortable:destroy={EventHandler}
  on:sortable:activate={EventHandler} on:sortable:deactivate={EventHandler}
  on:sort:start={EventHandler} on:sort:move={EventHandler} on:sort:stop={EventHandler}
  on:sortable:over={EventHandler} on:sortable:out={EventHandler}
  on:sortable:change={EventHandler} on:sortable:update={EventHandler}
  on:sortable:remove={EventHandler} on:sortable:receive={EventHandler}
>...</div>

Important Note for mobile Users

Since agnostic-draggable itself handles MouseEvents only, you will presumably also need a module such as svelte-touch-to-mouse which maps TouchEvents to MouseEvents. All examples for svelte-agnostic-draggable already use that module and may therefore also be used on smartphones and tablets.

Examples

All examples are available on the Svelte REPL - feel free to play with them!

Here are some basic ones:

  • Draggable (with Event Log - open the REPL console)
  • Droppable (with Event Log - open the REPL console)
  • Sortable (with Event Log - open the REPL console)

For more detailled examples, see below.

Background Information

svelte-agnostic-draggable is a thin wrapper around agnostic-draggable which makes jQuery UI-like interactions draggable, droppable and sortable available to web applications without any dependency for jQuery and jQuery UI.

With this wrapper, these interactions may now be used as "actions" in Svelte components. For the available options and their format, please, consider the documentation of the underlying library.

The wrapper itself is almost trivial, all the "heavy lifting" is done by agnostic-draggable - thus, credits primarily go to Marcos Pont.

More detailled Examples

All examples are available on the Svelte REPL.

Build Instructions

You may easily build this package yourself.

Just install NPM according to the instructions for your platform and follow these steps:

  1. either clone this repository using git or download a ZIP archive with its contents to your disk and unpack it there
  2. open a shell and navigate to the root directory of this repository
  3. run npm install in order to install the complete build environment
  4. execute npm run build to create a new build

You may also look into the author's build-configuration-study for a general description of his build environment.

License

MIT License

svelte-agnostic-draggable's People

Contributors

alanblanchet avatar rozek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

svelte-agnostic-draggable's Issues

Module not found: Error: Package path . is not exported from package ...node_modules/svelte-agnostic-draggable

I get this issue when compiling with webpack (svelte-loader).

I solved by adding this to exports in package.json:
".": "./dist/svelte-agnostic-draggable.esm.js"

so my package.json looks as follows
{ "name": "svelte-agnostic-draggable", "version": "0.1.12", "description": "jQuery UI interactions draggable/droppable/sortable for Svelte", "module": "./dist/svelte-agnostic-draggable.esm.js", "svelte": "./dist/svelte-agnostic-draggable.esm.js", "types": "./dist/svelte-agnostic-draggable.d.ts", "exports": { "./package.json": "./package.json", ".": "./dist/svelte-agnostic-draggable.esm.js" }, "scripts": { "build": "rimraf dist && rollup -c rollup.config.js && tsc && mv src/*.d.ts dist && rm src/*.js*", "agadoo": "agadoo ./dist/svelte-agnostic-draggable.esm.js", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "git+https://github.com/rozek/svelte-agnostic-draggable.git" }, "keywords": [ "svelte3", "svelte-action", "jquery-ui", "draggable", "droppable", "sortable" ], "author": "Andreas Rozek <[email protected]> (https://www.rozek.de/)", "license": "MIT", "bugs": { "url": "https://github.com/rozek/svelte-agnostic-draggable/issues" }, "homepage": "https://github.com/rozek/svelte-agnostic-draggable#readme", "dependencies": { "agnostic-draggable-bug-fixed": "^1.0.13" }, "devDependencies": { "@rollup/plugin-commonjs": "^19.0.0", "@rollup/plugin-node-resolve": "^13.0.0", "@rollup/plugin-typescript": "^8.2.1", "agadoo": "^2.0.0", "rimraf": "^3.0.2", "rollup": "^2.51.2", "rollup-plugin-terser": "^7.0.2", "typescript": "^4.3.2" } }

The requested module does not provide an export named 'default'

svelte-agnostic-draggable doesn't seem to load as expected after running npm install svelte-touch-to-mouse && npm install svelte-touch-to-mouse.

  • Sveltekit errors 500 loading this directly from within a component: The requested module '/node_modules/agnostic-draggable-bug-fixed/dist/agnostic-draggable.js?v=e9af1830' does not provide an export named 'default'
  • wrapping within onMount throws in the console: svelte-agnostic-draggable.esm.js:1 Uncaught (in promise) SyntaxError: The requested module '/node_modules/agnostic-draggable-bug-fixed/dist/agnostic-draggable.js?v=e9af1830' does not provide an export named 'default'

So, depending how this script is referenced it's possible to replicate there seems to be no export named 'default'.

Any ideas what might be going wrong?

Thanks for this, it looks really comprehensive otherwise.

Consider making options optional and adding types

Hello community,

In my personal project I had to write this piece of code to make it easier to use.
I have set the options to be optional and added the types from @types/jqueryui.

import {
  draggable as Dra,
  droppable as Dro,
  sortable as S
} from "svelte-agnostic-draggable";

export const draggable = (
  node: Element,
  options: JQueryUI.DraggableOptions = {}
) => Dra(node, options);
export const droppable = (
  node: Element,
  options: JQueryUI.DroppableOptions = {}
) => Dro(node, options);
export const sortable = (
  node: Element,
  options: JQueryUI.SortableOptions = {}
) => S(node, options);

Thanks

Trying to add to project

Getting this error message:

500
The requested module '/node_modules/agnostic-draggable-bug-fixed/dist/agnostic-draggable.js?v=e1269044' does not provide an export named 'default'
SyntaxError: The requested module '/node_modules/agnostic-draggable-bug-fixed/dist/agnostic-draggable.js?v=e1269044' does not provide an export named 'default'

Code:

draggable.svelte

<!------------------------------------------------------------------------------
--  for additional, more detailled examples visit                             --
--  https://github.com/rozek/svelte-agnostic-draggable/blob/main/README.md    --
------------------------------------------------------------------------------->

<style>
    .draggable {
      -webkit-touch-callout:none;
      -ms-touch-action:none; touch-action:none;
      -moz-user-select:none; -webkit-user-select:none; -ms-user-select:none; user-select:none;
    }
  </style>
  
  <script>
    import { onMount } from 'svelte';
    import { draggable } from 'svelte-agnostic-draggable'
  
  /**** map all touch events to mouse events ****/
  
    import mapTouchToMouseFor from 'svelte-touch-to-mouse'


  onMount(() => {
    mapTouchToMouseFor('.draggable')
  
  /**** Svelte Event Handling ****/
  
    function onDraggableInit ()    { console.log('Draggable was created') }
    function onDragStart ()        { console.log('dragging started') }
    function onDragMove ()         { console.log('dragging continues') }
    function onDragStop ()         { console.log('dragging was stopped') }
    function onDraggableDestroy () { console.log('Draggable was destroyed') }
  });

  </script>
  

  
  
    <div class="draggable" use:draggable={{
      containment:'parent', cursor:'grabbing'
    }} style="
      display:block; position:absolute;
      left:20px; top:20px; width:100px; text-align:center;
      padding:10px; background:forestgreen; color:white; cursor:grab
    " on:draggable:init={onDraggableInit} on:draggable:destroy={onDraggableDestroy}
      on:drag:start={onDragStart} on:drag:move={onDragMove} on:drag:stop={onDragStop}
    >Drag me!</div>

    <div use:draggable={{
        containment:'parent', cursor:'grabbing'
      }} class='draggable' on:draggable:init={onDraggableInit} on:draggable:destroy={onDraggableDestroy}
    on:drag:start={onDragStart} on:drag:move={onDragMove} on:drag:stop={onDragStop}>
        <img alt="alt" src="https://via.placeholder.com/150/FF0000/FFFFFF?Text=Down.com" />
    </div>
    <div use:draggable={{
        containment:'parent', cursor:'grabbing'
      }} class='draggable' on:draggable:init={onDraggableInit} on:draggable:destroy={onDraggableDestroy}
    on:drag:start={onDragStart} on:drag:move={onDragMove} on:drag:stop={onDragStop}>
        <img alt="alt" src="https://via.placeholder.com/150/000000/FFFFFF/?text=IPaddress.net" />
    </div>
    <div use:draggable={{
        containment:'parent', cursor:'grabbing'
      }} class='draggable' on:draggable:init={onDraggableInit} on:draggable:destroy={onDraggableDestroy}
    on:drag:start={onDragStart} on:drag:move={onDragMove} on:drag:stop={onDragStop}>
        <img alt="alt" src="https://via.placeholder.com/150/FFFF00/000000?Text=WebsiteBuilders.com" />
    </div>


hero.svelte

<script>
	import Draggable from '../components/Draggable.svelte';
</script>


<section class="hero">
	<div class="background">
		<img src="images/logo.png" alt="An Asset of Community Value" width="120" height="57" />
	</div>

	<div class="foreground">
		<Draggable />
	</div>
	
</section>

<style>
	.hero {
		height: 100vh;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
	}

	.fake-image {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
	}

	.background {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: -1;
		background: white;
	}

	.foreground {
		position: relative;
		z-index: 5;
		height: 100%;
    	width: 100%;
	}

	.foreground img {
		user-select: none;
	}
</style>

any ideas?
I've followd your documentation but no luck

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.