-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathKToaster.vue
More file actions
221 lines (196 loc) · 6.03 KB
/
KToaster.vue
File metadata and controls
221 lines (196 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<template>
<TransitionGroup
class="k-toaster"
name="kongponents-slide-up-transition"
tag="div"
>
<div
v-for="toaster in toasterState"
:key="toaster.key"
class="toaster"
:class="`${toaster.appearance}`"
role="alert"
>
<div class="toaster-icon-container">
<component
:is="getToastIcon(toaster.appearance)"
class="toaster-icon"
:color="`var(--kui-color-text, ${KUI_COLOR_TEXT})`"
decorative
/>
</div>
<div class="toaster-content">
<span
v-if="toaster.title"
class="toaster-title"
>
{{ toaster.title }}
</span>
<p
v-if="toaster.message"
class="toaster-message"
>
{{ toaster.message }}
</p>
</div>
<button
aria-label="Close"
class="toaster-close-icon"
data-testid="toaster-close-icon"
type="button"
@click.stop="emit('close', toaster.key)"
>
<CloseIcon
:color="`var(--kui-color-text-neutral-weak, ${KUI_COLOR_TEXT_NEUTRAL_WEAK})`"
:size="KUI_ICON_SIZE_50"
/>
</button>
</div>
</TransitionGroup>
</template>
<script lang="ts" setup>
import type { ToasterAppearance, ToasterProps, ToasterEmits } from '@/types'
import { InfoIcon, CheckCircleIcon, WarningIcon, ClearIcon, KongIcon, CloseIcon } from '@kong/icons'
import { KUI_COLOR_TEXT, KUI_COLOR_TEXT_NEUTRAL_WEAK, KUI_ICON_SIZE_50 } from '@kong/design-tokens'
type ToastIcon = typeof InfoIcon | typeof CheckCircleIcon | typeof WarningIcon | typeof ClearIcon | typeof KongIcon
const {
toasterState = [],
} = defineProps<ToasterProps>()
const emit = defineEmits<ToasterEmits>()
const getToastIcon = (appearance?: ToasterAppearance): ToastIcon => {
switch (appearance) {
case 'info':
return InfoIcon
case 'success':
return CheckCircleIcon
case 'warning':
return WarningIcon
case 'danger':
return ClearIcon
case 'system':
return KongIcon
default:
return InfoIcon // info as default in case of invalid appearance
}
}
</script>
<style lang="scss">
// Global styles for shared container and instance wrappers
#kongponents-toaster-container {
bottom: 16px;
display: flex;
flex-direction: column;
gap: var(--kui-space-50, $kui-space-50);
pointer-events: none;
position: fixed;
right: 50%;
transform: translateX(50%);
width: 90%;
@media (min-width: $kui-breakpoint-mobile) {
right: 16px;
transform: none;
width: 400px;
}
}
// Wrapper styles for instance separation
[id^="kongponents-toaster-wrapper-"] {
display: flex;
flex-direction: column;
gap: var(--kui-space-50, $kui-space-50);
pointer-events: auto;
// Hide wrapper if it contains no toasts
&:not(:has(.toaster)) {
display: none;
}
}
</style>
<style lang="scss" scoped>
.k-toaster {
display: flex;
flex-direction: column;
gap: var(--kui-space-50, $kui-space-50);
.toaster {
align-items: flex-start;
align-items: center;
background-color: var(--kui-color-background-inverse, $kui-color-background-inverse);
border-radius: var(--kui-border-radius-30, $kui-border-radius-30);
box-shadow: var(--kui-shadow, $kui-shadow);
box-sizing: border-box;
color: var(--kui-color-text-inverse, $kui-color-text-inverse);
display: flex;
gap: var(--kui-space-50, $kui-space-50);
padding: var(--kui-space-50, $kui-space-50);
width: 100%;
.toaster-icon-container {
align-items: center;
background-color: var(--kui-color-background-info-weak, $kui-color-background-info-weak); // info appearance as default in case of invalid appearance
border-radius: var(--kui-border-radius-circle, $kui-border-radius-circle);
display: flex;
height: 32px;
justify-content: center;
width: 32px;
}
.toaster-content {
display: flex;
flex: 1;
flex-direction: column;
gap: var(--kui-space-30, $kui-space-30);
min-width: 0;
overflow-wrap: break-word;
.toaster-title {
font-family: var(--kui-font-family-text, $kui-font-family-text);
font-size: var(--kui-font-size-50, $kui-font-size-50);
font-weight: var(--kui-font-weight-semibold, $kui-font-weight-semibold);
line-height: var(--kui-line-height-40, $kui-line-height-40);
}
.toaster-message {
font-family: var(--kui-font-family-text, $kui-font-family-text);
font-size: var(--kui-font-size-30, $kui-font-size-30);
font-weight: var(--kui-font-weight-regular, $kui-font-weight-regular);
line-height: var(--kui-line-height-30, $kui-line-height-30);
margin: var(--kui-space-0, $kui-space-0);
}
}
.toaster-close-icon {
@include defaultButtonReset;
border-radius: var(--kui-border-radius-20, $kui-border-radius-20);
margin-left: var(--kui-space-auto, $kui-space-auto);
outline: none;
&:hover, &:focus {
:deep(#{$kongponentsKongIconSelector}) {
color: var(--kui-color-text-neutral-weaker, $kui-color-text-neutral-weaker) !important;
}
}
&:focus-visible {
box-shadow: var(--kui-shadow-focus, $kui-shadow-focus);
}
}
// appearances
&.info {
.toaster-icon-container {
background-color: var(--kui-color-background-info-weak, $kui-color-background-info-weak);
}
}
&.success {
.toaster-icon-container {
background-color: var(--kui-color-background-success-weak, $kui-color-background-success-weak);
}
}
&.warning {
.toaster-icon-container {
background-color: var(--kui-color-background-warning-weak, $kui-color-background-warning-weak);
}
}
&.danger {
.toaster-icon-container {
background-color: var(--kui-color-background-danger-weak, $kui-color-background-danger-weak);
}
}
&.system {
.toaster-icon-container {
background-color: var(--kui-color-background-neutral-weak, $kui-color-background-neutral-weak);
}
}
}
}
</style>