Skip to content
This repository has been archived by the owner on Dec 1, 2019. It is now read-only.

Commit

Permalink
Include skip link JS for IE. (#937)
Browse files Browse the repository at this point in the history
* Include skip link JS for IE.

* Output minified script without enqueuing it

Fixes #935
  • Loading branch information
Alex Tran authored and ianbelanger79 committed Oct 28, 2019
1 parent 9c86374 commit 41b4bf3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
33 changes: 33 additions & 0 deletions assets/js/skip-link-focus-fix.js
Original file line number Diff line number Diff line change
@@ -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 );
}
}() );
21 changes: 19 additions & 2 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,32 @@ 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`.
?>
<script>
/(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())},!1);
</script>
<?php
}
add_action( 'wp_print_footer_scripts', 'twentytwenty_skip_link_focus_fix' );

/** Enqueue non-latin language styles
*
* @since 1.0.0
*
* @return void
*/
function twentytwenty_non_latin_languages() {
$custom_css = TwentyTwenty_Non_Latin_Languages::get_non_latin_css( 'front-end' );

if ( $custom_css ) {
wp_add_inline_style( 'twentytwenty-style', $custom_css );
}
Expand Down

0 comments on commit 41b4bf3

Please sign in to comment.