Download wp-calypso/packages/accessible-focus/src/index.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 2.56 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/packages/accessible-focus/src/index.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/wp-calypso/packages/accessible-focus/src/index.ts
-
curl -L -o index.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/packages/accessible-focus/src/index.ts
2.56 kB
| // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode#non-printable_keys_function_keys | |
| const keyboardNavigationKeycodes = [ 9, 32, 37, 38, 39, 40 ]; // keyCodes for tab, space, left, up, right, down respectively | |
| // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values | |
| const keyboardNavigationKeyValues = [ | |
| 'Tab', | |
| ' ', | |
| // Some browsers returned `'Spacebar'` rather than `' '` for the Space key | |
| 'Spacebar', | |
| 'ArrowDown', | |
| 'ArrowUp', | |
| 'ArrowLeft', | |
| 'ArrowRight', | |
| ]; | |
| declare global { | |
| interface KeyboardEvent { | |
| /** | |
| * https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyIdentifier | |
| * Provides Safari support | |
| * @deprecated | |
| */ | |
| keyIdentifier: string; | |
| } | |
| } | |
| /** | |
| * `event.keyCode` is [deprecated](https://stackoverflow.com/a/35395154), so we must check each of the following: | |
| * 1. `event.key` | |
| * 2. `event.keyIdentifier` | |
| * 3. `event.keyCode` (for posterity) | |
| * | |
| * However, [`event.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) | |
| * and [`event.keyIdentifier`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyIdentifier) | |
| * both return a string rather than a numerical key code as the old `keyCode` API did. | |
| * | |
| * Therefore, we have two arrays of keycodes, one for the string APIs and one for the | |
| * older number based API. | |
| * @param event The keyboard event to detect keyboard navigation against. | |
| */ | |
| export function detectKeyboardNavigation( event: KeyboardEvent ): boolean { | |
| let code: number | string | undefined; | |
| if ( event.key !== undefined ) { | |
| code = event.key; | |
| } else if ( event.keyIdentifier !== undefined ) { | |
| code = event.keyIdentifier; | |
| } else if ( event.keyCode !== undefined ) { | |
| code = event.keyCode; | |
| } | |
| // This shouldn't ever happen but we do it to appease TypeScript | |
| if ( code === undefined ) { | |
| return false; | |
| } | |
| if ( typeof code === 'string' ) { | |
| return keyboardNavigationKeyValues.indexOf( code ) !== -1; | |
| } | |
| return keyboardNavigationKeycodes.indexOf( code ) !== -1; | |
| } | |
| let keyboardNavigation = false; | |
| export default function accessibleFocus(): void { | |
| document.addEventListener( 'keydown', function ( event ) { | |
| if ( keyboardNavigation ) { | |
| return; | |
| } | |
| if ( detectKeyboardNavigation( event ) ) { | |
| keyboardNavigation = true; | |
| document.documentElement.classList.add( 'accessible-focus' ); | |
| } | |
| } ); | |
| document.addEventListener( 'mouseup', function () { | |
| if ( ! keyboardNavigation ) { | |
| return; | |
| } | |
| keyboardNavigation = false; | |
| document.documentElement.classList.remove( 'accessible-focus' ); | |
| } ); | |
| } | |