@molecule/app-reorderable-list-react 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +115 -0
- package/README.md +147 -0
- package/dist/ReorderableList.d.ts +32 -0
- package/dist/ReorderableList.d.ts.map +1 -0
- package/dist/ReorderableList.js +115 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +52 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-08-04T00:40:56.591Z
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
# @molecule/app-reorderable-list-react
|
|
10
|
+
|
|
11
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
12
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
13
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
14
|
+
|
|
15
|
+
Drag-handle reorderable list (HTML5 drag-and-drop).
|
|
16
|
+
|
|
17
|
+
Exports `<ReorderableList>` and the `ReorderableItem<T>` type
|
|
18
|
+
(`{ id: string; data: T }`). Fully controlled: it renders rows and calls
|
|
19
|
+
`onReorder(next)` with the complete reordered array — the app owns state.
|
|
20
|
+
Props: `items`, `onReorder`, `renderItem(item, isDragging)`,
|
|
21
|
+
`renderHandle?` (when set, only the handle is draggable — e.g. a "≡"
|
|
22
|
+
glyph; otherwise the whole row drags), `className?`.
|
|
23
|
+
|
|
24
|
+
Reordering works with a mouse (HTML5 drag) AND the keyboard: every row has
|
|
25
|
+
move-up/move-down buttons (`data-mol-id="reorderable-move-{up,down}-<id>"`),
|
|
26
|
+
and a focused row responds to Alt+ArrowUp / Alt+ArrowDown.
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
import { useState } from 'react'
|
|
32
|
+
|
|
33
|
+
import { ReorderableList } from '@molecule/app-reorderable-list-react'
|
|
34
|
+
import type { ReorderableItem } from '@molecule/app-reorderable-list-react'
|
|
35
|
+
|
|
36
|
+
function Steps() {
|
|
37
|
+
const [items, setItems] = useState<ReorderableItem<{ label: string }>[]>([
|
|
38
|
+
{ id: '1', data: { label: 'First' } },
|
|
39
|
+
{ id: '2', data: { label: 'Second' } },
|
|
40
|
+
{ id: '3', data: { label: 'Third' } },
|
|
41
|
+
])
|
|
42
|
+
return (
|
|
43
|
+
<ReorderableList
|
|
44
|
+
items={items}
|
|
45
|
+
onReorder={setItems}
|
|
46
|
+
renderItem={(item) => <span>{item.data.label}</span>}
|
|
47
|
+
/>
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Type
|
|
53
|
+
|
|
54
|
+
`feature`
|
|
55
|
+
|
|
56
|
+
## Installation
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
npm install @molecule/app-reorderable-list-react @molecule/app-react @molecule/app-ui @molecule/app-ui-react react
|
|
60
|
+
npm install -D @types/react
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## API
|
|
64
|
+
|
|
65
|
+
### Interfaces
|
|
66
|
+
|
|
67
|
+
#### `ReorderableItem`
|
|
68
|
+
|
|
69
|
+
A single item in a ReorderableList, pairing a stable string id with arbitrary data.
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
interface ReorderableItem<T> {
|
|
73
|
+
id: string
|
|
74
|
+
data: T
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
#### `ReorderableListProps`
|
|
79
|
+
|
|
80
|
+
Props accepted by the {@link ReorderableList} component.
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
interface ReorderableListProps<T> {
|
|
84
|
+
items: ReorderableItem<T>[]
|
|
85
|
+
/** Called with the new order (full list). */
|
|
86
|
+
onReorder: (next: ReorderableItem<T>[]) => void
|
|
87
|
+
/** Per-item renderer. Receives item + isDragging flag. */
|
|
88
|
+
renderItem: (item: ReorderableItem<T>, isDragging: boolean) => ReactNode
|
|
89
|
+
/** Optional drag-handle slot — when omitted, the entire row is the drag target. */
|
|
90
|
+
renderHandle?: () => ReactNode
|
|
91
|
+
/** Extra classes. */
|
|
92
|
+
className?: string
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Functions
|
|
97
|
+
|
|
98
|
+
#### `ReorderableList(props)`
|
|
99
|
+
|
|
100
|
+
Drag-handle reorderable list with HTML5 DnD AND keyboard reordering. Apps own
|
|
101
|
+
the data; this component only renders + emits new order on `onReorder`.
|
|
102
|
+
|
|
103
|
+
Use the optional `renderHandle` slot to limit drag to a specific
|
|
104
|
+
element (e.g. a "≡" handle on the left). Every row also ships explicit
|
|
105
|
+
move-up/move-down buttons and supports Alt+ArrowUp / Alt+ArrowDown on the
|
|
106
|
+
focused row, so the list is fully reorderable without a mouse.
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
function ReorderableList({
|
|
110
|
+
items,
|
|
111
|
+
onReorder,
|
|
112
|
+
renderItem,
|
|
113
|
+
renderHandle,
|
|
114
|
+
className,
|
|
115
|
+
}: ReorderableListProps<T>): ReactElement<unknown, string | JSXElementConstructor<any>>
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
- `props` — Component props (see {@link ReorderableListProps}).
|
|
119
|
+
|
|
120
|
+
## Injection Notes
|
|
121
|
+
|
|
122
|
+
### Requirements
|
|
123
|
+
|
|
124
|
+
Peer dependencies:
|
|
125
|
+
|
|
126
|
+
- `@molecule/app-react` ^1.0.0
|
|
127
|
+
- `@molecule/app-ui` ^1.0.0
|
|
128
|
+
- `@molecule/app-ui-react` ^1.0.0
|
|
129
|
+
- `react` ^18.0.0 || ^19.0.0
|
|
130
|
+
|
|
131
|
+
### Runtime Dependencies
|
|
132
|
+
|
|
133
|
+
- `@molecule/app-react`
|
|
134
|
+
- `@molecule/app-ui`
|
|
135
|
+
- `@molecule/app-ui-react`
|
|
136
|
+
- `react`
|
|
137
|
+
|
|
138
|
+
- Native HTML5 drag works with MOUSE/POINTER only — touch devices
|
|
139
|
+
(iOS/Android) fire no HTML5 drag events. The keyboard path (move buttons +
|
|
140
|
+
Alt+Arrow) is the a11y/touch-safe fallback and is always present; add a
|
|
141
|
+
pointer/touch drag library only if you also need touch DRAG specifically.
|
|
142
|
+
- Requires a bonded ClassMap (`setClassMap()` at startup) and an
|
|
143
|
+
`<I18nProvider>` (for the control labels) or rendering throws.
|
|
144
|
+
- Item `id`s must be unique — drop resolution matches by id.
|
|
145
|
+
- Control labels ("Move up/down", "Drag to reorder", row position) render
|
|
146
|
+
through `t(...)` with English defaults; no companion locale bond ships yet,
|
|
147
|
+
so translate them by wiring a `reorderableList.*` bond when needed.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ReactElement, ReactNode } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* A single item in a ReorderableList, pairing a stable string id with arbitrary data.
|
|
4
|
+
*/
|
|
5
|
+
export interface ReorderableItem<T> {
|
|
6
|
+
id: string;
|
|
7
|
+
data: T;
|
|
8
|
+
}
|
|
9
|
+
/** Props accepted by the {@link ReorderableList} component. */
|
|
10
|
+
export interface ReorderableListProps<T> {
|
|
11
|
+
items: ReorderableItem<T>[];
|
|
12
|
+
/** Called with the new order (full list). */
|
|
13
|
+
onReorder: (next: ReorderableItem<T>[]) => void;
|
|
14
|
+
/** Per-item renderer. Receives item + isDragging flag. */
|
|
15
|
+
renderItem: (item: ReorderableItem<T>, isDragging: boolean) => ReactNode;
|
|
16
|
+
/** Optional drag-handle slot — when omitted, the entire row is the drag target. */
|
|
17
|
+
renderHandle?: () => ReactNode;
|
|
18
|
+
/** Extra classes. */
|
|
19
|
+
className?: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Drag-handle reorderable list with HTML5 DnD AND keyboard reordering. Apps own
|
|
23
|
+
* the data; this component only renders + emits new order on `onReorder`.
|
|
24
|
+
*
|
|
25
|
+
* Use the optional `renderHandle` slot to limit drag to a specific
|
|
26
|
+
* element (e.g. a "≡" handle on the left). Every row also ships explicit
|
|
27
|
+
* move-up/move-down buttons and supports Alt+ArrowUp / Alt+ArrowDown on the
|
|
28
|
+
* focused row, so the list is fully reorderable without a mouse.
|
|
29
|
+
* @param props - Component props (see {@link ReorderableListProps}).
|
|
30
|
+
*/
|
|
31
|
+
export declare function ReorderableList<T>({ items, onReorder, renderItem, renderHandle, className, }: ReorderableListProps<T>): ReactElement;
|
|
32
|
+
//# sourceMappingURL=ReorderableList.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ReorderableList.d.ts","sourceRoot":"","sources":["../src/ReorderableList.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA4B,YAAY,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAM9E;;GAEG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC;IAChC,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,CAAC,CAAA;CACR;AAED,+DAA+D;AAC/D,MAAM,WAAW,oBAAoB,CAAC,CAAC;IACrC,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,EAAE,CAAA;IAC3B,6CAA6C;IAC7C,SAAS,EAAE,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC,CAAC,EAAE,KAAK,IAAI,CAAA;IAC/C,0DAA0D;IAC1D,UAAU,EAAE,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,OAAO,KAAK,SAAS,CAAA;IACxE,mFAAmF;IACnF,YAAY,CAAC,EAAE,MAAM,SAAS,CAAA;IAC9B,qBAAqB;IACrB,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,EACjC,KAAK,EACL,SAAS,EACT,UAAU,EACV,YAAY,EACZ,SAAS,GACV,EAAE,oBAAoB,CAAC,CAAC,CAAC,GAAG,YAAY,CAiKxC"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
import { useTranslation } from '@molecule/app-react';
|
|
4
|
+
import { getClassMap } from '@molecule/app-ui';
|
|
5
|
+
/**
|
|
6
|
+
* Drag-handle reorderable list with HTML5 DnD AND keyboard reordering. Apps own
|
|
7
|
+
* the data; this component only renders + emits new order on `onReorder`.
|
|
8
|
+
*
|
|
9
|
+
* Use the optional `renderHandle` slot to limit drag to a specific
|
|
10
|
+
* element (e.g. a "≡" handle on the left). Every row also ships explicit
|
|
11
|
+
* move-up/move-down buttons and supports Alt+ArrowUp / Alt+ArrowDown on the
|
|
12
|
+
* focused row, so the list is fully reorderable without a mouse.
|
|
13
|
+
* @param props - Component props (see {@link ReorderableListProps}).
|
|
14
|
+
*/
|
|
15
|
+
export function ReorderableList({ items, onReorder, renderItem, renderHandle, className, }) {
|
|
16
|
+
const cm = getClassMap();
|
|
17
|
+
const { t } = useTranslation();
|
|
18
|
+
const [draggingId, setDraggingId] = useState(null);
|
|
19
|
+
const [overId, setOverId] = useState(null);
|
|
20
|
+
/**
|
|
21
|
+
* Moves the item at `index` by `delta` positions and emits the new order.
|
|
22
|
+
* No-ops at the list boundaries. Shared by the move buttons and the
|
|
23
|
+
* keyboard (Alt+Arrow) path so mouse and keyboard reordering stay identical.
|
|
24
|
+
* @param index - Current position of the item to move.
|
|
25
|
+
* @param delta - -1 to move up, 1 to move down.
|
|
26
|
+
*/
|
|
27
|
+
function moveItem(index, delta) {
|
|
28
|
+
const to = index + delta;
|
|
29
|
+
if (to < 0 || to >= items.length)
|
|
30
|
+
return;
|
|
31
|
+
const next = [...items];
|
|
32
|
+
const [moved] = next.splice(index, 1);
|
|
33
|
+
next.splice(to, 0, moved);
|
|
34
|
+
onReorder(next);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Keyboard reordering for a focused row: Alt+ArrowUp / Alt+ArrowDown move it.
|
|
38
|
+
* Alt is required so plain arrows still scroll/move the caret and browser
|
|
39
|
+
* history navigation is left untouched.
|
|
40
|
+
* @param index - Position of the focused row.
|
|
41
|
+
* @param e - The keyboard event.
|
|
42
|
+
*/
|
|
43
|
+
function onItemKeyDown(index, e) {
|
|
44
|
+
if (!e.altKey)
|
|
45
|
+
return;
|
|
46
|
+
if (e.key === 'ArrowUp') {
|
|
47
|
+
e.preventDefault();
|
|
48
|
+
moveItem(index, -1);
|
|
49
|
+
}
|
|
50
|
+
else if (e.key === 'ArrowDown') {
|
|
51
|
+
e.preventDefault();
|
|
52
|
+
moveItem(index, 1);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Initiates a drag operation, storing the dragged item id in the transfer payload.
|
|
57
|
+
* @param id
|
|
58
|
+
* @param e
|
|
59
|
+
*/
|
|
60
|
+
function onDragStart(id, e) {
|
|
61
|
+
e.dataTransfer.setData('text/plain', id);
|
|
62
|
+
e.dataTransfer.effectAllowed = 'move';
|
|
63
|
+
setDraggingId(id);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Marks the hovered item as the current drop target and allows the drop.
|
|
67
|
+
* @param id
|
|
68
|
+
* @param e
|
|
69
|
+
*/
|
|
70
|
+
function onDragOver(id, e) {
|
|
71
|
+
e.preventDefault();
|
|
72
|
+
e.dataTransfer.dropEffect = 'move';
|
|
73
|
+
setOverId(id);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Handles a drop event by computing the new item order and calling onReorder.
|
|
77
|
+
* @param targetId
|
|
78
|
+
* @param e
|
|
79
|
+
*/
|
|
80
|
+
function onDrop(targetId, e) {
|
|
81
|
+
e.preventDefault();
|
|
82
|
+
const sourceId = e.dataTransfer.getData('text/plain');
|
|
83
|
+
if (!sourceId || sourceId === targetId) {
|
|
84
|
+
reset();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const from = items.findIndex((i) => i.id === sourceId);
|
|
88
|
+
const to = items.findIndex((i) => i.id === targetId);
|
|
89
|
+
if (from < 0 || to < 0) {
|
|
90
|
+
reset();
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const next = [...items];
|
|
94
|
+
const [moved] = next.splice(from, 1);
|
|
95
|
+
next.splice(to, 0, moved);
|
|
96
|
+
onReorder(next);
|
|
97
|
+
reset();
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Clears dragging and over-target state after a drop or drag-end event.
|
|
101
|
+
*/
|
|
102
|
+
function reset() {
|
|
103
|
+
setDraggingId(null);
|
|
104
|
+
setOverId(null);
|
|
105
|
+
}
|
|
106
|
+
return (_jsx("ul", { className: cm.cn(cm.stack(1), className), role: "list", children: items.map((item, index) => {
|
|
107
|
+
const isDragging = draggingId === item.id;
|
|
108
|
+
const isOver = overId === item.id && draggingId !== item.id;
|
|
109
|
+
const position = t('reorderableList.position', { index: index + 1, total: items.length }, { defaultValue: 'Item {{index}} of {{total}}' });
|
|
110
|
+
return (_jsxs("li", { draggable: !renderHandle, onDragStart: renderHandle ? undefined : (e) => onDragStart(item.id, e), onDragEnd: reset, onDragOver: (e) => onDragOver(item.id, e), onDrop: (e) => onDrop(item.id, e), onKeyDown: (e) => onItemKeyDown(index, e), tabIndex: 0, "aria-label": position, className: cm.cn(cm.flex({ align: 'center', gap: 'sm' })), style: {
|
|
111
|
+
opacity: isDragging ? 0.4 : 1,
|
|
112
|
+
borderTop: isOver ? '2px solid currentColor' : undefined,
|
|
113
|
+
}, children: [renderHandle && (_jsx("span", { draggable: true, onDragStart: (e) => onDragStart(item.id, e), onDragEnd: reset, className: cm.cursorPointer, "aria-label": t('reorderableList.dragHandle', {}, { defaultValue: 'Drag to reorder' }), children: renderHandle() })), _jsx("div", { className: cm.flex1, children: renderItem(item, isDragging) }), _jsxs("div", { className: cm.cn(cm.flex({ align: 'center', gap: 'xs' })), children: [_jsx("button", { type: "button", "data-mol-id": `reorderable-move-up-${item.id}`, onClick: () => moveItem(index, -1), disabled: index === 0, "aria-label": t('reorderableList.moveUp', {}, { defaultValue: 'Move up' }), className: cm.cn(cm.cursorPointer, cm.sp('p', 1)), children: _jsx("span", { "aria-hidden": "true", children: "\u2191" }) }), _jsx("button", { type: "button", "data-mol-id": `reorderable-move-down-${item.id}`, onClick: () => moveItem(index, 1), disabled: index === items.length - 1, "aria-label": t('reorderableList.moveDown', {}, { defaultValue: 'Move down' }), className: cm.cn(cm.cursorPointer, cm.sp('p', 1)), children: _jsx("span", { "aria-hidden": "true", children: "\u2193" }) })] })] }, item.id));
|
|
114
|
+
}) }));
|
|
115
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drag-handle reorderable list (HTML5 drag-and-drop).
|
|
3
|
+
*
|
|
4
|
+
* Exports `<ReorderableList>` and the `ReorderableItem<T>` type
|
|
5
|
+
* (`{ id: string; data: T }`). Fully controlled: it renders rows and calls
|
|
6
|
+
* `onReorder(next)` with the complete reordered array — the app owns state.
|
|
7
|
+
* Props: `items`, `onReorder`, `renderItem(item, isDragging)`,
|
|
8
|
+
* `renderHandle?` (when set, only the handle is draggable — e.g. a "≡"
|
|
9
|
+
* glyph; otherwise the whole row drags), `className?`.
|
|
10
|
+
*
|
|
11
|
+
* Reordering works with a mouse (HTML5 drag) AND the keyboard: every row has
|
|
12
|
+
* move-up/move-down buttons (`data-mol-id="reorderable-move-{up,down}-<id>"`),
|
|
13
|
+
* and a focused row responds to Alt+ArrowUp / Alt+ArrowDown.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* import { useState } from 'react'
|
|
18
|
+
*
|
|
19
|
+
* import { ReorderableList } from '@molecule/app-reorderable-list-react'
|
|
20
|
+
* import type { ReorderableItem } from '@molecule/app-reorderable-list-react'
|
|
21
|
+
*
|
|
22
|
+
* function Steps() {
|
|
23
|
+
* const [items, setItems] = useState<ReorderableItem<{ label: string }>[]>([
|
|
24
|
+
* { id: '1', data: { label: 'First' } },
|
|
25
|
+
* { id: '2', data: { label: 'Second' } },
|
|
26
|
+
* { id: '3', data: { label: 'Third' } },
|
|
27
|
+
* ])
|
|
28
|
+
* return (
|
|
29
|
+
* <ReorderableList
|
|
30
|
+
* items={items}
|
|
31
|
+
* onReorder={setItems}
|
|
32
|
+
* renderItem={(item) => <span>{item.data.label}</span>}
|
|
33
|
+
* />
|
|
34
|
+
* )
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* - Native HTML5 drag works with MOUSE/POINTER only — touch devices
|
|
40
|
+
* (iOS/Android) fire no HTML5 drag events. The keyboard path (move buttons +
|
|
41
|
+
* Alt+Arrow) is the a11y/touch-safe fallback and is always present; add a
|
|
42
|
+
* pointer/touch drag library only if you also need touch DRAG specifically.
|
|
43
|
+
* - Requires a bonded ClassMap (`setClassMap()` at startup) and an
|
|
44
|
+
* `<I18nProvider>` (for the control labels) or rendering throws.
|
|
45
|
+
* - Item `id`s must be unique — drop resolution matches by id.
|
|
46
|
+
* - Control labels ("Move up/down", "Drag to reorder", row position) render
|
|
47
|
+
* through `t(...)` with English defaults; no companion locale bond ships yet,
|
|
48
|
+
* so translate them by wiring a `reorderableList.*` bond when needed.
|
|
49
|
+
*
|
|
50
|
+
* @module
|
|
51
|
+
*/
|
|
52
|
+
export * from './ReorderableList.js';
|
|
53
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AAEH,cAAc,sBAAsB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drag-handle reorderable list (HTML5 drag-and-drop).
|
|
3
|
+
*
|
|
4
|
+
* Exports `<ReorderableList>` and the `ReorderableItem<T>` type
|
|
5
|
+
* (`{ id: string; data: T }`). Fully controlled: it renders rows and calls
|
|
6
|
+
* `onReorder(next)` with the complete reordered array — the app owns state.
|
|
7
|
+
* Props: `items`, `onReorder`, `renderItem(item, isDragging)`,
|
|
8
|
+
* `renderHandle?` (when set, only the handle is draggable — e.g. a "≡"
|
|
9
|
+
* glyph; otherwise the whole row drags), `className?`.
|
|
10
|
+
*
|
|
11
|
+
* Reordering works with a mouse (HTML5 drag) AND the keyboard: every row has
|
|
12
|
+
* move-up/move-down buttons (`data-mol-id="reorderable-move-{up,down}-<id>"`),
|
|
13
|
+
* and a focused row responds to Alt+ArrowUp / Alt+ArrowDown.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* import { useState } from 'react'
|
|
18
|
+
*
|
|
19
|
+
* import { ReorderableList } from '@molecule/app-reorderable-list-react'
|
|
20
|
+
* import type { ReorderableItem } from '@molecule/app-reorderable-list-react'
|
|
21
|
+
*
|
|
22
|
+
* function Steps() {
|
|
23
|
+
* const [items, setItems] = useState<ReorderableItem<{ label: string }>[]>([
|
|
24
|
+
* { id: '1', data: { label: 'First' } },
|
|
25
|
+
* { id: '2', data: { label: 'Second' } },
|
|
26
|
+
* { id: '3', data: { label: 'Third' } },
|
|
27
|
+
* ])
|
|
28
|
+
* return (
|
|
29
|
+
* <ReorderableList
|
|
30
|
+
* items={items}
|
|
31
|
+
* onReorder={setItems}
|
|
32
|
+
* renderItem={(item) => <span>{item.data.label}</span>}
|
|
33
|
+
* />
|
|
34
|
+
* )
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* - Native HTML5 drag works with MOUSE/POINTER only — touch devices
|
|
40
|
+
* (iOS/Android) fire no HTML5 drag events. The keyboard path (move buttons +
|
|
41
|
+
* Alt+Arrow) is the a11y/touch-safe fallback and is always present; add a
|
|
42
|
+
* pointer/touch drag library only if you also need touch DRAG specifically.
|
|
43
|
+
* - Requires a bonded ClassMap (`setClassMap()` at startup) and an
|
|
44
|
+
* `<I18nProvider>` (for the control labels) or rendering throws.
|
|
45
|
+
* - Item `id`s must be unique — drop resolution matches by id.
|
|
46
|
+
* - Control labels ("Move up/down", "Drag to reorder", row position) render
|
|
47
|
+
* through `t(...)` with English defaults; no companion locale bond ships yet,
|
|
48
|
+
* so translate them by wiring a `reorderableList.*` bond when needed.
|
|
49
|
+
*
|
|
50
|
+
* @module
|
|
51
|
+
*/
|
|
52
|
+
export * from './ReorderableList.js';
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/app-reorderable-list-react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Vertical drag-handle reorder list with onReorder callback",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"keywords": [
|
|
24
|
+
"molecule",
|
|
25
|
+
"reorderable-list",
|
|
26
|
+
"react"
|
|
27
|
+
],
|
|
28
|
+
"license": "Apache-2.0",
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@molecule/app-react": "^1.0.0",
|
|
31
|
+
"@molecule/app-ui": "^1.0.0",
|
|
32
|
+
"@molecule/app-ui-react": "^1.0.0",
|
|
33
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@molecule/app-react": "1.0.0",
|
|
37
|
+
"@molecule/app-ui": "1.0.0",
|
|
38
|
+
"@molecule/app-ui-react": "1.0.0",
|
|
39
|
+
"@testing-library/react": "16.3.2",
|
|
40
|
+
"@types/node": "26.1.2",
|
|
41
|
+
"@types/react": "19.2.17",
|
|
42
|
+
"@types/react-dom": "19.2.3",
|
|
43
|
+
"jsdom": "30.0.1",
|
|
44
|
+
"react": "19.2.8",
|
|
45
|
+
"react-dom": "19.2.8",
|
|
46
|
+
"typescript": "6.0.3",
|
|
47
|
+
"vitest": "4.1.10"
|
|
48
|
+
}
|
|
49
|
+
}
|