-
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
7 changed files
with
170 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
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
108 changes: 108 additions & 0 deletions
108
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,108 @@ | ||
<template> | ||
<transition-group tag="div" class="notifications" :name="isMobile ? 'slide' : 'sf-fade'"> | ||
<SfNotification | ||
v-for="(notification, index) in notifications" | ||
:key="notification.id" | ||
:visible="true" | ||
:message="notification.message" | ||
:action="notification.action && notification.action.text" | ||
:type="notification.type" | ||
@click:close="remove(index)" | ||
@click:action="notification.action && notification.action.onClick()" | ||
> | ||
<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'; | ||
import { ref, onMounted, onBeforeUnmount} from '@vue/composition-api'; | ||
export default { | ||
name: 'Notification', | ||
components: { | ||
SfNotification, | ||
SfIcon | ||
}, | ||
setup () { | ||
const { notifications } = useUiNotification(); | ||
const isMobile = ref(false); | ||
const remove = (index) => notifications.value.splice(index, 1); | ||
const mobileHandler = (event) => { | ||
isMobile.value = event.matches; | ||
}; | ||
onMounted(() => { | ||
isMobile.value = | ||
Math.max(document.documentElement.clientWidth, window.innerWidth) <= | ||
1023; | ||
window.matchMedia('(max-width: 1023px)').addListener(mobileHandler); | ||
}); | ||
onBeforeUnmount(() => { | ||
window | ||
.matchMedia('(max-width: 1023px)') | ||
.removeListener(mobileHandler); | ||
}); | ||
return { | ||
notifications, | ||
remove, | ||
isMobile | ||
}; | ||
} | ||
}; | ||
</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-enter-active, | ||
.slide-leave-active { | ||
transition: all 0.3s; | ||
} | ||
.slide-enter { | ||
transform: translateY(40px); | ||
} | ||
.slide-leave-to { | ||
transform: translateY(80px); | ||
} | ||
</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 | ||
}; |
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
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; | ||
} | ||
|
||
interface Notifications { | ||
notifications: Array<UiNotification>; | ||
} | ||
|
||
const state = reactive<Notifications>({ | ||
notifications: [] | ||
}); | ||
|
||
const useUiNotification = () => { | ||
const remove = (id: symbol) => { | ||
const index = state.notifications.findIndex(notification => notification.id === id); | ||
|
||
if (index !== -1) state.notifications.splice(index, 1); | ||
}; | ||
|
||
const send = (notification: UiNotification) => { | ||
const id = Symbol(); | ||
const newNotification = { id, ...notification }; | ||
|
||
state.notifications.push(newNotification); | ||
if (state.notifications.length > 3) state.notifications.shift(); | ||
|
||
if (!notification.persist) { | ||
setTimeout(() => { | ||
remove(id); | ||
}, 3000); | ||
} | ||
}; | ||
|
||
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