Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Emit ScrollBox scroll event when dragging content #193

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/ScrollBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ export class ScrollBox extends Container
{
if (!this.isDragging) return;

const isVertical: boolean = this.options.type !== 'horizontal';
const touchPoint = this.worldTransform.applyInverse(e.global);

if (this.dragStarTouchPoint)
Expand Down Expand Up @@ -427,6 +428,8 @@ export class ScrollBox extends Container

this.pressedChild = null;
}

this.onScroll?.emit(isVertical ? this.scrollY : this.scrollX);
});

document.addEventListener('wheel', this.onMouseScrollBinding, true);
Expand Down Expand Up @@ -565,9 +568,6 @@ export class ScrollBox extends Container

protected onMouseScroll(event: WheelEvent): void
{
const isVertical: boolean = this.options.type !== 'horizontal';
const oldScrollPos = isVertical ? this.scrollY : this.scrollX;

if (!this.isOver && !this.options.globalScroll) return;

this.renderAllItems();
Expand All @@ -592,6 +592,8 @@ export class ScrollBox extends Container

this._trackpad.xAxis.value = Math.min(max, Math.max(min, targetPos));
}

this.onScroll?.emit(this._trackpad.xAxis.value);
}
else if (typeof event.deltaY !== 'undefined')
{
Expand All @@ -608,16 +610,11 @@ export class ScrollBox extends Container

this._trackpad.yAxis.value = Math.min(max, Math.max(min, targetPos));
}

this.onScroll?.emit(this._trackpad.yAxis.value);
}

this.stopRenderHiddenItems();

const newScrollPos = isVertical ? this.scrollY : this.scrollX;

if (newScrollPos !== oldScrollPos)
{
this.onScroll?.emit(newScrollPos);
}
}

/** Makes it scroll down to the last element. */
Expand Down
5 changes: 4 additions & 1 deletion src/stories/scrollBox/ScrollBoxSprite.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ const args = {
disableEasing: false,
type: [undefined, 'vertical', 'horizontal'],
onPress: action('Button pressed'),
onScroll: action('Scrolled'),
globalScroll: true,
shiftScroll: false
};

export const UseSprite: StoryFn = ({
fontColor, elementsMargin, itemsAmount, onPress, disableEasing, type, globalScroll, shiftScroll
fontColor, elementsMargin, itemsAmount, onPress, onScroll, disableEasing, type, globalScroll, shiftScroll
}: any) =>
{
fontColor = getColor(fontColor);
Expand Down Expand Up @@ -61,6 +62,8 @@ export const UseSprite: StoryFn = ({

scrollBox.addItems(items);

scrollBox.onScroll.connect((val) => onScroll(val));

scrollBox.x = (window.width / 2) - (scrollBox.width / 2);
scrollBox.y = (window.height / 2) - (scrollBox.height / 2) + 18;

Expand Down
Loading