All files / src/dom scroll-progress.ts

0% Statements 0/71
0% Branches 0/39
0% Functions 0/17
0% Lines 0/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
import * as func from '../func/func';
import * as is from '../is/is';
import * as mathf from '../mathf/mathf';
import {Raf} from '../raf/raf';
import * as cssUnit from '../string/css-unit';
import {
  type EventCallback,
  type EventDispatcher,
  EventManager,
} from '../ui/events';
import * as dom from './dom';
import {DomWatcher} from './dom-watcher';
import type {InviewProgress} from './inview-progress';
import type {ProgressWatcher} from './progress-watcher';
 
export enum ScrollProgressEvents {
  RAF = 'raf',
  RESIZE = 'resize',
  INIT = 'init',
}
 
export interface ScrollProgressSettings {
  // debug: false (boolean, optional) True outputs progress in the dev console.
  debug?: boolean;
  //  top: '0px' (string) A css number to offset where the progress begins.  Accepts %, px, vh.
  top?: string;
  //  bottom: '0px' (string) A css number to offset the progress ends.  Accepts %, px, vh.
  bottom?: string;
  //  height: '100px' (string) Optional.  An absolute height to use to calculate the percent.  Accepts %, px, vh.  In most cases you won't need this.
  height?: string | null;
  // http://degu.surge.sh/classes/mathf.mathf-1.html#damp
  //  lerp: 0.18 Optional lerp.  Defaults to 1 assuming no asymptotic averaging.
  lerp?: number;
  //  damp: 0.18 Optional damp.  Defaults to 1 assuming no damping.
  damp?: number;
  // Whether to force clamp the progress to 0-1 range.  Defaults to true.
  clamp?: boolean;
 
  // Limits the lerping to only when values are beteween 0-1.   Defaults to true.
  lerpOnlyInRange?: boolean;
 
  // The precision rounding on the lerp.  Used to cull / avoid layout thrashes.
  //  precision: (number) Defaults to 3.  Lower precision means less dom updates but less accuracy.
  precision?: number;
 
  // The rafEvOptions so that you can add rootMargin etc to the base raf.
  //  rafEvOptions:
  //   rootMargin: '0px 0px 0px 0px'
  rafEvOptions?: Object;
 
  // Optional mobile lerp.
  // Requires mobileBreakpoint to be enabled.
  lerpMobile?: number;
  // Optional mobile damp.
  // Requires mobileBreakpoint to be enabled.
  dampMobile?: number;
  // The breakpoint width of mobile.
  mobileBreakpoint?: number;
 
  // Optionally pass inviewProgress.
  // This can be used to trigger css classes at specific breakpoints.
  inviewProgress?: InviewProgress | null;
 
  // Optionally pass progressWatcher.
  progressWatcher?: ProgressWatcher | null;
}
 
/**
 * A class that can be used to monitor the scroll progress of a given element.
 *
 * Simple usage:
 *
 * const scrollProgress = new ScrollProgress(element);
 * // Add your settings.
 * const settings = {
 *    debug: false,
 *    top: '0px'
 *    bottom: '10px'
 * }
 * scrollProgress.init(settings);
 *
 * scrollProgress.on(ScrollProgressEvents.RAF, ()=> {
 *   console.log(scrollProgress.getProgress());
 * });
 *
 * ## How do I add css classes to elements?
 * You can pass an instance of inviewProgress to scroll progress.
 *
 * ```ts
 * const inviewProgress = new InviewProgress();
 *
 * // Add the class "active" when from range 0.2 - 0.4
 * inviewProgress.add({
 *    range: [0.2, 0.4],
 *    element: el,
 *    className: "active"
 * })
 *
 * // Setup scroll progress
 * const settings = {
 *    debug: false,
 *    top: '0px'
 *    bottom: '10px',
 *    inviewProgress: inviewProgress
 * }
 * const parallaxer = new ScrollProgress(el);
 * parallaxer.init(settings, [])
 * ```
 *
 *
 * ## How do add callbacks at specific points?
 * You can pass an instance of progressWatcher to scroll progress
 *
 * ```
 * const pg = new ProgressWatcher();
 * pg.add({
 *    range: [0.2, 0.4],
 *    callback: (progress: number, direction: number)=> {
 *          console.log(progress, direction);
 *    }
 * })
 *
 * const settings = {
 *    debug: false,
 *    top: '0px'
 *    bottom: '10px',
 *    progressWatcher: pg
 * }
 * const sp = new ScrollProgress(el);
 * sp.init(settings, [])
 *
 */
export class ScrollProgress implements EventDispatcher {
  private eventManager: EventManager;
  private element: HTMLElement;
  private domWatcher: DomWatcher;
  private raf: Raf;
  private settingsData: ScrollProgressSettings | null = null;
  private initialized = false;
  private currentProgress = 0;
  /**
   * The top offset for progress
   */
  private topOffset = 0;
  /**
   * The bottom offset for progress
   */
  private bottomOffset = 0;
 
  /**
   * The height value if specified.
   */
  private height: number | null = null;
 
  constructor(element: HTMLElement) {
    this.eventManager = new EventManager();
    this.element = element;
    this.raf = new Raf(this.onRaf.bind(this));
    this.raf.runWhenElementIsInview(this.element);
    this.domWatcher = new DomWatcher();
    this.domWatcher.add({
      element: window,
      on: 'smartResize',
      callback: func.debounce(() => {
        this.onWindowResize();
      }, 300),
    });
  }
 
