index.mjs 1.2 KB

1234567891011121314151617181920212223242526272829
  1. import { isClient as isClient$1 } from "../../utils/browser.mjs";
  2. import { debugWarn } from "../../utils/error.mjs";
  3. import { useGetDerivedNamespace } from "../use-namespace/index.mjs";
  4. import { computedEager } from "@vueuse/core";
  5. import { getCurrentInstance, inject, unref } from "vue";
  6. //#region ../../packages/hooks/use-id/index.ts
  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$1 && idInjection === defaultIdInjection) debugWarn("IdInjection", `Looks like you are using server rendering, you must provide a id provider to ensure the hydration process to be succeed
  18. usage: app.provide(ID_INJECTION_KEY, {
  19. prefix: number,
  20. current: number,
  21. })`);
  22. const namespace = useGetDerivedNamespace();
  23. return computedEager(() => unref(deterministicId) || `${namespace.value}-id-${idInjection.prefix}-${idInjection.current++}`);
  24. };
  25. //#endregion
  26. export { ID_INJECTION_KEY, useId, useIdInjection };
  27. //# sourceMappingURL=index.mjs.map