diff --git a/README.md b/README.md index 8006ff7..ee41808 100755 --- a/README.md +++ b/README.md @@ -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... @@ -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", }); ``` diff --git a/package-lock.json b/package-lock.json index 2c4160f..faa023a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@phntms/reveal-on-scroll", - "version": "0.1.0", + "version": "0.1.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@phntms/reveal-on-scroll", - "version": "0.1.0", + "version": "0.1.1", "license": "MIT", "devDependencies": { "@babel/preset-env": "^7.4.5", diff --git a/package.json b/package.json index 0a6fcff..d5fe828 100755 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index a583365..292bb15 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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() { @@ -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 { @@ -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() { @@ -120,3 +118,4 @@ class RevealOnScroll { } export default RevealOnScroll; +export { RevealConfig, DEFAULT_REVEAL_CONFIG } from "./types"; diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..7507f8a --- /dev/null +++ b/src/types.ts @@ -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", +}; diff --git a/src/utils/platform.ts b/src/utils/platform.ts index 1bace3f..3ab0c05 100644 --- a/src/utils/platform.ts +++ b/src/utils/platform.ts @@ -1,3 +1,3 @@ -export const IN_BROWSER = window !== undefined; +export const IS_BROWSER = window !== undefined; export const HAS_INTERSECTION_OBSERVER = !!window.IntersectionObserver; diff --git a/src/utils/types.ts b/src/utils/types.ts deleted file mode 100644 index d998c9c..0000000 --- a/src/utils/types.ts +++ /dev/null @@ -1,13 +0,0 @@ -export const REVEAL_CLASS = "reveal-on-scroll"; -export const VISIBLE_CLASS = "reveal-scrolled"; -export const HIDDEN_CLASS = "reveal-hidden"; - -export interface Config { - delayBetweenQueuedElements: number; - thresholdToRevealElements: number; -} - -export const DEFAULT_CONFIG: Config = { - delayBetweenQueuedElements: 150, - thresholdToRevealElements: 0.2, -};