Skip to content

Commit

Permalink
feat: add dragTargetSelf prop #972
Browse files Browse the repository at this point in the history
  • Loading branch information
daybrush committed Jul 8, 2023
1 parent c9db51f commit 4537dfe
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/react-moveable/src/MoveableManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { setStoreCache } from "./store/Store";
export default class MoveableManager<T = {}>
extends React.PureComponent<MoveableManagerProps<T>, MoveableManagerState> {
public static defaultProps: Required<MoveableManagerProps> = {
dragTargetSelf: false,
target: null,
dragTarget: null,
container: null,
Expand Down
1 change: 1 addition & 0 deletions packages/react-moveable/src/ables/Default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export default {
name: "",
props: [
"target",
"dragTargetSelf",
"dragTarget",
"dragContainer",
"container",
Expand Down
11 changes: 9 additions & 2 deletions packages/react-moveable/src/gesto/getAbleGesto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,21 @@ export function getTargetAbleGesto(
) {
const controlBox = moveable.controlBox;
const targets: Array<HTMLElement | SVGElement> = [];
const dragTarget = moveable.props.dragTarget;
const props = moveable.props;
const dragArea = props.dragArea;
const target = moveable.state.target;
const dragTarget = props.dragTarget;

targets.push(controlBox);

if (!moveable.props.dragArea || dragTarget) {
if (!dragArea || dragTarget) {
targets.push(moveableTarget);
}

if (!dragArea && dragTarget && target && moveableTarget !== target && props.dragTargetSelf) {
targets.push(target);
}

return getAbleGesto(moveable, targets, "targetAbles", eventAffix, {
dragStart: checkMoveableTarget(moveable),
pinchStart: checkMoveableTarget(moveable),
Expand Down
8 changes: 7 additions & 1 deletion packages/react-moveable/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@ export interface DefaultOptions {
*/
target?: SVGElement | HTMLElement | null;
/**
* The target(s) to drag Moveable target(s)
* The external target(s) to drag Moveable target(s)
* @default target
*/
dragTarget?: MoveableRefType | null;
/**
* If dragTarget is set directly, taget itself cannot be dragged.
* Whether to drag the target as well.
* @default false
*/
dragTargetSelf?: boolean;
/**
* Container area where drag works
* @default window
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,10 @@ export const OptionsDragTarget = add("other dragTarget", {
app: require("./ReactDragTargetApp").default,
path: require.resolve("./ReactDragTargetApp"),
});



export const OptionsDragTargetSelf = add("dragTargetSelf with other dragTarget", {
app: require("./ReactDragTargetSelfApp").default,
path: require.resolve("./ReactDragTargetSelfApp"),
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ export default function App() {
scalable={true}
rotatable={true}
dragTarget={".drag-area"}
padding={{
left: 10,
right: 20,
top: 30,
bottom: 40,
}}
onRender={e => {
e.target.style.cssText += e.cssText;
}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as React from "react";
import Moveable from "@/react-moveable";

export default function App() {
return <div className="container">
<div className="drag-area" style={{
width: "100px",
height: "100px",
}}>Drag</div>
<div className="target"></div>
<Moveable
target={".target"}
draggable={true}
scalable={true}
rotatable={true}
dragTarget={".drag-area"}
dragTargetSelf={true}
onRender={e => {
e.target.style.cssText += e.cssText;
}}
/>
</div>;
}
6 changes: 0 additions & 6 deletions storybook/stories/4-Options/react/ReactDragTargetApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ export default function App() {
scalable={true}
rotatable={true}
dragTarget={".drag-area"}
padding={{
left: 10,
right: 20,
top: 30,
bottom: 40,
}}
onRender={e => {
e.target.style.cssText += e.cssText;
}}
Expand Down

0 comments on commit 4537dfe

Please sign in to comment.