Skip to content

Commit

Permalink
Export types in index.ts (#4)
Browse files Browse the repository at this point in the history
* Export types in index.ts

* Fix import

* Update RevealConfig + tweak how lib is exported

* Group exports

* Expose DEFAULT_REVEAL_CONFIG

* Update README.md

Co-authored-by: John Richard Chipps-Harding <john.chipps-harding@phntms.com>

* Rename `IN_BROWSER` to `IS_BROWSER`

Co-authored-by: John Richard Chipps-Harding <john.chipps-harding@phntms.com>
  • Loading branch information
PauloMFJ and John Richard Chipps-Harding authored Apr 28, 2021
1 parent 6d04d2f commit 10e21ce
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 35 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ npm i @phntms/reveal-on-scroll
To use, initialize a new `RevealOnScroll()` component.

```ts
import RevealOnScroll from "@pnhtms/reveal-on-scroll";

new RevealOnScroll();

// Or...
Expand All @@ -38,6 +40,15 @@ new RevealOnScroll({

// Threshold of element that has to be in view, before revealing element
thresholdToRevealElements: 0.2,

// Used to querySelectAll with the following
revealSelector: ".reveal-on-scroll",

// Class added to element when revealed
visibleClass: "reveal-scrolled",

// If used alongside revealSelector, ignores reveal events till removed
hiddenClass: "reveal-hidden",
});
```

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@phntms/reveal-on-scroll",
"description": "Lightweight library to reveal elements on scroll.",
"version": "0.1.0",
"version": "0.1.1",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"homepage": "https://github.com/phantomstudios/reveal-on-scroll#readme",
Expand Down
35 changes: 17 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
import { IN_BROWSER, HAS_INTERSECTION_OBSERVER } from "./utils/platform";
import {
Config,
DEFAULT_CONFIG,
HIDDEN_CLASS,
REVEAL_CLASS,
VISIBLE_CLASS,
} from "./utils/types";
import { RevealConfig, DEFAULT_REVEAL_CONFIG } from "./types";
import { IS_BROWSER, HAS_INTERSECTION_OBSERVER } from "./utils/platform";

class RevealOnScroll {
config!: Config;
config!: RevealConfig;
readonly elements: Element[] = [];
readonly _queueToShow: Element[] = [];
private _intersectionObserver!: IntersectionObserver;
private _canRevealNext = true;

constructor(config?: Config) {
constructor(config?: RevealConfig) {
// If not in browser (SSR), ignore
if (!IN_BROWSER) return;
if (!IS_BROWSER) return;

this.config = Object.assign(DEFAULT_REVEAL_CONFIG, config);
this.elements = this.getAllElementsToReveal();

// If intersectionObserver isn't supported (IE), force show all
if (!HAS_INTERSECTION_OBSERVER) this.revealAllElements();
else {
this.config = Object.assign(DEFAULT_CONFIG, config);
this._intersectionObserver = this._createIntersectionObserver();
this._observeElements(this.elements);
}
}

getAllElementsToReveal() {
return Array.from(document.getElementsByClassName(REVEAL_CLASS));
return Array.from(document.querySelectorAll(this.config.revealSelector));
}

revealAllElements() {
this.elements.forEach((element) => element.classList.add(VISIBLE_CLASS));
this.elements.forEach((element) =>
element.classList.add(this.config.visibleClass)
);
}

private _createIntersectionObserver() {
Expand All @@ -46,8 +42,10 @@ class RevealOnScroll {
const element = entry.target as Element;

const queued = this._queueToShow.includes(element);
const alreadyVisible = element.classList.contains(VISIBLE_CLASS);
const hidden = element.classList.contains(HIDDEN_CLASS);
const alreadyVisible = element.classList.contains(
this.config.visibleClass
);
const hidden = element.classList.contains(this.config.hiddenClass);

if (queued || alreadyVisible || hidden) return;
else {
Expand Down Expand Up @@ -102,8 +100,8 @@ class RevealOnScroll {
}

revealElement(element: Element) {
const alreadyVisible = element.classList.contains(VISIBLE_CLASS);
if (!alreadyVisible) element.classList.add(VISIBLE_CLASS);
const alreadyVisible = element.classList.contains(this.config.visibleClass);
if (!alreadyVisible) element.classList.add(this.config.visibleClass);
}

private _revealNextElement() {
Expand All @@ -120,3 +118,4 @@ class RevealOnScroll {
}

export default RevealOnScroll;
export { RevealConfig, DEFAULT_REVEAL_CONFIG } from "./types";
15 changes: 15 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface RevealConfig {
delayBetweenQueuedElements: number;
thresholdToRevealElements: number;
revealSelector: string;
visibleClass: string;
hiddenClass: string;
}

export const DEFAULT_REVEAL_CONFIG: RevealConfig = {
delayBetweenQueuedElements: 150,
thresholdToRevealElements: 0.2,
revealSelector: ".reveal-on-scroll",
visibleClass: "reveal-scrolled",
hiddenClass: "reveal-hidden",
};
2 changes: 1 addition & 1 deletion src/utils/platform.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const IN_BROWSER = window !== undefined;
export const IS_BROWSER = window !== undefined;

export const HAS_INTERSECTION_OBSERVER = !!window.IntersectionObserver;
13 changes: 0 additions & 13 deletions src/utils/types.ts

This file was deleted.

0 comments on commit 10e21ce

Please sign in to comment.