<!--
 * @Author: changjun
 * @FilePath: /yuan-qingdao-zld-browser/src/components/ProgressBar/ProgressBar.vue
 * @Date: 2022-07-08 08:52:06
 * @Description: 进度条组件
 * @LastEditTime: 2022-09-08 14:56:49
 * @LastEditors: changjun
-->
<script setup lang="ts">
interface ProgressBarProps {
  percentage: number,
  color?: string,
  backgroundColor?: string,
}
const { percentage = 0, color = '#0095FF', backgroundColor = '#F4F4F4' } = defineProps<ProgressBarProps>()

const getCss = computed(() => {
  return {
    background: unref(color)
  }
})

const getBackgroundBarCss = computed(() => {
  return {
    background: unref(backgroundColor)
  }
})

const beforeEnter = (el) => {
  el.style.width = 0
}
const enter = (el) => {
  el.style.width = `${unref(percentage)}%`
  el.style.transition = `width 1s linear`
}

</script>

<template>
  <div class="progress-bar">
    <div class="background-bar" :style="getBackgroundBarCss"></div>
    <transition appear @before-appear="beforeEnter" @after-appear="enter">
      <div class="tracker-bar" :style="getCss"></div>
    </transition>
  </div>
</template>

<style scoped lang="scss">
.progress-bar {
  width: 100%;

  .background-bar {
    width: 100%;
    height: 4px;
    border-radius: 4px;
  }

  .tracker-bar {
    background: transparent;
    height: 4px;
    width: 0;
    transition: width 0.5s linear;
    margin-top: -4px;
    border-radius: 4px;
  }
}
</style>
