All files / src/dom inview.ts

0% Statements 0/117
0% Branches 0/56
0% Functions 0/31
0% Lines 0/115

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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
import {DomWatcher} from '../dom/dom-watcher';
import * as mathf from '../mathf/mathf';
import {Raf} from '../raf/raf';
import {
  type EventCallback,
  type EventDispatcher,
  EventManager,
} from '../ui/events';
 
export enum InviewEvents {
  IN = 'in',
  IN_ONCE = 'in-once',
  OUT = 'out',
}
 
interface InviewClassNames {
  READY?: string;
  IN?: string;
  IN_ONCE?: string;
  IN_FOLD?: string;
  DOWN?: string;
  UP?: string;
  OUT?: string;
}
 
const InviewDefaultClassNames: InviewClassNames = {
  READY: 'ready',
  IN: 'in',
  IN_ONCE: 'in-once',
  IN_FOLD: 'in-fold',
  DOWN: 'down',
  UP: 'up',
  OUT: 'out',
};
 
export interface InviewConfig {
  /**
   * The root element to track inview status.
   */
  element: HTMLElement;
 
  /**
   * Defines the baseline of the element for inview..
   * The element baseline is the location in which we should use to
   * check the current element position.  Since we want to check
   * where in a viewport an element is, we need to know what point to use
   * in the element.  Should we use the top (0), middle (0.5) or bottom of the
   * element.
   */
  elementBaseline?: number;
 
  /**
   * Defines the viewport offset. This is a number between 0-1 where 0 is
   * the bottom of the screen and 1 is the top of the screen.  Defaults to 0.
   */
  viewportOffset?: number;
 
  /**
   * Defines the baseline of the element for triggering outview.  This value
   * defaults to to 1 (bottom of element) unless specified.
   * See elementBaseline option to understand what this means.
   */
  outviewElementBaseline?: number;
 
  /**
   * Defines the viewport offset. This is a number between 0-1 where 0 is
   * the bottom of the screen and 1 is the top of the screen.  Defaults to 1.
   * This value can be ignored if using outviewOnlyOnElementExit or downOnlyMode.
   */
  outviewViewportOffset?: number;
 
  /**
   * Inview ClassNames. An optional object defining the inview class names
   * that gets merge with InviewClassNames.
   */
  inviewClassNames?: InviewClassNames;
 
  /**
   * Optionally pass child selectors to add inview states to children.
   *
   * Example:
   * childSelector: [add-inview]
   *
   * Now any element with the attribute add-inview within the main element
   * will also receive inview class.
   *
   * Child selelectors also received 'inview-index' such as 'inview-index="0"
   * etc, allows you to use it to stagger inview.
   */
  childSelector?: string;
 
  /**
   * An additional options to pass to raf culling.  In most cases you can ignore
   * this but you can override the internal culling by passing a different
   * setting to fine tune optimization.
   *
   * Expects intersection observer options.  Defaults to:
   *
   * ```
   *    {
   *      rootMargin: '500px 0px 500px 0px'
   *    }
   * ```
   *
   */
  evIntersectionObserverOptions?: Object;
 
  /**
   * A flag in which if set to true will slightly modify the behavior
   * of inview.  Once inview is fired, it will stay in that state until
   * the element is completely out of view.
   */
  outviewOnlyOnElementExit?: boolean;
 
  /**
   * A flag that sets this inview to down only mode. See below for more.
   */
  downOnlyMode?: boolean;
}
 
