index.ts 943 B

12345678910111213141516171819202122232425262728293031323334
  1. import { computed, inject, ref, unref } from 'vue'
  2. import { isNumber } from '@element-plus/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 = zIndexOverrides || inject(zIndexContextKey, undefined)
  10. const initialZIndex = computed(() => {
  11. const zIndexFromInjection = unref(zIndexInjection)
  12. return isNumber(zIndexFromInjection)
  13. ? zIndexFromInjection
  14. : defaultInitialZIndex
  15. })
  16. const currentZIndex = computed(() => initialZIndex.value + zIndex.value)
  17. const nextZIndex = () => {
  18. zIndex.value++
  19. return currentZIndex.value
  20. }
  21. return {
  22. initialZIndex,
  23. currentZIndex,
  24. nextZIndex,
  25. }
  26. }
  27. export type UseZIndexReturn = ReturnType<typeof useZIndex>