@teammodlr/ui-kit 2.1.46 → 2.1.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,3 @@
1
- import type { InjectionKey } from 'vue';
1
+ import { type InjectionKey } from 'vue';
2
2
  export declare function createInjectionKey<T>(key: string): InjectionKey<T>;
3
+ export declare function createContext<ContextValue>(providerComponentName: string | string[], contextName?: string): readonly [<T extends ContextValue | null | undefined = ContextValue>(fallback?: T) => T extends null ? ContextValue | null : ContextValue, (contextValue: ContextValue) => ContextValue];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teammodlr/ui-kit",
3
- "version": "2.1.46",
3
+ "version": "2.1.47",
4
4
  "description": "",
5
5
  "main": "lib/index.cjs.js",
6
6
  "module": "lib/index.es.js",
@@ -89,4 +89,4 @@
89
89
  "maska": "^2.1.9",
90
90
  "treemate": "^0.3.11"
91
91
  }
92
- }
92
+ }
@@ -1,26 +1,31 @@
1
- <script lang="ts">
2
- export interface ConfigProviderProps {
3
- useId?: () => string
4
- }
5
- export default {
6
- name: 'ConfigProvider',
7
- }
8
- </script>
9
-
10
- <script setup lang="ts">
11
- import {provide} from "vue";
12
- import type {ConfigProviderInjection} from "./types";
13
-
14
- const props = withDefaults(defineProps<ConfigProviderProps>(), {
15
- useId: undefined
16
- })
17
-
18
- const api: ConfigProviderInjection = {
19
- useId: props['useId'],
20
- } as const;
21
-
22
- provide('m-config', api);
23
- </script>
24
- <template>
25
- <slot />
26
- </template>
1
+ <script lang="ts">
2
+ import { createContext } from "../../utils/injection";
3
+
4
+ interface ConfigProviderContextValue {
5
+ useId?: () => string
6
+ }
7
+
8
+ export const [injectConfigProviderContext, provideConfigProviderContext] = createContext<ConfigProviderContextValue>('ConfigProvider');
9
+
10
+ export interface ConfigProviderProps {
11
+ useId?: () => string
12
+ }
13
+
14
+ export default {
15
+ name: 'ConfigProvider',
16
+ }
17
+ </script>
18
+
19
+ <script setup lang="ts">
20
+
21
+ const props = withDefaults(defineProps<ConfigProviderProps>(), {
22
+ useId: undefined
23
+ })
24
+
25
+ provideConfigProviderContext({
26
+ useId: props.useId
27
+ })
28
+ </script>
29
+ <template>
30
+ <slot />
31
+ </template>
@@ -1,4 +1,4 @@
1
- import {inject} from 'vue';
1
+ import { injectConfigProviderContext } from '../components/configprovider/ConfigProvider.vue';
2
2
 
3
3
  let count = 0
4
4
 
@@ -6,7 +6,7 @@ export function useId(deterministicId?: string | null | undefined, prefix = 'ui'
6
6
  if (deterministicId) {
7
7
  return deterministicId;
8
8
  }
9
- const {useId} = inject('m-config', {useId: undefined});
9
+ const { useId } = injectConfigProviderContext({ useId: undefined })
10
10
  if (useId && typeof useId === 'function') {
11
11
  // @ts-ignore
12
12
  return `${prefix}-${useId()}`;
@@ -1,5 +1,49 @@
1
- import type { InjectionKey } from 'vue';
1
+ import { type InjectionKey, inject, provide } from 'vue';
2
2
 
3
3
  export function createInjectionKey<T>(key: string): InjectionKey<T> {
4
4
  return key as any;
5
5
  }
6
+
7
+ export function createContext<ContextValue>(
8
+ providerComponentName: string | string[],
9
+ contextName?: string
10
+ ) {
11
+ const symbolDescription
12
+ = typeof providerComponentName === 'string' && !contextName
13
+ ? `${providerComponentName}Context`
14
+ : contextName
15
+
16
+ const injectionKey: InjectionKey<ContextValue | null> = Symbol(symbolDescription);
17
+
18
+ const injectContext = <
19
+ T extends ContextValue | null | undefined = ContextValue,
20
+ >(
21
+ fallback?: T,
22
+ ): T extends null ? ContextValue | null : ContextValue => {
23
+ const context = inject(injectionKey, fallback);
24
+ if (context) {
25
+ return context;
26
+ }
27
+
28
+ if (context === null) {
29
+ return context as any;
30
+ }
31
+
32
+ throw new Error(
33
+ `Injection \`${injectionKey.toString()}\` not found. Component must be used within ${
34
+ Array.isArray(providerComponentName)
35
+ ? `one of the following components: ${providerComponentName.join(
36
+ ', ',
37
+ )}`
38
+ : `\`${providerComponentName}\``
39
+ }`,
40
+ )
41
+ }
42
+
43
+ const provideContext = (contextValue: ContextValue) => {
44
+ provide(injectionKey, contextValue);
45
+ return contextValue;
46
+ }
47
+
48
+ return [injectContext, provideContext] as const;
49
+ }