Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit c25ee4e

Browse files
authored
feat(toasts): export lower level toast components (#33)
To easier create custom toasts we need to export the lower level components that are used for the predefined toasts.
1 parent 7f65e0d commit c25ee4e

File tree

2 files changed

+60
-40
lines changed

2 files changed

+60
-40
lines changed

packages/core/src/Toast/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
export * from './toastActions'
22
export * from './useToasts'
33
export * from './context'
4+
export {
5+
ToastLabel,
6+
ToastMessage,
7+
ToastIconWrapper,
8+
ToastIconType,
9+
} from './toastCreators'

packages/core/src/Toast/toastCreators.tsx

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,29 @@ import {
2020
ProgressToast,
2121
BaseToastValue,
2222
} from './context'
23+
import { Theme } from '../theme'
24+
25+
const getIconColor = (theme: Theme, iconType?: ToastIconType) => {
26+
const { color } = theme
27+
28+
switch (iconType) {
29+
case ToastIconType.SUCCESS:
30+
return color.elementSuccess()
31+
case ToastIconType.WARNING:
32+
return color.elementWarning()
33+
case ToastIconType.ERROR:
34+
return color.elementError()
35+
case ToastIconType.ACTION:
36+
return color.elementPrimary()
37+
case ToastIconType.INFO:
38+
default:
39+
return color.text04()
40+
}
41+
}
2342

24-
const ToastLabel = styled(Typography).attrs({ variant: 'chip-tag-text' })<{
43+
export const ToastLabel = styled(Typography).attrs({
44+
variant: 'chip-tag-text',
45+
})<{
2546
readonly hasCloseButton: boolean
2647
readonly isError: boolean
2748
readonly hasEmphasis: boolean
@@ -54,38 +75,31 @@ const ToastLabel = styled(Typography).attrs({ variant: 'chip-tag-text' })<{
5475
`}
5576
`
5677

57-
const IconWrapper = styled.div`
78+
export enum ToastIconType {
79+
SUCCESS = 'success',
80+
WARNING = 'warning',
81+
INFO = 'info',
82+
ERROR = 'error',
83+
ACTION = 'action',
84+
}
85+
86+
export const ToastIconWrapper = styled.div<{
87+
readonly iconType?: ToastIconType
88+
}>`
5889
height: ${iconSize.medium};
5990
width: ${iconSize.medium};
6091
display: flex;
6192
align-items: center;
6293
justify-content: center;
94+
color: ${({ theme, iconType }) => getIconColor(theme, iconType)};
6395
`
6496

65-
const Message = styled(Typography).attrs({ variant: 'chip-tag-text' })`
97+
export const ToastMessage = styled(Typography).attrs({
98+
variant: 'chip-tag-text',
99+
})`
66100
white-space: normal;
67101
`
68102

69-
const SuccessNotificationIconColor = styled(IconWrapper)`
70-
color: ${({ theme }) => theme.color.elementSuccess()};
71-
`
72-
73-
const WarningIconColor = styled(IconWrapper)`
74-
color: ${({ theme }) => theme.color.elementWarning()};
75-
`
76-
77-
const InfoIconColor = styled(IconWrapper)`
78-
color: ${({ theme }) => theme.color.text04()};
79-
`
80-
81-
const ErrorIconColor = styled(IconWrapper)`
82-
color: ${({ theme }) => theme.color.elementError()};
83-
`
84-
85-
const ActionIconColor = styled(IconWrapper)`
86-
color: ${({ theme }) => theme.color.elementPrimary()};
87-
`
88-
89103
export type Toast = SimpleToast | ActionToast
90104

91105
type ToastCreator<T = Toast> = (toast: T) => BaseToastValue
@@ -113,13 +127,13 @@ export const createSuccessToast: SimpleToastCreator = ({
113127
</ToastLabel>
114128
)
115129
const icon = (
116-
<SuccessNotificationIconColor>
130+
<ToastIconWrapper iconType={ToastIconType.SUCCESS}>
117131
<Icon icon={CheckIcon} />
118-
</SuccessNotificationIconColor>
132+
</ToastIconWrapper>
119133
)
120134

121135
const messageComponent =
122-
message !== undefined ? <Message>{message}</Message> : undefined
136+
message !== undefined ? <ToastMessage>{message}</ToastMessage> : undefined
123137

124138
return {
125139
icon,
@@ -149,13 +163,13 @@ export const createErrorToast: SimpleToastCreator = ({
149163
</ToastLabel>
150164
)
151165
const icon = (
152-
<ErrorIconColor>
166+
<ToastIconWrapper iconType={ToastIconType.ERROR}>
153167
<Icon icon={AlertIcon} />
154-
</ErrorIconColor>
168+
</ToastIconWrapper>
155169
)
156170

157171
const messageComponent =
158-
message !== undefined ? <Message>{message}</Message> : undefined
172+
message !== undefined ? <ToastMessage>{message}</ToastMessage> : undefined
159173

160174
return {
161175
icon,
@@ -184,13 +198,13 @@ export const createWarningToast: SimpleToastCreator = ({
184198
</ToastLabel>
185199
)
186200
const icon = (
187-
<WarningIconColor>
201+
<ToastIconWrapper iconType={ToastIconType.WARNING}>
188202
<Icon icon={WarningIcon} />
189-
</WarningIconColor>
203+
</ToastIconWrapper>
190204
)
191205

192206
const messageComponent =
193-
message !== undefined ? <Message>{message}</Message> : undefined
207+
message !== undefined ? <ToastMessage>{message}</ToastMessage> : undefined
194208

195209
return {
196210
icon,
@@ -219,13 +233,13 @@ export const createInfoToast: SimpleToastCreator = ({
219233
</ToastLabel>
220234
)
221235
const icon = (
222-
<InfoIconColor>
236+
<ToastIconWrapper iconType={ToastIconType.INFO}>
223237
<Icon icon={InfoIcon} />
224-
</InfoIconColor>
238+
</ToastIconWrapper>
225239
)
226240

227241
const messageComponent =
228-
message !== undefined ? <Message>{message}</Message> : undefined
242+
message !== undefined ? <ToastMessage>{message}</ToastMessage> : undefined
229243

230244
return {
231245
icon,
@@ -264,13 +278,13 @@ export const createActionToast: ActionToastCreator = ({
264278
</>
265279
)
266280
const icon = (
267-
<ActionIconColor>
281+
<ToastIconWrapper iconType={ToastIconType.ACTION}>
268282
<Icon icon={action.icon} />
269-
</ActionIconColor>
283+
</ToastIconWrapper>
270284
)
271285

272286
const messageComponent =
273-
message !== undefined ? <Message>{message}</Message> : undefined
287+
message !== undefined ? <ToastMessage>{message}</ToastMessage> : undefined
274288

275289
return {
276290
icon,
@@ -309,7 +323,7 @@ export const createLoadingToast: SimpleToastCreator = ({
309323
)
310324

311325
const messageComponent =
312-
message !== undefined ? <Message>{message}</Message> : undefined
326+
message !== undefined ? <ToastMessage>{message}</ToastMessage> : undefined
313327

314328
return {
315329
icon,
@@ -356,7 +370,7 @@ export const createProgressToast: ProgressToastCreator = ({
356370
)
357371

358372
const messageComponent =
359-
message !== undefined ? <Message>{message}</Message> : undefined
373+
message !== undefined ? <ToastMessage>{message}</ToastMessage> : undefined
360374

361375
return {
362376
icon,

0 commit comments

Comments
 (0)