Skip to content

Commit

Permalink
fix: fix threshold option not applying snap threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
malangfox committed Jan 10, 2024
1 parent d88869d commit 39b1edd
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/control/SnapControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class SnapControl extends Control {
return Promise.reject(new FlickingError(ERROR.MESSAGE.POSITION_NOT_REACHABLE(position), ERROR.CODE.POSITION_NOT_REACHABLE));
}

const snapThreshold = this._calcSnapThreshold(position, activeAnchor);
const snapThreshold = this._calcSnapThreshold(flicking.threshold, position, activeAnchor);

const posDelta = flicking.animating
? state.delta
Expand Down Expand Up @@ -210,7 +210,7 @@ class SnapControl extends Control {
return adjacentAnchor;
}

private _calcSnapThreshold(position: number, activeAnchor: AnchorPoint): number {
private _calcSnapThreshold(threshold: number, position: number, activeAnchor: AnchorPoint): number {
const isNextDirection = position > activeAnchor.position;
const panel = activeAnchor.panel;
const panelSize = panel.size;
Expand All @@ -222,9 +222,9 @@ class SnapControl extends Control {
* |<------>|<------------>|
* [ |<-Anchor ]
*/
return isNextDirection
return Math.max(threshold, isNextDirection
? panelSize - alignPos + panel.margin.next
: alignPos + panel.margin.prev;
: alignPos + panel.margin.prev);
}
}

Expand Down
4 changes: 4 additions & 0 deletions test/manual/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ header {
background-color: #423789;
}

.vertical {
height: 200px;
}

.flicking-panel {
border: 10px solid transparent;
box-sizing: border-box;
Expand Down
2 changes: 1 addition & 1 deletion test/manual/js/vertical.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const flicking = new Flicking("#flicking", {
horizontal: false,
align: "prev",
threshold: 100,
threshold: 50,
});
18 changes: 18 additions & 0 deletions test/unit/Flicking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,24 @@ describe("Flicking", () => {

expect(flicking.index).equals(beforeIndex);
});

it("should change panel when moving above threshold (vertical)", async () => {
const flicking = await createFlicking(El.DEFAULT_VERTICAL, { threshold: 50, defaultIndex: 2, horizontal: false, align: "prev" });
const beforeIndex = flicking.index;

await simulate(flicking.element, { deltaY: 1000, duration: 3000 });

expect(flicking.index).not.to.equal(beforeIndex);
});

it("should not change panel when moving below threshold (vertical)", async () => {
const flicking = await createFlicking(El.DEFAULT_VERTICAL, { threshold: 50, defaultIndex: 2, horizontal: false, align: "prev" });
const beforeIndex = flicking.index;

await simulate(flicking.element, { deltaY: 49, duration: 3000 });

expect(flicking.index).equals(beforeIndex);
});
});

describe("interruptable", () => {
Expand Down
12 changes: 6 additions & 6 deletions test/unit/helper/El.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ class El {
* @example
* - Viewport (width: 1000px, height: 1000px)
* - Camera
* - Panel (width: 100%, height: 100%)
* - Panel (width: 100%, height: 100%)
* - Panel (width: 100%, height: 100%)
* - Panel (width: 100%, height: 1000px)
* - Panel (width: 100%, height: 1000px)
* - Panel (width: 100%, height: 1000px)
*/
public static get DEFAULT_VERTICAL() {
return El.viewport("1000px", "1000px").addClass(EL_CLASS.VERTICAL).add(
El.camera().add(
El.panel("100%", "100%"),
El.panel("100%", "100%"),
El.panel("100%", "100%"),
El.panel().setWidth("100%").setHeight(1000),
El.panel().setWidth("100%").setHeight(1000),
El.panel().setWidth("100%").setHeight(1000),
),
);
}
Expand Down

0 comments on commit 39b1edd

Please sign in to comment.