Skip to content

Commit

Permalink
Merge pull request #5 from gen-tech/horizontal-scroll
Browse files Browse the repository at this point in the history
Horizontal scroll
  • Loading branch information
alisahinozcelik authored Nov 19, 2018
2 parents 75442ee + bd7996c commit c18ed50
Show file tree
Hide file tree
Showing 13 changed files with 582 additions and 168 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ after_success:
- npm run cover
before_deploy:
- npm run prepare-github-pages
- gtvu $TRAVIS_TAG --safe
deploy:
- provider: pages
skip-cleanup: true
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
"typings": "index.d.ts",
"homepage": "https://gen-tech.github.io/scroll-manager",
"scripts": {
"document": "typedoc --out docs --excludeNotExported --excludePrivate --disableOutputCheck --gitRevision master",
"document": "typedoc --out docs --excludeNotExported --excludePrivate --disableOutputCheck --gitRevision master --exclude \"**/*index.ts\"",
"prepare-github-pages": "npm run document && echo \"\" > ./docs/.nojekyll",
"test": "karma start ./karma.conf.js --single-run",
"test:debug": "karma start",
"cover": "cat ./coverage/lcovonly | coveralls",
"publish:config-npm": "replace '<EMAIL>' $NPM_EMAIL .npmrc && replace '<TOKEN>' $NPM_TOKEN .npmrc",
"publish:copy-files": "cpy 'LICENSE' 'README.md' '.npmrc' '.npmignore' 'package.json' dist",
"publish:pack": "cd dist && npm pack",
"publish:prepare": "tsc && npm run publish:config-npm && npm run publish:copy-files && npm run publish:pack",
"publish:package": "npm publish ./dist/gen-tech-scroll-manager-$TRAVIS_TAG.tgz",
"prepare-and-publish": "npm run publish:prepare && npm run publish:package"
"publish:prepare": "gtvu $TRAVIS_TAG && tsc && npm run publish:config-npm && npm run publish:copy-files && npm run publish:pack",
"publish:publish": "npm publish ./dist/gen-tech-scroll-manager-$TRAVIS_TAG.tgz",
"prepare-and-publish": "npm run publish:prepare && npm run publish:publish"
},
"repository": {
"type": "git",
Expand Down
17 changes: 16 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ import { ScrollManager } from "./scroll-manager";

export { ScrollManager } from "./scroll-manager";
export { ScrollObserver } from "./scroll-observer";
export { ScrollToElementOptions, ScrollableContent, ScrollDirection, ScrollPhase } from "./models";

export {
ScrollToElementOptions,
ScrollableContent,
ScrollDirection,
ScrollPhase,
ScrollPosition,
HorizontalScrollDirection,
VerticalScrollDirection,
RemainingScrollPosition
} from "./models";

/**
* ### Scroll Manager
*
* Default Scroll Manager Instance
*/
export default new ScrollManager();
2 changes: 2 additions & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from "./scroll-buffer.interface";
export * from "./scroll-direction.enum";
export * from "./scroll-phase.enum";
export * from "./scroll-position.interface";
export * from "./scroll-to-element-options.interface";
export * from "./scrollable-content.type";
28 changes: 28 additions & 0 deletions src/models/scroll-buffer.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Observable } from "rxjs";
import { ScrollPosition } from './scroll-position.interface';

