@synerise/ds-sortable 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.md +21 -0
- package/README.md +30 -0
- package/dist/Sortable.d.ts +4 -0
- package/dist/Sortable.js +93 -0
- package/dist/Sortable.styles.d.ts +6 -0
- package/dist/Sortable.styles.js +17 -0
- package/dist/Sortable.types.d.ts +27 -0
- package/dist/Sortable.types.js +1 -0
- package/dist/SortableItem.d.ts +3 -0
- package/dist/SortableItem.js +42 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/modules.d.js +1 -0
- package/package.json +50 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Synerise
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: sortable
|
|
3
|
+
title: Sortable
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
Sortable UI Component
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
```
|
|
10
|
+
npm i @synerise/ds-sortable
|
|
11
|
+
or
|
|
12
|
+
yarn add @synerise/ds-sortable
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
```
|
|
17
|
+
import Sortable from '@synerise/ds-sortable'
|
|
18
|
+
|
|
19
|
+
<Sortable />
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Demo
|
|
24
|
+
|
|
25
|
+
<iframe src="/storybook-static/iframe.html?id=components-sortable--default"></iframe>
|
|
26
|
+
|
|
27
|
+
## API
|
|
28
|
+
|
|
29
|
+
| Property | Description | Type | Default |
|
|
30
|
+
| --- | --- | --- | --- |
|
package/dist/Sortable.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
2
|
+
import React, { useMemo, useState, useEffect } from 'react';
|
|
3
|
+
import { v4 as uuid } from 'uuid';
|
|
4
|
+
import { DndContext, closestCenter, KeyboardSensor, PointerSensor, MouseSensor, useSensor, useSensors, DragOverlay } from '@dnd-kit/core';
|
|
5
|
+
import { arrayMove, SortableContext, sortableKeyboardCoordinates, verticalListSortingStrategy, horizontalListSortingStrategy, rectSortingStrategy } from '@dnd-kit/sortable';
|
|
6
|
+
import { restrictToVerticalAxis, restrictToHorizontalAxis, restrictToParentElement } from '@dnd-kit/modifiers';
|
|
7
|
+
import { SortableItem } from './SortableItem';
|
|
8
|
+
export var Sortable = function Sortable(_ref) {
|
|
9
|
+
var items = _ref.items,
|
|
10
|
+
onOrderChange = _ref.onOrderChange,
|
|
11
|
+
ItemComponent = _ref.ItemComponent,
|
|
12
|
+
axis = _ref.axis;
|
|
13
|
+
var _useState = useState(items),
|
|
14
|
+
order = _useState[0],
|
|
15
|
+
setOrder = _useState[1];
|
|
16
|
+
var _useState2 = useState(),
|
|
17
|
+
activeItem = _useState2[0],
|
|
18
|
+
setActiveItem = _useState2[1];
|
|
19
|
+
var sensors = useSensors(useSensor(MouseSensor), useSensor(PointerSensor), useSensor(KeyboardSensor, {
|
|
20
|
+
coordinateGetter: sortableKeyboardCoordinates
|
|
21
|
+
}));
|
|
22
|
+
var sortableId = useMemo(function () {
|
|
23
|
+
return uuid();
|
|
24
|
+
}, []);
|
|
25
|
+
var handleDragEnd = function handleDragEnd(event) {
|
|
26
|
+
var active = event.active,
|
|
27
|
+
over = event.over;
|
|
28
|
+
if (active.id !== (over == null ? void 0 : over.id)) {
|
|
29
|
+
setOrder(function (oldOrder) {
|
|
30
|
+
var oldIndex = oldOrder.findIndex(function (item) {
|
|
31
|
+
return item.id === active.id;
|
|
32
|
+
});
|
|
33
|
+
var newIndex = oldOrder.findIndex(function (item) {
|
|
34
|
+
return item.id === (over == null ? void 0 : over.id);
|
|
35
|
+
});
|
|
36
|
+
var updatedOrder = arrayMove(oldOrder, oldIndex, newIndex);
|
|
37
|
+
onOrderChange && onOrderChange(updatedOrder);
|
|
38
|
+
return updatedOrder;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
setActiveItem(undefined);
|
|
42
|
+
};
|
|
43
|
+
var handleDragStart = function handleDragStart(event) {
|
|
44
|
+
setActiveItem(items.find(function (item) {
|
|
45
|
+
return item.id === event.active.id;
|
|
46
|
+
}));
|
|
47
|
+
};
|
|
48
|
+
useEffect(function () {
|
|
49
|
+
setOrder(items);
|
|
50
|
+
}, [items]);
|
|
51
|
+
var contextModifiers = useMemo(function () {
|
|
52
|
+
if (axis === 'x') {
|
|
53
|
+
return [restrictToHorizontalAxis];
|
|
54
|
+
}
|
|
55
|
+
if (axis === 'y') {
|
|
56
|
+
return [restrictToVerticalAxis];
|
|
57
|
+
}
|
|
58
|
+
return [];
|
|
59
|
+
}, [axis]);
|
|
60
|
+
var sortingStrategy = useMemo(function () {
|
|
61
|
+
if (axis === 'x') {
|
|
62
|
+
return horizontalListSortingStrategy;
|
|
63
|
+
}
|
|
64
|
+
if (axis === 'y') {
|
|
65
|
+
return verticalListSortingStrategy;
|
|
66
|
+
}
|
|
67
|
+
return rectSortingStrategy;
|
|
68
|
+
}, [axis]);
|
|
69
|
+
return /*#__PURE__*/React.createElement(DndContext, {
|
|
70
|
+
sensors: sensors,
|
|
71
|
+
collisionDetection: closestCenter,
|
|
72
|
+
onDragEnd: handleDragEnd,
|
|
73
|
+
onDragStart: handleDragStart,
|
|
74
|
+
modifiers: contextModifiers
|
|
75
|
+
}, /*#__PURE__*/React.createElement(SortableContext, {
|
|
76
|
+
items: order,
|
|
77
|
+
strategy: sortingStrategy
|
|
78
|
+
}, order.map(function (item, index) {
|
|
79
|
+
return /*#__PURE__*/React.createElement(SortableItem, {
|
|
80
|
+
id: item.id,
|
|
81
|
+
index: index,
|
|
82
|
+
isDragged: (activeItem == null ? void 0 : activeItem.id) === item.id,
|
|
83
|
+
key: "sortable-item-" + sortableId + "-" + item.id,
|
|
84
|
+
data: item,
|
|
85
|
+
component: ItemComponent
|
|
86
|
+
});
|
|
87
|
+
}), /*#__PURE__*/React.createElement(DragOverlay, {
|
|
88
|
+
modifiers: [restrictToParentElement]
|
|
89
|
+
}, activeItem && /*#__PURE__*/React.createElement(ItemComponent, _extends({}, activeItem, {
|
|
90
|
+
index: -1
|
|
91
|
+
})))));
|
|
92
|
+
};
|
|
93
|
+
export default Sortable;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const SortableItemContent: import("styled-components").StyledComponent<"div", any, {}, never>;
|
|
2
|
+
export declare const SortableItemWrapper: import("styled-components").StyledComponent<"div", any, {
|
|
3
|
+
isGrabbed: boolean;
|
|
4
|
+
isDragged: boolean;
|
|
5
|
+
}, never>;
|
|
6
|
+
export declare const SortableItemHandle: import("styled-components").StyledComponent<"div", any, {}, never>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import styled, { css } from 'styled-components';
|
|
2
|
+
export var SortableItemContent = styled.div.withConfig({
|
|
3
|
+
displayName: "Sortablestyles__SortableItemContent",
|
|
4
|
+
componentId: "sc-1ggkbms-0"
|
|
5
|
+
})([""]);
|
|
6
|
+
export var SortableItemWrapper = styled.div.withConfig({
|
|
7
|
+
displayName: "Sortablestyles__SortableItemWrapper",
|
|
8
|
+
componentId: "sc-1ggkbms-1"
|
|
9
|
+
})(["", " ", ""], function (props) {
|
|
10
|
+
return props.isDragged && css(["", "{visibility:hidden;opacity:0;poiner-events:none;}position:relative;&:before{content:'';position:absolute;width:100%;height:100%;border:1px dashed ", ";background-color:", ";border-radius:3px;}"], SortableItemContent, props.theme.palette['blue-300'], props.theme.palette['blue-050']);
|
|
11
|
+
}, function (props) {
|
|
12
|
+
return props.isGrabbed && css(["", "{background:", ";box-shadow:0px 16px 32px 0px rgba(35,41,54,0.1);}"], SortableItemContent, props.theme.palette.white);
|
|
13
|
+
});
|
|
14
|
+
export var SortableItemHandle = styled.div.withConfig({
|
|
15
|
+
displayName: "Sortablestyles__SortableItemHandle",
|
|
16
|
+
componentId: "sc-1ggkbms-2"
|
|
17
|
+
})(["cursor:grab;"]);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ReactNode, ReactElement, ComponentType } from 'react';
|
|
2
|
+
import type { DraggableAttributes, DraggableSyntheticListeners } from '@dnd-kit/core';
|
|
3
|
+
import type { WithHTMLAttributes } from '@synerise/ds-utils';
|
|
4
|
+
export type DragHandlePropType = DraggableAttributes & DraggableSyntheticListeners;
|
|
5
|
+
export type BaseItem = {
|
|
6
|
+
id: string | number;
|
|
7
|
+
dragHandleProps?: DragHandlePropType;
|
|
8
|
+
};
|
|
9
|
+
type WithIndex<T> = T & {
|
|
10
|
+
index: number;
|
|
11
|
+
};
|
|
12
|
+
type ItemComponent<ItemType extends WithIndex<BaseItem>> = ComponentType<ItemType> | ((props: ItemType) => ReactElement);
|
|
13
|
+
export type SortableItemProps<ItemType extends BaseItem> = WithHTMLAttributes<HTMLDivElement, {
|
|
14
|
+
children?: ReactNode;
|
|
15
|
+
id: string | number;
|
|
16
|
+
index: number;
|
|
17
|
+
component: ItemComponent<WithIndex<ItemType>>;
|
|
18
|
+
data: ItemType;
|
|
19
|
+
isDragged?: boolean;
|
|
20
|
+
}>;
|
|
21
|
+
export type SortableProps<ItemType extends BaseItem> = {
|
|
22
|
+
items: ItemType[];
|
|
23
|
+
axis?: 'x' | 'y';
|
|
24
|
+
ItemComponent: ItemComponent<WithIndex<ItemType>>;
|
|
25
|
+
onOrderChange?: (newOrder: ItemType[]) => void;
|
|
26
|
+
};
|
|
27
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { SortableItemProps, BaseItem } from './Sortable.types';
|
|
3
|
+
export declare const SortableItem: <ItemType extends BaseItem>({ id, data, index, isDragged, component: Component, style, ...htmlAttributes }: SortableItemProps<ItemType>) => React.JSX.Element;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
var _excluded = ["id", "data", "index", "isDragged", "component", "style"];
|
|
2
|
+
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
3
|
+
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
4
|
+
import React from 'react';
|
|
5
|
+
import { useSortable } from '@dnd-kit/sortable';
|
|
6
|
+
import { CSS } from '@dnd-kit/utilities';
|
|
7
|
+
import * as S from './Sortable.styles';
|
|
8
|
+
export var SortableItem = function SortableItem(_ref) {
|
|
9
|
+
var id = _ref.id,
|
|
10
|
+
data = _ref.data,
|
|
11
|
+
index = _ref.index,
|
|
12
|
+
_ref$isDragged = _ref.isDragged,
|
|
13
|
+
isDragged = _ref$isDragged === void 0 ? false : _ref$isDragged,
|
|
14
|
+
Component = _ref.component,
|
|
15
|
+
style = _ref.style,
|
|
16
|
+
htmlAttributes = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
17
|
+
var _useSortable = useSortable({
|
|
18
|
+
id: id
|
|
19
|
+
}),
|
|
20
|
+
attributes = _useSortable.attributes,
|
|
21
|
+
listeners = _useSortable.listeners,
|
|
22
|
+
setNodeRef = _useSortable.setNodeRef,
|
|
23
|
+
transform = _useSortable.transform,
|
|
24
|
+
transition = _useSortable.transition;
|
|
25
|
+
var itemStyle = _extends({}, style, {
|
|
26
|
+
transform: CSS.Translate.toString(transform),
|
|
27
|
+
transition: transition,
|
|
28
|
+
opacity: 1
|
|
29
|
+
});
|
|
30
|
+
var handleProps = _extends({}, attributes, listeners);
|
|
31
|
+
return /*#__PURE__*/React.createElement(S.SortableItemWrapper, _extends({
|
|
32
|
+
style: itemStyle,
|
|
33
|
+
"data-testid": "ds-sortable-item"
|
|
34
|
+
}, htmlAttributes, {
|
|
35
|
+
isGrabbed: index === -1,
|
|
36
|
+
isDragged: isDragged,
|
|
37
|
+
ref: setNodeRef
|
|
38
|
+
}), /*#__PURE__*/React.createElement(S.SortableItemContent, null, /*#__PURE__*/React.createElement(Component, _extends({}, data, {
|
|
39
|
+
index: index,
|
|
40
|
+
dragHandleProps: handleProps
|
|
41
|
+
}))));
|
|
42
|
+
};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from './Sortable';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@testing-library/jest-dom/extend-expect';
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@synerise/ds-sortable",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Sortable UI Component for the Synerise Design System",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"repository": "Synerise/synerise-design",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"files": [
|
|
9
|
+
"/dist",
|
|
10
|
+
"CHANGELOG.md",
|
|
11
|
+
"README.md",
|
|
12
|
+
"package.json",
|
|
13
|
+
"LICENSE.md"
|
|
14
|
+
],
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "npm run build:js && npm run build:css && npm run defs",
|
|
20
|
+
"build:css": "node ../../../scripts/style/less.js",
|
|
21
|
+
"build:js": "babel --delete-dir-on-start --root-mode upward src --out-dir dist --extensions '.js,.ts,.tsx'",
|
|
22
|
+
"build:watch": "npm run build:js -- --watch",
|
|
23
|
+
"defs": "tsc --declaration --outDir dist/ --emitDeclarationOnly",
|
|
24
|
+
"pack:ci": "npm pack --pack-destination ../../storybook/storybook-static/static",
|
|
25
|
+
"prepublish": "npm run build",
|
|
26
|
+
"test": "jest",
|
|
27
|
+
"test:watch": "npm run test -- --watchAll",
|
|
28
|
+
"types": "tsc --noEmit",
|
|
29
|
+
"upgrade:ds": "ncu -f \"@synerise/ds-*\" -u"
|
|
30
|
+
},
|
|
31
|
+
"sideEffects": [
|
|
32
|
+
"dist/style/*",
|
|
33
|
+
"*.less"
|
|
34
|
+
],
|
|
35
|
+
"types": "dist/index.d.ts",
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@dnd-kit/core": "^6.1.0",
|
|
38
|
+
"@dnd-kit/modifiers": "^7.0.0",
|
|
39
|
+
"@dnd-kit/sortable": "^8.0.0",
|
|
40
|
+
"@dnd-kit/utilities": "^3.2.2",
|
|
41
|
+
"@synerise/ds-utils": "^1.0.1",
|
|
42
|
+
"uuid": "^8.3.2"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@synerise/ds-core": "*",
|
|
46
|
+
"react": ">=16.9.0 <= 18.3.1",
|
|
47
|
+
"styled-components": "^5.3.3"
|
|
48
|
+
},
|
|
49
|
+
"gitHead": "7b4d547241901238853719350ca648924735bfc7"
|
|
50
|
+
}
|