Skip to content

Commit dc10625

Browse files
arashsheydaantfu
andauthored
feat(ui): NNotification component (#443)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent f708a19 commit dc10625

11 files changed

Lines changed: 85 additions & 66 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<script setup lang='ts'>
2+
import { ref } from 'vue'
3+
4+
import type { DevtoolsUiShowNotificationPosition } from '../composables/notification'
5+
import { devtoolsUiProvideNotificationFn } from '../composables/notification'
6+
7+
const show = ref(false)
8+
const icon = ref<string>()
9+
const text = ref<string>()
10+
const classes = ref<string>()
11+
const position = ref<DevtoolsUiShowNotificationPosition>('top-center')
12+
13+
devtoolsUiProvideNotificationFn((data) => {
14+
text.value = data.message
15+
icon.value = data.icon
16+
classes.value = data.classes ?? 'text-primary border-primary'
17+
icon.value = data.icon
18+
show.value = true
19+
position.value = data.position ?? 'top-center'
20+
setTimeout(() => {
21+
show.value = false
22+
}, data.duration ?? 1500)
23+
})
24+
</script>
25+
26+
<template>
27+
<div
28+
fixed left-0 right-0 z-999 text-center
29+
:class="[
30+
{ 'pointer-events-none overflow-hidden': !show },
31+
{ 'top-0': position.startsWith('top') },
32+
{ 'bottom-0': position.startsWith('bottom') },
33+
]"
34+
>
35+
<div flex :style="{ justifyContent: position.includes('right') ? 'right' : position.includes('left') ? 'left' : 'center' }">
36+
<div
37+
border="~ base"
38+
flex="~ inline gap2"
39+
m-3 inline-block items-center rounded px-4 py-1 transition-all duration-300 bg-base
40+
:style="show ? {} : { transform: `translateY(${position.startsWith('top') ? '-' : ''}300%)` }"
41+
:class="[show ? 'shadow' : 'shadow-none', classes]"
42+
>
43+
<div v-if="icon" :class="icon" />
44+
<div>{{ text }}</div>
45+
</div>
46+
</div>
47+
</div>
48+
</template>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
let _devtoolsUiShowNotification: typeof devtoolsUiShowNotification
2+
3+
export type DevtoolsUiShowNotificationPosition = 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right'
4+
5+
export function devtoolsUiShowNotification(data: {
6+
message: string
7+
icon?: string
8+
classes?: string
9+
duration?: number
10+
position?: DevtoolsUiShowNotificationPosition
11+
}) {
12+
_devtoolsUiShowNotification?.(data)
13+
}
14+
15+
export function devtoolsUiProvideNotificationFn(fn: typeof devtoolsUiShowNotification) {
16+
_devtoolsUiShowNotification = fn
17+
}

packages/devtools-ui-kit/src/module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { fileURLToPath } from 'node:url'
2-
import { addComponentsDir, createResolver, defineNuxtModule, installModule } from '@nuxt/kit'
2+
import { addComponentsDir, addImportsDir, createResolver, defineNuxtModule, installModule } from '@nuxt/kit'
33
import defu from 'defu'
44
import { extendUnocssOptions } from './unocss'
55

