UNPKG

element-plus

Version:

A Component Library for Vue 3

1 lines 2.46 kB
{"version":3,"file":"index.mjs","sources":["../../../../../packages/hooks/use-id/index.ts"],"sourcesContent":["import { getCurrentInstance, inject, unref } from 'vue'\nimport { computedEager } from '@vueuse/core'\nimport { debugWarn, isClient } from '@element-plus/utils'\nimport { useGetDerivedNamespace } from '../use-namespace'\n\nimport type { MaybeRef } from '@vueuse/core'\nimport type { InjectionKey, Ref } from 'vue'\n\nexport type ElIdInjectionContext = {\n prefix: number\n current: number\n}\n\nconst defaultIdInjection = {\n prefix: Math.floor(Math.random() * 10000),\n current: 0,\n}\n\nexport const ID_INJECTION_KEY: InjectionKey<ElIdInjectionContext> =\n Symbol('elIdInjection')\n\nexport const useIdInjection = (): ElIdInjectionContext => {\n return getCurrentInstance()\n ? inject(ID_INJECTION_KEY, defaultIdInjection)\n : defaultIdInjection\n}\n\nexport const useId = (deterministicId?: MaybeRef<string>): Ref<string> => {\n const idInjection = useIdInjection()\n if (!isClient && idInjection === defaultIdInjection) {\n debugWarn(\n 'IdInjection',\n `Looks like you are using server rendering, you must provide a id provider to ensure the hydration process to be succeed\nusage: app.provide(ID_INJECTION_KEY, {\n prefix: number,\n current: number,\n})`\n )\n }\n\n const namespace = useGetDerivedNamespace()\n\n // NOTE: Here we use `computedEager` to calculate the id value immediately, avoiding inconsistent id generation due to the lazy feature of `computed` when server rendering.\n const idRef = computedEager(\n () =>\n unref(deterministicId) ||\n `${namespace.value}-id-${idInjection.prefix}-${idInjection.current++}`\n )\n\n return idRef\n}\n"],"names":[],"mappings":";;;;;AAaA,MAAM,kBAAA,GAAqB;AAAA,EACzB,QAAQ,IAAA,CAAK,KAAA,CAAM,IAAA,CAAK,MAAA,KAAW,GAAK,CAAA;AAAA,EACxC,OAAA,EAAS;AACX,CAAA;AAEO,MAAM,gBAAA,0BACJ,eAAe;AAEjB,MAAM,iBAAiB,MAA4B;AACxD,EAAA,OAAO,kBAAA,EAAmB,GACtB,MAAA,CAAO,gBAAA,EAAkB,kBAAkB,CAAA,GAC3C,kBAAA;AACN;AAEO,MAAM,KAAA,GAAQ,CAAC,eAAA,KAAoD;AACxE,EAAA,MAAM,cAAc,cAAA,EAAe;AACnC,EAAA,IAAI,CAAC,QAAA,IAAY,WAAA,KAAgB,kBAAA,EAAoB;AACnD,IAAA,SAAA;AAAA,MACE,aAAA;AAAA,MACA,CAAA;AAAA;AAAA;AAAA;AAAA,EAAA;AAAA,KAKF;AAAA,EACF;AAEA,EAAA,MAAM,YAAY,sBAAA,EAAuB;AAGzC,EAAA,MAAM,KAAA,GAAQ,aAAA;AAAA,IACZ,MACE,KAAA,CAAM,eAAe,CAAA,IACrB,CAAA,EAAG,SAAA,CAAU,KAAK,CAAA,IAAA,EAAO,WAAA,CAAY,MAAM,CAAA,CAAA,EAAI,WAAA,CAAY,OAAA,EAAS,CAAA;AAAA,GACxE;AAEA,EAAA,OAAO,KAAA;AACT;;;;"}