@bluecateng/smooth-reorder

中文文档
2.2.1发布于 2026/10/30ISCTypeScriptCommonJS构建来源已验证近 30 天下载 3408

Smooth list reordering in vanilla JS

原始使用文档
文档目录4 节
  1. Options
  2. Lifecycle
  3. State attribute
  4. Output

@bluecateng/smooth-reorder GitHub license npm version

Allows reordering a list with either mouse dragging or keyboard using only vanilla JS. Elements are moved with CSS transitions where possible or with @bluecateng/nano-spring, so it feels more natural. The callbacks allow implementation of feedback for accessibility.

The article 4 Major Patterns for Accessible Drag and Drop / Sorting a List in Medium has an example of how to implement accessibility.

Size: 4,102 bytes before compression.

Installation

npm i -S @bluecateng/smooth-reorder

Example

import reorder from '@bluecateng/smooth-reorder';

const container = document.querySelector('#test');
reorder(container, {
    onStart: (element, position) => console.log(`Started moving element ${element} from ${position}`),
    onMove: (element, position) => console.log(`Element ${element} will move to ${position}`),
    onMoveEnd: (element, position) => console.log(`Element ${element} is now at ${position}`),
    onFinish: (element) => console.log(`Element ${element} moved`),
    onCancel: (element) => console.log(`Element ${element} move cancelled`),
});

This CSS must be added to the page:

.draggable {
    cursor: move;
    cursor: grab;
    touch-action: none;
}
.dragging {
    z-index: 1000;
}
.placeholder {
    opacity: 0;
}
.clone {
    position: absolute;
    left: 0;
    top: 0;
    will-change: transform;
}

Options

OptionDescriptionDefault
horizontalWhether the list is horizontal.false
selectorCSS selector for the elements to reorder..draggable
handleSelectorCSS selector for the reorder handle.selector
focusSelectorCSS selector for the element to focus.handleSelector
durationDuration in milliseconds of the move animation, 0 disables it.200
onStartCalled when reordering starts.
onMoveCalled when the user asks to move the element with the keyboard (optional).
onMoveEndCalled after the element has been moved in the DOM (optional).
onFinishCalled when the element is dropped.
onCancelCalled when reordering is cancelled.

If the user prefers reduced motion (prefers-reduced-motion: reduce) the animation is disabled, the same as setting duration to 0.

Lifecycle

Keyboard reordering: Space on the focused element grabs it, the arrow keys move it, Space drops it and Escape cancels.

  1. onStart(element, index) is called immediately when the element is grabbed.
  2. Each arrow key calls onMove(element, index) immediately, with the index the element will have. The siblings then slide with a CSS transition and the DOM is not changed until the animation ends.
  3. When the animation ends (duration plus 5 ms) the element is moved in the DOM and onMoveEnd(element, index) is called with the actual index. This is the right place for announcements.
  4. Space calls onFinish(element); Escape calls onCancel(element) after the element is moved back to its original position. If an animation is running, keys pressed meanwhile (including drop and cancel) are queued and processed in order as each animation ends, so onFinish and onCancel always see the final DOM.

Pointer reordering calls onMoveEnd each time the placeholder changes position, and onFinish once the dragged element has settled in its final position. onStart and onMove are not called for the pointer.

State attribute

The container has the attribute data-reorder-state with one of these values, which can be used for styling or for tests to know when it is safe to continue:

  • idle: no element is being reordered.
  • grabbed: an element is grabbed or dragged and no animation is running.
  • moving: a move animation is running.

The attribute is removed when the function returned by reorder is called.

Output

Output

版本与发布记录

@bluecateng/smooth-reorder 共发布 10 个版本,首次发布于 2020/05/20;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.2.1latest2026/10/309 天前
2.2.02026/10/0930 天前
2.1.42026/04/216 个月前
2.1.32025/05/141.5 年前
2.1.22025/04/051.6 年前
2.1.12024/06/062 年前

关于 @bluecateng/smooth-reorder 的常见问题

@bluecateng/smooth-reorder 是什么?

Smooth list reordering in vanilla JS

如何安装 @bluecateng/smooth-reorder?

在项目目录运行 npm install @bluecateng/smooth-reorder,或使用 pnpm add / yarn add / bun add。

@bluecateng/smooth-reorder 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bluecateng/smooth-reorder 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bluecateng/smooth-reorder 可以商用吗?

@bluecateng/smooth-reorder 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bluecateng/smooth-reorder 的下载量有多少?

根据 npm 官方下载统计,@bluecateng/smooth-reorder 在 2026/10/09 – 2026/11/07 这 30 天内被下载 3,408 次,日均约 114 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bluecateng/smooth-reorder 还在维护吗?

最新版本 2.2.1 发布于 2026/10/30(9 天前);至今共发布 10 个版本,其中过去 12 个月发布了 3 个;首次发布于 2020/05/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。