Skip to content

Commit 6167676

Browse files
dimensiNafranetsVK
andauthored
feat: added pan on scroll, improve the build (#447)
Co-authored-by: Nikita Nafranets <n.nafranets@vk.team>
1 parent 11c0f41 commit 6167676

8 files changed

Lines changed: 73 additions & 9 deletions

File tree

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
runs-on: ubuntu-latest
1818
steps:
1919
- name: Wait on tests
20-
uses: lewagon/wait-on-check-action@v1.1.2
20+
uses: lewagon/wait-on-check-action@v1.3.1
2121
with:
2222
ref: ${{ github.ref }}
2323
check-name: "Run tests"

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"module": "dist/index.esm.js",
99
"types": "dist/index.d.ts",
1010
"source": "src/index.ts",
11+
"files": [
12+
"dist"
13+
],
1114
"scripts": {
1215
"build": "rollup -c",
1316
"build:docs": "build-storybook",

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default [
2929
exclude: "node_modules/**",
3030
}),
3131
del({ targets: ["dist/*"] }),
32-
typescript({ sourceMap: false }),
32+
typescript({ sourceMap: false, declaration: false }),
3333
postcss({
3434
modules: true,
3535
}),

src/constants/state.constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const initialSetup: LibrarySetup = {
3737
allowLeftClickPan: true,
3838
allowMiddleClickPan: true,
3939
allowRightClickPan: true,
40+
wheelPanning: false,
4041
activationKeys: [],
4142
excluded: [],
4243
},

src/core/instance.core.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ import {
2626
handleWheelZoom,
2727
handleWheelStop,
2828
} from "./wheel/wheel.logic";
29-
import { isPanningAllowed, isPanningStartAllowed } from "./pan/panning.utils";
29+
import {
30+
getPaddingValue,
31+
handleNewPosition,
32+
isPanningAllowed,
33+
isPanningStartAllowed,
34+
} from "./pan/panning.utils";
3035
import {
3136
handlePanning,
3237
handlePanningEnd,
@@ -68,6 +73,7 @@ export class ZoomPanPinch {
6873
public wheelAnimationTimer: ReturnType<typeof setTimeout> | null = null;
6974
// panning helpers
7075
public isPanning = false;
76+
public isWheelPanning = false;
7177
public startCoords: StartCoordsType = null;
7278
public lastTouch: number | null = null;
7379
// pinch helpers
@@ -112,6 +118,11 @@ export class ZoomPanPinch {
112118
const passive = makePassiveEventOption();
113119
const currentDocument = this.wrapperComponent?.ownerDocument;
114120
const currentWindow = currentDocument?.defaultView;
121+
this.wrapperComponent?.addEventListener(
122+
"wheel",
123+
this.onWheelPanning,
124+
passive,
125+
);
115126
// Panning on window to allow panning when mouse is out of component wrapper
116127
currentWindow?.addEventListener("mousedown", this.onPanningStart, passive);
117128
currentWindow?.addEventListener("mousemove", this.onPanning, passive);
@@ -197,6 +208,44 @@ export class ZoomPanPinch {
197208
// Pan
198209
/// ///////
199210

211+
onWheelPanning = (event: WheelEvent): void => {
212+
const { disabled, wheel, panning } = this.setup;
213+
if (
214+
!this.wrapperComponent ||
215+
!this.contentComponent ||
216+
disabled ||
217+
!wheel.wheelDisabled ||
218+
panning.disabled ||
219+
!panning.wheelPanning ||
220+
event.ctrlKey
221+
) {
222+
return;
223+
}
224+
225+
event.preventDefault();
226+
event.stopPropagation();
227+
228+
const { positionX, positionY } = this.transformState;
229+
const mouseX = positionX - event.deltaX;
230+
const mouseY = positionY - event.deltaY;
231+
const newPositionX = panning.lockAxisX ? positionX : mouseX;
232+
const newPositionY = panning.lockAxisY ? positionY : mouseY;
233+
234+
const { sizeX, sizeY } = this.setup.alignmentAnimation;
235+
const paddingValueX = getPaddingValue(this, sizeX);
236+
const paddingValueY = getPaddingValue(this, sizeY);
237+
238+
if (newPositionX === positionX && newPositionY === positionY) return;
239+
240+
handleNewPosition(
241+
this,
242+
newPositionX,
243+
newPositionY,
244+
paddingValueX,
245+
paddingValueY,
246+
);
247+
};
248+
200249
onPanningStart = (event: MouseEvent): void => {
201250
const { disabled } = this.setup;
202251
const { onPanningStart } = this.props;

src/models/context.model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export type ReactZoomPanPinchProps = {
8686
allowRightClickPan?: boolean;
8787
activationKeys?: string[];
8888
excluded?: string[];
89+
wheelPanning?: boolean;
8990
};
9091
pinch?: {
9192
step?: number;

src/stories/docs/props.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ export const wrapperPropsTable: ComponentProps = {
230230
defaultValue: String(initialSetup.panning.disabled),
231231
description: "Disable the panning feature.",
232232
},
233+
wheelPanning: {
234+
type: ["boolean"],
235+
defaultValue: String(initialSetup.panning.wheelPanning),
236+
description: "Enable wheel/trackpad panning.",
237+
},
233238
velocityDisabled: {
234239
type: ["boolean"],
235240
defaultValue: String(initialSetup.panning.velocityDisabled),
@@ -251,20 +256,17 @@ export const wrapperPropsTable: ComponentProps = {
251256
allowLeftClickPan: {
252257
type: ["boolean"],
253258
defaultValue: String(initialSetup.panning.allowLeftClickPan),
254-
description:
255-
"Allow left click to initiate panning",
259+
description: "Allow left click to initiate panning",
256260
},
257261
allowMiddleClickPan: {
258262
type: ["boolean"],
259263
defaultValue: String(initialSetup.panning.allowMiddleClickPan),
260-
description:
261-
"Allow middle click to initiate panning",
264+
description: "Allow middle click to initiate panning",
262265
},
263266
allowRightClickPan: {
264267
type: ["boolean"],
265268
defaultValue: String(initialSetup.panning.allowRightClickPan),
266-
description:
267-
"Allow right click to initiate panning",
269+
description: "Allow right click to initiate panning",
268270
},
269271
activationKeys: {
270272
type: ["string[]"],

src/stories/types/args.types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ export const argsTypes = {
7171
disable: true,
7272
},
7373
},
74+
"panning.wheelPanning": {
75+
defaultValue: initialSetup.panning.wheelPanning,
76+
control: { type: "boolean" },
77+
table: {
78+
defaultValue: { summary: "false" },
79+
type: { summary: "boolean" },
80+
},
81+
},
7482
"panning.disabled": {
7583
defaultValue: initialSetup.panning.disabled,
7684
control: { type: "boolean" },

0 commit comments

Comments
 (0)