@@ -60,8 +60,7 @@ class MasterbarItem extends Component< MasterbarItemProps > {
60
60
isOpenForNonMouseFlow : false ,
61
61
} ;
62
62
63
- componentButtonRef = React . createRef < HTMLButtonElement > ( ) ;
64
- componentDivRef = React . createRef < HTMLDivElement > ( ) ;
63
+ wrapperRef = React . createRef < HTMLDivElement > ( ) ;
65
64
66
65
_preloaded = false ;
67
66
@@ -191,12 +190,8 @@ class MasterbarItem extends Component< MasterbarItemProps > {
191
190
}
192
191
193
192
// Check refs to see if the touch event started inside our component, if it didn't, close the menu.
194
- const isInComponentButtonRef = this . componentButtonRef . current ?. contains (
195
- event . target as Node
196
- ) ;
197
- const isInComponentDivRef = this . componentDivRef . current ?. contains ( event . target as Node ) ;
198
-
199
- if ( ! isInComponentButtonRef && ! isInComponentDivRef ) {
193
+ const isInWrapper = this . wrapperRef . current ?. contains ( event . target as Node ) ;
194
+ if ( ! isInWrapper ) {
200
195
this . setState ( { isOpenForNonMouseFlow : false } ) ;
201
196
}
202
197
} ;
@@ -221,49 +216,30 @@ class MasterbarItem extends Component< MasterbarItemProps > {
221
216
disabled : this . props . disabled ,
222
217
} ;
223
218
224
- if ( this . props . url && ! this . props . subItems ) {
225
- return (
219
+ const MenuItem = ( props : React . HTMLAttributes < HTMLElement > ) =>
220
+ this . props . url ? (
226
221
< a
227
- { ...attributes }
228
222
href = { this . props . url }
229
223
ref = { this . props . innerRef as LegacyRef < HTMLAnchorElement > }
230
- >
231
- { this . renderChildren ( ) }
232
- </ a >
224
+ { ...props }
225
+ />
226
+ ) : (
227
+ < button ref = { this . props . innerRef as LegacyRef < HTMLButtonElement > } { ...props } />
233
228
) ;
234
- }
235
-
236
- if ( this . props . url && this . props . subItems ) {
237
- return (
238
- < button
239
- { ...attributes }
240
- ref = { this . componentButtonRef }
241
- onKeyDown = { this . toggleMenuByKey }
242
- >
243
- < a
244
- href = { this . props . url }
245
- ref = { this . props . innerRef as LegacyRef < HTMLAnchorElement > }
246
- onTouchEnd = { this . toggleMenuByTouch }
247
- tabIndex = { - 1 }
248
- >
249
- { this . renderChildren ( ) }
250
- </ a >
251
- { this . renderSubItems ( ) }
252
- </ button >
253
- ) ;
254
- }
255
229
256
230
return (
257
- < div className = { this . props . wrapperClassName } ref = { this . componentDivRef } >
258
- < button
231
+ < div
232
+ className = { clsx ( 'masterbar__item-wrapper' , this . props . wrapperClassName ) }
233
+ ref = { this . wrapperRef }
234
+ >
235
+ < MenuItem
259
236
{ ...attributes }
260
- ref = { this . props . innerRef as LegacyRef < HTMLButtonElement > }
261
237
onKeyDown = { this . props . subItems && this . toggleMenuByKey }
262
238
onTouchEnd = { this . props . subItems && this . toggleMenuByTouch }
263
239
>
264
240
{ this . renderChildren ( ) }
265
- { this . renderSubItems ( ) }
266
- </ button >
241
+ </ MenuItem >
242
+ { this . renderSubItems ( ) }
267
243
</ div >
268
244
) ;
269
245
}
0 commit comments