-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
146 additions
and
5 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
62 changes: 62 additions & 0 deletions
62
packages/core/nuxt-theme-module/theme/components/Notification.vue
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,62 @@ | ||
<template> | ||
<div class="notifications"> | ||
<SfNotification | ||
v-for="notification in notifications" | ||
:key="notification.id" | ||
:visible="!!notification.id" | ||
:title="notification.title" | ||
:message="notification.message" | ||
:action="notification.action && notification.action.text" | ||
:type="notification.type" | ||
@click:close="removeNotification(notification.id)" | ||
@click:action="notification.action && notification.action.onClick()" | ||
> | ||
<template #icon="{icon}" v-if="notification.options && notification.options.icon"> | ||
<SfIcon :icon="notification.options.icon" :color="notification.options.iconColor || 'white'"/> | ||
</template> | ||
</SfNotification> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { SfNotification, SfIcon } from '@storefront-ui/vue'; | ||
import { useUINotification } from '~/composables'; | ||
export default { | ||
name: 'Notification', | ||
components: { | ||
SfNotification, | ||
SfIcon | ||
}, | ||
setup () { | ||
const { notifications, removeNotification } = useUINotification(); | ||
return { | ||
notifications, | ||
removeNotification | ||
}; | ||
} | ||
}; | ||
</script> | ||
|
||
<style scoped lang="scss"> | ||
.notifications { | ||
position: fixed; | ||
width: 100%; | ||
left: 0; | ||
right: 0; | ||
bottom: var(--bottom-navigation-height, 3.75rem); | ||
z-index: 9; | ||
@include for-desktop { | ||
left: auto; | ||
right: 5%; | ||
width: 320px; | ||
} | ||
} | ||
.sf-notification { | ||
margin: 0 auto var(--spacer-xs) auto; | ||
@include for-desktop { | ||
margin: 0 0 var(--spacer-xs) 0; | ||
} | ||
} | ||
</style> |
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,7 +1,9 @@ | ||
import useUiHelpers from './useUiHelpers'; | ||
import useUiState from './useUiState'; | ||
import useUINotification from './useUINotification'; | ||
|
||
export { | ||
useUiHelpers, | ||
useUiState | ||
useUiState, | ||
useUINotification | ||
}; |
55 changes: 55 additions & 0 deletions
55
packages/core/nuxt-theme-module/theme/composables/useUINotification/index.ts
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,55 @@ | ||
import { computed, reactive } from '@vue/composition-api'; | ||
|
||
interface Options { | ||
persist: boolean; | ||
timeToLive?: number; | ||
icon?: string; | ||
iconColor?: string; | ||
} | ||
|
||
interface Action { | ||
text: string; | ||
onClick?: Function; | ||
} | ||
|
||
interface Notification { | ||
type: string; | ||
title?: string; | ||
message: string; | ||
action: Action; | ||
options?: Options; | ||
} | ||
|
||
const state = reactive({ | ||
notifications: [] | ||
}); | ||
|
||
const useUINotification = () => { | ||
const removeNotification = (id: symbol) => { | ||
const index = state.notifications.findIndex(notification => notification.id === id); | ||
|
||
if (index !== -1) state.notifications.splice(index, 1); | ||
}; | ||
|
||
const spawnNotification = (notification: Notification) => { | ||
const id = Symbol(); | ||
const newNotification = { id, ...notification }; | ||
|
||
state.notifications.push(newNotification); | ||
if (state.notifications.length > 3) state.notifications.shift(); | ||
|
||
if (!notification.options?.persist) { | ||
setTimeout(() => { | ||
removeNotification(id); | ||
}, notification.options?.timeToLive || 3000); | ||
} | ||
}; | ||
|
||
return { | ||
spawnNotification, | ||
removeNotification, | ||
notifications: computed(() => state.notifications) | ||
}; | ||
}; | ||
|
||
export default useUINotification; |
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