Skip to content

Commit 683b0bf

Browse files
committed
refactor: CToast, CToaster
- enable CToast display manipulation by 'show' prop, - fix props inheritance, - remove broken animation system (to rework), - add min-width to toast and toaster, - fix z-index
1 parent 080b8df commit 683b0bf

File tree

3 files changed

+53
-56
lines changed

3 files changed

+53
-56
lines changed

src/components/Toast/CToast.vue

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11
<template>
2-
<transition
3-
:name="0 ? null : 'fade'"
4-
:appear="true"
5-
v-if="!isClosed"
2+
<div
3+
:class="toastClasses"
4+
role="alert"
5+
aria-live="assertive"
6+
aria-atomic="true"
7+
:style="computedStyles"
8+
v-if="isShowed"
69
>
7-
<div
8-
:class="[toastClasses]"
9-
role="alert"
10-
aria-live="assertive"
11-
aria-atomic="true"
12-
style="z-index:1100"
13-
:style="computedStyles"
14-
>
15-
<div v-if="!props.noHeader" class="c-toast-header">
10+
<div v-if="!props.noHeader" class="c-toast-header">
11+
<slot name="title">
1612
<strong class="c-mr-auto" v-html="props.titleHtml"></strong>
17-
<CButtonClose
18-
v-if="!props.noCloseButton"
19-
@click="close()"
20-
class="c-ml-2 c-mb-1"
21-
/>
22-
</div>
23-
<slot :close="close">
24-
<div class="c-toast-body" v-html="props.bodyHtml">
25-
</div>
2613
</slot>
14+
<CButtonClose
15+
v-if="!props.noCloseButton"
16+
@click="close()"
17+
class="c-ml-2 c-mb-1"
18+
/>
2719
</div>
28-
</transition>
20+
<slot>
21+
<div class="c-toast-body" v-html="props.bodyHtml"></div>
22+
</slot>
23+
</div>
2924
</template>
3025

3126
<script>
3227
import toastMixin from './toastMixin'
3328
import CButtonClose from '../Button/CButtonClose'
29+
const props = Object.assign({}, toastMixin.props, {
30+
show: Boolean
31+
})
3432
3533
export default {
3634
name: 'CToast',
3735
mixins: [ toastMixin ],
36+
props,
3837
components: {
3938
CButtonClose
4039
},
@@ -45,37 +44,52 @@ export default {
4544
},
4645
data () {
4746
return {
48-
isClosed: false,
47+
isShowed: this.show,
4948
timeout: null
5049
}
5150
},
51+
watch: {
52+
show (val) {
53+
this.isShowed = val
54+
}
55+
},
5256
mounted () {
5357
if (this.props.autohide) {
5458
this.setAutohide()
5559
}
5660
},
5761
computed: {
5862
toastClasses () {
59-
return ['c-toast',
63+
return [
64+
'c-toast',
6065
{
61-
'c-show': this.props.show,
66+
'c-show': this.isShowed,
6267
'c-full': this.props.position.includes('full')
6368
}
6469
]
6570
},
71+
directlyDeclaredProps () {
72+
return Object.keys(this.$options.propsData)
73+
},
74+
injectedProps () {
75+
return this.toaster && this.toaster.props ? this.toaster.props : {}
76+
},
6677
props () {
67-
return Object.keys(toastMixin.props).reduce((props, key) => {
68-
const propUndefined = !Object.keys(this.$options.propsData).includes(key)
69-
const propInheritedFromToaster = propUndefined && this.toaster.props[key]
70-
props[key] = propInheritedFromToaster ? this.toaster.props[key] : this[key]
71-
return props
78+
return Object.keys(toastMixin.props).reduce((computedProps, key) => {
79+
const propIsDirectlyDeclared = this.directlyDeclaredProps.includes(key)
80+
const parentPropExists = this.injectedProps[key] !== undefined
81+
const propIsInherited = parentPropExists && !propIsDirectlyDeclared
82+
computedProps[key] = propIsInherited ? this.injectedProps[key] : this[key]
83+
return computedProps
7284
}, {})
7385
}
7486
},
7587
methods: {
7688
close () {
77-
this.isClosed = true
89+
this.isShowed = false
90+
this.$emit('update:show', false)
7891
this.$el.removeEventListener('mouseover', this.onHover)
92+
this.$el.removeEventListener('mouseout', this.onHoverOut)
7993
},
8094
onHover () {
8195
this.$el.removeEventListener('mouseover', this.onHover)
@@ -100,12 +114,6 @@ export default {
100114
.toast.full {
101115
max-width: 100%;
102116
}
103-
.fade-enter, .fade-leave-to {
104-
opacity: 0;
105-
}
106-
.fade-enter-active, .fade-leave-active {
107-
transition: opacity .3s;
108-
}
109117
@import "~@coreui/coreui/scss/partials/toasts.scss";
110118
@import "~@coreui/coreui/scss/utilities/_spacing.scss";
111119

src/components/Toast/CToaster.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<script>
1111
import toastMixin from './toastMixin'
12+
1213
export default {
1314
name: 'CToaster',
1415
provide () {
@@ -34,9 +35,6 @@ export default {
3435
</script>
3536

3637
<style lang="scss">
37-
.toaster {
38-
z-index: 1100
39-
}
4038
@import "~@coreui/coreui/scss/partials/toasts.scss";
4139
@import "~@coreui/coreui/scss/utilities/_flex.scss";
4240
</style>

src/components/Toast/toastMixin.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,19 @@ export default {
1010
].includes(position)
1111
}
1212
},
13-
show: Boolean,
14-
autohide: Number,
13+
titleHtml: String,
14+
bodyHtml: String,
1515
noHeader: Boolean,
16-
titleHtml: {
17-
type: String,
18-
default: 'CToast component'
19-
},
20-
bodyHtml: {
21-
type: String,
22-
default: 'Hello, world! This is a <b>toast</b> message.'
23-
},
16+
autohide: Number,
2417
noCloseButton: Boolean
2518
},
2619
computed: {
2720
computedStyles () {
2821
const position = this.props ? this.props.position : this.position
2922
if (position !== 'static' && !this.toaster) {
3023
return [
24+
{ 'z-index': 1100 },
25+
{ 'min-width': '350px' },
3126
{ position: 'fixed' },
3227
this.getVerticalPosition(this.position),
3328
this.getHorizontalPosition(this.position)
@@ -37,11 +32,7 @@ export default {
3732
},
3833
methods: {
3934
getVerticalPosition (position) {
40-
if (position.includes('bottom')) {
41-
return { bottom: 0 }
42-
} else {
43-
return { top: 0 }
44-
}
35+
return position.includes('bottom') ? { bottom: 0 } : { top: 0 }
4536
},
4637
getHorizontalPosition (position) {
4738
if (position.includes('right')) {

0 commit comments

Comments
 (0)