"src/components/src/components/GrowingNumber.vue" did not exist on "c017255fdf2b5b5ff436f46465d2650103ad532a"
GrowingNumber.vue 1.29 KB
Newer Older
Yoelvis Gonzalez's avatar
Yoelvis Gonzalez committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
<template>
  <div>{{ prefix }}{{ newValueFormatted }}{{ suffix }}</div>
</template>

<script>
  import { computed, ref, watch, onMounted } from 'vue'
  import numeral from 'numeral'

  export default {
    name: 'GrowingNumber',
    props: {
      prefix: {
        type: String,
        default: null,
      },
      suffix: {
        type: String,
        default: null,
      },
      value: {
        type: Number,
        default: 0,
      },
      duration: {
        type: Number,
        default: 500,
      },
    },
    setup (props) {
      const newValue = ref(0)

      const newValueFormatted = computed(
        () => newValue.value < 1000 ? newValue.value : numeral(newValue.value).format('0,0'),
      )

      const value = computed(() => props.value)

      const grow = m => {
        const v = Math.ceil(newValue.value + m)

        if (v > value.value) {
          newValue.value = value.value
          return false
        }

        newValue.value = v

        setTimeout(() => {
          grow(m)
        }, 25)
      }

      const growInit = () => {
        grow(props.value / (props.duration / 25))
      }

      watch(value, () => {
        growInit()
      })

      onMounted(() => {
        growInit()
      })

      return {
        newValueFormatted,
      }
    },
  }
</script>