index.mjs 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. import { getCurrentInstance, inject, computed, unref } from 'vue';
  2. import { isClient } from '@vueuse/core';
  3. import '../../utils/index.mjs';
  4. import { useGlobalConfig } from '../use-global-config/index.mjs';
  5. import { defaultNamespace } from '../use-namespace/index.mjs';
  6. import { debugWarn } from '../../utils/error.mjs';
  7. const defaultIdInjection = {
  8. prefix: Math.floor(Math.random() * 1e4),
  9. current: 0
  10. };
  11. const ID_INJECTION_KEY = Symbol("elIdInjection");
  12. const useIdInjection = () => {
  13. return getCurrentInstance() ? inject(ID_INJECTION_KEY, defaultIdInjection) : defaultIdInjection;
  14. };
  15. const useId = (deterministicId) => {
  16. const idInjection = useIdInjection();
  17. if (!isClient && idInjection === defaultIdInjection) {
  18. debugWarn("IdInjection", `Looks like you are using server rendering, you must provide a id provider to ensure the hydration process to be succeed
  19. usage: app.provide(ID_INJECTION_KEY, {
  20. prefix: number,
  21. current: number,
  22. })`);
  23. }
  24. const namespace = useGlobalConfig("namespace", defaultNamespace);
  25. const idRef = computed(() => unref(deterministicId) || `${namespace.value}-id-${idInjection.prefix}-${idInjection.current++}`);
  26. return idRef;
  27. };
  28. export { ID_INJECTION_KEY, useId, useIdInjection };
  29. //# sourceMappingURL=index.mjs.map