1
+ // Based on https://github.com/leerob/leerob.io/blob/main/components/SuccessMessage.tsx
2
+ import React , { useEffect , useState } from 'react' ;
3
+
4
+ export default function Alert ( { type= "message" , children } ) {
5
+ // An success message is green with a checkmark
6
+ // An error message is red with an x
7
+ // An info message is blue with a question mark
8
+ // An warning message is yellow with a warning sign
9
+ // An message is black with a question mark
10
+ const [ color , setColor ] = useState ( ) ;
11
+ const [ icon , setIcon ] = useState ( ) ;
12
+
13
+ useEffect ( ( ) => {
14
+ if ( type === 'success' ) {
15
+ setColor ( "text-green-700 dark:text-green-400" )
16
+ setIcon ( "M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" )
17
+ } else if ( type === "error" ) {
18
+ setColor ( "text-red-700 dark:text-red-400" )
19
+ setIcon ( "M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" )
20
+ } else if ( type === "info" ) {
21
+ setColor ( "text-blue-700 dark:text-blue-400" )
22
+ setIcon ( "M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-8-3a1 1 0 00-.867.5 1 1 0 11-1.731-1A3 3 0 0113 8a3.001 3.001 0 01-2 2.83V11a1 1 0 11-2 0v-1a1 1 0 011-1 1 1 0 100-2zm0 8a1 1 0 100-2 1 1 0 000 2z" )
23
+ } else if ( type === "warning" ) {
24
+ setColor ( "text-yellow-700 dark:text-yellow-400" )
25
+ setIcon ( "M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z" )
26
+ } else {
27
+ setColor ( "text-black" )
28
+ setIcon ( "M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-8-3a1 1 0 00-.867.5 1 1 0 11-1.731-1A3 3 0 0113 8a3.001 3.001 0 01-2 2.83V11a1 1 0 11-2 0v-1a1 1 0 011-1 1 1 0 100-2zm0 8a1 1 0 100-2 1 1 0 000 2z" )
29
+ }
30
+ } , [ type ] )
31
+
32
+
33
+ return (
34
+ < >
35
+ { color && icon && (
36
+ < p className = { "flex items-center text-sm font-bold " + color } >
37
+ < svg
38
+ xmlns = "http://www.w3.org/2000/svg"
39
+ viewBox = "0 0 20 20"
40
+ fill = "currentColor"
41
+ className = "mr-2 h-4 w-4"
42
+ >
43
+ < path
44
+ fillRule = "evenodd"
45
+ d = { icon }
46
+ clipRule = "evenodd"
47
+ />
48
+ </ svg >
49
+ { children }
50
+ </ p >
51
+ ) }
52
+ </ >
53
+ ) ;
54
+ }
0 commit comments