/**
* Scroll Buffer Object to use in every observer instance
*/
export interface Buffer {
/**
* Cache for observables of both positions
*/
both: {
[key: number]: Observable<ScrollPosition>;
};

/**
* Cache for observable of x position
*/
x: {
[key: number]: Observable<number>;
};

/**
* Cache for observable of x position
*/
y: {
[key: number]: Observable<number>;
};
}
39 changes: 19 additions & 20 deletions src/models/scroll-direction.enum.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
/**
* ### Scroll Direction
* ### Vertical Scroll Direction
*/
export enum ScrollDirection {
/**
* Scrolling up
*/
UP,

/**
* Scrolling down
*/
DOWN,
export enum VerticalScrollDirection {
TOP = 1 << 0,
BOTTOM = 1 << 1
}

/**
* Scrolling left
*/
LEFT,
/**
* ### Horizontal Scroll Direction
*/
export enum HorizontalScrollDirection {
LEFT = 1 << 2,
RIGHT = 1 << 3
}

/**
* Scrolling right
*/
RIGHT,
}
/**
* ### Scroll Direction
*/
export const ScrollDirection = {
...VerticalScrollDirection,
...HorizontalScrollDirection
}
31 changes: 31 additions & 0 deletions src/models/scroll-position.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* ### Scroll Position
* Represents the scroll positions
*/
export interface ScrollPosition {
/**
* Top position in pixels
*/
top: number;

/**
* Left position in pixels
*/
left: number;
}

/**
* ### Remaining Scroll Position
* Represents the remaining scroll positions
*/
export interface RemainingScrollPosition {
/**
* Bottom position in pixels
*/
bottom: number;

/**
* Right position in pixels
*/
right: number;
}
8 changes: 7 additions & 1 deletion src/models/scroll-to-element-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ export interface ScrollToElementOptions {
* Space between the window top and the element will be scrolled into
* @default 0
*/
offset?: number;
offsetTop?: number;

/**
* Space between the window left and the element will be scrolled into
* @default 0
*/
offsetLeft?: number;
}
2 changes: 1 addition & 1 deletion src/models/scrollable-content.type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* Scrollable Content
* ### Scrollable Content
*/
export type ScrollableContent = Document | HTMLElement;
3 changes: 1 addition & 2 deletions src/scroll-manager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import "jasmine";
import { Subscription } from "rxjs";

import { ScrollManager } from "./scroll-manager";
Expand Down Expand Up @@ -76,7 +75,7 @@ describe("Scroll Manager", () => {
const manager = new ScrollManager();

manager
.scrollToElement(testContainer, {offset: 50})
.scrollToElement(testContainer, {offsetTop: 50})
.then(() => {
expect(testContainer.getBoundingClientRect().top).toBe(-50);
done();
Expand Down
20 changes: 12 additions & 8 deletions src/scroll-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "smoothscroll-polyfill";

import { first } from "rxjs/operators";
import { ScrollObserver } from "./scroll-observer";
import { ScrollToElementOptions, ScrollableContent } from './models';
import { ScrollToElementOptions, ScrollableContent, ScrollPosition } from './models';

/**
* ### Scroll Manager
Expand Down Expand Up @@ -63,8 +63,8 @@ export class ScrollManager {
}

/**
* ### Scroll the window into an element
*
* ### Scroll to Element
* Scroll the window into an element
* * * *
* Example:
* ```typescript
Expand All @@ -79,18 +79,22 @@ export class ScrollManager {
*/
public scrollToElement(
element: HTMLElement,
{animate = true, offset = 0 }: ScrollToElementOptions = {animate: true, offset: 0}
): Promise<number> {
window.scrollBy({top: element.getBoundingClientRect().top + offset, behavior: animate ? "smooth" : "instant"});
{animate = true, offsetTop = 0, offsetLeft = 0 }: ScrollToElementOptions = {animate: true, offsetTop: 0, offsetLeft: 0}
): Promise<ScrollPosition> {
const { top, left } = element.getBoundingClientRect();

window.scrollBy({top: top + offsetTop, left: left + offsetLeft, behavior: animate ? "smooth" : "instant"});

return this.root.scrollEnd.pipe(first()).toPromise();
}

/**
* Scrolls to the top smoothly
* @returns A promise which resolves when the scrolling is completed
*/
public scrollTop(): Promise<number> {
window.scrollTo({top: 0, behavior: "smooth"});
public scrollTop(): Promise<ScrollPosition> {
window.scrollTo({top: 0, behavior: "smooth"});

return this.root.scrollEnd.pipe(first()).toPromise();
}
}
Loading

0 comments on commit c18ed50

Please sign in to comment.