Skip to content

Commit

Permalink
Add :focus-visible polyfill (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
moroshko authored Jul 5, 2020
1 parent f018af0 commit 6c8da38
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 44 deletions.
33 changes: 33 additions & 0 deletions src/components/internal/FocusVisiblePolyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useState, useEffect } from "react";
import useWindow from "../../hooks/useWindow";

function FocusVisiblePolyfill() {
const [isKeyboardMode, setIsKeyboardMode] = useState(false);
const onKeyDown = () => setIsKeyboardMode(true);
const onMouseDown = () => setIsKeyboardMode(false);
const window = useWindow();

useEffect(() => {
if (window) {
window.addEventListener("keydown", onKeyDown);
window.addEventListener("mousedown", onMouseDown);
}

return () => {
if (window) {
window.removeEventListener("keydown", onKeyDown);
window.removeEventListener("mousedown", onMouseDown);
}
};
}, [window]);

useEffect(() => {
if (window) {
window.document.body.dataset.basisKeyboardMode = String(isKeyboardMode);
}
}, [window, isKeyboardMode]);

return null;
}

export default FocusVisiblePolyfill;
2 changes: 2 additions & 0 deletions src/providers/BasisProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import WindowProvider from "./WindowProvider";
import BreakpointProvider from "./BreakpointProvider";
import LinkProvider from "./LinkProvider";
import { enhanceTheme } from "../utils/theme";
import FocusVisiblePolyfill from "../components/internal/FocusVisiblePolyfill";

export const ThemeContext = React.createContext();

Expand All @@ -19,6 +20,7 @@ function BasisProvider({
return (
<ThemeContext.Provider value={enhancedTheme}>
<WindowProvider window={window}>
<FocusVisiblePolyfill />
<BreakpointProvider>
<LinkProvider
InternalLink={InternalLink}
Expand Down
1 change: 1 addition & 0 deletions src/themes/default/accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default (theme) => ({
textAlign: "left",
...textStyles(theme)["subtitle2"],
...textStyles(theme)["subtitle2.bold"],
outline: 0,
...theme.focusStyles.focusVisible,
},
accordionHeaderContent: {
Expand Down
1 change: 1 addition & 0 deletions src/themes/default/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default (theme) => ({
overflow: "hidden",
transition: theme.transitions.button,
borderRadius: theme.radii[1],
outline: 0,
...theme.focusStyles.focusVisible,
"a &": {
cursor: "pointer",
Expand Down
1 change: 1 addition & 0 deletions src/themes/default/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default (theme) => ({
margin: 0,
border: 0,
borderRadius: theme.radii[0],
outline: 0,
...theme.focusStyles.focusVisible,
},
dropdownButtonPlaceholder: {
Expand Down
55 changes: 11 additions & 44 deletions src/themes/default/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,61 +200,28 @@ theme.shadows = {
focus: `0 0 0px ${theme.radii[1]} ${theme.colors.secondary.lightBlue.t80}`,
};

const focusStyle = {
boxShadow: theme.shadows.focus,
// Make sure that the focus style sits above the surrounding elements with normal page flow
position: "relative",
zIndex: theme.zIndices.aboveNormalFlow,
};

theme.focusStyles = {
// https://github.com/WICG/focus-visible#backwards-compatibility
focusVisible: {
// Provide basic, default focus styles.
":focus": {
outline: 0,
boxShadow: theme.shadows.focus,
// Make sure that the focus style sits above the surrounding elements with normal page flow
position: "relative",
zIndex: theme.zIndices.aboveNormalFlow,
},
// Remove default focus styles for mouse users ONLY if :focus-visible is supported on this platform.
":focus:not(:focus-visible)": {
boxShadow: "none",
position: "initial",
zIndex: "initial",
},
// If :focus-visible is supported on this platform, provide enhanced focus styles for keyboard focus.
":focus-visible": {
boxShadow: theme.shadows.focus,
// Make sure that the focus style sits above the surrounding elements with normal page flow
position: "relative",
zIndex: theme.zIndices.aboveNormalFlow,
'[data-basis-keyboard-mode="true"] &': focusStyle,
},
},
focusVisibleAdjacentLabel: {
":focus + label": {
boxShadow: theme.shadows.focus,
// Make sure that the focus style sits above the surrounding elements with normal page flow
position: "relative",
zIndex: theme.zIndices.aboveNormalFlow,
},
":focus:not(:focus-visible) + label": {
boxShadow: "none",
position: "initial",
zIndex: "initial",
},
":focus-visible + label": {
boxShadow: theme.shadows.focus,
// Make sure that the focus style sits above the surrounding elements with normal page flow
position: "relative",
zIndex: theme.zIndices.aboveNormalFlow,
'[data-basis-keyboard-mode="true"] &': focusStyle,
},
},
};

theme.focusStyles.__keyboardFocus = {
...theme.focusStyles.focusVisible[":focus"],
...theme.focusStyles.focusVisible[":focus-visible"],
};

theme.focusStyles.__keyboardFocusAdjacentLabel = {
...theme.focusStyles.focusVisibleAdjacentLabel[":focus + label"],
...theme.focusStyles.focusVisibleAdjacentLabel[":focus-visible + label"],
};
theme.focusStyles.__keyboardFocus = focusStyle;
theme.focusStyles.__keyboardFocusAdjacentLabel = focusStyle;

export default {
...theme,
Expand Down
1 change: 1 addition & 0 deletions src/themes/default/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default (theme) => ({
link: {
textDecoration: "none",
borderRadius: theme.radii[0],
outline: 0,
...theme.focusStyles.focusVisible,
},
"link.light-bg": {
Expand Down
1 change: 1 addition & 0 deletions website/src/themes/website/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default (theme) => ({
overflow: "hidden",
transition:
"transform 100ms ease, color 100ms ease, border-color 100ms ease",
outline: 0,
...theme.focusStyles.focusVisible,
},
"button:disabled": {
Expand Down
1 change: 1 addition & 0 deletions website/src/themes/website/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export default (theme) => ({
display: "inline-block",
textDecoration: "none",
borderRadius: theme.radii[0],
outline: 0,
...theme.focusStyles.focusVisible,
},
"link.light-bg": {
Expand Down

1 comment on commit 6c8da38

@vercel
Copy link

@vercel vercel bot commented on 6c8da38 Jul 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.