Skip to content

Commit

Permalink
Show focus styles only when needed (#1022)
Browse files Browse the repository at this point in the history
* Apply focus styles only when appropriate

* Simplify Checkbox and RadioButton focus styles

* Revert Selector refactor to apply new focus styles

* Fix focus styles for ImageInput and Table
  • Loading branch information
connor-baer authored Jul 6, 2021
1 parent cee2c0c commit afb2eb2
Show file tree
Hide file tree
Showing 50 changed files with 847 additions and 263 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-dolphins-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sumup/circuit-ui': minor
---

Added the `focusVisible` style mixin that shows a focus outline only when the user agent determines via heuristics that the focus should be made evident on the element (see [`:focus-visible`](https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible)).
14 changes: 6 additions & 8 deletions packages/circuit-ui/components/Anchor/Anchor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { css } from '@emotion/core';
import { Dispatch as TrackingProps } from '@sumup/collector';
import { Theme } from '@sumup/design-tokens';

import { focusOutline } from '../../styles/style-mixins';
import { focusVisible } from '../../styles/style-mixins';
import { ReturnType } from '../../types/return-type';
import { Body, BodyProps } from '../Body/Body';
import { useComponents } from '../ComponentsContext';
Expand All @@ -51,7 +51,7 @@ type ButtonElProps = Omit<HTMLProps<HTMLButtonElement>, 'size' | 'onClick'>;

export type AnchorProps = BaseProps & LinkElProps & ButtonElProps;

const baseStyles = (theme: Theme) => css`
const anchorStyles = (theme: Theme) => css`
display: inline-block;
text-decoration: underline;
text-decoration-skip-ink: auto;
Expand All @@ -63,6 +63,7 @@ const baseStyles = (theme: Theme) => css`
margin-left: 0;
margin-right: 0;
color: ${theme.colors.p500};
border-radius: ${theme.borderRadius.byte};
transition: opacity ${theme.transitions.default},
color ${theme.transitions.default},
background-color ${theme.transitions.default},
Expand All @@ -86,10 +87,7 @@ const baseStyles = (theme: Theme) => css`
}
}
&:focus {
${focusOutline({ theme })};
border-radius: ${theme.borderRadius.byte};
}
${focusVisible(theme)};
`;

/**
Expand Down Expand Up @@ -119,7 +117,7 @@ export const Anchor = forwardRef(
return (
<Body
{...props}
css={baseStyles}
css={anchorStyles}
as={Link}
ref={ref}
onClick={handleClick}
Expand All @@ -131,7 +129,7 @@ export const Anchor = forwardRef(
<Body
as="button"
{...props}
css={baseStyles}
css={anchorStyles}
ref={ref}
onClick={handleClick}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exports[`Anchor styles should render with custom styles 1`] = `
margin-left: 0;
margin-right: 0;
color: #3063E9;
border-radius: 8px;
-webkit-transition: opacity 120ms ease-in-out,color 120ms ease-in-out,background-color 120ms ease-in-out,border-color 120ms ease-in-out;
transition: opacity 120ms ease-in-out,color 120ms ease-in-out,background-color 120ms ease-in-out,border-color 120ms ease-in-out;
color: rebeccapurple;
Expand All @@ -44,13 +45,16 @@ exports[`Anchor styles should render with custom styles 1`] = `
.circuit-0:focus {
outline: 0;
box-shadow: 0 0 0 4px #AFD0FE;
border-radius: 8px;
}
.circuit-0:focus::-moz-focus-inner {
border: 0;
}
.circuit-0:focus:not(:focus-visible) {
box-shadow: none;
}
<a
class="circuit-0"
href="https://sumup.com"
Expand All @@ -77,6 +81,7 @@ exports[`Anchor styles should render with default styles 1`] = `
margin-left: 0;
margin-right: 0;
color: #3063E9;
border-radius: 8px;
-webkit-transition: opacity 120ms ease-in-out,color 120ms ease-in-out,background-color 120ms ease-in-out,border-color 120ms ease-in-out;
transition: opacity 120ms ease-in-out,color 120ms ease-in-out,background-color 120ms ease-in-out,border-color 120ms ease-in-out;
}
Expand All @@ -102,13 +107,16 @@ exports[`Anchor styles should render with default styles 1`] = `
.circuit-0:focus {
outline: 0;
box-shadow: 0 0 0 4px #AFD0FE;
border-radius: 8px;
}
.circuit-0:focus::-moz-focus-inner {
border: 0;
}
.circuit-0:focus:not(:focus-visible) {
box-shadow: none;
}
<a
class="circuit-0"
href="https://sumup.com"
Expand Down
7 changes: 2 additions & 5 deletions packages/circuit-ui/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import styled, { StyleProps } from '../../styles/styled';
import {
typography,
disableVisually,
focusOutline,
focusVisible,
} from '../../styles/style-mixins';
import { ReturnType } from '../../types/return-type';
import { useComponents } from '../ComponentsContext';
Expand Down Expand Up @@ -139,10 +139,6 @@ const baseStyles = ({ theme }: StyleProps) => css`
background-color ${theme.transitions.default},
border-color ${theme.transitions.default};
&:focus {
${focusOutline({ theme })};
}
&:disabled,
&[disabled] {
${disableVisually()};
Expand Down Expand Up @@ -271,6 +267,7 @@ const StyledButton = styled('button', {
shouldForwardProp: (prop) => isPropValid(prop) && prop !== 'size',
})<ButtonProps>(
typography('one'),
focusVisible,
baseStyles,
primaryStyles,
secondaryStyles,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ exports[`Button styles should render a button with icon 1`] = `
border: 0;
}
.circuit-1:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-1:disabled,
.circuit-1[disabled] {
opacity: 0.5;
Expand Down Expand Up @@ -137,6 +141,10 @@ exports[`Button styles should render a disabled button 1`] = `
border: 0;
}
.circuit-0:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-0:disabled,
.circuit-0[disabled] {
opacity: 0.5;
Expand Down Expand Up @@ -206,6 +214,10 @@ exports[`Button styles should render a giga button 1`] = `
border: 0;
}
.circuit-0:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-0:disabled,
.circuit-0[disabled] {
opacity: 0.5;
Expand Down Expand Up @@ -274,6 +286,10 @@ exports[`Button styles should render a kilo button 1`] = `
border: 0;
}
.circuit-0:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-0:disabled,
.circuit-0[disabled] {
opacity: 0.5;
Expand Down Expand Up @@ -342,6 +358,10 @@ exports[`Button styles should render a primary button by default 1`] = `
border: 0;
}
.circuit-0:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-0:disabled,
.circuit-0[disabled] {
opacity: 0.5;
Expand Down Expand Up @@ -410,6 +430,10 @@ exports[`Button styles should render a secondary button 1`] = `
border: 0;
}
.circuit-0:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-0:disabled,
.circuit-0[disabled] {
opacity: 0.5;
Expand Down Expand Up @@ -479,6 +503,10 @@ exports[`Button styles should render a stretched button 1`] = `
border: 0;
}
.circuit-0:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-0:disabled,
.circuit-0[disabled] {
opacity: 0.5;
Expand Down Expand Up @@ -549,6 +577,10 @@ exports[`Button styles should render a tertiary button 1`] = `
border: 0;
}
.circuit-0:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-0:disabled,
.circuit-0[disabled] {
opacity: 0.5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ exports[`ButtonGroup Center aligment should render with center alignment styles
border: 0;
}
.circuit-0:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-0:disabled,
.circuit-0[disabled] {
opacity: 0.5;
Expand Down Expand Up @@ -104,6 +108,10 @@ exports[`ButtonGroup Center aligment should render with center alignment styles
border: 0;
}
.circuit-1:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-1:disabled,
.circuit-1[disabled] {
opacity: 0.5;
Expand Down Expand Up @@ -229,6 +237,10 @@ exports[`ButtonGroup Left aligment should render with left alignment styles 1`]
border: 0;
}
.circuit-0:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-0:disabled,
.circuit-0[disabled] {
opacity: 0.5;
Expand Down Expand Up @@ -289,6 +301,10 @@ exports[`ButtonGroup Left aligment should render with left alignment styles 1`]
border: 0;
}
.circuit-1:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-1:disabled,
.circuit-1[disabled] {
opacity: 0.5;
Expand Down Expand Up @@ -462,6 +478,10 @@ exports[`ButtonGroup should render with default styles 1`] = `
border: 0;
}
.circuit-0:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-0:disabled,
.circuit-0[disabled] {
opacity: 0.5;
Expand Down Expand Up @@ -522,6 +542,10 @@ exports[`ButtonGroup should render with default styles 1`] = `
border: 0;
}
.circuit-1:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-1:disabled,
.circuit-1[disabled] {
opacity: 0.5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,10 @@ exports[`RangePicker should render with default styles 1`] = `
border: 0;
}
.circuit-2 .DayPickerNavigation_button__horizontal:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-2 .DayPickerNavigation_button__horizontal:disabled,
.circuit-2 .DayPickerNavigation_button__horizontal[disabled] {
opacity: 0.5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,10 @@ exports[`SingleDayPicker should render with default styles 1`] = `
border: 0;
}
.circuit-0 .DayPickerNavigation_button__horizontal:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-0 .DayPickerNavigation_button__horizontal:disabled,
.circuit-0 .DayPickerNavigation_button__horizontal[disabled] {
opacity: 0.5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { withTheme } from 'emotion-theming';
import {
typography,
shadowTriple,
focusOutline,
focusVisible,
disableVisually,
} from '../../../../styles/style-mixins';

Expand Down Expand Up @@ -178,9 +178,7 @@ const navButtons = ({ theme }) => css`
border-color: ${theme.colors.n800};
}
&:focus {
${focusOutline({ theme })};
}
${focusVisible(theme)};
&:disabled,
&[disabled] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,10 @@ exports[`CalendarWrapper should render with default styles 1`] = `
border: 0;
}
.circuit-0 .DayPickerNavigation_button__horizontal:focus:not(:focus-visible) {
box-shadow: none;
}
.circuit-0 .DayPickerNavigation_button__horizontal:disabled,
.circuit-0 .DayPickerNavigation_button__horizontal[disabled] {
opacity: 0.5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ exports[`CalendarTag should render with default styles 1`] = `
border: 0;
}
.circuit-0:focus:not(:focus-visible) {
box-shadow: none;
}
<div>
<div
class="circuit-1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ exports[`CalendarTagTwoStep should render with default styles 1`] = `
border: 0;
}
.circuit-0:focus:not(:focus-visible) {
box-shadow: none;
}
<div>
<div
class="circuit-1"
Expand Down
Loading

0 comments on commit afb2eb2

Please sign in to comment.