File size: 921 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
 * Accessibility helper function for lists of navigation items which have onClick events.
 * This triggers a callback on a keydown event, only if the key pressed is space or enter
 * to mirror button functionality. It will also focus the next/previous sibling (if one
 * exists) if the down/up arrows are pressed.
 * @param {Function} callback A callback function
 * @returns {Function} the callback to fire on a keydown event
 */

export default function ( callback ) {
	return ( event ) => {
		if ( event.key === 'Enter' || event.key === ' ' ) {
			event.preventDefault();
			callback( event );
		} else if ( event.key === 'ArrowDown' ) {
			if ( event.target.nextSibling ) {
				event.preventDefault();
				event.target.nextSibling.focus();
			}
		} else if ( event.key === 'ArrowUp' ) {
			if ( event.target.previousSibling ) {
				event.preventDefault();
				event.target.previousSibling.focus();
			}
		}
	};
}