layer.vue 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 setup>
  16. // watchEffect
  17. import { computed, defineProps, provide, ref, watch } from 'vue'
  18. import { normalizeUnitToStyle } from '@kankan/utils'
  19. import { Relation } from './constant'
  20. const contentInstances = ref([])
  21. const props = defineProps({
  22. index: {
  23. type: [Number, String],
  24. default: 0,
  25. },
  26. height: {
  27. type: [Number, String],
  28. },
  29. })
  30. const slideIndex = computed(() => (props.index > contentInstances.value.length - 1 ? contentInstances.value.length - 1 : props.index < 0 ? 0 : props.index))
  31. watch([contentInstances, slideIndex], () => {
  32. for (let i = 0; i < contentInstances.value.length; i++) {
  33. const instance = contentInstances.value[i]
  34. instance.value = i === slideIndex.value
  35. }
  36. })
  37. provide(Relation, contentInstances)
  38. </script>
  39. <script>
  40. export default { name: 'UiGate' }
  41. </script>