tuil
ReferencePackages@mwillbanks/tuil-scrollAPI

ScrollManager

class exported by @mwillbanks/tuil-scroll.

View rawEdit

class

Public class exported by @mwillbanks/tuil-scroll.

export class ScrollManager {
  readonly #areas = new Map<string, ScrollAreaState>();
  readonly #restoration = new Map<string, ScrollPosition>();

  register(area: ScrollAreaState): () => void {
    if (this.#areas.has(area.id))
      throw new Error(`Scroll area "${area.id}" is already registered`);
    if (area.parentId && !this.#areas.has(area.parentId)) {
      throw new Error(
        `Parent scroll area "${area.parentId}" is not registered`,
      );
    }
    this.#areas.set(area.id, area);
    const restored = this.#restoration.get(area.id);
    if (restored) area.scrollTo(restored);
    return () => {
      this.#restoration.set(area.id, area.snapshot().position);
      this.#areas.delete(area.id);
    };
  }

  get(id: string): ScrollAreaState | undefined {
    return this.#areas.get(id);
  }

  routeWheel(
    id: string,
    deltaX: number,
    deltaY: number,
  ): ScrollSnapshot | undefined {
    let current = this.#areas.get(id);
    while (current) {
      const before = current.snapshot();
      const after = current.wheel(deltaX, deltaY);
      if (
        before.position.x !== after.position.x ||
        before.position.y !== after.position.y
      )
        return after;
      current = current.parentId
        ? this.#areas.get(current.parentId)
        : undefined;
    }
    return undefined;
  }

  focus(
    id: string,
    bounds: Parameters<ScrollAreaState["scrollIntoView"]>[0],
  ): ScrollSnapshot | undefined {
    const area = this.#areas.get(id);
    return area?.followFocus ? area.scrollIntoView(bounds) : area?.snapshot();
  }

  snapshots(): readonly ScrollSnapshot[] {
    return Object.freeze(
      [...this.#areas.values()].map((area) => area.snapshot()),
    );
  }
}

Members

MemberTypeRequiredDescriptionRelated types
#areasMap<string, ScrollAreaState>YesThe #areas member uses the Map<string, ScrollAreaState> contract.ScrollAreaState
#restorationMap<string, ScrollPosition>YesThe #restoration member uses the Map<string, ScrollPosition> contract.ScrollPosition
register(area: ScrollAreaState) => () => voidYesThe register member uses the (area: ScrollAreaState) => () => void contract.ScrollAreaState
get(id: string) => ScrollAreaState | undefinedYesThe get member uses the (id: string) => ScrollAreaState | undefined contract.ScrollAreaState
routeWheel(id: string, deltaX: number, deltaY: number) => ScrollSnapshot | undefinedYesThe routeWheel member uses the (id: string, deltaX: number, deltaY: number) => ScrollSnapshot | undefined contract.ScrollSnapshot
focus(id: string, bounds: Parameters<ScrollAreaState["scrollIntoView"]>[0]) => ScrollSnapshot | undefinedYesThe focus member uses the (id: string, bounds: Parameters<ScrollAreaState["scrollIntoView"]>[0]) => ScrollSnapshot | undefined contract.ScrollAreaState, ScrollSnapshot
snapshots() => readonly ScrollSnapshot[]YesThe snapshots member uses the () => readonly ScrollSnapshot[] contract.ScrollSnapshot

Parameters

This declaration has no public members.

Returns

This declaration does not return a value.

Throws

No thrown errors are documented for this declaration.

Source

View the secondary source reference

Package

@mwillbanks/tuil-scroll

On this page