-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin): set initial options by plugin, and override by useNoti() (
#10)
- Loading branch information
Showing
8 changed files
with
70 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
import { inject } from 'vue' | ||
|
||
import { INJECT_KEY } from '../constant' | ||
import type { NotiEvent } from '../types' | ||
import type { NotiOptions } from '../types' | ||
import useEventBus from './useEventBus' | ||
|
||
export function useNoti() { | ||
const noti = inject(INJECT_KEY) as NotiEvent | ||
const bus = useEventBus() | ||
const noti = (options: NotiOptions) => bus.emit(options) | ||
return noti | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { inject } from 'vue' | ||
import { DEFAULT_SETTING, INJECT_KEY } from '../constant' | ||
import type { NotiContext, NotiOptions } from '../types' | ||
|
||
export function useNotiProvideArguments(initialOptions: NotiOptions) { | ||
const provideArguments = [INJECT_KEY, { | ||
options: { | ||
...DEFAULT_SETTING, | ||
...initialOptions, | ||
}, | ||
}] as const | ||
|
||
return provideArguments | ||
} | ||
|
||
export function useNotiContext(): NotiContext { | ||
return inject(INJECT_KEY) || { options: DEFAULT_SETTING } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,13 @@ | ||
import type { App, Plugin } from 'vue' | ||
|
||
import { INJECT_KEY } from '../constant' | ||
import useEventBus from '../composables/useEventBus' | ||
import { useNotiProvideArguments } from '../composables/useNotiContext' | ||
import type { NotiOptions } from '../types' | ||
|
||
// app: App, options: any | ||
export const NotiPlugin: Plugin = { | ||
install: (app: App) => { | ||
const bus = useEventBus() | ||
const noti = (options: NotiOptions) => bus.emit(options) | ||
install: (app: App, initialOptions: NotiOptions) => { | ||
const notiArguments = useNotiProvideArguments(initialOptions) | ||
|
||
// Plugin code goes here | ||
app.provide(INJECT_KEY, noti) | ||
app.provide(...notiArguments) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
import { NotiPlugin } from '@vue3-noti/core' | ||
import type { NotiOptions } from '@vue3-noti/core' | ||
import { createApp } from 'vue' | ||
import './style.css' | ||
import App from './App.vue' | ||
|
||
createApp(App).use(NotiPlugin).mount('#app') | ||
const notiOptions: NotiOptions = { | ||
message: 'Hello', | ||
type: 'success', | ||
duration: 1000, | ||
position: 'top-right', | ||
} | ||
|
||
createApp(App).use(NotiPlugin, notiOptions).mount('#app') |