Skip to content

Commit 959da32

Browse files
committed
Add test & update docs for multiple classes feature
1 parent 689c45a commit 959da32

File tree

2 files changed

+73
-11
lines changed

2 files changed

+73
-11
lines changed

src/Draggable/README.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ Removes containers from this draggable instance.
7575
**`draggable.getClassNameFor(name: String): String`**
7676
Returns class name for class identifier, check the classes table below for identifiers.
7777

78+
**`draggable.getClassNamesFor(name: String): String[]`**
79+
Returns array of class name for class identifier, useful when working with atomic css, check the classes table below for identifiers.
80+
7881
**`draggable.isDragging(): Boolean`**
7982
Returns true or false, depending on this draggables dragging state.
8083

@@ -117,9 +120,11 @@ plugins controls the mirror movement. Default: `[]`
117120
Sensors dictate how drag operations get triggered, by listening to native browser events.
118121
By default draggable includes the `MouseSensor` & `TouchSensor`. Default: `[]`
119122

120-
**`classes {Object}`**
123+
**`classes {{String: String|String[]}}`**
121124
Draggable adds classes to elements to indicate state. These classes can be used to add styling
122-
on elements in certain states.
125+
on elements in certain states. Accept String or Array of strings.
126+
127+
**NOTE**: When specifying multiple classes to an indicate state, the first class MUST be unique for that state to avoid duplicate classes for other states.
123128

124129
**`exclude {plugins: Plugin[], sensors: Sensor[]}`**
125130
Allow excluding default plugins and default sensors. Use with caution as it may create strange behavior.
@@ -156,15 +161,15 @@ Allow excluding default plugins and default sensors. Use with caution as it may
156161

157162
| Name | Description | Default |
158163
| -------------------- | ------------------------------------------------------------------- | ---------------------------------- |
159-
| `body:dragging` | Class added on the document body while dragging | `draggable--is-dragging` |
160-
| `container:dragging` | Class added on the container where the draggable was picked up from | `draggable-container--is-dragging` |
161-
| `source:dragging` | Class added on the draggable element that has been picked up | `draggable-source--is-dragging` |
162-
| `source:placed` | Class added on the draggable element on `drag:stop` | `draggable-source--placed` |
163-
| `container:placed` | Class added on the draggable container element on `drag:stop` | `draggable-container--placed` |
164-
| `draggable:over` | Class added on draggable element you are dragging over | `draggable--over` |
165-
| `container:over` | Class added on draggable container element you are dragging over | `draggable-container--over` |
166-
| `source:original` | Class added on the original source element, which is hidden on drag | `draggable--original` |
167-
| `mirror` | Class added on the mirror element | `draggable-mirror` |
164+
| `body:dragging` | Classes added on the document body while dragging | `draggable--is-dragging` |
165+
| `container:dragging` | Classes added on the container where the draggable was picked up from | `draggable-container--is-dragging` |
166+
| `source:dragging` | Classes added on the draggable element that has been picked up | `draggable-source--is-dragging` |
167+
| `source:placed` | Classes added on the draggable element on `drag:stop` | `draggable-source--placed` |
168+
| `container:placed` | Classes added on the draggable container element on `drag:stop` | `draggable-container--placed` |
169+
| `draggable:over` | Classes added on draggable element you are dragging over | `draggable--over` |
170+
| `container:over` | Classes added on draggable container element you are dragging over | `draggable-container--over` |
171+
| `source:original` | Classes added on the original source element, which is hidden on drag | `draggable--original` |
172+
| `mirror` | Classes added on the mirror element | `draggable-mirror` |
168173

169174
### Example
170175

@@ -195,3 +200,16 @@ const draggable = new Draggable(document.querySelectorAll('ul'), {
195200
}
196201
});
197202
```
203+
204+
Create draggable with specific classes:
205+
206+
```js
207+
import { Draggable } from '@shopify/draggable';
208+
209+
const draggable = new Draggable(document.querySelectorAll('ul'), {
210+
draggable: 'li',
211+
classes: {
212+
'draggable:over': ['draggable--over', '.bg-red-200', 'bg-opacity-25'],
213+
},
214+
});
215+
```

src/Draggable/tests/Draggable.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,50 @@ describe('Draggable', () => {
869869
expect(draggableElement.classList.contains(newInstance.getClassNameFor('source:original'))).toBeFalsy();
870870
});
871871

872+
it('should have multiple classes for `source:original` on start', () => {
873+
const sourceOriginalClasses = ['draggable--original', 'class1', 'class2'];
874+
const newInstance = new Draggable(containers, {
875+
draggable: 'li',
876+
classes: {
877+
'source:original': sourceOriginalClasses,
878+
},
879+
});
880+
const draggableElement = sandbox.querySelector('li');
881+
document.elementFromPoint = () => draggableElement;
882+
883+
triggerEvent(draggableElement, 'mousedown', {button: 0});
884+
885+
// Wait for delay
886+
waitForDragDelay();
887+
888+
expect(newInstance.getClassNamesFor('source:original')).toEqual(sourceOriginalClasses);
889+
890+
triggerEvent(document.body, 'mouseup', {button: 0});
891+
});
892+
893+
it('should removes all draggable classes for `source:original` on stop', () => {
894+
const sourceOriginalClasses = ['draggable--original', 'class1', 'class2'];
895+
const newInstance = new Draggable(containers, {
896+
draggable: 'li',
897+
classes: {
898+
'source:original': sourceOriginalClasses,
899+
},
900+
});
901+
const draggableElement = sandbox.querySelector('li');
902+
document.elementFromPoint = () => draggableElement;
903+
904+
triggerEvent(draggableElement, 'mousedown', {button: 0});
905+
906+
// Wait for delay
907+
waitForDragDelay();
908+
909+
triggerEvent(document.body, 'mouseup', {button: 0});
910+
911+
newInstance.getClassNamesFor('source:original').forEach((className) => {
912+
expect(draggableElement.classList).not.toContain(className);
913+
});
914+
});
915+
872916
it('`drag:out:container` event specifies leaving container', () => {
873917
const newInstance = new Draggable(containers, {
874918
draggable: 'li',

0 commit comments

Comments
 (0)