Skip to content

Commit

Permalink
Local modifiers for draggable
Browse files Browse the repository at this point in the history
  • Loading branch information
clauderic committed Aug 21, 2023
1 parent 136129b commit f14a5f6
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 16 deletions.
8 changes: 5 additions & 3 deletions apps/docs/stories/react/Draggable/DraggableExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,25 @@ export function DraggableExample({container, modifiers}: Props) {
const Wrapper = container ? Container : 'div';

return (
<DragDropProvider modifiers={modifiers}>
<DragDropProvider>
<Wrapper>
<Draggable id="draggable" />
<Draggable id="draggable" modifiers={modifiers} />
</Wrapper>
</DragDropProvider>
);
}

interface DraggableProps {
id: UniqueIdentifier;
modifiers?: Modifiers;
}

function Draggable({id}: DraggableProps) {
function Draggable({id, modifiers}: DraggableProps) {
const [element, setElement] = useState<Element | null>(null);

const {isDragSource} = useDraggable({
id,
modifiers,
element,
});

Expand Down
2 changes: 1 addition & 1 deletion packages/abstract/src/core/manager/dragOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export function DragOperationManager<

const transform = computed(() => {
const {x, y} = position.delta;
const {modifiers} = manager;
const modifiers = source?.value?.modifiers ?? manager.modifiers;

let transform = {x, y};
const initialShape = shape.initial.peek();
Expand Down
2 changes: 1 addition & 1 deletion packages/abstract/src/core/modifiers/modifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class Modifier<
U extends ModifierOptions = ModifierOptions,
> extends Plugin<T, U> {
constructor(
protected manager: T,
public manager: T,
public options?: U
) {
super(manager, options);
Expand Down
21 changes: 17 additions & 4 deletions packages/abstract/src/core/nodes/draggable/draggable.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import {derived, effect, reactive} from '@dnd-kit/state';
import {derived, reactive} from '@dnd-kit/state';

import type {DragDropManager} from '../../manager/index.js';
import {Node} from '../node/index.js';
import type {NodeInput, Data, Type} from '../node/index.js';
import {Modifier} from '../../modifiers/index.js';
import type {Modifiers} from '../../modifiers/index.js';
import type {DragDropManager} from '../../manager/index.js';
import type {NodeInput, Data, Type} from '../node/index.js';
import {descriptor} from '../../plugins/index.js';

export interface Input<
T extends Data = Data,
U extends Draggable<T> = Draggable<T>,
> extends NodeInput<T, U> {
type?: Type;
modifiers?: Modifiers;
}

export class Draggable<T extends Data = Data> extends Node<T> {
constructor(
input: Input<T>,
{modifiers, ...input}: Input<T>,
public manager: DragDropManager
) {
super(input, manager);

if (modifiers?.length) {
this.modifiers = modifiers.map((modifier) => {
const {plugin, options} = descriptor(modifier);

return new plugin(manager, options);
});
}
}

public modifiers: Modifier[] | undefined;

@reactive
public type: Type | undefined;

Expand Down
2 changes: 1 addition & 1 deletion packages/abstract/src/core/nodes/node/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface Input<T extends Data = Data, U extends Node<T> = Node<T>> {
export class Node<T extends Data = Data> {
constructor(
input: Input<T>,
protected manager: DragDropManager
public manager: DragDropManager
) {
const {effects: inputEffects, id, data = null, disabled = false} = input;

Expand Down
2 changes: 1 addition & 1 deletion packages/abstract/src/core/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class Plugin<
U extends PluginOptions = PluginOptions,
> {
constructor(
protected manager: T,
public manager: T,
public options?: U
) {}

Expand Down
2 changes: 1 addition & 1 deletion packages/abstract/src/core/sensors/sensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export abstract class Sensor<
U extends SensorOptions = SensorOptions,
> extends Plugin<T, U> {
constructor(
protected manager: T,
public manager: T,
public options?: U
) {
super(manager, options);
Expand Down
2 changes: 1 addition & 1 deletion packages/dom/src/core/sensors/drag/DragSensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class DragSensor extends Sensor<DragDropManager, DragSensorOptions> {
private unbind: CleanupFunction | undefined;

constructor(
protected manager: DragDropManager,
public manager: DragDropManager,
public options: DragSensorOptions
) {
super(manager);
Expand Down
2 changes: 1 addition & 1 deletion packages/dom/src/core/sensors/keyboard/KeyboardSensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class KeyboardSensor extends Sensor<
KeyboardSensorOptions
> {
constructor(
protected manager: DragDropManager,
public manager: DragDropManager,
public options?: KeyboardSensorOptions
) {
super(manager);
Expand Down
2 changes: 1 addition & 1 deletion packages/dom/src/core/sensors/pointer/PointerSensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class PointerSensor extends Sensor<
#document: Document | undefined;

constructor(
protected manager: DragDropManager,
public manager: DragDropManager,
public options?: PointerSensorOptions
) {
super(manager);
Expand Down
2 changes: 1 addition & 1 deletion packages/dom/src/sortable/sortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class Sortable<T extends Data = Data> {
transition = defaultSortableTransition,
...input
}: SortableInput<T>,
protected manager: AbstractDragDropManager<any, any>
public manager: AbstractDragDropManager<any, any>
) {
this.draggable = new SortableDraggable<T>({...input, sensors}, manager);
this.droppable = new SortableDroppable<T>(input, manager);
Expand Down

0 comments on commit f14a5f6

Please sign in to comment.