/**
 * Implements a basic type 1 inview.
 *
 * ```
 *        new Inview({
 *           element: document.getElementById('test2'),
 *           elementBaseline: 0,
 *           viewportOffset: 0.2,
 *           waitForOutOfViewToRefireInview: false,
 *           downOnlyMode: false,
 *           // Optionally override default class names.
 *           inviewClassNames: {
 *             IN: 'mymodule--in'
 *           }
 *       });
 * ```
 *
 *
 * Type 1 inview:
 * - will add an '.in' class to an element when the element is at
 *   at certain percentage of the viewport. (viewport offset) or
 *   when an X percentage of the element is visible or BOTH!.  See below.
 * - will add an '.in-once' when inview happens the first time.
 * - will immediately remove the '.in' class when the element goes out of
 *   view
 * - will immediately add '.out' class when the element goes out of
 *   view
 * - will add 'up' and 'down' class to the element based on the scroll direction
 *   allowing you to add directional inview.
 *
 * - waitForOutOfViewToRefireInview, downOnlyMode are options to modify the
 *   inview triggers.
 *
 *
 * # Inview Logic
 * The "logic" of how inview is calculated is important.  Two factors
 * are taken into consideration.
 *
 * The first is the elementBaseline.
 * The elementBaseline defaults to the very top of the element as that is the
 * most common case.  However, this is the line or point in the element in which the
 * trigger point is evaluated.  You can set it to a given percentage of
 * the element (like the very bottom of the element instead of the top).
 *
 * The second is the viewport offset.
 * The viewport offset ranges from 0-1 in which 0 is the bottom of the viewport window
 * and the 1 is the top.
 *
 *
 * Here are some examples of different settings.
 *
 * elementBaseline - 0 (top), viewportOffset - 0.2
 * => inview should happen when the top of the element crosses the bottom 20% of the window.
 *
 * elementBaseline - 0 (top), viewportOffset - 0.5
 * => inview should happen when the top of the element crosses the middle of the window.
 *
 * elementBaseline - 1 (bottom), viewportOffset - 0.5
 * => inview should happen when the bottom of the element crosses the middle of the window.
 *
 * elementBaseline - 0.2, viewportOffset - 0
 * => inview should happen when 20% of the element is visible.
 *
 * elementBaseline - 0.5, viewportOffset - 0
 * => inview should happen when 50% of the element is visible.
 *
 * elementBaseline - 0.5, viewportOffset - 0.5
 * => inview should happen when 50% of the element cross the middle of the screen.
 *
 * elementBaseline - 0, viewportOffset - 1
 * => inview should happen when then top of the element 0%, hits the top of the screen.
 *
 * elementBaseline - 0, viewportOffset - 0.2, outviewViewportPortOffset - 0.8, outviewElementBaseline - 1
 * => inview should happen when the top of the element crosses the bottom 20% of the viewport.
 * => outview should happen when the bottom of the element crosses the top 80% of the viewport.
 *
 * elementBaseline - 0, viewportOffset - 0, outviewViewportPortOffset - 0.8, outviewElementBaseline - 0
 * => inview should happen when the top of the element crosses the bottom 20% of the viewport.
 * => outview should happen when the top of the element crosses the top 80% of the viewport.
 *
 *
 * # FOUC
 * While inview is booting up, there is a split second where it needs to evaluate the
 * inview state.  When inview is ready, the element will receive '.ready' class.
 *
 * You can do something like:
 *
 * ```
 * .myelement
 *    visibility: hidden
 * .myelement.ready
 *    visibility: visible
 *
 * ```
 *
 *
 * # outviewOnlyOnElementExit Mode
 * This changes the behavior of inview a bit.
 *
 * You can set it so that the outview doesn't fire based on the elementBaseline
 * but instead, when the full elmement, top and bottom are out of view.
 *
 * In short, once inview is fired, the element will stay in an inview state
 * until the element is compeltely out off view.
 *
 *
 * To clarify:
 * When scrolling back and up, based on your inview trigger point, the
 * user might see the element go from an instate to outstate back and forth.
 * For example, let's say you trigger at the top of the element (elementBaseline 0)
 * and 0.2 viewport offset.  As the user scrolls down,
 * the top of the element hits the bottom 20%
 * of the viewport, the element becomes in an inview state.  If the user scrolls back
 * up immediately, and the element is at 0.19, the element goes back to outview.
 *
 * This might be undesired.    By setting this option, it will fire outview until the
 * element has completely exited and therefore, preventing refiring of inview multiple
 * times while the element is inview.
 *
 * # downOnlyMode Mode
 * Downonly modes assumes your primary experience is scrolling down.  So
 * it will add an inview, when the element crosses the inview threshold when
 * scrolling down and then "KEEP" that inview attached.  Outview is ONLY
 * fired when the element is completley out of view AND below the viewport.
 *
 *
 *
 */
export class Inview implements EventDispatcher {
  private static gatherTargetElements(config: InviewConfig): HTMLElement[] {
    const targetElements = [config.element];
    if (config.childSelector) {
      Array.from(config.element.querySelectorAll(config.childSelector)).forEach(
        (el) => targetElements.push(<HTMLElement>el)
      );
    }
    return targetElements;
  }
 
  private static assignDefaultsToConfig(config: InviewConfig): InviewConfig {
    return Object.assign(
      {
        elementBaseline: 0,
        viewportOffset: 0,
        outviewOnlyOnElementExit: false,
        downOnlyMode: false,
        outviewElementBaseline: 1,
        outviewViewportOffset: 1,
      },
      config
    );
  }
  private readonly raf: Raf;
  private readonly readWrite: Raf;
  private readonly watcher: DomWatcher;
  private readonly config: InviewConfig;
 
