index.js 1.3 KB

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