Skip to content

Commit 49e46aa

Browse files
authored
Support plugins (#307)
* support plugins * add plugin test * Update README
1 parent c12680a commit 49e46aa

9 files changed

+80
-13
lines changed

README.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Or tinker with CountUp in [Stackblitz](https://stackblitz.com/edit/countup-types
1515
- **[Usage](#usage)**
1616
- **[Including CountUp](#including-countup)**
1717
- **[Contributing](#contributing)**
18+
- **[Creating Animation Plugins](#creating-animation-plugins)**
1819

1920

2021
## CountUp for frameworks and plugins:
@@ -25,7 +26,7 @@ Or tinker with CountUp in [Stackblitz](https://stackblitz.com/edit/countup-types
2526
- **[CountUp.js with Svelte](https://gist.github.com/inorganik/85a66941ab88cc10c5fa5b26aead5f2a)**
2627
- **[CountUp.js Vue component wrapper](https://github.com/xlsdg/vue-countup-v2)**
2728
- **[CountUp.js WordPress Plugin](https://wordpress.org/plugins/countup-js/)**
28-
- **[CountUp.js jQuery Plugin](https://gist.github.com/inorganik/b63dbe5b3810ff2c0175aee4670a4732)**
29+
- **[CountUp.js with jQuery](https://gist.github.com/inorganik/b63dbe5b3810ff2c0175aee4670a4732)**
2930

3031

3132
## Features
@@ -67,6 +68,7 @@ interface CountUpOptions {
6768
scrollSpyDelay?: number; // delay (ms) after target comes into view
6869
scrollSpyOnce?: boolean; // run only once
6970
onCompleteCallback?: () => any; // gets called when animation completes
71+
plugin?: CountUpPlugin; // for alternate animations
7072
}
7173
```
7274

@@ -207,3 +209,30 @@ Before you make a pull request, please be sure to follow these instructions:
207209
4. npm publish
208210
209211
-->
212+
213+
---
214+
215+
## Creating Animation Plugins
216+
217+
CountUp supports plugins as of v2.6.0. Plugins implement their own render method to display each frame's formatted value. A class instance or object can be passed to the `plugin` property of CountUpOptions, and the plugin's render method will be called instead of CountUp's.
218+
219+
```ts
220+
export declare interface CountUpPlugin {
221+
render(elem: HTMLElement, formatted: string): void;
222+
}
223+
```
224+
225+
An example of a plugin:
226+
```ts
227+
export class SomePlugin implements CountUpPlugin {
228+
// ...some properties here
229+
230+
constructor(options: SomePluginOptions) {
231+
// ...setup code here if you need it
232+
}
233+
234+
render(elem: HTMLElement, formatted: string): void {
235+
// render DOM here
236+
}
237+
}
238+
```

dist/countUp.d.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@ export interface CountUpOptions {
1818
scrollSpyDelay?: number;
1919
scrollSpyOnce?: boolean;
2020
onCompleteCallback?: () => any;
21+
plugin?: CountUpPlugin;
22+
}
23+
export declare interface CountUpPlugin {
24+
render(elem: HTMLElement, formatted: string): void;
2125
}
2226
export declare class CountUp {
2327
private endVal;
2428
options?: CountUpOptions;
2529
version: string;
2630
private defaults;
27-
private el;
2831
private rAF;
2932
private startTime;
3033
private remaining;
3134
private finalEndVal;
3235
private useEasing;
3336
private countDown;
37+
el: HTMLElement | HTMLInputElement;
3438
formattingFn: (num: number) => string;
3539
easingFn?: (t: number, b: number, c: number, d: number) => number;
3640
error: string;

dist/countUp.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var CountUp = /** @class */ (function () {
1515
var _this = this;
1616
this.endVal = endVal;
1717
this.options = options;
18-
this.version = '2.5.0';
18+
this.version = '2.6.0';
1919
this.defaults = {
2020
startVal: 0,
2121
decimalPlaces: 0,
@@ -258,9 +258,14 @@ var CountUp = /** @class */ (function () {
258258
this.rAF = requestAnimationFrame(this.count);
259259
};
260260
CountUp.prototype.printValue = function (val) {
261-
var result = this.formattingFn(val);
261+
var _a;
262262
if (!this.el)
263263
return;
264+
var result = this.formattingFn(val);
265+
if ((_a = this.options.plugin) === null || _a === void 0 ? void 0 : _a.render) {
266+
this.options.plugin.render(this.el, result);
267+
return;
268+
}
264269
if (this.el.tagName === 'INPUT') {
265270
var input = this.el;
266271
input.value = result;

dist/countUp.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/countUp.umd.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
var _this = this;
2222
this.endVal = endVal;
2323
this.options = options;
24-
this.version = '2.5.0';
24+
this.version = '2.6.0';
2525
this.defaults = {
2626
startVal: 0,
2727
decimalPlaces: 0,
@@ -264,9 +264,14 @@
264264
this.rAF = requestAnimationFrame(this.count);
265265
};
266266
CountUp.prototype.printValue = function (val) {
267-
var result = this.formattingFn(val);
267+
var _a;
268268
if (!this.el)
269269
return;
270+
var result = this.formattingFn(val);
271+
if ((_a = this.options.plugin) === null || _a === void 0 ? void 0 : _a.render) {
272+
this.options.plugin.render(this.el, result);
273+
return;
274+
}
270275
if (this.el.tagName === 'INPUT') {
271276
var input = this.el;
272277
input.value = result;

0 commit comments

Comments
 (0)