Skip to content

Commit

Permalink
feat: add snapHorizontalThreshold, snapVerticalThreshold #1044
Browse files Browse the repository at this point in the history
  • Loading branch information
daybrush committed Nov 26, 2023
1 parent 9f4c9f4 commit 6cd5114
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 22 deletions.
2 changes: 2 additions & 0 deletions packages/react-moveable/src/ables/Clippable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ background: var(--bounds-color);
guideXPoses,
guideYPoses,
5,
5,
);
let snapOffsetY = horizontalSnapInfo.offset;
let snapOffsetX = verticalSnapInfo.offset;
Expand Down Expand Up @@ -767,6 +768,7 @@ background: var(--bounds-color);
guideXPoses,
guideYPoses,
1,
1,
);

if (originalDraggable) {
Expand Down
5 changes: 3 additions & 2 deletions packages/react-moveable/src/ables/Snappable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ color: #f55;
poses,
snapRenderInfo.direction,
snapRenderThreshold,
snapRenderThreshold,
)
);
}
Expand All @@ -767,7 +768,7 @@ color: #f55;
(rect as any).middle = (rect.top + rect.bottom) / 2;
(rect as any).center = (rect.left + rect.right) / 2;
}
snapInfos.push(checkSnaps(moveable, rect, snapRenderThreshold));
snapInfos.push(checkSnaps(moveable, rect, snapRenderThreshold, snapRenderThreshold));
}
if (hasExternalPoses) {
if (snapRenderInfo.center) {
Expand All @@ -776,7 +777,7 @@ color: #f55;
(externalRect as any).center =
(externalRect.left + externalRect.right) / 2;
}
snapInfos.push(checkSnaps(moveable, externalRect, snapRenderThreshold));
snapInfos.push(checkSnaps(moveable, externalRect, snapRenderThreshold, snapRenderThreshold));
}
snapInfos.forEach((snapInfo) => {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export function getGapGuidelines(
});
return gapGuidelines;
}

