@@ -36,6 +36,10 @@ export default class MDCSnackbarFoundation extends MDCFoundation {
36
36
setActionText : ( /* actionText: string */ ) => { } ,
37
37
setActionAriaHidden : ( ) => { } ,
38
38
unsetActionAriaHidden : ( ) => { } ,
39
+ registerFocusHandler : ( ) => { } ,
40
+ deregisterFocusHandler : ( ) => { } ,
41
+ registerBlurHandler : ( ) => { } ,
42
+ deregisterBlurHandler : ( ) => { } ,
39
43
registerActionClickHandler : ( /* handler: EventListener */ ) => { } ,
40
44
deregisterActionClickHandler : ( /* handler: EventListener */ ) => { } ,
41
45
registerTransitionEndHandler : ( /* handler: EventListener */ ) => { } ,
@@ -51,9 +55,37 @@ export default class MDCSnackbarFoundation extends MDCFoundation {
51
55
super ( Object . assign ( MDCSnackbarFoundation . defaultAdapter , adapter ) ) ;
52
56
53
57
this . active_ = false ;
58
+ this . actionWasClicked_ = false ;
54
59
this . dismissOnAction_ = true ;
60
+ this . firstFocus_ = true ;
61
+ this . snackbarHasFocus_ = false ;
62
+ this . snackbarData_ = null ;
55
63
this . queue_ = [ ] ;
56
- this . actionClickHandler_ = ( ) => this . invokeAction_ ( ) ;
64
+
65
+ this . actionClickHandler_ = ( ) => {
66
+ this . actionWasClicked_ = true ;
67
+ this . invokeAction_ ( ) ;
68
+ } ;
69
+ this . focusHandler_ = ( ) => {
70
+ if ( this . firstFocus_ ) {
71
+ this . setFocusOnAction_ ( ) ;
72
+ }
73
+
74
+ this . firstFocus_ = false ;
75
+ } ;
76
+ this . blurHandler_ = ( ) => {
77
+ this . snackbarHasFocus_ = false ;
78
+ clearTimeout ( this . timeoutId_ ) ;
79
+ this . timeoutId_ = setTimeout ( this . cleanup_ . bind ( this ) , this . snackbarData_ . timeout || MESSAGE_TIMEOUT ) ;
80
+ } ;
81
+ this . visibilitychangeHandler_ = ( ) => {
82
+ clearTimeout ( this . timeoutId_ ) ;
83
+ this . snackbarHasFocus_ = true ;
84
+
85
+ if ( ! this . adapter_ . visibilityIsHidden ( ) ) {
86
+ setTimeout ( this . cleanup_ . bind ( this ) , this . snackbarData_ . timeout || MESSAGE_TIMEOUT ) ;
87
+ }
88
+ } ;
57
89
}
58
90
59
91
init ( ) {
@@ -64,6 +96,8 @@ export default class MDCSnackbarFoundation extends MDCFoundation {
64
96
65
97
destroy ( ) {
66
98
this . adapter_ . deregisterActionClickHandler ( this . actionClickHandler_ ) ;
99
+ this . adapter_ . deregisterFocusHandler ( this . focusHandler_ ) ;
100
+ this . adapter_ . deregisterBlurHandler ( this . focusHandler_ ) ;
67
101
}
68
102
69
103
dismissesOnAction ( ) {
@@ -75,37 +109,43 @@ export default class MDCSnackbarFoundation extends MDCFoundation {
75
109
}
76
110
77
111
show ( data ) {
78
- if ( ! data ) {
112
+ this . snackbarData_ = data ;
113
+ this . firstFocus_ = true ;
114
+ this . adapter_ . registerVisbilityChangeHandler ( this . visibilitychangeHandler_ ) ;
115
+ this . adapter_ . registerFocusHandler ( this . focusHandler_ ) ;
116
+ this . adapter_ . registerBlurHandler ( this . blurHandler_ ) ;
117
+
118
+ if ( ! this . snackbarData_ ) {
79
119
throw new Error (
80
120
'Please provide a data object with at least a message to display.' ) ;
81
121
}
82
- if ( ! data . message ) {
122
+ if ( ! this . snackbarData_ . message ) {
83
123
throw new Error ( 'Please provide a message to be displayed.' ) ;
84
124
}
85
- if ( data . actionHandler && ! data . actionText ) {
125
+ if ( this . snackbarData_ . actionHandler && ! this . snackbarData_ . actionText ) {
86
126
throw new Error ( 'Please provide action text with the handler.' ) ;
87
127
}
88
128
89
129
if ( this . active ) {
90
- this . queue_ . push ( data ) ;
130
+ this . queue_ . push ( this . snackbarData_ ) ;
91
131
return ;
92
132
}
93
133
94
134
const { ACTIVE , MULTILINE , ACTION_ON_BOTTOM } = cssClasses ;
95
135
const { MESSAGE_TIMEOUT } = numbers ;
96
136
97
- this . adapter_ . setMessageText ( data . message ) ;
137
+ this . adapter_ . setMessageText ( this . snackbarData_ . message ) ;
98
138
99
- if ( data . multiline ) {
139
+ if ( this . snackbarData_ . multiline ) {
100
140
this . adapter_ . addClass ( MULTILINE ) ;
101
- if ( data . actionOnBottom ) {
141
+ if ( this . snackbarData_ . actionOnBottom ) {
102
142
this . adapter_ . addClass ( ACTION_ON_BOTTOM ) ;
103
143
}
104
144
}
105
145
106
- if ( data . actionHandler ) {
107
- this . adapter_ . setActionText ( data . actionText ) ;
108
- this . actionHandler_ = data . actionHandler ;
146
+ if ( this . snackbarData_ . actionHandler ) {
147
+ this . adapter_ . setActionText ( this . snackbarData_ . actionText ) ;
148
+ this . actionHandler_ = this . snackbarData_ . actionHandler ;
109
149
this . setActionHidden_ ( false ) ;
110
150
} else {
111
151
this . setActionHidden_ ( true ) ;
@@ -117,7 +157,12 @@ export default class MDCSnackbarFoundation extends MDCFoundation {
117
157
this . adapter_ . addClass ( ACTIVE ) ;
118
158
this . adapter_ . unsetAriaHidden ( ) ;
119
159
120
- this . timeoutId_ = setTimeout ( this . cleanup_ . bind ( this ) , data . timeout || MESSAGE_TIMEOUT ) ;
160
+ this . timeoutId_ = setTimeout ( this . cleanup_ . bind ( this ) , this . snackbarData_ . timeout || MESSAGE_TIMEOUT ) ;
161
+ }
162
+
163
+ setFocusOnAction_ ( ) {
164
+ this . adapter_ . setFocus ( ) ;
165
+ this . snackbarHasFocus_ = true ;
121
166
}
122
167
123
168
invokeAction_ ( ) {
@@ -136,21 +181,25 @@ export default class MDCSnackbarFoundation extends MDCFoundation {
136
181
}
137
182
138
183
cleanup_ ( ) {
139
- const { ACTIVE , MULTILINE , ACTION_ON_BOTTOM } = cssClasses ;
140
-
141
- this . adapter_ . removeClass ( ACTIVE ) ;
142
-
143
- const handler = ( ) => {
144
- this . adapter_ . deregisterTransitionEndHandler ( handler ) ;
145
- this . adapter_ . removeClass ( MULTILINE ) ;
146
- this . adapter_ . removeClass ( ACTION_ON_BOTTOM ) ;
147
- this . setActionHidden_ ( true ) ;
148
- this . adapter_ . setAriaHidden ( ) ;
149
- this . active_ = false ;
150
- this . showNext_ ( ) ;
151
- } ;
184
+ if ( ! this . snackbarHasFocus_ || this . actionWasClicked_ ) {
185
+ const { ACTIVE , MULTILINE , ACTION_ON_BOTTOM } = cssClasses ;
186
+
187
+ this . adapter_ . removeClass ( ACTIVE ) ;
188
+
189
+ const handler = ( ) => {
190
+ this . adapter_ . deregisterTransitionEndHandler ( handler ) ;
191
+ this . adapter_ . removeClass ( MULTILINE ) ;
192
+ this . adapter_ . removeClass ( ACTION_ON_BOTTOM ) ;
193
+ this . setActionHidden_ ( true ) ;
194
+ this . adapter_ . setAriaHidden ( ) ;
195
+ this . active_ = false ;
196
+ this . snackbarHasFocus_ = false ;
197
+ clearTimeout ( this . timeoutId_ ) ;
198
+ this . showNext_ ( ) ;
199
+ } ;
152
200
153
- this . adapter_ . registerTransitionEndHandler ( handler ) ;
201
+ this . adapter_ . registerTransitionEndHandler ( handler ) ;
202
+ }
154
203
}
155
204
156
205
showNext_ ( ) {
0 commit comments