1.11.0 • Published 1 month ago

@noction/vue-draggable-grid v1.11.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

vue-draggable-grid

NPM package NPM monthly downloads codecov code style

Grid layout for vue 3 with draggable, resize, responsive events

Rewrote to TypeScript, Composition API and migrated to Vue3

Usage

import { createApp } from 'vue'
import VueDraggableGrid from '@noction/vue-draggable-grid'
import App from './App.vue'
import '@noction/vue-draggable-grid/styles'

const app = createApp(App)

app.use(VueDraggableGrid)

app.mount('#app')
<script setup lang="ts">
import { ref } from 'vue'

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

<template>
  <grid-layout
    v-model:layout="layout"
    :col-num="12"
    :row-height="30"
    @noc-resize="handleResize"
    @noc-move="handleMove"
    @noc-move-end="handleMoveEnd"
  >
    <template #item="{ item }">
      {{ item.id }}
    </template>
  </grid-layout>
</template>

or

<script setup>
import { ref } from 'vue'

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

<template>
  <grid-layout
    v-model:layout="layout"
    :col-num="12"
    :row-height="30"
  >
    <template #default="{ gridItemProps }">
      <grid-item
        v-for="item in layout"
        v-bind="gridItemProps"
        :id="item.id"
        :key="item.id"
        :x="item.x"
        :y="item.y"
        :w="item.w"
        :h="item.h"
        @noc-resize="handleResize"
        @noc-move="handleMove"
        @noc-move-end="handleMoveEnd"
      >
        {{ item.id }}
      </grid-item>
    </template>
  </grid-layout>
</template>

Props

GridLayout Props

Properties with no default values are Required

NameTypeDefault ValueDescription
colNumnumberNumber of columns in the grid.
layoutLayoutInitial layout of the grid.
autoSizebooleantrueAutomatically adjust the size of items.
breakpointsBreakpoints{ lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0 }Responsive breakpoints for grid.
colsBreakpoints{ lg: 12, md: 10, sm: 6, xs: 4, xxs: 2 }Number of columns at different breakpoints.
horizontalShiftbooleanfalseAllow horizontal shifting of items.
intersectionObserverConfigIntersectionObserverConfig{ root: null, rootMargin: '8px', threshold: 0.40 }Configuration for the Intersection Observer.
isDraggablebooleantrueEnable dragging of grid items.
isResizablebooleantrueEnable resizing of grid items.
marginnumber[][10, 10]Margin around grid items.
maxRowsnumberInfinityMaximum number of rows in the grid.
preventCollisionbooleanfalsePrevent items from colliding with each other.
responsivebooleanfalseEnable responsive layout.
responsiveLayoutsResponsiveLayout{}Responsive layouts for different breakpoints.
rowHeightnumber150Height of each row in the grid.
useCssTransformsbooleantrueUse CSS transforms for positioning.
useObserverbooleanfalseUse Intersection Observer for item visibility.
verticalCompactbooleantrueEnable vertical compacting of items.

GridItem Props

Properties with no default values are Required

NameTypeDefault ValueDescription
breakpointColsBreakpointsNumber of columns at different breakpoints.
colNumnumberNumber of columns in the grid.
containerWidthnumberWidth of the container.
hnumberHeight of the grid item.
idIdUnique identifier for the grid item.
isDraggablebooleanEnable dragging of the grid item.
isResizablebooleanEnable resizing of the grid item.
lastBreakpointBreakpointsKeysLast breakpoint at which the item was resized.
marginnumber[]Margin around the grid item.
maxRowsnumberMaximum number of rows the item can occupy.
rowHeightnumberHeight of each row the item occupies.
useCssTransformsbooleanUse CSS transforms for positioning.
wnumberWidth of the grid item.
xnumberX position of the grid item.
ynumberY position of the grid item.
dragAllowFromstring \| nullnullSelector for allowing drag from specific elements.
dragIgnoreFromstring'a, button'Selector for elements to ignore during drag.
dragOptionobject{}Options for configuring drag behavior.
maxHnumberInfinityMaximum height of the grid item.
maxWnumberInfinityMaximum width of the grid item.
minHnumber1Minimum height of the grid item.
minWnumber1Minimum width of the grid item.
observerIntersectionObserver \| undefinedundefinedIntersection Observer for item visibility.
isStaticbooleanfalseMake the grid item static (non-draggable).

Custom Events

Every custom event is prefixed with noc so it will not collide with default events

This does not include events that update a value written in a v-model

GridLayout Events

NamePayloadDescription
noc-intersection-observeid: Id[]Notify when items are observed for intersection.
noc-intersection-unobserveid: Id[]Notify when items are unobserved for intersection.
noc-item-moveGridItemEvents['noc-move']Notify when a grid item is being moved.
noc-item-move-endGridItemEvents['noc-move-end']Notify when a grid item's move operation ends.
noc-item-resizeGridItemEvents['noc-resize']Notify when a grid item is being resized.
noc-item-resize-endGridItemEvents['noc-resize-end']Notify when a grid item's resize operation ends.
noc-layout-before-mountlayout: LayoutNotify before the grid layout is mounted.
noc-layout-createlayout: LayoutNotify when a new layout is created.
noc-layout-mountlayout: LayoutNotify when the grid layout is mounted.
noc-layout-readylayout: LayoutNotify when the grid layout is ready.
noc-layout-updatelayout: LayoutNotify when the grid layout is updated.
noc-resize-containerpayload: ResizePayloadNotify when the container is resized.

GridLayout v-model

v-model's are not prefixed with noc

NamePayloadDescription
update:layoutlayout: LayoutUpdates the Layout
update:breakpointsbreakpoints: BreakpointsKeysUpdates the Breakpoints

GridItem Events

NamePayloadDescription
noc-resize-containerpayload: ResizePayloadNotify when the container is resized.
noc-resizepayload: ResizePayloadNotify when a grid item is being resized.
noc-resize-endpayload: ResizePayloadNotify when a grid item's resize operation ends.
noc-movepayload: MovePayloadNotify when a grid item is being moved.
noc-move-endpayload: MovePayloadNotify when a grid item's move operation ends.