Skip to content

feat: make scroll directive standalone #427

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

Merged
merged 2 commits into from
May 30, 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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,33 @@ export class AppModule {}
platformBrowserDynamic().bootstrapModule(AppModule);
```

or for standalone import InfiniteScrollDirective:

```typescript
import { InfiniteScrollDirective } from 'ngx-infinite-scroll';
import { Component } from '@angular/core';

@Component({
selector: 'app',
template: `
<div
class="search-results"
infiniteScroll
[infiniteScrollDistance]="2"
[infiniteScrollThrottle]="50"
(scrolled)="onScroll()"
></div>
`,
standalone: true,
imports: [InfiniteScrollDirective]
})
export class AppComponent {
protected onScroll() {
console.log('scrolled!!');
}
}
```

In this example, the **onScroll** callback will be invoked when the window is scrolled down:

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
SimpleChanges,
} from '@angular/core';
import { Subscription } from 'rxjs';

import { IInfiniteScrollEvent, IInfiniteScrollAction } from '../models';
import { hasWindowDefined, inputPropChanged } from './services/ngx-ins-utils';
import {
Expand All @@ -21,6 +20,7 @@ import {

@Directive({
selector: '[infiniteScroll], [infinite-scroll], [data-infinite-scroll]',
standalone: true
})
export class InfiniteScrollDirective
implements OnDestroy, OnChanges, AfterViewInit
Expand Down Expand Up @@ -70,26 +70,30 @@ export class InfiniteScrollDirective
}
}

setup() {
if (hasWindowDefined()) {
this.zone.runOutsideAngular(() => {
this.disposeScroller = createScroller({
fromRoot: this.fromRoot,
alwaysCallback: this.alwaysCallback,
disable: this.infiniteScrollDisabled,
downDistance: this.infiniteScrollDistance,
element: this.element,
horizontal: this.horizontal,
scrollContainer: this.infiniteScrollContainer,
scrollWindow: this.scrollWindow,
throttle: this.infiniteScrollThrottle,
upDistance: this.infiniteScrollUpDistance,
}).subscribe((payload) => this.handleOnScroll(payload));
});
}
ngOnDestroy() {
this.destroyScroller();
}

handleOnScroll({ type, payload }: IInfiniteScrollAction) {
private setup() {
if (!hasWindowDefined()) { return; }

this.zone.runOutsideAngular(() => {
this.disposeScroller = createScroller({
fromRoot: this.fromRoot,
alwaysCallback: this.alwaysCallback,
disable: this.infiniteScrollDisabled,
downDistance: this.infiniteScrollDistance,
element: this.element,
horizontal: this.horizontal,
scrollContainer: this.infiniteScrollContainer,
scrollWindow: this.scrollWindow,
throttle: this.infiniteScrollThrottle,
upDistance: this.infiniteScrollUpDistance,
}).subscribe((payload) => this.handleOnScroll(payload));
});
}

private handleOnScroll({ type, payload }: IInfiniteScrollAction) {
const emitter =
type === InfiniteScrollActions.DOWN ? this.scrolled : this.scrolledUp;

Expand All @@ -98,11 +102,7 @@ export class InfiniteScrollDirective
}
}

ngOnDestroy() {
this.destroyScroller();
}

destroyScroller() {
private destroyScroller() {
if (this.disposeScroller) {
this.disposeScroller.unsubscribe();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { NgModule } from '@angular/core';

import { InfiniteScrollDirective } from './ngx-infinite-scroll.directive';

/**
* @deprecated Import InfiniteScrollDirective instead
*/
@NgModule({
declarations: [InfiniteScrollDirective],
exports: [InfiniteScrollDirective],
imports: [],
providers: [],
imports: [InfiniteScrollDirective],
})
export class InfiniteScrollModule {}

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion projects/ngx-infinite-scroll/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* Public API Surface of ngx-infinite-scroll
*/

export * from './lib/ngx-infinite-scroll.service';
export { InfiniteScrollDirective } from './lib/ngx-infinite-scroll.directive';
export { InfiniteScrollModule } from './lib/ngx-infinite-scroll.module';
export {
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"es2020",
"dom"
],
"useDefineForClassFields": false
"useDefineForClassFields": false,
"removeComments": false
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
Expand Down