Skip to content

Instantly share code, notes, and snippets.

@polarbirke
Last active August 31, 2020 16:21
Show Gist options
  • Save polarbirke/9efd6be7e4c23b1c11eca52c8c02fb38 to your computer and use it in GitHub Desktop.
Save polarbirke/9efd6be7e4c23b1c11eca52c8c02fb38 to your computer and use it in GitHub Desktop.
Sass Mixin for focus stiles with :focus-visble via the WICG polyfill
@mixin focus(
$focus-color: null,
$focus-not-visible-color: null,
$focus-bg-color: null,
$focus-not-visible-bg-color: null,
$text-decoration: null,
$focus-not-visible-text-decoration: null,
$focus-outline: null,
$focus-outline-offset: null
) {
// Set focus highlighting for all use cases
&:focus {
color: $focus-color;
background: $focus-bg-color;
text-decoration: $text-decoration;
outline: $focus-outline;
outline-offset: $focus-outline-offset;
}
// Progressive Enhancement: reset focus highlighting only if
// 1) the :focus-visible polyfill is active (guarded by reverse parent selector for
// a class on the html element)
// 2) the polyfill determined that the input method does not warrant focus highlighting
// (guarded by check for a class on the element the user is interacting with)
.js-focus-visible & { // [1]
&:focus:not(.focus-visible) { // [2]
color: $focus-not-visible-color;
background-color: $focus-not-visible-bg-color;
text-decoration: $focus-not-visible-text-decoration;
outline: 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment