Skip to content

Commit 9903a0c

Browse files
committed
feat(InViewport): export directive to be able to trigger inviewport check manually
fix #9
1 parent 50c9112 commit 9903a0c

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ Useful if element is within another scrollable element
135135
</div>
136136
```
137137

138+
### Trigger inviewport check manually
139+
140+
Window scroll and resize events doesn't cover all potential use cases for the inViewport status check. For example if using directive inside a carousel. To trigger a check manually simply assign a template variable value to the directive and call `calculateInViewportStatus` when you require.
141+
142+
#### `app.component.html`
143+
144+
```html
145+
<p snInViewport #foo="snInViewport">
146+
Amet tempor excepteur occaecat nulla.
147+
</p>
148+
149+
<button (click)="foo.calculateInViewportStatus()">Check status</button>
150+
```
151+
138152
## Development server
139153

140154
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.

src/app/app.component.html

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
<h1>Scroll down ↓ and right →</h1>
2+
23
<div class="spacer"></div>
3-
<p class="small-element" snInViewport (inViewportChange)="onInViewportChange($event)" [ngClass]="{'highlight': highlight}">Amet tempor excepteur occaecat nulla. Aute deserunt esse duis velit mollit exercitation nisi officia. Anim do irure cillum esse esse ea magna elit. Velit reprehenderit eiusmod aliqua do reprehenderit elit commodo enim do deserunt consequat ea elit. Incididunt dolore officia consectetur anim ex ea commodo ullamco veniam ipsum esse amet cupidatat commodo. Id consequat duis aliqua minim aliquip enim officia elit. Sint est fugiat ex ex proident ad veniam do excepteur.</p>
4+
5+
<p
6+
class="small-element"
7+
snInViewport
8+
(inViewportChange)="onInViewportChange($event)"
9+
[ngClass]="{'highlight': highlight}">
10+
Amet tempor excepteur occaecat nulla. Aute deserunt esse duis velit mollit exercitation nisi officia. Anim do irure cillum esse esse ea magna elit. Velit reprehenderit eiusmod aliqua do reprehenderit elit commodo enim do deserunt consequat ea elit. Incididunt dolore officia consectetur anim ex ea commodo ullamco veniam ipsum esse amet cupidatat commodo. Id consequat duis aliqua minim aliquip enim officia elit. Sint est fugiat ex ex proident ad veniam do excepteur.
11+
</p>
412

513
<p class="large-element" snInViewport>very large element</p>
614

15+
<button (click)="insideScrollable.calculateInViewportStatus()">Check</button>
716
<div class="scrollable" #container>
17+
818
<div class="spacer"></div>
9-
<p class="inside-scrollable" snInViewport [parent]="container">Element inside scrollable div</p>
19+
20+
<p
21+
#insideScrollable="snInViewport"
22+
class="inside-scrollable"
23+
snInViewport
24+
[parent]="container">
25+
Element inside scrollable div
26+
</p>
27+
1028
<div class="spacer"></div>
29+
1130
</div>
31+
1232
<div class="spacer"></div>

src/app/in-viewport/in-viewport.directive.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import * as eventData from './shared/event-data';
3131
* ```
3232
*/
3333
@Directive({
34-
selector: '[snInViewport]'
34+
selector: '[snInViewport]',
35+
exportAs: 'snInViewport'
3536
})
3637
export class InViewportDirective implements AfterViewInit, OnDestroy {
3738
/**

0 commit comments

Comments
 (0)