11// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22// SPDX-License-Identifier: Apache-2.0
3- import React from 'react' ;
3+ import React , { useRef } from 'react' ;
4+
5+ import { useCurrentMode } from '@cloudscape-design/component-toolkit/internal' ;
46
57import { Alert as CloudscapeAlert , Button , SpaceBetween } from '~components' ;
68
@@ -66,6 +68,12 @@ interface CustomAlertProps {
6668}
6769
6870function CustomAlert ( { children, type, dismissible, action } : CustomAlertProps ) {
71+ const mode = useCurrentMode ( useRef ( document . body ) ) ;
72+ const background = backgrounds [ mode ] [ type ] ;
73+ const borderColor = borderColors [ mode ] [ type ] ;
74+ const borderWidth = borderWidths [ type ] ;
75+ const color = colors [ mode ] ;
76+ const iconColor = iconColors [ mode ] [ type ] ;
6977 return (
7078 < CloudscapeAlert
7179 dismissible = { dismissible }
@@ -74,22 +82,26 @@ function CustomAlert({ children, type, dismissible, action }: CustomAlertProps)
7482 i18nStrings = { i18nStrings }
7583 style = { {
7684 root : {
77- background : getBackground ( type ) ,
78- borderColor : getBorderColor ( type ) ,
85+ background,
86+ borderColor,
7987 borderRadius : '8px' ,
80- borderWidth : getBorderWidth ( type ) ,
81- color : getColor ( ) ,
88+ borderWidth,
89+ color,
8290 focusRing : {
8391 borderColor : palette . red60 ,
8492 borderRadius : '4px' ,
8593 borderWidth : '2px' ,
8694 } ,
8795 } ,
8896 icon : {
89- color : getIconColor ( type ) ,
97+ color : iconColor ,
9098 } ,
9199 dismissButton : {
92- color : getDismissButtonColor ( type ) ,
100+ color : {
101+ default : dismissButtonColors [ mode ] [ type ] . default ,
102+ hover : dismissButtonColors [ mode ] [ type ] . hover ,
103+ active : dismissButtonColors [ mode ] [ type ] . active ,
104+ } ,
93105 focusRing : {
94106 borderColor : palette . red60 ,
95107 borderRadius : '4px' ,
@@ -103,72 +115,106 @@ function CustomAlert({ children, type, dismissible, action }: CustomAlertProps)
103115 ) ;
104116}
105117
106- function getBackground ( type : string ) {
107- const backgrounds = {
108- info : `light-dark(${ palette . blue80 } , ${ palette . blue40 } )` ,
109- success : `light-dark(${ palette . green80 } , ${ palette . green20 } )` ,
110- error : `light-dark(${ palette . red80 } , ${ palette . red30 } )` ,
111- warning : `light-dark(${ palette . teal90 } , ${ palette . teal20 } )` ,
112- } ;
113- return backgrounds [ type as keyof typeof backgrounds ] ;
114- }
118+ const backgrounds = {
119+ light : {
120+ info : palette . blue80 ,
121+ success : palette . green80 ,
122+ error : palette . red80 ,
123+ warning : palette . teal90 ,
124+ } ,
125+ dark : {
126+ info : palette . blue40 ,
127+ success : palette . green20 ,
128+ error : palette . red30 ,
129+ warning : palette . teal20 ,
130+ } ,
131+ } ;
115132
116- function getColor ( ) {
117- return `light-dark(${ palette . neutral10 } , ${ palette . neutral100 } )` ;
118- }
133+ const colors = {
134+ light : palette . neutral10 ,
135+ dark : palette . neutral100 ,
136+ } ;
119137
120- function getBorderColor ( type : string ) {
121- const borderColors = {
122- info : `light-dark(${ palette . neutral80 } , ${ palette . neutral20 } )` ,
123- success : `light-dark(${ palette . green80 } , ${ palette . green30 } )` ,
124- error : `light-dark(${ palette . blue90 } , ${ palette . red60 } )` ,
125- warning : `light-dark(${ palette . orange80 } , ${ palette . orange40 } )` ,
126- } ;
127- return borderColors [ type as keyof typeof borderColors ] ;
128- }
138+ const borderColors = {
139+ light : {
140+ info : palette . neutral80 ,
141+ success : palette . green80 ,
142+ error : palette . blue90 ,
143+ warning : palette . orange80 ,
144+ } ,
145+ dark : {
146+ info : palette . neutral20 ,
147+ success : palette . green30 ,
148+ error : palette . red60 ,
149+ warning : palette . orange40 ,
150+ } ,
151+ } ;
129152
130- function getBorderWidth ( type : string ) {
131- const borderWidths = {
132- info : '4px' ,
133- success : '0px' ,
134- error : '6px' ,
135- warning : '0px' ,
136- } ;
137- return borderWidths [ type as keyof typeof borderWidths ] ;
138- }
153+ const borderWidths = {
154+ info : '4px' ,
155+ success : '0px' ,
156+ error : '6px' ,
157+ warning : '0px' ,
158+ } ;
139159
140- function getIconColor ( type : string ) {
141- const iconColors = {
142- info : `light-dark(${ palette . orange80 } , ${ palette . red30 } )` ,
143- success : `light-dark(${ palette . red60 } , ${ palette . red60 } )` ,
144- error : `light-dark(${ palette . blue80 } , ${ palette . blue40 } )` ,
145- warning : `light-dark(${ palette . neutral10 } , ${ palette . neutral90 } )` ,
146- } ;
147- return iconColors [ type as keyof typeof iconColors ] ;
148- }
160+ const iconColors = {
161+ light : {
162+ info : palette . orange80 ,
163+ success : palette . red60 ,
164+ error : palette . blue80 ,
165+ warning : palette . neutral10 ,
166+ } ,
167+ dark : {
168+ info : palette . red30 ,
169+ success : palette . red60 ,
170+ error : palette . blue40 ,
171+ warning : palette . neutral90 ,
172+ } ,
173+ } ;
149174
150- function getDismissButtonColor ( type : string ) {
151- const dismissButtonColors = {
175+ const dismissButtonColors = {
176+ light : {
152177 info : {
153- default : `light-dark( ${ palette . green60 } , ${ palette . neutral40 } )` ,
154- hover : `light-dark( ${ palette . neutral80 } , ${ palette . neutral20 } )` ,
155- active : `light-dark( ${ palette . neutral90 } , ${ palette . neutral10 } )` ,
178+ default : palette . green60 ,
179+ hover : palette . neutral80 ,
180+ active : palette . neutral90 ,
156181 } ,
157182 success : {
158- default : `light-dark( ${ palette . green60 } , ${ palette . green30 } )` ,
159- hover : `light-dark( ${ palette . green80 } , ${ palette . green20 } )` ,
160- active : `light-dark( ${ palette . green90 } , ${ palette . green10 } )` ,
183+ default : palette . green60 ,
184+ hover : palette . green80 ,
185+ active : palette . green90 ,
161186 } ,
162187 error : {
163- default : `light-dark( ${ palette . red60 } , ${ palette . red60 } )` ,
164- hover : `light-dark( ${ palette . red60 } , ${ palette . red20 } )` ,
165- active : `light-dark( ${ palette . red80 } , ${ palette . red10 } )` ,
188+ default : palette . red60 ,
189+ hover : palette . red60 ,
190+ active : palette . red80 ,
166191 } ,
167192 warning : {
168- default : `light-dark( ${ palette . orange60 } , ${ palette . orange40 } )` ,
169- hover : `light-dark( ${ palette . orange80 } , ${ palette . orange20 } )` ,
170- active : `light-dark( ${ palette . orange90 } , ${ palette . orange10 } )` ,
193+ default : palette . orange60 ,
194+ hover : palette . orange80 ,
195+ active : palette . orange90 ,
171196 } ,
172- } ;
173- return dismissButtonColors [ type as keyof typeof dismissButtonColors ] ;
174- }
197+ } ,
198+ dark : {
199+ info : {
200+ default : palette . neutral40 ,
201+ hover : palette . neutral20 ,
202+ active : palette . neutral10 ,
203+ } ,
204+ success : {
205+ default : palette . green30 ,
206+ hover : palette . green20 ,
207+ active : palette . green10 ,
208+ } ,
209+ error : {
210+ default : palette . red60 ,
211+ hover : palette . red20 ,
212+ active : palette . red10 ,
213+ } ,
214+ warning : {
215+ default : palette . orange40 ,
216+ hover : palette . orange20 ,
217+ active : palette . orange10 ,
218+ } ,
219+ } ,
220+ } ;
0 commit comments