Skip to content

Commit

Permalink
fix: add findActiveAnchor fallback for bound flicking (#851)
Browse files Browse the repository at this point in the history
  • Loading branch information
malangfox authored Mar 29, 2024
1 parent 022546a commit 2a35048
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/camera/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,11 @@ class Camera {
*/
public findActiveAnchor(): AnchorPoint | null {
const flicking = getFlickingAttached(this._flicking);
const activeIndex = flicking.control.activeIndex;
const activePanel = flicking.control.activePanel;

if (!activePanel) return null;

return find(this._anchors, anchor => anchor.panel.index === activeIndex);
return find(this._anchors, anchor => anchor.panel.index === activePanel.index) ?? this.findNearestAnchor(activePanel.position);
}

/**
Expand Down
25 changes: 24 additions & 1 deletion test/unit/control/SnapControl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ describe("SnapControl", () => {
expect(position).to.equal(flicking.panels[0].position);
});

it("Should move to the nearest panel from the camera, when animation is interrupted by user input", async () => {
it("should move to the nearest panel from the camera, when animation is interrupted by user input", async () => {
const flicking = await createFlicking(El.DEFAULT_HORIZONTAL, {
moveType: MOVE_TYPE.SNAP,
align: "prev",
Expand All @@ -305,6 +305,29 @@ describe("SnapControl", () => {
expect(moveSpy.calledTwice).to.be.true;
expect(control.activePanel.index).to.equal(camera.findNearestAnchor(position).panel.index);
});

it("should use the closest anchor even if there is no anchor for the active panel.", async () => {
const flicking = await createFlicking(
El.viewport().add(
El.camera()
.add(El.panel(`33%`))
.add(El.panel(`33%`))
.add(El.panel(`33%`))
.add(El.panel(`33%`))
), {
moveType: MOVE_TYPE.SNAP,
bound: true // when bound is true, there is no anchor for first panel
}
);

// Set active panel to first panel
void flicking.moveTo(0, 0);
await simulate(flicking.element, { deltaX: -1000, duration: 100 });

const status = flicking.getStatus();

expect(status.index).equals(2);
});
});
});
});

0 comments on commit 2a35048

Please sign in to comment.