From 0feb063fd8a9b24266445696fd1f5137deeeb12f Mon Sep 17 00:00:00 2001 From: Alphability Date: Fri, 10 Sep 2021 17:42:43 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20handle=20tweens=20update=20+=20?= =?UTF-8?q?=F0=9F=9A=A8=20alerting=20on=20bad=20triggerOffset?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/alice/src/detect.ts | 19 ++++++++++++++---- packages/alice/src/index.ts | 17 ++++++++++++++++ packages/alice/src/speed.ts | 28 +++++++++++++++++---------- packages/alice/src/utils/view.ts | 33 +++++++++++++++++++++++--------- 4 files changed, 74 insertions(+), 23 deletions(-) diff --git a/packages/alice/src/detect.ts b/packages/alice/src/detect.ts index 9f6f5c9..f2f3e5a 100644 --- a/packages/alice/src/detect.ts +++ b/packages/alice/src/detect.ts @@ -167,20 +167,31 @@ export class Detect extends Tween { return; } - // Handling all tweens global reset during resize (debounced by using static method). - Tween._handleResize(); + // Update tweens positions and specific features. + this.update(); }, outerHeight: (val: number) => { if (!val) { return; } - // Handling all tweens global reset during resize (debounced by using static method). - Tween._handleResize(); + // Update tweens positions and specific features. + this.update(); }, }); } + /** + * @description Update tweens positions and specific features. + * @author Alphability + * @memberof Detect + */ + + public update(): void { + // Handling all tweens global reset during resize (debounced by using static method). + Tween._handleResize(); + } + /** * @description Destroying the tweens. * @author Alphability diff --git a/packages/alice/src/index.ts b/packages/alice/src/index.ts index 2e68c33..4fc62fe 100644 --- a/packages/alice/src/index.ts +++ b/packages/alice/src/index.ts @@ -251,6 +251,23 @@ export class Alice { this._scrollEventHandler(); } + /** + * @description Update tweens positions and specific features. + * @author Alphability + * @returns {void} + * @memberof Alice + */ + + public update(): void { + // We can't boot scroll values if Alice has not been initialized. + if (!this._isInitialized) { + return; + } + + Alice._detect.update(); + Alice._speed.update(); + } + /** * @description Reactive scroll properties object's getter. * @readonly diff --git a/packages/alice/src/speed.ts b/packages/alice/src/speed.ts index c9a839d..f2dcb31 100644 --- a/packages/alice/src/speed.ts +++ b/packages/alice/src/speed.ts @@ -285,26 +285,34 @@ export class Speed extends Tween { return; } - // Clear transforms before cleaning tweens - this._handleSpeedResize(); - - // Handling all tweens global reset during resize (debounced by using static method). - Tween._handleResize(); + // Update tweens positions and specific features. + this.update(); }, outerHeight: (val) => { if (!val) { return; } - // Clear transforms before cleaning tweens - this._handleSpeedResize(); - - // Handling all tweens global reset during resize (debounced by using static method). - Tween._handleResize(); + // Update tweens positions and specific features. + this.update(); }, }); } + /** + * @description Update tweens positions and specific features. + * @author Alphability + * @memberof Speed + */ + + public update(): void { + // Clear transforms before cleaning tweens + this._handleSpeedResize(); + + // Handling all tweens global reset during resize (debounced by using static method). + Tween._handleResize(); + } + /** * @description Destroying the tweens. * @author Alphability diff --git a/packages/alice/src/utils/view.ts b/packages/alice/src/utils/view.ts index a333ac0..4f09692 100644 --- a/packages/alice/src/utils/view.ts +++ b/packages/alice/src/utils/view.ts @@ -45,16 +45,31 @@ export const getTriggerOffset = ( bottom: triggerOffsetBottom, } = inputOffset.reduce( (acc, offset, index) => { - let parsedOffset = null; + let parsedOffset = 0; const key = index === 0 ? 'top' : 'bottom'; - if (typeof offset === 'number' && !isNaN(offset)) { - parsedOffset = offset; - } else if (typeof offset === 'string' && offset.endsWith('vh')) { - parsedOffset = - parseFloat(offset.replace('vh', '')) * (window.innerHeight / 100); - } else if (typeof offset === 'string' && offset.endsWith('%')) { - parsedOffset = - parseInt(offset.replace('%', ''), 10) * (boundings.height / 100); + + try { + if (typeof offset === 'number' && !isNaN(offset)) { + parsedOffset = offset; + } else if ( + typeof offset === 'string' && + offset.match(/^[0-9]{1,}vh$/g) + ) { + parsedOffset = + parseFloat(offset.replace('vh', '')) * (window.innerHeight / 100); + } else if ( + typeof offset === 'string' && + offset.match(/^[0-9]{1,}%$/g) + ) { + parsedOffset = + parseInt(offset.replace('%', ''), 10) * (boundings.height / 100); + } else { + throw new Error( + 'There is a problem with the syntax of one of your triggerOffset option.' + ); + } + } catch (error) { + console.error(error); } return { ...acc, [key]: parsedOffset }; },