Skip to content

Commit

Permalink
reduceMotion
Browse files Browse the repository at this point in the history
  • Loading branch information
mrienstra committed Jul 12, 2024
1 parent ec7e69e commit 3417ab0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
3 changes: 3 additions & 0 deletions dist/countUp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface CountUpOptions {
onCompleteCallback?: () => any;
onStartCallback?: () => any;
plugin?: CountUpPlugin;
reduceMotion?: boolean | 'auto';
}
export declare interface CountUpPlugin {
render(elem: HTMLElement, formatted: string): void;
Expand Down Expand Up @@ -64,4 +65,6 @@ export declare class CountUp {
private resetDuration;
formatNumber: (num: number) => string;
easeOutExpo: (t: number, b: number, c: number, d: number) => number;
private prefersReducedMotion;
private motionOK;
}
8 changes: 7 additions & 1 deletion dist/countUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var CountUp = /** @class */ (function () {
enableScrollSpy: false,
scrollSpyDelay: 200,
scrollSpyOnce: false,
reduceMotion: 'auto',
};
this.finalEndVal = null; // for smart easing
this.useEasing = true;
Expand Down Expand Up @@ -115,6 +116,11 @@ 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 @@ -212,7 +218,7 @@ var CountUp = /** @class */ (function () {
if (callback) {
this.options.onCompleteCallback = callback;
}
if (this.duration > 0) {
if (this.duration > 0 && this.motionOK()) {
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 3417ab0

Please sign in to comment.