Skip to content

Commit 3fb302d

Browse files
committed
Chore(lib): add TS support for directive
1 parent 442a96a commit 3fb302d

File tree

5 files changed

+19
-25
lines changed

5 files changed

+19
-25
lines changed

docs/index.md

+1-14
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export default {
125125
</template>
126126
<script setup>
127127
import {inject} from "vue";
128-
const $dialog = inject('$dialog')
128+
const $dialog = inject(Symbol.for('$dialog'))
129129
const openDialog = () => $dialog.alert('Hello world!')
130130
</script>
131131
```
@@ -145,19 +145,6 @@ Typescript is supported out of the box using (SFC: be sure to add the `lang="ts"
145145
- Type augmentation for the options API `this.$dialog.alert('Hello!')`
146146
- Type augmentation for the directives API `v-confirm="'Hello!'"`
147147

148-
```vue title="App.vue"
149-
<template>
150-
<button @click="openDialog">Open dialog</button>
151-
</template>
152-
<script lang="ts" setup>
153-
import {inject} from "vue";
154-
import {injectionKey} from 'vuejs-dialog'
155-
156-
const $dialog = inject(injectionKey)
157-
const openDialog = () => $dialog.alert('Hello world!')
158-
</script>
159-
```
160-
161148

162149
::: info
163150
You may want to look at the [module resolution option](https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#--moduleresolution-bundler) (introduced in Typescript 5.0) if you are experiencing [issues](https://stackoverflow.com/questions/75870063/vscode-and-typescript-5-moduleresolution-bundler) with typescript especially if your config is set to "bundler"

src/plugin/directive.dialog.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import {Directive} from "vue";
12
import { noop, clickNode, cloneObj } from './utilities'
23
import {CONFIRM_TYPES, DIRECTIVE_ATTRIBUTE_KEY} from './constants'
3-
import type PromiseDialog from './promise.dialog'
4+
import type { PromiseDialog } from './promise.dialog'
5+
import type {DialogResolverPayload} from "./interface";
46

57

68
export class ConfirmDirective {
79
shouldIgnoreClick = false
8-
bindingOptions = {}
910

1011
constructor(private readonly dialog: PromiseDialog) {}
1112

@@ -33,10 +34,10 @@ export class ConfirmDirective {
3334
if (binding?.value && binding.value.ok) {
3435
return dialog => binding.value.ok({ ...dialog, node: el })
3536
}
36-
return dialog => {
37+
return (dialog: DialogResolverPayload) => {
3738
// If we got here, it means the default action is to be executed
38-
// We'll then stop the loader if it's enabled and close the dialog
39-
dialog.loading && dialog.close()
39+
// We'll then close the dialog even if it has loading enabled
40+
dialog.close && dialog.close()
4041
this.shouldIgnoreClick = true
4142
clickNode(el)
4243
this.shouldIgnoreClick = false
@@ -67,7 +68,7 @@ export class ConfirmDirective {
6768
})
6869
}
6970

70-
public static createInstaller(dialog: PromiseDialog) {
71+
public static createInstaller(dialog: PromiseDialog): Directive {
7172
const directive = new ConfirmDirective(dialog)
7273
return {
7374
mounted: (el, binding) => {

src/plugin/interface.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ export interface DialogOptions {
4444
}
4545

4646
export type DialogResolverPayload = {
47-
data: string;
47+
canceled: boolean;
48+
data?: string;
4849
close?: () => void;
4950
node?: HTMLElement;
50-
} | { canceled: boolean; }
51+
}
5152

5253
export interface DialogWindowOptions extends DialogOptions {
5354
message: MessageWithTitle;

src/plugin/promise.dialog.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import {ConfirmDirective} from "./directive.dialog";
1414
interface DialogPluginOptions extends Omit<DialogWindowOptions, 'id'>{}
1515

1616
export const propertyKey = '$dialog'
17-
export const injectionKey: InjectionKey<PromiseDialog> = Symbol(propertyKey)
17+
export const injectionKey: InjectionKey<PromiseDialog> = Symbol.for(propertyKey)
1818

1919
export class PromiseDialog {
2020
private dgApp: App;
21-
private dgAppComponentInstance: ComponentInstance<DialogComponent>;
21+
private dgAppComponentInstance: ComponentInstance<typeof DialogComponent>;
2222

2323
/**
2424
* @internal
@@ -86,7 +86,7 @@ export class PromiseDialog {
8686
}
8787

8888
this.dgApp = dialogApp
89-
this.dgAppComponentInstance = dialogApp.mount(node) as ComponentInstance<DialogComponent>
89+
this.dgAppComponentInstance = dialogApp.mount(node) as ComponentInstance<typeof DialogComponent>
9090
this.mounted = true
9191
}
9292

src/plugin/vue-shim.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import {PromiseDialog} from "./promise.dialog";
2+
import {ConfirmDirective} from './directive.dialog'
23

34
declare module 'vue' {
45
export interface ComponentCustomProperties {
56
$dialog: typeof PromiseDialog;
67
}
8+
9+
export interface GlobalDirectives {
10+
confirm: ReturnType<typeof ConfirmDirective.createInstaller>;
11+
}
712
}

0 commit comments

Comments
 (0)