@teammodlr/ui-kit 2.1.46 → 2.1.49

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.49",
4
4
  "description": "",
5
5
  "main": "lib/index.cjs.js",
6
6
  "module": "lib/index.es.js",
@@ -1,26 +1,38 @@
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
+ import { type Ref, toRefs } from 'vue';
4
+ interface ConfigProviderContextValue {
5
+ useId?: () => string,
6
+ dir?: Ref<string>
7
+ }
8
+
9
+ export const [injectConfigProviderContext, provideConfigProviderContext]
10
+ = createContext<ConfigProviderContextValue>('ConfigProvider')
11
+
12
+ export interface ConfigProviderProps {
13
+ useId?: () => string,
14
+ dir?: string
15
+ }
16
+
17
+ export default {
18
+ name: 'ConfigProvider',
19
+ }
20
+ </script>
21
+
22
+ <script setup lang="ts">
23
+
24
+ const props = withDefaults(defineProps<ConfigProviderProps>(), {
25
+ useId: undefined,
26
+ dir: 'ltr'
27
+ })
28
+
29
+ const { dir } = toRefs(props)
30
+
31
+ provideConfigProviderContext({
32
+ useId: props.useId,
33
+ dir
34
+ })
35
+ </script>
36
+ <template>
37
+ <slot />
38
+ </template>
@@ -1,83 +1,82 @@
1
- <template>
2
- <slot />
3
- <template v-if="dialogList.length > 0">
4
- <Teleport :to="teleportTo ?? 'body'">
5
- <MConfirm
6
- v-for="dialog in dialogList"
7
- :key="dialog.key"
8
- :internalKey="dialog.key"
9
- :title="dialog.title"
10
- :content="dialog.content"
11
- :okText="dialog.okText"
12
- :cancelText="dialog.cancelText"
13
- :on-ok="dialog.onOk"
14
- :on-cancel="dialog.onCancel"
15
- :ok-variant="dialog.okVariant"
16
- ref="dialogRefs" />
17
- </Teleport>
18
- </template>
19
- </template>
20
-
21
- <script lang="ts">
22
- import { defineComponent, provide, ref } from 'vue';
23
- import { useId } from '../../composables/useCachedUid';
24
- import { providerProps } from './providerProps';
25
- import type { Confirm, ConfirmApiInjection, ConfirmOptions } from './types';
26
- import MConfirm from './Confirm.vue';
27
-
28
- interface PrivateConfirm extends Confirm {
29
- key: string;
30
- }
31
-
32
- interface PrivateConfirmRef extends Confirm {
33
- key: string;
34
- hide: () => void;
35
- }
36
-
37
- export default defineComponent({
38
- name: 'ConfirmProvider',
39
- props: providerProps,
40
- components: {
41
- MConfirm
42
- },
43
- setup(props, { slots }) {
44
- const dialogListRef = ref<PrivateConfirm[]>([]);
45
- const dialogRefs: Record<string, PrivateConfirmRef> = {};
46
-
47
- function create(options: ConfirmOptions): Confirm {
48
- const id = useId(null, 'm-confirm');
49
- const key = id;
50
- const dialog: PrivateConfirm = {
51
- ...options,
52
- key,
53
- destroy: () => {
54
- dialogRefs[key].hide();
55
- }
56
- };
57
-
58
- dialogListRef.value.push(dialog);
59
- return dialog;
60
- }
61
-
62
- function destroyAll(): void {
63
- // @ts-ignore
64
- Object.values(dialogRefs.value).forEach(dialog => {
65
- dialog.hide();
66
- });
67
- }
68
-
69
- const api: ConfirmApiInjection = {
70
- create,
71
- destroyAll
72
- } as const;
73
-
74
- provide('m-confirm', api);
75
-
76
- return {
77
- ...api,
78
- dialogRefs,
79
- dialogList: dialogListRef
80
- };
81
- }
82
- });
83
- </script>
1
+ <template>
2
+ <slot />
3
+ <template v-if="dialogList.length > 0">
4
+ <Teleport :to="teleportTo ?? 'body'">
5
+ <MConfirm
6
+ v-for="dialog in dialogList"
7
+ :key="dialog.key"
8
+ :internalKey="dialog.key"
9
+ :title="dialog.title"
10
+ :content="dialog.content"
11
+ :okText="dialog.okText"
12
+ :cancelText="dialog.cancelText"
13
+ :on-ok="dialog.onOk"
14
+ :on-cancel="dialog.onCancel"
15
+ :ok-variant="dialog.okVariant"
16
+ ref="dialogRefs" />
17
+ </Teleport>
18
+ </template>
19
+ </template>
20
+
21
+ <script lang="ts">
22
+ import { defineComponent, provide, ref } from 'vue';
23
+ import { useId } from '../../composables/useCachedUid';
24
+ import { providerProps } from './providerProps';
25
+ import type { Confirm, ConfirmApiInjection, ConfirmOptions } from './types';
26
+ import MConfirm from './Confirm.vue';
27
+
28
+ interface PrivateConfirm extends Confirm {
29
+ key: string;
30
+ }
31
+
32
+ interface PrivateConfirmRef extends Confirm {
33
+ key: string;
34
+ hide: () => void;
35
+ }
36
+
37
+ export default defineComponent({
38
+ name: 'ConfirmProvider',
39
+ props: providerProps,
40
+ components: {
41
+ MConfirm
42
+ },
43
+ setup(props, { slots }) {
44
+ const dialogListRef = ref<PrivateConfirm[]>([]);
45
+ const dialogRefs: Record<string, PrivateConfirmRef> = {};
46
+ const id = useId(null, 'm-confirm');
47
+ function create(options: ConfirmOptions): Confirm {
48
+ const key = id;
49
+ const dialog: PrivateConfirm = {
50
+ ...options,
51
+ key,
52
+ destroy: () => {
53
+ dialogRefs[key].hide();
54
+ }
55
+ };
56
+
57
+ dialogListRef.value.push(dialog);
58
+ return dialog;
59
+ }
60
+
61
+ function destroyAll(): void {
62
+ // @ts-ignore
63
+ Object.values(dialogRefs.value).forEach(dialog => {
64
+ dialog.hide();
65
+ });
66
+ }
67
+
68
+ const api: ConfirmApiInjection = {
69
+ create,
70
+ destroyAll
71
+ } as const;
72
+
73
+ provide('m-confirm', api);
74
+
75
+ return {
76
+ ...api,
77
+ dialogRefs,
78
+ dialogList: dialogListRef
79
+ };
80
+ }
81
+ });
82
+ </script>
@@ -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,9 +6,8 @@ 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
- // @ts-ignore
12
11
  return `${prefix}-${useId()}`;
