@@ -2,102 +2,107 @@ import React, { useEffect, useRef, useState } from "react"
2
2
import Button , { Appearance } from "@atlaskit/button"
3
3
import { token } from "@atlaskit/tokens"
4
4
5
- export type MessageUrgency = "success" | "error" | "warning" | "information" | "danger" | undefined
5
+ export type MessageUrgency =
6
+ | "success"
7
+ | "error"
8
+ | "warning"
9
+ | "information"
10
+ | "danger"
11
+ | undefined
6
12
7
13
export type Message = {
8
- text : string | JSX . Element ,
9
- urgency ?: MessageUrgency ,
10
- timeOut ?: number , // in seconds
14
+ text : string | JSX . Element
15
+ urgency ?: MessageUrgency
16
+ timeOut ?: number // in seconds
11
17
}
12
18
13
- export default function InlineMessage ( {
19
+ export default function InlineMessage ( {
14
20
message,
15
21
display = "block" ,
16
22
} : {
17
- message : Message ,
18
- display ?: "inline-block" | "block" ,
19
- } ) {
20
-
21
- const [ open , setOpen ] = useState ( true )
22
- const [ msg , setMessage ] = useState ( message )
23
+ message : Message
24
+ display ?: "inline-block" | "block"
25
+ } ) {
26
+ const [ open , setOpen ] = useState ( true )
27
+ const [ msg , setMessage ] = useState ( message )
23
28
const currentTimeOut = useRef < number > ( )
24
29
25
- useEffect ( ( ) => {
26
- setMessage ( message )
27
- setOpen ( ! ! message ?. text )
28
- if ( currentTimeOut . current ) {
29
- clearTimeout ( currentTimeOut . current )
30
+ useEffect ( ( ) => {
31
+ setMessage ( message )
32
+ setOpen ( ! ! message ?. text )
33
+ if ( currentTimeOut . current ) {
34
+ clearTimeout ( currentTimeOut . current )
30
35
currentTimeOut . current = undefined
31
36
}
32
- if ( message . timeOut && message . text ) {
33
- currentTimeOut . current = setTimeout ( ( ) => {
34
- setOpen ( false )
37
+ if ( message . timeOut && message . text ) {
38
+ currentTimeOut . current = setTimeout ( ( ) => {
39
+ setOpen ( false )
35
40
currentTimeOut . current = undefined
36
- } , message . timeOut * 1000 )
41
+ } , message . timeOut * 1000 )
37
42
}
38
- } , [ message ] )
43
+ } , [ message ] )
39
44
40
45
let bgColor = undefined
41
46
let textColor = undefined
42
47
let borderColor = undefined
43
48
let closeBtnAppearance : Appearance = "default"
44
- switch ( message . urgency ) {
49
+ switch ( message . urgency ) {
45
50
case "success" :
46
- bgColor = token ( "color.background.success" )
47
- textColor = token ( "color.text.success" )
48
- borderColor = token ( "color.border.success" )
51
+ bgColor = token ( "color.background.success" )
52
+ textColor = token ( "color.text.success" )
53
+ borderColor = token ( "color.border.success" )
49
54
break
50
55
case "error" :
51
- bgColor = token ( "color.background.danger" )
52
- textColor = token ( "color.text.danger" )
53
- borderColor = token ( "color.border.danger" )
56
+ bgColor = token ( "color.background.danger" )
57
+ textColor = token ( "color.text.danger" )
58
+ borderColor = token ( "color.border.danger" )
54
59
closeBtnAppearance = "danger"
55
60
break
56
61
case "warning" :
57
- bgColor = token ( "color.background.warning" )
58
- textColor = token ( "color.text.warning" )
59
- borderColor = token ( "color.border.warning" )
62
+ bgColor = token ( "color.background.warning" )
63
+ textColor = token ( "color.text.warning" )
64
+ borderColor = token ( "color.border.warning" )
60
65
closeBtnAppearance = "warning"
61
66
break
62
67
case "information" :
63
- bgColor = token ( "color.background.information" )
64
- textColor = token ( "color.text.information" )
65
- borderColor = token ( "color.border.information" )
68
+ bgColor = token ( "color.background.information" )
69
+ textColor = token ( "color.text.information" )
70
+ borderColor = token ( "color.border.information" )
66
71
closeBtnAppearance = "primary"
67
72
break
68
73
case "danger" :
69
- bgColor = token ( "color.background.danger" )
70
- textColor = token ( "color.text.danger" )
71
- borderColor = token ( "color.border.danger" )
74
+ bgColor = token ( "color.background.danger" )
75
+ textColor = token ( "color.text.danger" )
76
+ borderColor = token ( "color.border.danger" )
72
77
closeBtnAppearance = "danger"
73
78
break
74
79
default :
75
- borderColor = token ( "color.border" )
80
+ borderColor = token ( "color.border" )
76
81
break
77
82
}
78
83
79
84
return (
80
85
< div
81
- onMouseEnter = { ( ) => {
82
- if ( ! message . text ) return
83
- setOpen ( true )
84
- } }
85
- onMouseLeave = { ( ) => {
86
- if ( ! message . text ) return
87
- if ( message . timeOut ) {
88
- clearTimeout ( currentTimeOut . current )
89
- currentTimeOut . current = setTimeout ( ( ) => {
90
- setOpen ( false )
86
+ onMouseEnter = { ( ) => {
87
+ if ( ! message . text ) return
88
+ setOpen ( true )
89
+ } }
90
+ onMouseLeave = { ( ) => {
91
+ if ( ! message . text ) return
92
+ if ( message . timeOut ) {
93
+ clearTimeout ( currentTimeOut . current )
94
+ currentTimeOut . current = setTimeout ( ( ) => {
95
+ setOpen ( false )
91
96
currentTimeOut . current = undefined
92
- } , message . timeOut * 1000 )
97
+ } , message . timeOut * 1000 )
93
98
}
94
- } }
99
+ } }
95
100
>
96
101
< div
97
- style = { {
102
+ style = { {
98
103
display,
99
104
backgroundColor : bgColor ,
100
- border : `2px solid ${ borderColor } ` ,
105
+ border : `2px solid ${ borderColor } ` ,
101
106
borderRadius : "2px" ,
102
107
color : textColor ,
103
108
padding : "2px" ,
@@ -106,29 +111,29 @@ export default function InlineMessage ( {
106
111
overflow : "hidden" ,
107
112
scale : open ? "1 1" : "1 0" ,
108
113
transformOrigin : "top" ,
109
- } }
114
+ } }
110
115
>
111
116
< div
112
- style = { {
117
+ style = { {
113
118
display : "flex" ,
114
119
flexDirection : "row" ,
115
120
justifyContent : "space-between" ,
116
121
alignItems : "center" ,
117
- } }
122
+ } }
118
123
>
119
- { msg ?. text ?? "" }
124
+ { msg ?. text ?? "" }
120
125
< Button
121
- appearance = { closeBtnAppearance }
122
- style = { {
126
+ appearance = { closeBtnAppearance }
127
+ style = { {
123
128
borderRadius : "100%" ,
124
129
userSelect : "none" ,
125
- } }
126
- onClick = { ( ) => setOpen ( false ) }
130
+ } }
131
+ onClick = { ( ) => setOpen ( false ) }
127
132
>
128
133
X
129
134
</ Button >
130
135
</ div >
131
136
</ div >
132
137
</ div >
133
138
)
134
- }
139
+ }
0 commit comments