Skip to content

Commit 8895e9a

Browse files
committed
documentation has been created
1 parent a4a27f1 commit 8895e9a

File tree

7 files changed

+208
-31
lines changed

7 files changed

+208
-31
lines changed

README.md

Lines changed: 104 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,125 @@
1-
# vue3-number-counter
1+
# Vue 3 Number Counter
22

3-
This template should help get you started developing with Vue 3 in Vite.
3+
**Vue 3 animated number counter directive**
44

5-
## Recommended IDE Setup
5+
<p align="center">
66

7-
[VSCode](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar) (and disable Vetur) + [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.vscode-typescript-vue-plugin).
7+
[![vue version](https://img.shields.io/npm/v/vue3-number-counter.svg)](https://www.npmjs.com/package/vue3-number-counter)
8+
[![vue version](https://img.shields.io/badge/vue-3.2-brightgreen.svg)](https://www.npmjs.com/package/vue3-number-counter)
89

9-
## Type Support for `.vue` Imports in TS
10+
</p>
1011

11-
TypeScript cannot handle type information for `.vue` imports by default, so we replace the `tsc` CLI with `vue-tsc` for type checking. In editors, we need [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.vscode-typescript-vue-plugin) to make the TypeScript language service aware of `.vue` types.
1212

13-
If the standalone TypeScript plugin doesn't feel fast enough to you, Volar has also implemented a [Take Over Mode](https://github.com/johnsoncodehk/volar/discussions/471#discussioncomment-1361669) that is more performant. You can enable it by the following steps:
13+
## Installing
1414

15-
1. Disable the built-in TypeScript Extension
16-
1) Run `Extensions: Show Built-in Extensions` from VSCode's command palette
17-
2) Find `TypeScript and JavaScript Language Features`, right click and select `Disable (Workspace)`
18-
2. Reload the VSCode window by running `Developer: Reload Window` from the command palette.
15+
### Package manager
1916

20-
## Customize configuration
17+
Using npm:
2118

22-
See [Vite Configuration Reference](https://vitejs.dev/config/).
19+
```bash
20+
npm i vue3-number-counter
21+
```
22+
23+
Then, import and register the component:
24+
25+
26+
## Global Registration
27+
28+
main.js
29+
```
30+
import numberCounter from "vue3-number-counter";
31+
app.use(numberCounter);
32+
```
33+
34+
## Local Registration
35+
36+
* Composition API
2337

24-
## Project Setup
38+
```
39+
<script setup>
40+
import { numberCounter as vNumberCounter } from "vue3-number-counter";
41+
</script>
42+
```
2543

26-
```sh
27-
npm install
44+
* Options API
45+
```
46+
<script>
47+
import { numberCounter } from "vue3-number-counter";
48+
49+
export default {
50+
directives: {
51+
"number-counter": numberCounter
52+
},
53+
}
54+
</script>
2855
```
2956

30-
### Compile and Hot-Reload for Development
3157

32-
```sh
33-
npm run dev
58+
## Usage
59+
* Example 1
60+
```
61+
import { numberCounter } from "vue3-number-counter";
62+
63+
export default {
64+
directives: {
65+
"number-counter": numberCounter
66+
},
67+
data () {
68+
return {
69+
counter: {
70+
countFrom: 0,
71+
countTo: 100,
72+
duration: 1000,
73+
text: "% completed"
74+
}
75+
}
76+
}
77+
}
78+
79+
80+
<template>
81+
<div v-number-counter="counter">0% completed</div>
82+
</template>
3483
```
3584

36-
### Type-Check, Compile and Minify for Production
85+
* Example 2
3786

38-
```sh
39-
npm run build
87+
```
88+
<script setup>
89+
import { numberCounter as vNumberCounter } from "vue3-number-counter";
90+
91+
const counter = {
92+
countFrom: 0,
93+
countTo: 100,
94+
duration: 1000,
95+
text: "% completed",
96+
};
97+
</script>
98+
99+
<template>
100+
<div v-number-counter="counter">0% completed</div>
101+
</template>
40102
```
41103

42-
### Lint with [ESLint](https://eslint.org/)
104+
* Example 3
43105

44-
```sh
45-
npm run lint
46106
```
107+
const counter = {
108+
countFrom: 0,
109+
countTo: 100,
110+
duration: 1000,
111+
text: "% completed",
112+
};
113+
</script>
114+
115+
<template>
116+
<div v-number-counter="counter">0% completed</div>
117+
</template>
118+
```
119+
120+
121+
# Demo
122+
* https://vue-number-counter.netlify.app/
123+
124+
## License
125+
[![License](https://img.shields.io/badge/LICENSE-GPL--3.0-orange)](https://github.com/mustafadalga/vue3-number-counter/blob/main/LICENSE)

package.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
{
22
"name": "vue3-number-counter",
3-
"version": "0.0.0",
3+
"version": "0.0.1",
4+
"type": "module",
5+
"exports": {
6+
".": "./dist/index.js"
7+
},
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/mustafadalga/vue3-number-counter"
11+
},
12+
"files": [
13+
"dist"
14+
],
15+
"license": "GNU General Public License v3.0",
16+
"keywords": [
17+
"number-counter",
18+
"vue-number-counter",
19+
"vue3-number-counter",
20+
"animated-number-counter",
21+
"counter",
22+
"animated-number-counters"
23+
],
424
"scripts": {
525
"dev": "vite",
626
"build": "run-p type-check build-only",

src/App.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<script setup lang="ts">
2-
32
</script>
43

54
<template>
6-
75
</template>

src/directives/index.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
interface IBinding {
2+
value: {
3+
countFrom: number,
4+
countTo: number,
5+
duration: number,
6+
text?: number
7+
}
8+
}
9+
10+
interface IHTMLElement extends HTMLElement {
11+
animationFrameID: number
12+
}
13+
14+
15+
const vNumberCounter = {
16+
mounted(element: IHTMLElement, binding: IBinding) {
17+
const {countFrom, countTo, duration, text} = binding.value;
18+
19+
if (countTo <= 0) return;
20+
21+
const animate = () => {
22+
let startTime: number;
23+
24+
const step = (currentTime: number) => {
25+
if (!startTime) {
26+
startTime = currentTime;
27+
}
28+
29+
const progress = Math.min((currentTime - startTime) / duration, 1);
30+
const currentNumber = Math.floor(progress * (countTo - countFrom));
31+
32+
element.textContent = `${currentNumber + countFrom} ${text || ""}`;
33+
34+
if (progress < 1) {
35+
element.animationFrameID = window.requestAnimationFrame(step);
36+
} else {
37+
window.cancelAnimationFrame(element.animationFrameID);
38+
}
39+
};
40+
41+
window.requestAnimationFrame(step);
42+
};
43+
44+
animate();
45+
},
46+
unmounted(element: IHTMLElement) {
47+
window.cancelAnimationFrame(element.animationFrameID);
48+
}
49+
};
50+
51+
export {
52+
vNumberCounter
53+
};

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createApp } from 'vue'
22
import App from './App.vue'
33

4-
createApp(App).mount('#app')
4+
const app = createApp(App);
5+
app.mount('#app')

src/number-counter.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { vNumberCounter } from "@/directives";
2+
import type { App } from "vue";
3+
4+
export default {
5+
install: (app: App) => {
6+
app.directive("number-counter", vNumberCounter);
7+
},
8+
};
9+
10+
export { vNumberCounter };

vite.config.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,26 @@ import vue from '@vitejs/plugin-vue'
55

66
// https://vitejs.dev/config/
77
export default defineConfig({
8-
plugins: [vue()],
8+
plugins: [ vue() ],
99
resolve: {
1010
alias: {
1111
'@': fileURLToPath(new URL('./src', import.meta.url))
1212
}
13+
},
14+
build: {
15+
lib: {
16+
entry: "./src/number-counter.ts",
17+
formats: [ "es", "cjs" ],
18+
name: "number-counter",
19+
fileName: (format) => (format === "es" ? "index.js" : "index.cjs"),
20+
},
21+
rollupOptions: {
22+
external: [ "vue" ],
23+
output: {
24+
globals: {
25+
vue: "Vue",
26+
},
27+
},
28+
},
1329
}
14-
})
30+
})

0 commit comments

Comments
 (0)