Skip to content

Commit efc2043

Browse files
authored
feat: Enables disabling of trackpad axes (#121)
Allows users to completely disable horizontal or vertical scrolling on the trackpad by setting the constrain percentage to a negative value. Updates the documentation to reflect this new functionality.
1 parent 4c82a74 commit efc2043

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

docs/docs/guides/core/components.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,10 @@ Trackpad options you can configure include:
190190
| `disableEasing` | `boolean` | `false` | Disable momentum scrolling/easing when releasing |
191191
| `xEase` | `ConstrainEase` | `ScrollSpring` | Custom easing for x-axis scrolling |
192192
| `yEase` | `ConstrainEase` | `ScrollSpring` | Custom easing for y-axis scrolling |
193-
| `xConstrainPercent` | `number` | `0` | Percentage of overflow allowed when dragging beyond x-axis limit |
194-
| `yConstrainPercent` | `number` | `0` | Percentage of overflow allowed when dragging beyond y-axis limit |
193+
| `xConstrainPercent` | `number` | `0` | Percentage of overflow allowed when dragging beyond x-axis limit. Setting this value to a negative number disables x-axis scrolling entirely. |
194+
195+
| `yConstrainPercent` | `number` | `0` | Percentage of overflow allowed when dragging beyond y-axis limit. Setting this value to a negative number disables y-axis scrolling entirely. |
196+
195197

196198
These options are passed when creating a `LayoutContainer` or `LayoutView`.
197199

@@ -207,8 +209,8 @@ const container = new LayoutContainer({
207209
// Constrain scrolling within bounds
208210
constrain: true,
209211

210-
// No x-axis overflow allowed when dragging beyond bounds
211-
xConstrainPercent: 0,
212+
// No x-axis scrolling allowed when dragging beyond bounds
213+
xConstrainPercent: -1,
212214
// No y-axis overflow allowed when dragging beyond bounds
213215
yConstrainPercent: 0,
214216

src/components/LayoutContainer.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,19 @@ export class LayoutContainer extends Container {
186186
const targetX = this._trackpad.xAxis.value - deltaX;
187187
const targetY = this._trackpad.yAxis.value - deltaY;
188188

189-
this._trackpad.xAxis.value = Math.max(
190-
this._trackpad.xAxis.max,
191-
Math.min(this._trackpad.xAxis.min, targetX),
192-
);
193-
this._trackpad.yAxis.value = Math.max(
194-
this._trackpad.yAxis.max,
195-
Math.min(this._trackpad.yAxis.min, targetY),
196-
);
189+
if (this._trackpad.xAxis.constrainPercent > 0) {
190+
this._trackpad.xAxis.value = Math.max(
191+
this._trackpad.xAxis.max,
192+
Math.min(this._trackpad.xAxis.min, targetX),
193+
);
194+
}
195+
196+
if (this._trackpad.yAxis.constrainPercent > 0) {
197+
this._trackpad.yAxis.value = Math.max(
198+
this._trackpad.yAxis.max,
199+
Math.min(this._trackpad.yAxis.min, targetY),
200+
);
201+
}
197202
});
198203
Ticker.shared.add(this.update, this);
199204
}

src/components/trackpad/SlidingNumber.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ export class SlidingNumber {
150150
this._prev = this.position;
151151

152152
if (this.constrain) {
153-
if (this.constrainPercent === 0) {
153+
// If constrain percentage is less than 0 we want to not allow any movement
154+
if (this.constrainPercent < 0) {
155+
this.position = 0;
156+
this._speed = 0;
157+
this._hasStopped = true;
158+
} else if (this.constrainPercent === 0) {
154159
if (this.position > this.min) {
155160
this.position = this.min;
156161
} else if (this.position < this.max) {

src/components/trackpad/Trackpad.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ export interface TrackpadOptions {
99
xEase?: ConstrainEase;
1010
/** Custom easing function for y-axis constraint bouncing */
1111
yEase?: ConstrainEase;
12-
/** Percentage of overflow allowed when dragging beyond x-axis constraints */
12+
/**
13+
* Percentage of overflow allowed when dragging beyond x-axis constraints
14+
* Setting this value to a negative number disables x-axis scrolling entirely.
15+
*/
1316
xConstrainPercent?: number;
14-
/** Percentage of overflow allowed when dragging beyond y-axis constraints */
17+
/**
18+
* Percentage of overflow allowed when dragging beyond y-axis constraints
19+
* Setting this value to a negative number disables y-axis scrolling entirely.
20+
*/
1521
yConstrainPercent?: number;
1622
/** Maximum speed for both axes. Default: 400 */
1723
maxSpeed?: number;

0 commit comments

Comments
 (0)