  /**
   * The last known scrollY
   */
  private scrollY: number;
 
  /**
   * Last known scroll direction. 1 down, -1 up, 0 no direction.
   */
  private scrollDirection = 0;
 
  /**
   * A flag to keep track of whether the element was inview atleast once.
   */
  private inOnce = false;
 
  /**
   * A flag to keep track of in or out state.
   */
  private isInState = false;
 
  /**
   * The list of target elements to add inview to.
   */
  private readonly targetElements: HTMLElement[];
 
  /**
   * A list of inview classnames.
   */
  private readonly inviewClassNames: InviewClassNames;
 
  /**
   * A caching flag. Set on events that would change the inview status of an
   * element, cleared when a RAF loop has run.
   * This will improve performance by culling RAF runs before work is done.
   */
  private shouldRun = false;
 
  private eventManager: EventManager;
 
  constructor(config: InviewConfig) {
    this.eventManager = new EventManager();
 
    // Establish defaults on the config as needed and then handle errors for bad
    // configs.
    this.config = Inview.assignDefaultsToConfig(config);
    if (!this.config.element) {
      throw new Error('No element is defined for inview');
    }
 
    // Assign property values
    this.raf = new Raf(() => this.onRaf());
    this.readWrite = new Raf();
    this.watcher = new DomWatcher();
    this.inviewClassNames = Object.assign(
      {},
      InviewDefaultClassNames,
      config.inviewClassNames || {}
    );
    this.scrollY = window.scrollY;
    this.targetElements = Inview.gatherTargetElements(this.config);
 
    // Initialize the class and properties
    this.init();
  }
 
  on(event: string, callback: EventCallback) {
    this.eventManager.on(event, callback);
  }
 
  off(event: string, callback: EventCallback) {
    this.eventManager.on(event, callback);
  }
 
  runInviewState(force?: boolean) {
    if (this.isInState && !force) {
      return;
    }
    this.readWrite.write(() => {
      this.targetElements.forEach((el) => {
        el.classList.remove(this.inviewClassNames.OUT!);
        el.classList.add(this.inviewClassNames.IN!);
 
        if (!this.inOnce) {
          el.classList.add(this.inviewClassNames.IN_ONCE!);
          this.eventManager.dispatch(InviewEvents.IN_ONCE);
          this.inOnce = true;
 
          if (window.scrollY === 0) {
            el.classList.add(this.inviewClassNames.IN_FOLD!);
          }
        }
 
        const isScrollingUp = this.scrollDirection === -1;
        el.classList.remove(
          isScrollingUp
            ? this.inviewClassNames.DOWN!
            : this.inviewClassNames.UP!
        );
        el.classList.add(
          isScrollingUp
            ? this.inviewClassNames.UP!
            : this.inviewClassNames.DOWN!
        );
        this.isInState = true;
        this.eventManager.dispatch(InviewEvents.IN);
      });
    });
  }
 
  runOutviewState(force?: boolean) {
    if (!this.isInState && !force) {
      return;
    }
    this.readWrite.write(() => {
      this.targetElements.forEach((el) => {
        el.classList.add(this.inviewClassNames.OUT!);
        el.classList.remove(this.inviewClassNames.IN!);
        el.classList.remove(this.inviewClassNames.UP!);
        el.classList.remove(this.inviewClassNames.DOWN!);
        el.classList.add(
          this.scrollDirection === -1
            ? this.inviewClassNames.UP!
            : this.inviewClassNames.DOWN!
        );
        this.isInState = false;
        this.eventManager.dispatch(InviewEvents.OUT);
      });
    });
  }
 
  dispose(): void {
    this.raf.dispose();
    this.raf.dispose();
    this.watcher.dispose();
  }
 
  private init(): void {
    this.initDomWatcher();
    this.addInviewIndicesToTargetElements();
    this.readyTargetElements();
    this.initInview();
 
    // Force update the state.
    this.onRaf(true);
  }
 
  private initDomWatcher(): void {
    this.watcher.add({
      element: window,
      on: 'scroll',
      callback: () => this.onWindowScroll(),
      eventOptions: {passive: true},
    });
    this.watcher.add({
      element: window,
      on: 'smartResize',
      callback: () => this.onWindowScroll(),
      eventOptions: {passive: true},
    });
  }
 
  private addInviewIndicesToTargetElements(): void {
    // Do nothing if there is only a single target element
    if (this.targetElements.length <= 1) {
      return;
    }
    this.readWrite.write(() => {
      this.targetElements.forEach((target, i) => {
        target.setAttribute('inview-index', `${i}`);
      });
    });
  }
 
