layer.vue 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <template>
  2. <div
  3. class="ui-gate-layer"
  4. :style="{
  5. height: normalizeUnitToStyle(height),
  6. '--len': contentInstances.length,
  7. '--current': slideIndex,
  8. }"
  9. >
  10. <div class="ui-gate-slides">
  11. <slot />
  12. </div>
  13. </div>
  14. </template>
  15. <script lang="ts" setup>
  16. // watchEffect
  17. import { computed, defineProps, provide, ref, watch } from 'vue'
  18. import { normalizeUnitToStyle } from '@kankan-components/utils'
  19. import { Relation } from './constant'
  20. defineOptions({
  21. name: 'UIGate',
  22. })
  23. const contentInstances = ref([])
  24. const props = defineProps({
  25. index: {
  26. type: [Number, String],
  27. default: 0,
  28. },
  29. height: {
  30. type: [Number, String],
  31. },
  32. })
  33. const slideIndex = computed(() => (props.index > contentInstances.value.length - 1 ? contentInstances.value.length - 1 : props.index < 0 ? 0 : props.index))
  34. watch([contentInstances, slideIndex], () => {
  35. for (let i = 0; i < contentInstances.value.length; i++) {
  36. const instance = contentInstances.value[i]
  37. instance.value = i === slideIndex.value
  38. }
  39. })
  40. provide(Relation, contentInstances)
  41. </script>
  42. <!-- <script>
  43. export default { name: 'UiGate' }
  44. </script> -->