index.ts 1013 B

123456789101112131415161718192021222324252627282930313233343536
  1. import { computed, getCurrentInstance, inject, ref, unref } from 'vue'
  2. import { isNumber } from '@kankan-components/utils'
  3. import type { InjectionKey, Ref } from 'vue'
  4. const zIndex = ref(0)
  5. export const defaultInitialZIndex = 2000
  6. export const zIndexContextKey: InjectionKey<Ref<number | undefined>> =
  7. Symbol('zIndexContextKey')
  8. export const useZIndex = (zIndexOverrides?: Ref<number>) => {
  9. const zIndexInjection =
  10. zIndexOverrides ||
  11. (getCurrentInstance() ? inject(zIndexContextKey, undefined) : undefined)
  12. const initialZIndex = computed(() => {
  13. const zIndexFromInjection = unref(zIndexInjection)
  14. return isNumber(zIndexFromInjection)
  15. ? zIndexFromInjection
  16. : defaultInitialZIndex
  17. })
  18. const currentZIndex = computed(() => initialZIndex.value + zIndex.value)
  19. const nextZIndex = () => {
  20. zIndex.value++
  21. return currentZIndex.value
  22. }
  23. return {
  24. initialZIndex,
  25. currentZIndex,
  26. nextZIndex,
  27. }
  28. }
  29. export type UseZIndexReturn = ReturnType<typeof useZIndex>