Skip to content

Commit

Permalink
✨ handle tweens update + 🚨 alerting on bad triggerOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
provok-me committed Sep 10, 2021
1 parent 1e32a2a commit 0feb063
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 23 deletions.
19 changes: 15 additions & 4 deletions packages/alice/src/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @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 <[email protected]>
Expand Down
17 changes: 17 additions & 0 deletions packages/alice/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,23 @@ export class Alice {
this._scrollEventHandler();
}

/**
* @description Update tweens positions and specific features.
* @author Alphability <[email protected]>
* @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
Expand Down
28 changes: 18 additions & 10 deletions packages/alice/src/speed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @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 <[email protected]>
Expand Down
33 changes: 24 additions & 9 deletions packages/alice/src/utils/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
},
Expand Down

0 comments on commit 0feb063

Please sign in to comment.