@@ -24,6 +24,9 @@ export default defineNuxtModule<ModuleOptions>({
2424
dev: false,
2525
},
2626
async setup(options, nuxt) {
27+
// composables
28+
addImportsDir(rPath('./composables'))
29+
2730
// Standard components
2831
addComponentsDir({ path: rPath('./components') })
2932

packages/devtools/client/app.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ registerCommands(() =>
8282
<template>
8383
<div fixed inset-0 h-screen w-screen font-sans>
8484
<NuxtLoadingIndicator />
85-
<Notification />
85+
<NNotification />
8686
<NLoading v-if="waiting">
8787
Connecting....
8888
</NLoading>

packages/devtools/client/components/AssetDetails.vue

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ async function saveTextContent() {
4242
}], '')
4343
editDialog.value = false
4444
textContentCounter.value++
45-
showNotification({
45+
devtoolsUiShowNotification({
4646
message: 'Updated',
4747
icon: 'i-carbon-checkmark',
4848
classes: 'text-green',
4949
})
5050
}
5151
catch (error) {
52-
showNotification({
52+
devtoolsUiShowNotification({
5353
message: 'Something went wrong!',
5454
icon: 'i-carbon-warning',
5555
classes: 'text-red',
@@ -129,14 +129,14 @@ async function deleteAsset() {
129129
await rpc.deleteStaticAsset(await ensureDevAuthToken(), asset.value.filePath)
130130
asset.value = undefined as any
131131
deleteDialog.value = false
132-
showNotification({
132+
devtoolsUiShowNotification({
133133
message: 'Asset deleted',
134134
icon: 'i-carbon-checkmark',
135135
classes: 'text-green',
136136
})
137137
}
138138
catch (error) {
139-
showNotification({
139+
devtoolsUiShowNotification({
140140
message: 'Something went wrong!',
141141
icon: 'i-carbon-warning',
142142
classes: 'text-red',
@@ -151,7 +151,7 @@ async function renameAsset() {
151151
const oldName = parts.slice(-1)[0].split('.').slice(0, -1).join('.')
152152
153153
if (!newName.value || newName.value === oldName) {
154-
return showNotification({
154+
return devtoolsUiShowNotification({
155155
message: 'Please enter a new name',
156156
icon: 'i-carbon-warning',
157157
classes: 'text-orange',
@@ -164,14 +164,14 @@ async function renameAsset() {
164164
await rpc.renameStaticAsset(await ensureDevAuthToken(), asset.value.filePath, fullPath)
165165
asset.value = undefined as any
166166
renameDialog.value = false
167-
showNotification({
167+
devtoolsUiShowNotification({
168168
message: 'Asset renamed',
169169
icon: 'i-carbon-checkmark',
170170
classes: 'text-green',
171171
})
172172
}
173173
catch (error) {
174-
showNotification({
174+
devtoolsUiShowNotification({
175175
message: 'Something went wrong!',
176176
icon: 'i-carbon-warning',
177177
classes: 'text-red',

packages/devtools/client/components/AssetDropZone.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function setFiles(data: FileList | null) {
5151
}
5252
else {
5353
if (file.type === '') {
54-
showNotification({
54+
devtoolsUiShowNotification({
5555
message: 'Folders are not supported yet',
5656
icon: 'carbon:face-dissatisfied',
5757
classes: 'text-orange',
@@ -61,7 +61,7 @@ function setFiles(data: FileList | null) {
6161
newFiles.push(file)
6262
}
6363
else {
64-
showNotification({
64+
devtoolsUiShowNotification({
6565
message: `"${file.type}" file type is not allowed`,
6666
icon: 'carbon:face-dissatisfied',
6767
classes: 'text-orange',
@@ -96,14 +96,14 @@ async function uploadFiles() {
9696
}
9797
await rpc.writeStaticAssets(await ensureDevAuthToken(), [...uploadFiles], props.folder).then(() => {
9898
close()
99-
showNotification({
99+
devtoolsUiShowNotification({
100100
message: 'Files uploaded successfully!',
101101
icon: 'i-carbon:checkmark',
102102
})
103103
}).catch((error) => {
104104
console.error(error)
105105
close()
106-
showNotification({
106+
devtoolsUiShowNotification({
107107
message: `Error uploading files: ${error}`,
108108
icon: 'i-carbon-warning',
109109
classes: 'text-red',

packages/devtools/client/components/Notification.vue

Lines changed: 0 additions & 34 deletions
This file was deleted.

packages/devtools/client/composables/dev-auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export async function ensureDevAuthToken() {
3737
isDevAuthed.value = await rpc.verifyAuthToken(devAuthToken.value!)
3838
if (!isDevAuthed.value) {
3939
devAuthToken.value = null
40-
showNotification({
40+
devtoolsUiShowNotification({
4141
message: 'Invalid auth token, action canceled',
4242
icon: 'i-carbon-warning-alt',
4343
classes: 'text-red',
@@ -74,7 +74,7 @@ async function authConfirmAction() {
7474

7575
if (result === false) {
7676
// @unocss-include
77-
showNotification({
77+
devtoolsUiShowNotification({
7878
message: 'Action canceled',
7979
icon: 'carbon-close',
8080
classes: 'text-orange',

packages/devtools/client/composables/dialog.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,3 @@ interface RestartDialog {
1212
export function useRestartDialogs() {
1313
return useState<RestartDialog[]>('devtools:restart-dialogs', () => [])
1414
}
15-
16-
let _showNotification: typeof showNotification
17-
18-
export function showNotification(data: {
19-
message: string
20-
icon?: string
21-
classes?: string
22-
duration?: number
23-
}) {
24-
_showNotification?.(data)
25-
}
26-
27-
export function provideNotificationFn(fn: typeof showNotification) {
28-
_showNotification = fn
29-
}

packages/devtools/client/composables/editor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function useCopy() {
3333
return (text: string) => {
3434
clipboard.copy(text)
3535

36-
showNotification({
36+
devtoolsUiShowNotification({
3737
message: 'Copied to clipboard',
3838
icon: 'carbon-checkmark',
3939
})

0 commit comments

Comments
 (0)