index.mjs 1.5 KB

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