@@ -24,10 +24,12 @@ const SELECTORS = {
2424export default {
2525 mounted ( ) {
2626 this . initialize ( )
27+ this . js ( ) . setAttribute ( this . el , 'data-prima-ready' , 'true' )
2728 } ,
2829
2930 updated ( ) {
3031 this . initialize ( )
32+ this . setFocus ( this . el . querySelector ( SELECTORS . FOCUSED_MENUITEM ) )
3133 } ,
3234
3335 reconnected ( ) {
@@ -42,20 +44,19 @@ export default {
4244 this . cleanup ( )
4345 this . setupElements ( )
4446 this . setupEventListeners ( )
45- this . el . setAttribute ( 'data-prima-ready' , 'true' )
47+ if ( this . isMenuVisible ( ) ) this . startAutoUpdate ( )
4648 } ,
4749
4850 setupElements ( ) {
4951 const button = this . el . querySelector ( SELECTORS . BUTTON )
5052 const menuWrapper = this . el . querySelector ( SELECTORS . MENU_WRAPPER )
5153 const menu = this . el . querySelector ( SELECTORS . MENU )
52- const items = this . el . querySelectorAll ( SELECTORS . MENUITEM )
5354
5455 const referenceSelector = menuWrapper ?. getAttribute ( 'data-reference' )
5556 const referenceElement = referenceSelector ? document . querySelector ( referenceSelector ) : button
5657
5758 this . setupAriaRelationships ( button , menu )
58- this . refs = { button, menuWrapper, menu, items , referenceElement }
59+ this . refs = { button, menuWrapper, menu, referenceElement }
5960 } ,
6061
6162 setupEventListeners ( ) {
@@ -85,6 +86,14 @@ export default {
8586 }
8687 } ,
8788
89+ startAutoUpdate ( ) {
90+ // Setup autoUpdate to reposition on scroll/resize
91+ this . cleanupAutoUpdate ( )
92+ this . autoUpdateCleanup = autoUpdate ( this . refs . referenceElement , this . refs . menuWrapper , ( ) => {
93+ this . positionMenu ( )
94+ } )
95+ } ,
96+
8897 cleanupAutoUpdate ( ) {
8998 if ( this . autoUpdateCleanup ) {
9099 this . autoUpdateCleanup ( )
@@ -242,13 +251,9 @@ export default {
242251 this . refs . menu . style . display = shouldBeOpen ? '' : 'none'
243252 if ( ! shouldBeOpen ) return
244253
245- this . refs . button . setAttribute ( 'aria-expanded' , 'true' )
254+ this . js ( ) . setAttribute ( this . refs . button , 'aria-expanded' , 'true' )
246255
247- // Setup autoUpdate to reposition on scroll/resize
248- this . cleanupAutoUpdate ( )
249- this . autoUpdateCleanup = autoUpdate ( this . refs . referenceElement , this . refs . menuWrapper , ( ) => {
250- this . positionMenu ( )
251- } )
256+ this . startAutoUpdate ( )
252257 } ,
253258
254259 handleHideEnd ( ) {
@@ -257,8 +262,8 @@ export default {
257262 if ( shouldBeOpen ) return
258263
259264 this . clearFocus ( )
260- this . refs . menu . removeAttribute ( 'aria-activedescendant' )
261- this . refs . button . setAttribute ( 'aria-expanded' , 'false' )
265+ this . js ( ) . removeAttribute ( this . refs . menu , 'aria-activedescendant' )
266+ this . js ( ) . setAttribute ( this . refs . button , 'aria-expanded' , 'false' )
262267 this . refs . menuWrapper . style . display = 'none'
263268 this . cleanupAutoUpdate ( )
264269 } ,
@@ -283,15 +288,16 @@ export default {
283288 setFocus ( el ) {
284289 this . clearFocus ( )
285290 if ( el && el . getAttribute ( 'aria-disabled' ) !== 'true' ) {
286- el . setAttribute ( 'data-focus' , '' )
287- this . refs . menu . setAttribute ( 'aria-activedescendant' , el . id )
291+ this . js ( ) . setAttribute ( el , 'data-focus' , '' )
292+ this . js ( ) . setAttribute ( this . refs . menu , 'aria-activedescendant' , el . id )
288293 } else {
289- this . refs . menu . removeAttribute ( 'aria-activedescendant' )
294+ this . js ( ) . removeAttribute ( this . refs . menu , 'aria-activedescendant' )
290295 }
291296 } ,
292297
293298 clearFocus ( ) {
294- this . el . querySelector ( SELECTORS . FOCUSED_MENUITEM ) ?. removeAttribute ( 'data-focus' )
299+ const focused = this . el . querySelector ( SELECTORS . FOCUSED_MENUITEM )
300+ if ( focused ) this . js ( ) . removeAttribute ( focused , 'data-focus' )
295301 } ,
296302
297303 hideMenu ( ) {
@@ -343,8 +349,8 @@ export default {
343349 } ,
344350
345351 setupAriaRelationships ( button , menu ) {
346- button . setAttribute ( 'aria-controls' , menu . id )
347- menu . setAttribute ( 'aria-labelledby' , button . id )
352+ this . js ( ) . setAttribute ( button , 'aria-controls' , menu . id )
353+ this . js ( ) . setAttribute ( menu , 'aria-labelledby' , button . id )
348354
349355 this . setupSectionLabels ( )
350356 } ,
@@ -357,7 +363,7 @@ export default {
357363 const firstChild = section . firstElementChild
358364 if ( firstChild && firstChild . getAttribute ( 'role' ) === 'presentation' ) {
359365 // Link the section to the heading
360- section . setAttribute ( 'aria-labelledby' , firstChild . id )
366+ this . js ( ) . setAttribute ( section , 'aria-labelledby' , firstChild . id )
361367 }
362368 } )
363369 } ,
0 commit comments