Skip to content

Commit

Permalink
feat(ScrollWrapper): add signals for button press at top and bottom o…
Browse files Browse the repository at this point in the history
…f content (#533)

* fix(ScrollWrapper): add signals for button press at top and bottom of content

* fix(ScrollWrapper): remove extra code from story

* fix: update ts definitions
  • Loading branch information
ImCoolNowRight authored Sep 16, 2024
1 parent 7dd1b24 commit a914066
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ declare namespace ScrollWrapper {
*/
showScrollBar?: boolean;
}

export interface TypeConfig extends lng.Component.TypeConfig {
SignalMapType: SignalMap;
}

export type SignalMap = {
/**
* emitted when user is at the top of the content and presses up again
*/
onUpAtTop(): void;

/**
* emitted when user is at the bottom of the content and presses bottom again
*/
onDownAtBottom(): void;
};
}

declare class ScrollWrapper<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ export default class ScrollWrapper extends Base {
this.fireAncestors('$scrollChanged', 'endDown', this);
this._updateFadeContainer();
}
} else {
this.signal('onDownAtBottom');
}
}

Expand Down Expand Up @@ -278,6 +280,8 @@ export default class ScrollWrapper extends Base {
this._isEndContentVisible = false;
this._updateFadeContainer();
}
} else {
this.signal('onUpAtTop');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,13 @@ Resets the `y` value of both the content and the scroll bar.
#### $scrollChanged('endUp'|'endDown', this)

Event fired via `fireAncestors`, is triggered when scroll reaches the top or bottom of the scroll boundaries.

### Signals

#### onUpAtTop

Fired when user is at the top of the content within the ScrollWrapper and presses up again.

#### onDownAtBottom

Fired when user is at the bottom of the content within the ScrollWrapper and presses down again.
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,27 @@ describe('ScrollWrapper', () => {
);
});

it('should fire a signal when pressing Up when already at the top of the scroll container', () => {
jest.spyOn(scrollWrapper, 'signal');
expect(scrollWrapper.signal).not.toHaveBeenCalled();

testRenderer.keyPress('Up');
testRenderer.update();
expect(scrollWrapper.signal).toHaveBeenCalledWith('onUpAtTop');
});

it('should fire a signal when pressing Down when already at the bottom of the scroll container', () => {
jest.spyOn(scrollWrapper, 'signal');
expect(scrollWrapper.signal).not.toHaveBeenCalled();

// Simulate scrolling to the bottom and then some
for (let i = 0; i < 50; i++) {
testRenderer.keyPress('Down');
testRenderer.update();
}
expect(scrollWrapper.signal).toHaveBeenCalledWith('onDownAtBottom');
});

it('should scroll up by the scroll step', () => {
scrollWrapper.scrollStep = 100;
testRenderer.keyPress('Down');
Expand Down

0 comments on commit a914066

Please sign in to comment.