UNPKG

tav-ui

Version:
1 lines 1.83 kB
{"version":3,"file":"useContext2.mjs","sources":["../../../../../../packages/hooks/core/useContext.ts"],"sourcesContent":["/* eslint-disable symbol-description */\nimport { readonly as defineReadonly, inject, provide, reactive } from 'vue'\nimport type { InjectionKey, UnwrapRef } from 'vue'\n\nexport interface CreateContextOptions {\n readonly?: boolean\n createProvider?: boolean\n native?: boolean\n}\n\ntype ShallowUnwrap<T> = {\n [P in keyof T]: UnwrapRef<T[P]>\n}\n\nexport function createContext<T>(\n context: any,\n key: InjectionKey<T> = Symbol(),\n options: CreateContextOptions = {}\n) {\n const { readonly = true, createProvider = false, native = false } = options\n\n const state = reactive(context)\n const provideData = readonly ? defineReadonly(state) : state\n !createProvider && provide(key, native ? context : provideData)\n\n return {\n state,\n }\n}\n\nexport function useContext<T>(key: InjectionKey<T>, native?: boolean): T\nexport function useContext<T>(key: InjectionKey<T>, defaultValue?: any, native?: boolean): T\n\nexport function useContext<T>(\n key: InjectionKey<T> = Symbol(),\n defaultValue?: any\n): ShallowUnwrap<T> {\n return inject(key, defaultValue || {})\n}\n"],"names":["readonly","defineReadonly"],"mappings":";;AACO,SAAS,aAAa,CAAC,OAAO,EAAE,GAAG,GAAG,MAAM,EAAE,EAAE,OAAO,GAAG,EAAE,EAAE;AACrE,EAAE,MAAM,YAAEA,UAAQ,GAAG,IAAI,EAAE,cAAc,GAAG,KAAK,EAAE,MAAM,GAAG,KAAK,EAAE,GAAG,OAAO,CAAC;AAC9E,EAAE,MAAM,KAAK,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;AAClC,EAAE,MAAM,WAAW,GAAGA,UAAQ,GAAGC,QAAc,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;AAC/D,EAAE,CAAC,cAAc,IAAI,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,GAAG,WAAW,CAAC,CAAC;AAClE,EAAE,OAAO;AACT,IAAI,KAAK;AACT,GAAG,CAAC;AACJ,CAAC;AACM,SAAS,UAAU,CAAC,GAAG,GAAG,MAAM,EAAE,EAAE,YAAY,EAAE;AACzD,EAAE,OAAO,MAAM,CAAC,GAAG,EAAE,YAAY,IAAI,EAAE,CAAC,CAAC;AACzC;;;;"}