hongluan-ui
Version:
Hongluan Component Library for Vue 3
1 lines • 2.39 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["../../../../../packages/hooks/use-id/index.ts"],"sourcesContent":["import { computed, getCurrentInstance, inject, unref } from 'vue'\nimport { debugWarn, isClient } from '@hongluan-ui/utils'\nimport { useGlobalConfig } from '../use-global-config'\nimport { defaultNamespace } from '../use-namespace'\n\nimport type { InjectionKey, Ref } from 'vue'\nimport type { MaybeRef } from '@vueuse/core'\n\nexport type IdInjectionContext = {\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<IdInjectionContext> =\n Symbol('IdInjection')\n\nexport const useIdInjection = (): IdInjectionContext => {\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 = useGlobalConfig('namespace', defaultNamespace)\n const idRef = computed(\n () =>\n unref(deterministicId) ||\n `${namespace.value}-id-${idInjection.prefix}-${idInjection.current++}`,\n )\n\n return idRef\n}\n"],"names":[],"mappings":";;;;;;;AAIA,MAAM,kBAAkB,GAAG;AAC3B,EAAE,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC;AACzC,EAAE,OAAO,EAAE,CAAC;AACZ,CAAC,CAAC;AACU,MAAC,gBAAgB,GAAG,MAAM,CAAC,aAAa,EAAE;AAC1C,MAAC,cAAc,GAAG,MAAM;AACpC,EAAE,OAAO,kBAAkB,EAAE,GAAG,MAAM,CAAC,gBAAgB,EAAE,kBAAkB,CAAC,GAAG,kBAAkB,CAAC;AAClG,EAAE;AACU,MAAC,KAAK,GAAG,CAAC,eAAe,KAAK;AAC1C,EAAE,MAAM,WAAW,GAAG,cAAc,EAAE,CAAC;AACvC,EAAE,IAAI,CAAC,QAAQ,IAAI,WAAW,KAAK,kBAAkB,EAAE;AACvD,IAAI,SAAS,CAAC,aAAa,EAAE,CAAC;AAC9B;AACA;AACA;AACA,EAAE,CAAC,CAAC,CAAC;AACL,GAAG;AACH,EAAE,MAAM,SAAS,GAAG,eAAe,CAAC,WAAW,EAAE,gBAAgB,CAAC,CAAC;AACnE,EAAE,MAAM,KAAK,GAAG,QAAQ,CAAC,MAAM,KAAK,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE,SAAS,CAAC,KAAK,CAAC,IAAI,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC,EAAE,WAAW,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC;AACjI,EAAE,OAAO,KAAK,CAAC;AACf;;;;"}