2022-06-04 06:38:26 +09:00
import $ from 'jquery' ;
let ariaIdCounter = 0 ;
function generateAriaId ( ) {
return ` _aria_auto_id_ ${ ariaIdCounter ++ } ` ;
}
function attachOneDropdownAria ( $dropdown ) {
2023-03-17 12:08:05 +09:00
if ( $dropdown . attr ( 'data-aria-attached' ) || $dropdown . hasClass ( 'custom' ) ) return ;
2022-06-04 06:38:26 +09:00
$dropdown . attr ( 'data-aria-attached' , 1 ) ;
2023-03-17 12:08:05 +09:00
// Dropdown has 2 different focusing behaviors
// * with search input: the input is focused, and it works with aria-activedescendant pointing another sibling element.
2022-06-04 06:38:26 +09:00
// * without search input (but the readonly text), the dropdown itself is focused. then the aria-activedescendant points to the element inside dropdown
2023-03-17 12:08:05 +09:00
// Some desktop screen readers may change the focus, but dropdown requires that the focus must be on its primary element, then they don't work well.
2022-06-04 06:38:26 +09:00
2023-03-17 12:08:05 +09:00
// Expected user interactions for dropdown with aria support:
2022-06-04 06:38:26 +09:00
// * user can use Tab to focus in the dropdown, then the dropdown menu (list) will be shown
// * user presses Tab on the focused dropdown to move focus to next sibling focusable element (but not the menu item)
// * user can use arrow key Up/Down to navigate between menu items
// * when user presses Enter:
// - if the menu item is clickable (eg: <a>), then trigger the click event
// - otherwise, the dropdown control (low-level code) handles the Enter event, hides the dropdown menu
// TODO: multiple selection is not supported yet.
2023-03-17 12:08:05 +09:00
const $textSearch = $dropdown . find ( 'input.search' ) . eq ( 0 ) ;
const $focusable = $textSearch . length ? $textSearch : $dropdown ; // the primary element for focus, see comment above
if ( ! $focusable . length ) return ;
2022-06-04 06:38:26 +09:00
2023-03-17 12:08:05 +09:00
// There are 2 possible solutions about the role: combobox or menu.
// The idea is that if there is an input, then it's a combobox, otherwise it's a menu.
// Since #19861 we have prepared the "combobox" solution, but didn't get enough time to put it into practice and test before.
const isComboBox = $dropdown . find ( 'input' ) . length > 0 ;
const focusableRole = isComboBox ? 'combobox' : 'button' ;
const listPopupRole = isComboBox ? 'listbox' : 'menu' ;
const listItemRole = isComboBox ? 'option' : 'menuitem' ;
// make the item has role=option/menuitem, add an id if there wasn't one yet, make items as non-focusable
// the elements inside the dropdown menu item should not be focusable, the focus should always be on the dropdown primary element.
function prepareMenuItem ( $item ) {
if ( ! $item . attr ( 'id' ) ) $item . attr ( 'id' , generateAriaId ( ) ) ;
$item . attr ( { 'role' : listItemRole , 'tabindex' : '-1' } ) ;
$item . find ( 'a' ) . attr ( 'tabindex' , '-1' ) ;
}
// delegate the dropdown's template function to add aria attributes.
// the "template" functions are used for dynamic creation (eg: AJAX)
const dropdownTemplates = { ... $dropdown . dropdown ( 'setting' , 'templates' ) } ;
const dropdownTemplatesMenuOld = dropdownTemplates . menu ;
dropdownTemplates . menu = function ( response , fields , preserveHTML , className ) {
// when the dropdown menu items are loaded from AJAX requests, the items are created dynamically
const menuItems = dropdownTemplatesMenuOld ( response , fields , preserveHTML , className ) ;
const $wrapper = $ ( '<div>' ) . append ( menuItems ) ;
const $items = $wrapper . find ( '> .item' ) ;
$items . each ( ( _ , item ) => prepareMenuItem ( $ ( item ) ) ) ;
return $wrapper . html ( ) ;
} ;
$dropdown . dropdown ( 'setting' , 'templates' , dropdownTemplates ) ;
// use tooltip's content as aria-label if there is no aria-label
if ( $dropdown . hasClass ( 'tooltip' ) && $dropdown . attr ( 'data-content' ) && ! $dropdown . attr ( 'aria-label' ) ) {
2022-06-04 06:38:26 +09:00
$dropdown . attr ( 'aria-label' , $dropdown . attr ( 'data-content' ) ) ;
}
2023-03-17 12:08:05 +09:00
// prepare dropdown menu list popup
const $menu = $dropdown . find ( '> .menu' ) ;
if ( ! $menu . attr ( 'id' ) ) $menu . attr ( 'id' , generateAriaId ( ) ) ;
2022-06-04 06:38:26 +09:00
$menu . find ( '> .item' ) . each ( ( _ , item ) => {
prepareMenuItem ( $ ( item ) ) ;
} ) ;
2023-03-17 12:08:05 +09:00
// this role could only be changed after its content is ready, otherwise some browsers+readers (like Chrome+AppleVoice) crash
$menu . attr ( 'role' , listPopupRole ) ;
2022-06-04 06:38:26 +09:00
2023-03-17 12:08:05 +09:00
// make the primary element (focusable) aria-friendly
$focusable . attr ( {
'role' : $focusable . attr ( 'role' ) ? ? focusableRole ,
'aria-haspopup' : listPopupRole ,
'aria-controls' : $menu . attr ( 'id' ) ,
'aria-expanded' : 'false' ,
} ) ;
2022-06-04 06:38:26 +09:00
2023-03-17 12:08:05 +09:00
// when showing, it has class: ".animating.in"
// when hiding, it has class: ".visible.animating.out"
const isMenuVisible = ( ) => ( $menu . hasClass ( 'visible' ) && ! $menu . hasClass ( 'out' ) ) || $menu . hasClass ( 'in' ) ;
2022-06-04 06:38:26 +09:00
2023-03-17 12:08:05 +09:00
// update aria attributes according to current active/selected item
const refreshAria = ( ) => {
const menuVisible = isMenuVisible ( ) ;
$focusable . attr ( 'aria-expanded' , menuVisible ? 'true' : 'false' ) ;
// if there is an active item, use it (the user is navigating between items)
// otherwise use the "selected" for combobox (for the last selected item)
const $active = $menu . find ( '> .item.active, > .item.selected' ) ;
// if the popup is visible and has an active/selected item, use its id as aria-activedescendant
if ( menuVisible ) {
$focusable . attr ( 'aria-activedescendant' , $active . attr ( 'id' ) ) ;
} else if ( ! isComboBox ) {
// for menu, when the popup is hidden, no need to keep the aria-activedescendant, and clear the active/selected item
$focusable . removeAttr ( 'aria-activedescendant' ) ;
$active . removeClass ( 'active' ) . removeClass ( 'selected' ) ;
}
2022-06-04 06:38:26 +09:00
} ;
$dropdown . on ( 'keydown' , ( e ) => {
// here it must use keydown event before dropdown's keyup handler, otherwise there is no Enter event in our keyup handler
if ( e . key === 'Enter' ) {
Make issue meta dropdown support Enter, confirm before reloading (#23014)
As the title. Label/assignee share the same code.
* Close #22607
* Close #20727
Also:
* partially fix for #21742, now the comment reaction and menu work with
keyboard.
* partially fix for #17705, in most cases the comment won't be lost.
* partially fix for #21539
* partially fix for #20347
* partially fix for #7329
### The `Enter` support
Before, if user presses Enter, the dropdown just disappears and nothing
happens or the window reloads.
After, Enter can be used to select/deselect labels, and press Esc to
hide the dropdown to update the labels (still no way to cancel ....
maybe you can do a Cmd+R or F5 to refresh the window to discard the
changes .....)
This is only a quick patch, the UX is still not perfect, but it's much
better than before.
### The `confirm` before reloading
And more fixes for the `reload` problem, the new behaviors:
* If nothing changes (just show/hide the dropdown), then the page won't
be reloaded.
* If there are draft comments, show a confirm dialog before reloading,
to avoid losing comments.
That's the best effect can be done at the moment, unless completely
refactor these dropdown related code.
Screenshot of the confirm dialog:
<details>
![image](https://user-images.githubusercontent.com/2114189/220538288-e2da8459-6a4e-43cb-8596-74057f8a03a2.png)
</details>
---------
Co-authored-by: Brecht Van Lommel <brecht@blender.org>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-02-24 10:26:27 +09:00
let $item = $dropdown . dropdown ( 'get item' , $dropdown . dropdown ( 'get value' ) ) ;
if ( ! $item ) $item = $menu . find ( '> .item.selected' ) ; // when dropdown filters items by input, there is no "value", so query the "selected" item
2023-03-08 12:26:37 +09:00
// if the selected item is clickable, then trigger the click event.
// we can not click any item without check, because Fomantic code might also handle the Enter event. that would result in double click.
2023-03-17 12:08:05 +09:00
if ( $item && ( $item . is ( 'a' ) || $item . hasClass ( 'js-aria-clickable' ) ) ) $item [ 0 ] . click ( ) ;
2022-06-04 06:38:26 +09:00
}
} ) ;
// use setTimeout to run the refreshAria in next tick (to make sure the Fomantic UI code has finished its work)
2023-03-17 12:08:05 +09:00
// do not return any value, jQuery has return-value related behaviors.
// when the popup is hiding, it's better to have a small "delay", because there is a Fomantic UI animation
// without the delay for hiding, the UI will be somewhat laggy and sometimes may get stuck in the animation.
const deferredRefreshAria = ( delay = 0 ) => { setTimeout ( refreshAria , delay ) } ;
2022-06-04 06:38:26 +09:00
$dropdown . on ( 'keyup' , ( e ) => { if ( e . key . startsWith ( 'Arrow' ) ) deferredRefreshAria ( ) ; } ) ;
2023-03-17 12:08:05 +09:00
// if the dropdown has been opened by focus, do not trigger the next click event again.
// otherwise the dropdown will be closed immediately, especially on Android with TalkBack
// * desktop event sequence: mousedown -> focus -> mouseup -> click
// * mobile event sequence: focus -> mousedown -> mouseup -> click
// Fomantic may stop propagation of blur event, use capture to make sure we can still get the event
let ignoreClickPreEvents = 0 , ignoreClickPreVisible = 0 ;
$dropdown [ 0 ] . addEventListener ( 'mousedown' , ( ) => {
ignoreClickPreVisible += isMenuVisible ( ) ? 1 : 0 ;
ignoreClickPreEvents ++ ;
} , true ) ;
$dropdown [ 0 ] . addEventListener ( 'focus' , ( ) => {
ignoreClickPreVisible += isMenuVisible ( ) ? 1 : 0 ;
ignoreClickPreEvents ++ ;
deferredRefreshAria ( ) ;
} , true ) ;
$dropdown [ 0 ] . addEventListener ( 'blur' , ( ) => {
ignoreClickPreVisible = ignoreClickPreEvents = 0 ;
deferredRefreshAria ( 100 ) ;
} , true ) ;
$dropdown [ 0 ] . addEventListener ( 'mouseup' , ( ) => {
setTimeout ( ( ) => {
ignoreClickPreVisible = ignoreClickPreEvents = 0 ;
deferredRefreshAria ( 100 ) ;
} , 0 ) ;
} , true ) ;
$dropdown [ 0 ] . addEventListener ( 'click' , ( e ) => {
if ( isMenuVisible ( ) &&
ignoreClickPreVisible !== 2 && // dropdown is switch from invisible to visible
ignoreClickPreEvents === 2 // the click event is related to mousedown+focus
) {
e . stopPropagation ( ) ; // if the dropdown menu has been opened by focus, do not trigger the next click event again
}
ignoreClickPreEvents = ignoreClickPreVisible = 0 ;
} , true ) ;
2022-06-04 06:38:26 +09:00
}
export function attachDropdownAria ( $dropdowns ) {
$dropdowns . each ( ( _ , e ) => attachOneDropdownAria ( $ ( e ) ) ) ;
}
2023-01-26 00:52:10 +09:00
export function attachCheckboxAria ( $checkboxes ) {
$checkboxes . checkbox ( ) ;
// Fomantic UI checkbox needs to be something like: <div class="ui checkbox"><label /><input /></div>
// It doesn't work well with <label><input />...</label>
// To make it work with aria, the "id"/"for" attributes are necessary, so add them automatically if missing.
// In the future, refactor to use native checkbox directly, then this patch could be removed.
for ( const el of $checkboxes ) {
const label = el . querySelector ( 'label' ) ;
const input = el . querySelector ( 'input' ) ;
if ( ! label || ! input || input . getAttribute ( 'id' ) ) continue ;
const id = generateAriaId ( ) ;
input . setAttribute ( 'id' , id ) ;
label . setAttribute ( 'for' , id ) ;
}
}