index.mjs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { ref, getCurrentInstance, inject, computed, unref } from 'vue';
  2. import '../../utils/index.mjs';
  3. import { isNumber } from '../../utils/types.mjs';
  4. import { isClient } from '@vueuse/core';
  5. import { debugWarn } from '../../utils/error.mjs';
  6. const initial = {
  7. current: 0
  8. };
  9. const zIndex = ref(0);
  10. const defaultInitialZIndex = 2e3;
  11. const ZINDEX_INJECTION_KEY = Symbol("elZIndexContextKey");
  12. const zIndexContextKey = Symbol("zIndexContextKey");
  13. const useZIndex = (zIndexOverrides) => {
  14. const increasingInjection = getCurrentInstance() ? inject(ZINDEX_INJECTION_KEY, initial) : initial;
  15. const zIndexInjection = zIndexOverrides || (getCurrentInstance() ? inject(zIndexContextKey, void 0) : void 0);
  16. const initialZIndex = computed(() => {
  17. const zIndexFromInjection = unref(zIndexInjection);
  18. return isNumber(zIndexFromInjection) ? zIndexFromInjection : defaultInitialZIndex;
  19. });
  20. const currentZIndex = computed(() => initialZIndex.value + zIndex.value);
  21. const nextZIndex = () => {
  22. increasingInjection.current++;
  23. zIndex.value = increasingInjection.current;
  24. return currentZIndex.value;
  25. };
  26. if (!isClient && !inject(ZINDEX_INJECTION_KEY)) {
  27. debugWarn("ZIndexInjection", `Looks like you are using server rendering, you must provide a z-index provider to ensure the hydration process to be succeed
  28. usage: app.provide(ZINDEX_INJECTION_KEY, { current: 0 })`);
  29. }
  30. return {
  31. initialZIndex,
  32. currentZIndex,
  33. nextZIndex
  34. };
  35. };
  36. export { ZINDEX_INJECTION_KEY, defaultInitialZIndex, useZIndex, zIndexContextKey };
  37. //# sourceMappingURL=index.mjs.map