ScrollManager
class exported by @mwillbanks/tuil-scroll.
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
| Member | Type | Required | Description | Related types |
|---|---|---|---|---|
#areas | Map<string, ScrollAreaState> | Yes | The #areas member uses the Map<string, ScrollAreaState> contract. | ScrollAreaState |
#restoration | Map<string, ScrollPosition> | Yes | The #restoration member uses the Map<string, ScrollPosition> contract. | ScrollPosition |
register | (area: ScrollAreaState) => () => void | Yes | The register member uses the (area: ScrollAreaState) => () => void contract. | ScrollAreaState |
get | (id: string) => ScrollAreaState | undefined | Yes | The get member uses the (id: string) => ScrollAreaState | undefined contract. | ScrollAreaState |
routeWheel | (id: string, deltaX: number, deltaY: number) => ScrollSnapshot | undefined | Yes | The routeWheel member uses the (id: string, deltaX: number, deltaY: number) => ScrollSnapshot | undefined contract. | ScrollSnapshot |
focus | (id: string, bounds: Parameters<ScrollAreaState["scrollIntoView"]>[0]) => ScrollSnapshot | undefined | Yes | The focus member uses the (id: string, bounds: Parameters<ScrollAreaState["scrollIntoView"]>[0]) => ScrollSnapshot | undefined contract. | ScrollAreaState, ScrollSnapshot |
snapshots | () => readonly ScrollSnapshot[] | Yes | The 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.
Related types
Source
View the secondary source reference