  private readyTargetElements(): void {
    this.readWrite.write(() => {
      this.targetElements.forEach((target: HTMLElement) => {
        target.classList.add(this.inviewClassNames.READY!);
      });
    });
  }
 
  private initInview(): void {
    const intersectionObserverOptions = this.config
      .evIntersectionObserverOptions || {rootMargin: '100px 0px 100px 0px'};
    const inviewPromise = this.raf.runWhenElementIsInview(
      this.config.element,
      intersectionObserverOptions
    );
    inviewPromise.then(() => this.raf.start());
  }
 
  private onWindowScroll(): void {
    this.raf.read(() => {
      // Calculate the scroll direction.
      const scrollY = window.scrollY;
      this.scrollDirection = mathf.direction(this.scrollY, scrollY);
      this.scrollY = scrollY;
      this.shouldRun = true;
    });
  }
 
  /**
   * Request Animation Frame handle.  This runs only when the element is
   * in the viewport.
   */
  private onRaf(force?: boolean): void {
    if (!force && !this.shouldRun) {
      // Flag was not set, skip this iteration as there is not reason to expect
      // any changes.
      return;
    }
    this.shouldRun = false; // Clear the flag as the iteration is set to run
    // Figure out how much of this element is visible.
    this.readWrite.read(() => {
      // Since generally, since we think in terms of scrolling down, 0 - 1 would
      // be represented as:
      // 1 ---> top of screen
      // 0.5 --> middle of screen
      // 0 --> bottom of screen
      //
      // Therefore, progress is represented as 0-1 where it goes from the bottom
      // of the screen to the top.
      //
      // Additionally, we need to know, what point in the element should be used
      // to see where the element resides.  We could use the top, center or
      // bottom.
      //
      // The elementBaseline is used to factor this in.  The default state is
      // calculated from the top of the element.
      const wh = window.innerHeight;
      const box = this.config.element.getBoundingClientRect();
      const elementBaseline =
        box.top + this.config.elementBaseline! * box.height;
 
      // This is the percent of where element baseline is.
      // So 0 would mean the elementbaseline is at the bottom of the viewport.
      // 1 would mean elementBaseline is at the top of the viewport.
      // A value less than viewport offset would mean that the element is above
      // the viewport == outview.
      const inPercent = 1 - mathf.inverseLerp(0, wh, elementBaseline, true);
 
      // This is the percent of where the BOTTOM of the element is in the
      // viewport.
      // We want to use this to valuate whether the element is out of view.
      // A value greater than 1 would mean that the element is above the
      // viewport == outview.
      const outPercent =
        1 -
        mathf.inverseLerp(
          0,
          wh,
          box.top + box.height * this.config.outviewElementBaseline!,
          true
        );
 
      if (this.config.outviewOnlyOnElementExit) {
        // This is the percent where the TOP of the element is in the viewport.
        const topPercent = 1 - mathf.inverseLerp(0, wh, box.top, true);
        // AKA
        const bottomPercent = outPercent;
        const completelyOutOfView =
          !mathf.isBetween(topPercent, 0, 1) &&
          !mathf.isBetween(bottomPercent, 0, 1);
        if (inPercent < this.config.viewportOffset! || outPercent >= 1) {
          if (completelyOutOfView) {
            this.runOutviewState(force);
          }
        } else {
          this.runInviewState(force);
        }
      } else if (this.config.downOnlyMode) {
        // This is the percent where the TOP of the element is in the viewport.
        const topPercent = 1 - mathf.inverseLerp(0, wh, box.top, true);
        const bottomPercent = outPercent;
        // Down only mode.
        const topOfElementIsBelowViewport = topPercent < 0;
        const bottomOfElementIsAboveViewport = bottomPercent >= 1;
        if (inPercent < this.config.viewportOffset! || outPercent >= 1) {
          if (topOfElementIsBelowViewport) {
            this.runOutviewState(force);
          }
 
          // If the bottom of the element is above the viewport, we should
          // ensure it is in the instate.
          if (bottomOfElementIsAboveViewport) {
            this.runInviewState(force);
          }
        } else {
          this.runInviewState(force);
        }
      } else {
        // NORMAL INVIEW
        // The outview conditions are in the outpercent (bottom of the element)
        // is greater than 1
        // or the inpercent (the element baseline) is below 0 under the screen.
        if (
          inPercent < this.config.viewportOffset! ||
          outPercent >= this.config.outviewViewportOffset!
        ) {
          this.runOutviewState(force);
        } else {
          this.runInviewState(force);
        }
      }
    });
  }
}