Skip to content

Commit

Permalink
feat(scroll-collapse): emit scroll direction events
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Matthews authored and edoparearyee committed Jun 7, 2018
1 parent 51cf0a3 commit 80c6eee
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Commitizen friendly][commitizen-badge]][commitizen]
[![code style: prettier][prettier-badge]][prettier-badge-url]

A simple lightweight library for [Angular][angular] that detects scroll direction and adds a `sn-scrolling-up` or `sn-scrolling-down` class to the element. The library can also detect when the user has scrolled passed the element and apply a `sn-affix` class. Useful for make a element sticky when the user has scrolled beyond it. This library can will also apply `sn-minimise` class after the user has scrolled beyond the height of the element.
A simple lightweight library for [Angular][angular] that detects scroll direction and adds a `sn-scrolling-up` or `sn-scrolling-down` class to the element and emits an appropriate event. The library can also detect when the user has scrolled passed the element and apply a `sn-affix` class. Useful for make a element sticky when the user has scrolled beyond it. This library can will also apply `sn-minimise` class after the user has scrolled beyond the height of the element.

This is a simple library for [Angular][angular], implemented in the [Angular Package Format v5.0][apfv5].

Expand Down Expand Up @@ -60,15 +60,15 @@ A working example can be found inside [/src](https://github.com/thisissoon/angul
### Scroll direction

```html
<nav class="foo" snScrollCollapse>
<nav class="foo" snScrollCollapse (scrollDirectionChange)="scrollDirectionHandler($event)">
...
</nav>
```

```css
.foo {
left: 0;
height: 100px;
left: 0;
position: fixed;
right: 0;
top: 0;
Expand Down
2 changes: 1 addition & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1>Scroll down ↓</h1>

<nav class="nav" snScrollCollapse>
<nav class="nav" snScrollCollapse (scrollDirectionChange)="scrollDirectionHandler($event)">
My nav&nbsp;
<span class="down">Going down</span>
<span class="up">Going up</span>
Expand Down
7 changes: 6 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Component } from '@angular/core';
import { Direction } from '..';

@Component({
selector: 'sn-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {}
export class AppComponent {
scrollDirectionHandler(event: Direction) {
console.log(event);
}
}
14 changes: 14 additions & 0 deletions src/app/scroll-collapse/scroll-collapse.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ElementRef, SimpleChanges } from '@angular/core';
import { WindowRef } from '@thisissoon/angular-inviewport';

import { ScrollCollapseDirective } from './scroll-collapse.directive';
import { Direction } from './shared/direction.enum';

describe('ScrollCollapseDirective', () => {
let node: HTMLElement;
Expand Down Expand Up @@ -113,6 +114,19 @@ describe('ScrollCollapseDirective', () => {
]);
expect(directive.isScrollingDown).toBeTruthy();
});
it('should emit scroll direction event on scroll', () => {
const spy = spyOn(directive.scrollDirectionChange, 'emit');
directive.calculateScrollDirection([
{ scrollX: 0, scrollY: 0, width: 1266, height: 768 },
{ scrollX: 0, scrollY: 100, width: 1266, height: 768 }
]);
expect(spy).toHaveBeenCalledWith(Direction.DOWN);
directive.calculateScrollDirection([
{ scrollX: 0, scrollY: 100, width: 1266, height: 768 },
{ scrollX: 0, scrollY: 0, width: 1266, height: 768 }
]);
expect(spy).toHaveBeenCalledWith(Direction.UP);
});
});

describe('minimise mode', () => {
Expand Down
11 changes: 10 additions & 1 deletion src/app/scroll-collapse/scroll-collapse.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
Input,
NgZone,
AfterViewInit,
OnDestroy
OnDestroy,
Output,
EventEmitter
} from '@angular/core';
import { fromEvent, merge, Subject } from 'rxjs';
import {
Expand Down Expand Up @@ -47,6 +49,12 @@ export class ScrollCollapseDirective implements AfterViewInit, OnDestroy {
* @memberof ScrollCollapseDirective
*/
private scrollDirection: Direction;
/**
* Emits scroll direction on scroll or window resize.
*
* @memberof ScrollCollapseDirective
*/
@Output() scrollDirectionChange = new EventEmitter<Direction>();
/**
* Original offsetTop of element
*
Expand Down Expand Up @@ -192,6 +200,7 @@ export class ScrollCollapseDirective implements AfterViewInit, OnDestroy {
}
this.scrollDirection =
pastEvent.scrollY > currentEvent.scrollY ? Direction.UP : Direction.DOWN;
this.scrollDirectionChange.emit(this.scrollDirection);
}
/**
* Calculate if the user has scrolled pass the origin height of
Expand Down

0 comments on commit 80c6eee

Please sign in to comment.