File tree Expand file tree Collapse file tree 3 files changed +33
-1
lines changed Expand file tree Collapse file tree 3 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 2424 </ style >
2525</ head >
2626< body >
27+ < h1 > Base examples</ h1 >
28+
2729 < details >
2830 < summary > Best robot: < span data-menu-button > Unknown</ span > </ summary >
2931 < details-menu >
6062 </ details-menu >
6163 </ details >
6264
65+ < h1 > Autofocus example</ h1 >
66+ < p > < code > summary</ code > may have < code > autofocus</ code > so it's the initially focused element in the page.</ p >
67+ < p > Then any focusable element inside the popup can declare autofocus too, so it gets focus when the popup is opened.</ p >
68+
69+ < details >
70+ < summary data-menu-button autofocus > Autofocus picker</ summary >
71+ < details-menu >
72+ < input type ="text " autofocus />
73+ < button type ="submit " name ="robot " value ="Hubot " role ="menuitemradio " data-menu-button-text > Hubot</ button >
74+ < button type ="submit " name ="robot " value ="Bender " role ="menuitemradio " data-menu-button-text > Bender</ button >
75+ < button type ="submit " name ="robot " value ="BB-8 " role ="menuitemradio " data-menu-button-text > BB-8</ button >
76+ </ details-menu >
77+ </ details >
78+
6379 < script type ="text/javascript ">
6480 document . addEventListener ( 'details-menu-selected' , e => console . log ( e ) )
6581 </ script >
Original file line number Diff line number Diff line change @@ -132,7 +132,7 @@ function closeCurrentMenu(details: Element) {
132132
133133function autofocus ( details : Element ) : boolean {
134134 if ( ! details . hasAttribute ( 'open' ) ) return false
135- const input = details . querySelector < HTMLElement > ( '[autofocus]' )
135+ const input = details . querySelector < HTMLElement > ( 'details-menu [autofocus]' )
136136 if ( input ) {
137137 input . focus ( )
138138 return true
Original file line number Diff line number Diff line change @@ -601,6 +601,22 @@ describe('details-menu element', function () {
601601
602602 assert . equal ( input , document . activeElement , 'toggle open focuses on [autofocus]' )
603603 } )
604+
605+ it ( 'summary autofocus should not impact with inner autofocus element' , function ( ) {
606+ const details = document . querySelector ( 'details' )
607+ const summary = details . querySelector ( 'summary' )
608+ const input = details . querySelector ( 'input' )
609+
610+ // Summary is the initial element of the entire page, while input is the initial element in the popup
611+ summary . setAttribute ( 'autofocus' , '' )
612+
613+ summary . focus ( )
614+ details . open = true
615+ summary . dispatchEvent ( new KeyboardEvent ( 'keydown' , { key : 'Enter' , bubbles : true } ) )
616+ details . dispatchEvent ( new CustomEvent ( 'toggle' ) )
617+
618+ assert . equal ( document . activeElement , input , 'toggle open focuses on [autofocus]' )
619+ } )
604620 } )
605621
606622 describe ( 'closing the menu' , function ( ) {
You can’t perform that action at this time.
0 commit comments