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

vue3-draggable-grid

v0.0.6

Published

基于vue3+grid布局实现的拖拽布局

Downloads

30

Readme

🎄 vue3-draggable-grid

一个基于vue3+grid布局的网格拖拽布局,支持网格吸附、碰撞检测等。

NPM npm bundle size (version) npm npm

演示文档

⭐️ 功能一览

  • 可拖拽
  • 可调整大小
  • 网格吸附
  • 碰撞检测
  • 兼容移动端(采用 pointer 事件编写,兼容移动端)

📦 安装

# 选择一个你喜欢的包管理器

# Npm
npm install vue3-draggable-grid --save

# Yarn
yarn add vue3-draggable-grid -D

# Pnpm
pnpm add vue3-draggable-grid -D

全量引入

// main.ts
import { createApp } from 'vue'
import App from './App.vue'
import Vue3DraggableGrid from 'vue3-draggable-grid'
import "vue3-draggable-grid-drop/dist/style.css"
createApp(App).use(Vue3DraggableGrid).mount('#app')

按需引入

// 组件中
import { GridLayout, GridItem } from 'vue3-draggable-grid'
import 'vue3-draggable-grid/dist/style.css'

组件中使用

<template>
  <div class="layout-box">
      <grid-layout
          v-model:data="layout"
          @draggableStart="draggableStart"
          @draggableHandle="draggableHandle"
          @draggableEnd="draggableEnd"
          @remove="remove"
      >
        <grid-item v-for="item in layout" :key="item.id" :id="item.id">
            {{ item.id }}
        </grid-item>
    </grid-layout>
  </div>
</template>

<script setup lang="ts">
import { GridLayout, GridItem, type Layout, type LayoutItem } from 'vue3-draggable-grid'
import 'vue3-draggable-grid/dist/style.css'
import { ref, watch } from 'vue'

const layout = ref<Layout>([
  { id: '1', x: 1, y: 1, h: 1, w: 1 },
  { id: '2', x: 2, y: 1, h: 1, w: 1 },
  { id: '3', x: 3, y: 1, h: 1, w: 1 },
  { id: '4', x: 4, y: 1, h: 1, w: 1 },
  { id: '5', x: 1, y: 2, h: 1, w: 1 },
  { id: '6', x: 1, y: 3, h: 1, w: 1 },
  { id: '7', x: 1, y: 4, h: 1, w: 1 },
  { id: '8', x: 1, y: 5, h: 4, w: 1 },
  { id: '9', x: 2, y: 2, h: 1, w: 1 },
  { id: '10', x: 2, y: 3, h: 1, w: 1 },
  { id: '11', x: 2, y: 4, h: 1, w: 1 },
  { id: '12', x: 5, y: 5, h: 1, w: 2 },
])

// 验证更新数据是否正确
watch(layout, () => {
  console.log('数据更新', layout.value)
}, {deep: true})

const draggableStart = (id: string) => {
  console.log('拖拽开始', id)
}

const draggableHandle = (id: string, data: LayoutItem) => {
  console.log('拖拽中', id, data)
}

const draggableEnd = (data: Layout) => {
  console.log('拖拽结束', data)
}

const remove = (id: string) => {
  console.log('删除', id)
}

</script>
<style>

.layout-box {
  width: 1000px;
}

</style>

这里需要注意的一点是,在组件的外层或者组件本身需要指定宽度,不然宽度会计算为0

🎁 Apis

参数类型

interface LayoutItem {
    id: string
    x: number
    y: number
    h: number
    w: number
    static?: boolean
}

type Layout = LayoutItem[]

GridLayout

Props

interface PropsData {
    data: Layout // 布局数据
    col?: number | string, // 列数
    rowH?: number | string // 行高
    gutter?: number | string // 网格间距
    drage?: boolean // 是否可拖拽
    resize?: boolean // 是否可拖拽
    remove?: boolean // 是否可拖拽
    isDrawGridLines?: boolean // 是否绘制网格线
    isCollision?: boolean // 是否碰撞
}

| 属性 | 类型 | 默认值 | 说明 | | --------------- | ---------------- | ------ | ----------------------------------------------- | | data | Layout | [] | 布局数据,支持双向绑定(v-model:data="layoutData") | | col | number/string | 12 | 列数 | | rowH | number/string | 50 | 行高 | | gutter | number/string | 10 | 网格间距 | | drage | boolean | true | 是否可拖拽 | | resize | boolean | true | 是否可缩放 | | remove | boolean | true | 是否可删除 | | isDrawGridLines | boolean | true | 是否绘制网格线 | | isCollision | boolean | true | 是否碰撞 |

🪢 事件

| 事件名 | 说明 | 类型 | | --------------- | ------------- | ------------------------------ | | draggableStart | 拖拽开始时触发 | (index: string) => void | | draggableHandle | 拖拽中触发 | (id: string, newItem: LayoutItem) => void | | draggableEnd | 拖拽结束时触发 | (id: string, newItem: LayoutItem) => void | | remove | 删除方块时触发 | (index: string) => void |

GridItem

Props

interface GridItem {
    id: string // 唯一标识
    draggableFrom?: string // 拖拽源,触发拖拽的元素id
}

| 名称 | 类型 | 默认值 | 说明 | | -------------- | -------- | ------ | --------------- | | id | string | | 子元素的唯一标识 | | draggableFrom | `string` | | 触发拖拽的元素id |

🎍 插槽

| 名称 | 说明 | | ------- | ------------------ | | default | 自定义每个元素的内容 | | resize | 自定义缩放 | | remove | 自定义删除 |