13
12
  }
14
13
 
@@ -1,5 +1,45 @@
1
- import type { InjectionKey } from 'vue';
2
-
3
- export function createInjectionKey<T>(key: string): InjectionKey<T> {
4
- return key as any;
5
- }
1
+ import { type InjectionKey, inject, provide } from 'vue';
2
+
3
+ export function createInjectionKey<T>(key: string): InjectionKey<T> {
4
+ return key as any;
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
+ const injectContext = <
18
+ T extends ContextValue | null | undefined = ContextValue,
19
+ >(
20
+ fallback?: T,
21
+ ): T extends null ? ContextValue | null : ContextValue => {
22
+ const context = inject(injectionKey, fallback)
23
+ if (context)
24
+ return context
25
+
26
+ if (context === null)
27
+ return context as any
28
+
29
+ throw new Error(
30
+ `Injection \`${injectionKey.toString()}\` not found. Component must be used within ${Array.isArray(providerComponentName)
31
+ ? `one of the following components: ${providerComponentName.join(
32
+ ', ',
33
+ )}`
34
+ : `\`${providerComponentName}\``
35
+ }`,
36
+ )
37
+ }
38
+
39
+ const provideContext = (contextValue: ContextValue) => {
40
+ provide(injectionKey, contextValue)
41
+ return contextValue
42
+ }
43
+
44
+ return [injectContext, provideContext] as const
45
+ }