  /**
   * Initializes module.
   * You can optionally rerun this method to refresh settings.
   * @param settings
   */
  public init(settings?: ScrollProgressSettings) {
    this.updateSettings(settings);
    this.calculateProgressOffsets();
    this.updateImmediately();
    this.raf.start();
    this.initialized = true;
    this.eventManager.dispatch(ScrollProgressEvents.INIT);
  }
 
  /**
   * Updates the css parallaxer settings.
   * @param settings
   */
  public updateSettings(settings?: ScrollProgressSettings) {
    // If we haven't set the settings data yet apply default.
    if (!this.settingsData) {
      this.settingsData = {
        ...{
          debug: false,
          clamp: true,
          top: '0px',
          bottom: '0px',
          height: null,
          lerp: 1,
          damp: 1,
          precision: 3,
          lerpOnlyInRange: true,
          inviewProgress: null,
          progressWatcher: null,
        },
        ...(settings || {}),
      };
    } else {
      // If we have already set once.
      this.settingsData = {
        ...this.settingsData,
        ...(settings || {}),
      };
    }
  }
 
  public on(event: string, callback: EventCallback) {
    this.eventManager.on(event, callback);
  }
 
  public off(event: string, callback: EventCallback) {
    this.eventManager.on(event, callback);
  }
 
  protected onRaf() {
    this.raf.read(() => {
      // Mobile case.
      if (
        this.settingsData!.mobileBreakpoint &&
        window.innerWidth < this.settingsData!.mobileBreakpoint &&
        this.settingsData!.dampMobile &&
        this.settingsData!.lerpMobile
      ) {
        this.updateProgress(
          this.settingsData!.lerpMobile,
          this.settingsData!.dampMobile
        );
      } else {
        // All others.
        this.updateProgress(this.settingsData!.lerp!, this.settingsData!.damp!);
      }
    });
 
    this.eventManager.dispatch(ScrollProgressEvents.RAF);
  }
 
  protected onWindowResize() {
    this.calculateProgressOffsets();
    this.updateImmediately();
    this.eventManager.dispatch(ScrollProgressEvents.RESIZE);
  }
 
  /**
   * Calculates the current progress and returns a value between 0-1.
   */
  public updateProgress(lerp: number, damp: number): number {
    const progress = dom.getElementScrolledPercent(
      this.element,
      this.topOffset,
      this.bottomOffset,
      true
    );
 
    // Don't apply lerp / damp when we are out of range.
    // The problem is that if you apply damp / lerp out of range,
    // Animations that depend on start (0) and end (1) end up
    // getting slightly delayed causing FOUC.
    if (
      (this.settingsData!.lerpOnlyInRange && progress <= 0) ||
      progress >= 1
    ) {
      this.currentProgress = progress;
    } else {
      // If no lerping, bypass.
      if (lerp === 1 && damp === 1) {
        this.currentProgress = progress;
      } else {
        this.currentProgress = mathf.damp(
          this.currentProgress,
          progress,
          lerp,
          damp
        );
      }
    }
 
    if (this.settingsData!.clamp) {
      this.currentProgress = mathf.clamp01(this.currentProgress);
    }
 
    if (this.settingsData!.debug) {
      console.log(this.currentProgress, this.topOffset, this.bottomOffset);
    }
 
    // Update inviewProgress if provided.
    if (this.settingsData!.inviewProgress) {
      this.settingsData!.inviewProgress.setProgress(this.currentProgress);
    }
 
    // Update progressWatcher if provided.
    if (this.settingsData!.progressWatcher) {
      this.settingsData!.progressWatcher.setProgress(this.currentProgress);
    }
 
    return this.currentProgress;
  }
 
  public updateImmediately() {
    this.updateProgress(1, 1);
  }
 
  /**
   * Takes a css string declaration such as '100px', '100vh' or '100%'
   * and converts that into a relative pixel number.
   * @param cssUnitObject
   */
  protected getPixelValue(cssValue: string): number {
    const unit = cssUnit.parse(cssValue);
    let base = 1;
    if (unit.unit === '%') {
      base = this.element.offsetHeight;
      return base * ((unit.value as number) / 100);
    }
    if (unit.unit === 'vh') {
      base = window.innerHeight;
      return base * ((unit.value as number) / 100);
    }
 
    return base * (unit.value as number);
  }
 
  protected calculateProgressOffsets() {
    this.topOffset = func.setDefault(
      this.getPixelValue(this.settingsData!.top!),
      0
    );
 
    this.bottomOffset = func.setDefault(
      this.getPixelValue(this.settingsData!.bottom!),
      0
    );
    this.height = is.string(this.settingsData!.height)
      ? this.getPixelValue(this.settingsData!.height!)
      : null;
 
    // If height is specified, we basically want to "shorten" the element
    // by the delta amount.
    // Example: el.offsetHeight = 500px, height: 100px.
    //        so bottomOffset should be el.offsetHeight - height = 400px
    requestAnimationFrame(() => {
      if (this.height) {
        this.bottomOffset = -(this.element.offsetHeight - this.height);
      }
    });
  }
 
  /**
   * Gets the current progress value.
   */
  public getProgress(): number {
    return this.currentProgress;
  }
 
  public dispose() {
    this.eventManager.dispose();
    this.raf && this.raf.stop();
    this.domWatcher.dispose();
  }
}