Skip to content

Commit

Permalink
check user preference in constructor only
Browse files Browse the repository at this point in the history
  • Loading branch information
mrienstra committed Jul 17, 2024
1 parent 634ec00 commit afa7755
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 22 deletions.
3 changes: 1 addition & 2 deletions dist/countUp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export declare class CountUp {
private finalEndVal;
private useEasing;
private countDown;
private reduceMotion;
el: HTMLElement | HTMLInputElement;
formattingFn: (num: number) => string;
easingFn?: (t: number, b: number, c: number, d: number) => number;
Expand Down Expand Up @@ -65,6 +66,4 @@ export declare class CountUp {
private resetDuration;
formatNumber: (num: number) => string;
easeOutExpo: (t: number, b: number, c: number, d: number) => number;
private prefersReducedMotion;
private motionOK;
}
10 changes: 4 additions & 6 deletions dist/countUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,6 @@ var CountUp = /** @class */ (function () {
this.easeOutExpo = function (t, b, c, d) {
return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b;
};
this.motionOK = function () {
if (_this.prefersReducedMotion === undefined)
_this.prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
return !_this.options.reduceMotion || (_this.options.reduceMotion === 'auto' && !_this.prefersReducedMotion.matches);
};
this.options = __assign(__assign({}, this.defaults), options);
this.formattingFn = (this.options.formattingFn) ?
this.options.formattingFn : this.formatNumber;
Expand Down Expand Up @@ -158,6 +153,9 @@ var CountUp = /** @class */ (function () {
console.error(this.error, target);
}
}
this.reduceMotion = (this.options.reduceMotion === 'auto') ?
window.matchMedia('(prefers-reduced-motion: reduce)').matches :
this.options.reduceMotion;
}
CountUp.prototype.handleScroll = function (self) {
if (!self || !window || self.once)
Expand Down Expand Up @@ -218,7 +216,7 @@ var CountUp = /** @class */ (function () {
if (callback) {
this.options.onCompleteCallback = callback;
}
if (this.duration > 0 && this.motionOK()) {
if (this.duration > 0 && !this.reduceMotion) {
this.determineDirectionAndSmartEasing();
this.paused = false;
this.rAF = requestAnimationFrame(this.count);
Expand Down
2 changes: 1 addition & 1 deletion dist/countUp.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit afa7755

Please sign in to comment.