diff --git a/assets/js/skip-link-focus-fix.js b/assets/js/skip-link-focus-fix.js new file mode 100644 index 000000000..181b79799 --- /dev/null +++ b/assets/js/skip-link-focus-fix.js @@ -0,0 +1,33 @@ +/** + * File skip-link-focus-fix.js. + * + * Helps with accessibility for keyboard only users. + * + * This is the source file for what is minified in the twentytwenty_skip_link_focus_fix() PHP function. + * + * Learn more: https://git.io/vWdr2 + */ +( function() { + var isIe = /(trident|msie)/i.test( navigator.userAgent ); + + if ( isIe && document.getElementById && window.addEventListener ) { + window.addEventListener( 'hashchange', function() { + var id = location.hash.substring( 1 ), + element; + + if ( ! ( /^[A-z0-9_-]+$/.test( id ) ) ) { + return; + } + + element = document.getElementById( id ); + + if ( element ) { + if ( ! ( /^(?:a|select|input|button|textarea)$/i.test( element.tagName ) ) ) { + element.tabIndex = -1; + } + + element.focus(); + } + }, false ); + } +}() ); diff --git a/functions.php b/functions.php index 804b455e2..631510316 100644 --- a/functions.php +++ b/functions.php @@ -209,7 +209,24 @@ function twentytwenty_register_scripts() { add_action( 'wp_enqueue_scripts', 'twentytwenty_register_scripts' ); /** - * Enqueue non-latin language styles + * Fix skip link focus in IE11. + * + * This does not enqueue the script because it is tiny and because it is only for IE11, + * thus it does not warrant having an entire dedicated blocking script being loaded. + * + * @link https://git.io/vWdr2 + */ +function twentytwenty_skip_link_focus_fix() { + // The following is minified via `terser --compress --mangle -- assets/js/skip-link-focus-fix.js`. + ?> + +