-
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
8 changed files
with
172 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
description: 'added useUiNotification composable', | ||
link: 'https://github.com/vuestorefront/vue-storefront/issues/5363', | ||
isBreaking: false, | ||
breakingChanges: [], | ||
author: 'Łukasz Jędrasik', | ||
linkToGitHubAccount: 'https://github.com/lukaszjedrasik' | ||
}; |
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
94 changes: 94 additions & 0 deletions
94
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,94 @@ | ||
<template> | ||
<transition-group tag="div" class="notifications" name="slide-fade"> | ||
<SfNotification | ||
v-for="notification in notifications" | ||
:key="notification.id" | ||
:message="notification.message" | ||
:action="notification.action && notification.action.text" | ||
:type="notification.type" | ||
@click:close="notification.dismiss" | ||
@click:action="notification.action && notification.action.onClick()" | ||
visible | ||
> | ||
<template #icon v-if="notification.icon"> | ||
<SfIcon :icon="notification.icon" color="white"/> | ||
</template> | ||
</SfNotification> | ||
</transition-group> | ||
</template> | ||
|
||
<script> | ||
import { SfNotification, SfIcon } from '@storefront-ui/vue'; | ||
import { useUiNotification } from '~/composables'; | ||
|
||
export default { | ||
name: 'Notification', | ||
components: { | ||
SfNotification, | ||
SfIcon | ||
}, | ||
setup () { | ||
const { notifications } = useUiNotification(); | ||
|
||
return { | ||
notifications | ||
}; | ||
} | ||
}; | ||
</script> | ||
|
||
<style scoped lang="scss"> | ||
.notifications { | ||
position: fixed; | ||
width: 100%; | ||
left: 0; | ||
bottom: 0; | ||
right: 0; | ||
z-index: 9; | ||
@include for-desktop { | ||
top: 100px; | ||
left: auto; | ||
bottom: auto; | ||
right: 5%; | ||
width: 320px; | ||
} | ||
} | ||
.sf-notification { | ||
max-width: 100%; | ||
margin: var(--spacer-xs) auto 0 auto; | ||
&:first-child { | ||
margin-top: 0; | ||
} | ||
@include for-mobile { | ||
--notification-border-radius: 0; | ||
--notification-max-width: 100%; | ||
--notification-background: var(--c-link); | ||
--notification-font-size: var(--font-size--sm); | ||
--notification-font-family: var(--font-family--primary); | ||
--notification-font-weight: var(--font-weight--normal); | ||
--notification-padding: var(--spacer-base) var(--spacer-lg); | ||
} | ||
@include for-desktop { | ||
margin: 0 0 var(--spacer-xs) 0; | ||
} | ||
} | ||
.slide-fade-enter-active, | ||
.slide-fade-leave-active { | ||
transition: all 0.3s; | ||
@include for-desktop { | ||
transition: opacity 0.25s linear; | ||
} | ||
} | ||
.slide-fade-enter { | ||
transform: translateY(40px); | ||
@include for-desktop { | ||
opacity: 0; | ||
} | ||
} | ||
.slide-fade-leave-to { | ||
transform: translateY(80px); | ||
@include for-desktop { | ||
opacity: 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 | ||
}; |
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
import { computed, reactive } from '@vue/composition-api'; | ||
|
||
interface UiNotification { | ||
message: string; | ||
action: { text: string; onClick: Function }; | ||
type: 'danger' | 'success' | 'info'; | ||
icon: string; | ||
persist: boolean; | ||
id: symbol; | ||
dismiss: () => void; | ||
} | ||
|
||
interface Notifications { | ||
notifications: Array<UiNotification>; | ||
} | ||
|
||
const state = reactive<Notifications>({ | ||
notifications: [] | ||
}); | ||
const maxVisibleNotifications = 3; | ||
const timeToLive = 3000; | ||
|
||
const useUiNotification = () => { | ||
const send = (notification: UiNotification) => { | ||
const id = Symbol(); | ||
|
||
const dismiss = () => { | ||
const index = state.notifications.findIndex(notification => notification.id === id); | ||
|
||
if (index !== -1) state.notifications.splice(index, 1); | ||
}; | ||
|
||
const newNotification = { | ||
...notification, | ||
id, | ||
dismiss | ||
}; | ||
|
||
state.notifications.push(newNotification); | ||
if (state.notifications.length > maxVisibleNotifications) state.notifications.shift(); | ||
|
||
if (!notification.persist) { | ||
setTimeout(dismiss, timeToLive); | ||
} | ||
}; | ||
|
||
return { | ||
send, | ||
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