Skip to content

Commit e3ffc97

Browse files
feat: use suspense to display content
1 parent 49e20ce commit e3ffc97

File tree

5 files changed

+33
-18
lines changed

5 files changed

+33
-18
lines changed

src/runtime/components/NinjaToaster.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { PropType } from 'vue'
22
import {
3+
Suspense,
34
Transition,
45
computed,
56
defineComponent,
@@ -14,7 +15,7 @@ import { defu } from 'defu'
1415

1516
import type { NinjaToastEventBus } from '../events'
1617
import type { NinjaToasterProps } from '../../types'
17-
import { usePausableTimeout } from '../composables/usePausableTimeout'
18+
import { useNinjaPausableTimeout } from '../composables/useNinjaPausableTimeout'
1819
import { useNinjaToasterContainer } from '../composables/useNinjaToasterContainer'
1920
import { createNinjaToasterState } from '../composables/useNinjaToasterState'
2021
import { type NinjaToasterRenderQueue, createRenderQueue } from '../queue'
@@ -66,7 +67,7 @@ export default defineComponent({
6667
})
6768

6869
const { container, containerId } = useNinjaToasterContainer(theme)
69-
const timer = usePausableTimeout(() => {
70+
const timer = useNinjaPausableTimeout(() => {
7071
close()
7172
}, props.duration)
7273

@@ -218,6 +219,7 @@ export default defineComponent({
218219
})
219220

220221
return () => {
222+
const contentSuspense = () => h(Suspense, null, [content.value])
221223
const wrapper = withDirectives(
222224
h(
223225
'div',
@@ -235,7 +237,7 @@ export default defineComponent({
235237
onKeydown,
236238
onClick
237239
},
238-
content.value
240+
contentSuspense
239241
),
240242
[[vShow, isActive.value]]
241243
)

src/runtime/composables/usePausableTimeout.ts renamed to src/runtime/composables/useNinjaPausableTimeout.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { ref } from 'vue'
22
import { tryOnBeforeUnmount } from '@vueuse/core'
3+
import { NinjaPausableTimeout } from '../../types'
34

4-
export function usePausableTimeout(callback: Function, timeout?: number) {
5+
6+
export function useNinjaPausableTimeout(callback: Function, timeout?: number): NinjaPausableTimeout {
57
const pausedAt = ref(0)
68
const startedAt = ref(0)
79
const remaining = ref(0)

src/runtime/composables/useNinjaToasterState.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
import type { InjectionKey, Ref } from 'vue'
1+
import type { InjectionKey } from 'vue'
22
import { computed, inject, provide, ref } from 'vue'
33
import { tryOnBeforeUnmount, tryOnMounted } from '@vueuse/core'
44

5-
import type { usePausableTimeout } from './usePausableTimeout'
6-
7-
export interface NinjaToasterState {
8-
isHovered: Ref<boolean>
9-
isActive: Ref<boolean>
10-
timer: ReturnType<typeof usePausableTimeout>
11-
duration: number
12-
click: (event: Event) => void | Promise<void>
13-
close: () => void | Promise<void>
14-
}
5+
import { NinjaToasterState } from '../../types'
156

167
export const NinjaToasterStateKey = Symbol.for(
178
'NinjaToasterState'

src/runtime/create.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type App, type VNode, h, render } from 'vue'
1+
import { type DefineComponent, type App, type VNode, h, render } from 'vue'
22
import { defu } from 'defu'
33

44
import type { NinjaToasterTheme, NinjaToasterBaseProps, NinjaToasterProps, NinjaToasterShow } from '../types'
@@ -9,8 +9,10 @@ import NinjaToaster from './components/NinjaToaster'
99
import { resolveComponent, useAppConfig, useNuxtApp } from '#imports'
1010
import type * as _AllComponents from '#components'
1111

12+
type DefaultProps = InstanceType<DefineComponent>['$props']
13+
1214
type ComponentProps<T> = T extends keyof typeof _AllComponents
13-
? InstanceType<(typeof _AllComponents)[T]>['$props']
15+
? Omit<InstanceType<(typeof _AllComponents)[T]>['$props'], keyof DefaultProps>
1416
: undefined
1517

1618
function createElement() {

src/types.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Component, TransitionProps } from 'vue'
22

3-
43
export interface ModuleOptions {
54
installPlugin?: boolean
65
}
@@ -31,3 +30,22 @@ export interface NinjaToasterTheme {
3130
transition?: TransitionProps
3231
maxToasts?: number
3332
}
33+
34+
export interface NinjaPausableTimeout {
35+
pausedAt: Ref<number>
36+
startedAt: Ref<number>
37+
remaining: Ref<number>
38+
start: () => void
39+
stop: () => void
40+
pause: () => void
41+
resume: () => void
42+
}
43+
44+
export interface NinjaToasterState {
45+
isHovered: Ref<boolean>
46+
isActive: Ref<boolean>
47+
timer: NinjaPausableTimeout
48+
duration: number
49+
click: (event: Event) => void | Promise<void>
50+
close: () => void | Promise<void>
51+
}

0 commit comments

Comments
 (0)