Skip to content
This repository was archived by the owner on Jan 9, 2022. It is now read-only.

Commit 72669ec

Browse files
committed
feat: create Vue Pluign for package
- add plugin methods to open dialogs programmatically (without template) - add plugin methods injection - add GDialogRoot to display programmatically added dialogs
1 parent caea3f6 commit 72669ec

File tree

7 files changed

+122
-3
lines changed

7 files changed

+122
-3
lines changed
File renamed without changes.

scripts/build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
const shell = require('shelljs')
44

55
shell.exec('vue-tsc --noEmit && vite build')
6-
shell.exec('cp ./types/* ./dist')
6+
shell.exec('cp ./@types/* ./dist')

src/components/GDialogRoot.vue

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<template>
2+
<Component
3+
:is="modal.component"
4+
v-for="(modal, index) in dialogs"
5+
:key="modal.id"
6+
v-bind="modal.props"
7+
@update:modelValue="onClose(index)"
8+
/>
9+
</template>
10+
11+
<script lang="ts">
12+
import { inject } from 'vue'
13+
14+
import { DialogMethodsKey, DialogDialogListKey } from '../index'
15+
16+
export default {
17+
name: 'GDialogRoot',
18+
19+
setup() {
20+
const dialogs = inject(DialogDialogListKey, [])
21+
const $dialog = inject(DialogMethodsKey, null)
22+
23+
if(!$dialog) {
24+
console.error('The plugin is not initialized')
25+
}
26+
27+
function onClose(index: number) {
28+
if(!$dialog) {
29+
throw new Error('good')
30+
}
31+
32+
$dialog.remove(index)
33+
}
34+
35+
return {
36+
dialogs,
37+
onClose,
38+
}
39+
},
40+
}
41+
</script>

src/composable/lazyActivation.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@ type UseLazyActivationFunc = (baseState: Ref<boolean>) => {
99
}
1010

1111
export const useLazyActivation: UseLazyActivationFunc = (baseState) => {
12-
const activatedOnce = ref(baseState.value)
13-
const active = ref(baseState.value)
12+
const activatedOnce = ref(false)
13+
const active = ref(false)
1414
const deactivating = ref(false)
1515

16+
if(baseState.value) {
17+
activatedOnce.value = true
18+
nextTick(() => {
19+
active.value = true
20+
})
21+
}
22+
1623
watch(
1724
() => baseState.value,
1825
value => {

src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1+
// standalone component
12
export { default as GDialog } from './components/GDialog.vue'
3+
4+
// using plugin
5+
export { default as GDialogRoot } from './components/GDialogRoot.vue'
6+
7+
export { DialogDialogListKey as DialogDialogListKey } from './plugin'
8+
export { DialogMethodsKey as DialogMethodsKey } from './plugin'
9+
export { plugin as plugin } from './plugin'

src/plugin.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {
2+
reactive, InjectionKey, shallowRef, Plugin,
3+
} from 'vue'
4+
5+
import { DialogMethods, IDialogItem } from './types/Plugin'
6+
7+
export const DialogMethodsKey: InjectionKey<DialogMethods> = Symbol('DialogMethod')
8+
export const DialogDialogListKey: InjectionKey<IDialogItem[]> = Symbol('DialogList')
9+
10+
const dialogs = reactive<IDialogItem[]>([])
11+
12+
export const plugin: Plugin = {
13+
install: (app) => {
14+
app.provide(DialogMethodsKey, {
15+
add: ({ component, props }) => {
16+
dialogs.push({
17+
component: shallowRef(component),
18+
id: Date.now() + Math.random(),
19+
20+
props: {
21+
modelValue: true,
22+
...props,
23+
},
24+
})
25+
},
26+
27+
remove: (index) => {
28+
const dialog = dialogs[index]
29+
dialog.props.modelValue = false
30+
setTimeout(() => {
31+
dialogs.splice(index, 1)
32+
}, 150)
33+
},
34+
})
35+
36+
app.provide(DialogDialogListKey, dialogs)
37+
},
38+
}

src/types/Plugin.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {
2+
Component, ShallowUnwrapRef,
3+
} from 'vue'
4+
5+
export interface IDialogItem {
6+
component: ShallowUnwrapRef<Component>
7+
id: number
8+
props: {
9+
modelValue: boolean
10+
}
11+
}
12+
13+
type DialogAddMethod = <T>(params: {
14+
component: Component
15+
props?: Omit<T, 'modelValue'> | undefined
16+
}) => void
17+
18+
type DialogRemoveMethod = (
19+
index: number
20+
) => void
21+
22+
export interface DialogMethods {
23+
add: DialogAddMethod
24+
remove: DialogRemoveMethod
25+
}

0 commit comments

Comments
 (0)