1.0.0 • Published 2 years ago

x-sortable v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Sortable Web Component

Examples

import { SortableElement } from 'x-sortable'

const style = document.createElement('style')
document.head.appendChild(style)
style.textContent = /*css*/ `
x-sortable {
  display: flex;
  flex-flow: column nowrap;
}
button {
  padding: 10px;
  background: #444;
  color: #aaa;
  border: 2px solid #988;
}
`

customElements.define('x-sortable', SortableElement)

const sortable = new SortableElement()

sortable.vertical = true
// sortable.selector = 'button'

const items: any = []

for (let i = 0; i < 10; i++) {
  const btn = document.createElement('button')
  btn.textContent = '' + i
  items.push(btn)
  sortable.appendChild(btn)
}

document.body.appendChild(sortable)

sortable.onsortend = ({ detail: { oldIndex, newIndex } }) => {
  const item = items.splice(oldIndex, 1)[0]
  items.splice(newIndex, 0, item)
  for (const item of items)
    sortable.appendChild(item)
}

API

Credits

Contributing

Fork or edit and submit a PR.

All contributions are welcome!

License

MIT © 2022 stagas

1.0.0

2 years ago

0.1.0

2 years ago

0.0.1

2 years ago