Skip to content

Commit

Permalink
feat(toast#4373): add bottom-center and top-center positions
Browse files Browse the repository at this point in the history
  • Loading branch information
m0ksem committed Oct 17, 2024
1 parent f811359 commit 8c5d061
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
29 changes: 29 additions & 0 deletions packages/ui/src/components/va-toast/VaToast.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import VaToastDemo from './VaToast.demo.vue'
import VaToast from './VaToast.vue'
import { StoryFn } from '@storybook/vue3'
import { expect } from '@storybook/jest'
import { useToast } from './index'

export default {
title: 'VaToast',
Expand Down Expand Up @@ -30,3 +31,31 @@ export const Color: StoryFn = () => ({
<VaToast inline color="danger" title="Title" message="Message" :duration="99999999" />
`,
})

export const PositionTopenter: StoryFn = () => ({
components: { VaToast: VaToast },

setup () {
const { notify } = useToast()

return {
notify,
}
},

template: '<button @click="notify({ message: \'Test\', position: \'top-center\' })">Show</button>',
})

export const PositionBottomCenter: StoryFn = () => ({
components: { VaToast: VaToast },

setup () {
const { notify } = useToast()

return {
notify,
}
},

template: '<button @click="notify({ message: \'Test\', position: \'bottom-center\' })">Show</button>',
})
25 changes: 17 additions & 8 deletions packages/ui/src/components/va-toast/VaToast.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,23 @@ const durationComputed = useNumericProp('duration') as ComputedRef<number>
const visible = ref(false)
const positionX = computed(() => {
return props.position.includes('right') ? 'right' : 'left'
})
const getPositionStyle = () => {
const vertical = props.position.includes('top') ? 'top' : 'bottom'
const horizontal = props.position.includes('center') ? 'center' : props.position.includes('right') ? 'right' : 'left'
if (horizontal === 'center') {
return {
[vertical]: `${offsetYComputed.value}px`,
left: '50%',
transform: 'translateX(-50%)',
}
}
const positionY = computed(() => {
return props.position.includes('top') ? 'top' : 'bottom'
})
return {
[vertical]: `${offsetYComputed.value}px`,
[horizontal]: `${offsetXComputed.value}px`,
}
}
const toastClasses = computed(() => [
props.customClass,
Expand All @@ -122,8 +132,7 @@ const toastClasses = computed(() => [
])
const toastStyles = computed(() => ({
[positionY.value]: `${offsetYComputed.value}px`,
[positionX.value]: `${offsetXComputed.value}px`,
...getPositionStyle(),
backgroundColor: getColor(props.color),
color: textColorComputed.value,
}))
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/src/components/va-toast/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export type ToastPosition =
| 'top-left'
| 'bottom-right'
| 'bottom-left'
| 'top-center'
| 'bottom-center'

export interface ToastOptions {
/** Title */
Expand Down

0 comments on commit 8c5d061

Please sign in to comment.