diff --git a/src/HashTarget.ts b/src/Hash.ts similarity index 79% rename from src/HashTarget.ts rename to src/Hash.ts index b21ff122f8cc4db44769bc3c8226ac5e7051dd81..89f0b47036f286d6de18de98b6da61917ff77a99 100644 --- a/src/HashTarget.ts +++ b/src/Hash.ts @@ -1,13 +1,13 @@ /** * Represents valid hash as per https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/hash */ -export class HashTarget +export class Hash { private constructor( private readonly value: string, ) {} - public static fromString(value: string): HashTarget + public static fromString(value: string): Hash { if (value === '' || value === '#') { throw new Error('Hash does not contain any fragment.'); @@ -16,12 +16,12 @@ export class HashTarget return new this(value); } - public getHash(): string + public getValue(): string { return this.value; } - public getRefElement(document: HTMLDocument): HTMLElement + public findTargetElementIn(document: HTMLDocument): HTMLElement { const targetElementId = this.value.substring(1); const targetElement = document.getElementById(targetElementId); diff --git a/src/handlers/linkClickScroll/initializeOnLinkClickScroll.ts b/src/handlers/linkClickScroll/initializeOnLinkClickScroll.ts index 31806d41e21171c13106a35d6e1c6dcdfe75c4bf..441d33f0ff03a8c18132a05d30376226e6a05cfe 100644 --- a/src/handlers/linkClickScroll/initializeOnLinkClickScroll.ts +++ b/src/handlers/linkClickScroll/initializeOnLinkClickScroll.ts @@ -1,6 +1,6 @@ import {scrollToTarget} from '../../scrollers/scrollToTarget'; import {assert} from '../../assert'; -import {HashTarget} from '../../HashTarget'; +import {Hash} from '../../Hash'; export function initializeOnLinkClickScroll(): void { @@ -15,6 +15,6 @@ export function initializeOnLinkClickScroll(): void } event.preventDefault(); - scrollToTarget(HashTarget.fromString(element.hash)); + scrollToTarget(Hash.fromString(element.hash)); }))); } diff --git a/src/handlers/loadScroll/initializeOnLoadScroll.ts b/src/handlers/loadScroll/initializeOnLoadScroll.ts index b4a42ce7d1849faca893b75db29c5f84b8e3a933..844098f90d7aa7a9bcd4d4487edf7b9a74f3ac32 100644 --- a/src/handlers/loadScroll/initializeOnLoadScroll.ts +++ b/src/handlers/loadScroll/initializeOnLoadScroll.ts @@ -1,5 +1,5 @@ import {scrollToTarget} from '../../scrollers/scrollToTarget'; -import {HashTarget} from '../../HashTarget'; +import {Hash} from '../../Hash'; import {assert} from '../../assert'; /** @@ -17,12 +17,12 @@ export function initializeOnLoadScroll(): void return; } - let hashTarget: HashTarget|null = null; + let targetHash: Hash|null = null; const start = performance.now(); document.addEventListener('DOMContentLoaded', () => { try { - hashTarget = HashTarget.fromString(hash); + targetHash = Hash.fromString(hash); } catch (e) {} // when URL contains hash which has no corresponding DOM element, just ignore it }); @@ -31,7 +31,7 @@ export function initializeOnLoadScroll(): void * all styles are loaded and offsets are computed correctly - so the scroll will be computed correctly. */ window.addEventListener('load', () => { - if (hashTarget === null) { // if target does not exist + if (targetHash === null) { // if hash does not exist return; } @@ -46,6 +46,6 @@ export function initializeOnLoadScroll(): void window.scroll({top: 0, left: 0}); // Then, scroll down to it smoothly. - scrollToTarget(hashTarget); + scrollToTarget(targetHash); }); } diff --git a/src/index.ts b/src/index.ts index 2c0337517a877842ef2954f54c602ec4401cd99a..18e51cecd6454ff09e38413b3ec9c2da5cb8a7bd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import {initializeOnLinkClickScroll} from './handlers/linkClickScroll/initialize import {scrollToElement} from './scrollers/scrollToElement'; import {scrollToOffset} from './scrollers/scrollToOffset'; import {scrollToTarget} from './scrollers/scrollToTarget'; -import {HashTarget} from './HashTarget'; +import {Hash} from './Hash'; // bind automatically on library import @@ -23,7 +23,7 @@ function handleOnLinkClickScroll(): void } export { - HashTarget, + Hash, handleOnLoadScroll, handleOnLinkClickScroll, scrollToElement, diff --git a/src/scrollers/scrollToTarget.ts b/src/scrollers/scrollToTarget.ts index 5906c2b2abf33685b7951062a9bb9e09715c3c05..ea5882dbacfc5d740f6c8af0eb8a9a2fdb20f5c5 100644 --- a/src/scrollers/scrollToTarget.ts +++ b/src/scrollers/scrollToTarget.ts @@ -1,18 +1,18 @@ -import {HashTarget} from '../HashTarget'; +import {Hash} from '../Hash'; import {scrollToElement} from './scrollToElement'; import {assert} from '../assert'; -export function scrollToTarget(hashTarget: HashTarget|string, onScrollFinishedCallback?: () => void): void +export function scrollToTarget(targetHash: Hash|string, onScrollFinishedCallback?: () => void): void { - if (typeof hashTarget === 'string') { - hashTarget = HashTarget.fromString(hashTarget); + if (typeof targetHash === 'string') { + targetHash = Hash.fromString(targetHash); } scrollToElement( - hashTarget.getRefElement(document), + targetHash.findTargetElementIn(document), () => { - assert(hashTarget instanceof HashTarget); - window.location.hash = hashTarget.getHash(); + assert(targetHash instanceof Hash); + window.location.hash = targetHash.getValue(); onScrollFinishedCallback !== undefined && onScrollFinishedCallback(); }, );