UNPKG

motion-v

Version:

<p align="center"> <img width="100" height="100" alt="Motion logo" src="https://user-images.githubusercontent.com/7850794/164965523-3eced4c4-6020-467e-acde-f11b7900ad62.png" /> </p> <h1 align="center">Motion for Vue</h1>

48 lines (47 loc) 1.68 kB
import { mixNumber } from "../../utils/mix/number.mjs"; import { motionValue } from "../../external/.pnpm/motion-dom@12.5.0/external/motion-dom/dist/es/value/index.mjs"; import "../../external/.pnpm/motion-utils@12.5.0/external/motion-utils/dist/es/errors.mjs"; import { isMotionValue } from "../../utils/motion-value.mjs"; function compareMin(a, b) { return a.layout.min - b.layout.min; } function getValue(item) { return item.value; } function checkReorder(order, value, offset, velocity) { if (!velocity) return order; const index = order.findIndex((item2) => item2.value === value); if (index === -1) return order; const nextOffset = velocity > 0 ? 1 : -1; const nextItem = order[index + nextOffset]; if (!nextItem) return order; const item = order[index]; const nextLayout = nextItem.layout; const nextItemCenter = mixNumber(nextLayout.min, nextLayout.max, 0.5); if (nextOffset === 1 && item.layout.max + offset > nextItemCenter || nextOffset === -1 && item.layout.min + offset < nextItemCenter) { return moveItem(order, index, index + nextOffset); } return order; } function moveItem([...arr], fromIndex, toIndex) { const startIndex = fromIndex < 0 ? arr.length + fromIndex : fromIndex; if (startIndex >= 0 && startIndex < arr.length) { const endIndex = toIndex < 0 ? arr.length + toIndex : toIndex; const [item] = arr.splice(fromIndex, 1); arr.splice(endIndex, 0, item); } return arr; } function useDefaultMotionValue(value, defaultValue = 0) { return isMotionValue(value) ? value : motionValue(defaultValue); } export { checkReorder, compareMin, getValue, moveItem, useDefaultMotionValue };