export function startGridGroupGuidelines(
moveable: MoveableManagerInterface<SnappableProps, SnappableState>,
clientLeft: number,
Expand Down
46 changes: 33 additions & 13 deletions packages/react-moveable/src/ables/snappable/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ export function checkMoveableSnapPoses(
posesY: number[],
dirXs: string[] = [],
dirYs: string[] = [],
customSnapThreshold?: number,
customSnapVerticalThreshold: number | undefined,
customSnapHorizontalThreshold: number | undefined,
) {
const props = moveable.props;
const snapThresholdMultiples = moveable.state.snapThresholdInfo?.multiples || [1, 1];
const snapThreshold = selectValue<number>(
customSnapThreshold,
props.snapThreshold,
const snapHorizontalThreshold = selectValue<number>(
customSnapHorizontalThreshold,
props.snapHorizontalThreshold,
5,
);
const snapVerticalThreshold = selectValue<number>(
customSnapVerticalThreshold,
props.snapVerticalThreshold,
5,
);

Expand All @@ -35,7 +41,8 @@ export function checkMoveableSnapPoses(
posesY,
dirXs,
dirYs,
snapThreshold,
snapHorizontalThreshold,
snapVerticalThreshold,
snapThresholdMultiples,
);
}
Expand All @@ -46,12 +53,13 @@ export function checkSnapPoses(
posesY: number[],
dirXs: string[],
dirYs: string[],
snapThreshold: number,
snapHorizontalThreshold: number,
snapVerticalThreshold: number,
multiples: number[],
) {
return {
vertical: checkSnap(guidelines, "vertical", posesX, snapThreshold * multiples[0], dirXs),
horizontal: checkSnap(guidelines, "horizontal", posesY, snapThreshold * multiples[1], dirYs),
vertical: checkSnap(guidelines, "vertical", posesX, snapVerticalThreshold * multiples[0], dirXs),
horizontal: checkSnap(guidelines, "horizontal", posesY, snapHorizontalThreshold * multiples[1], dirYs),
};
}
export function checkSnapKeepRatio(
Expand Down Expand Up @@ -88,7 +96,15 @@ export function checkSnapKeepRatio(
const {
vertical: verticalSnapInfo,
horizontal: horizontalSnapInfo,
} = checkMoveableSnapPoses(moveable, dx ? [endX] : [], dy ? [endY] : []);
} = checkMoveableSnapPoses(
moveable,
dx ? [endX] : [],
dy ? [endY] : [],
[],
[],
undefined,
undefined,
);

verticalSnapInfo.posInfos.filter(({ pos }) => {
return isRight ? pos >= startX : pos <= startX;
Expand Down Expand Up @@ -173,7 +189,8 @@ function getStringDirection(dir: number | string) {
export function checkSnaps(
moveable: MoveableManagerInterface<SnappableProps, SnappableState>,
rect: SnapDirectionPoses,
customSnapThreshold?: number,
customSnapVerticalThreshold: number | undefined,
customSnapHorizontalThreshold: number | undefined,
): { vertical: SnapDirectionInfo; horizontal: SnapDirectionInfo } {
const poses = splitSnapDirectionPoses(moveable.props.snapDirections, rect);

Expand All @@ -183,7 +200,8 @@ export function checkSnaps(
poses.horizontal,
poses.verticalNames.map(name => getStringDirection(name)),
poses.horizontalNames.map(name => getStringDirection(name)),
customSnapThreshold,
customSnapVerticalThreshold,
customSnapHorizontalThreshold,
);
const horizontalDirection = getStringDirection(poses.horizontalNames[result.horizontal.index]);
const verticalDirection = getStringDirection(poses.verticalNames[result.vertical.index]);
Expand Down Expand Up @@ -300,7 +318,8 @@ export function getSnapInfosByDirection(
// pos1 pos2 pos3 pos4
poses: number[][],
snapDirection: number[],
snapThreshold = 1,
customSnapVerticalThreshold: number | undefined,
customSnapHorizontalThreshold: number | undefined,
): { vertical: SnapDirectionInfo; horizontal: SnapDirectionInfo } {
let dirs: number[][] = [];

Expand Down Expand Up @@ -357,7 +376,8 @@ export function getSnapInfosByDirection(
xs, ys,
dirs.map(dir => getStringDirection(dir[0])),
dirs.map(dir => getStringDirection(dir[1])),
snapThreshold
customSnapVerticalThreshold,
customSnapHorizontalThreshold,
);
const verticalDirection = getStringDirection(dirs.map(dir => dir[0])[result.vertical.index]);
const horizontalDirection = getStringDirection(dirs.map(dir => dir[1])[result.horizontal.index]);
Expand Down
10 changes: 8 additions & 2 deletions packages/react-moveable/src/ables/snappable/snapBounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ export function checkMoveableSnapBounds(
moveable,
poses.vertical,
poses.horizontal,
undefined,
undefined,
undefined,
undefined,
);
const horizontalOffset = getSnapBound(
horizontalBoundInfos[0],
Expand Down Expand Up @@ -334,7 +338,8 @@ export function checkSnapBounds(
bounds: BoundType | undefined | false,
posesX: number[],
posesY: number[],
snapThreshold: number,
snapHorizontalThreshold: number,
snapVerticalThreshold: number,
multiples = [1, 1],
): DirectionSnapType<Required<SnapBoundInfo>> {
const {
Expand All @@ -351,7 +356,8 @@ export function checkSnapBounds(
vertical: verticalSnapInfo,
} = checkSnapPoses(
guideines, posesX, posesY, [], [],
snapThreshold,
snapHorizontalThreshold,
snapVerticalThreshold,
multiples,
);

Expand Down
14 changes: 13 additions & 1 deletion packages/react-moveable/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2608,9 +2608,21 @@ export interface SnappableOptions {
/**
/**
* Distance value that can snap to guidelines.
* @default 5
* Use `snapHorizontalThreshold` and `snapVerticalThreshold`
* @default 0
* @depreacted
*/
snapThreshold?: number;
/**
* Distance horizontal between horizontal value that can snap to guidelines.
* @default 5
*/
snapHorizontalThreshold?: number;
/**
* Distance Horizontal value that can snap to guidelines.
* @default 5
*/
snapVerticalThreshold?: number;
/**
* Distance value that render snapped guidelines.
* @default 1
Expand Down
9 changes: 7 additions & 2 deletions packages/react-moveable/stories/controls/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,14 @@ export const DEFAULT_SNAPPABLE_CONTROLS = {
description: makeLink("Snappable", "snapDirections"),
defaultValue: { top: true, left: true, bottom: true, right: true },
}),
snapThreshold: makeArgType({
snapHorizontalThreshold: makeArgType({
type: "number",
description: makeLink("Snappable", "snapThreshold"),
description: makeLink("Snappable", "snapHorizontalThreshold"),
defaultValue: 5,
}),
snapVerticalThreshold: makeArgType({
type: "number",
description: makeLink("Snappable", "snapVerticalThreshold"),
defaultValue: 5,
}),
};
Expand Down
9 changes: 7 additions & 2 deletions storybook/stories/controls/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,14 @@ export const DEFAULT_SNAPPABLE_CONTROLS = {
description: makeLink("Snappable", "snapDirections"),
defaultValue: { top: true, left: true, bottom: true, right: true },
}),
snapThreshold: makeArgType({
snapHorizontalThreshold: makeArgType({
type: "number",
description: makeLink("Snappable", "snapThreshold"),
description: makeLink("Snappable", "snapHorizontalThreshold"),
defaultValue: 5,
}),
snapVerticalThreshold: makeArgType({
type: "number",
description: makeLink("Snappable", "snapVerticalThreshold"),
defaultValue: 5,
}),
};
Expand Down

0 comments on commit 6cd5114

Please sign in to comment.