Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish updating global focus styles #2050

Merged
merged 15 commits into from May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silent-falcons-exist.md
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Finishes updating components with the global focus styles defined in Primer CSS ([this PR](https://github.com/primer/css/pull/1744))
4 changes: 2 additions & 2 deletions docs/content/SubNav.md
Expand Up @@ -39,7 +39,7 @@ This ensures that the NavLink gets `activeClassName='selected'`
<SubNav.Link href="#support">Support</SubNav.Link>
</SubNav.Links>

<TextInput type="search" icon={SearchIcon} sx={{width: 320}} />
<TextInput type="search" leadingVisual={SearchIcon} sx={{width: 320}} />
</SubNav>
```

Expand All @@ -64,7 +64,7 @@ This ensures that the NavLink gets `activeClassName='selected'`
</ActionList>
</ActionMenu.Overlay>
</ActionMenu>
<TextInput type="search" icon={SearchIcon} width={320} />
<TextInput type="search" leadingVisual={SearchIcon} width={320} />
</FilteredSearch>
<SubNav.Links>
<SubNav.Link href="#home" selected>
Expand Down
@@ -1,24 +1,33 @@
import {BaseStyles, Box, ThemeProvider, useTheme} from '@primer/react'
import {DropdownMenu, DropdownButton} from '@primer/react/deprecated'
import React from 'react'
import {ActionMenu, ActionList, BaseStyles, Box, ThemeProvider, useTheme} from '@primer/react'

function ThemeSwitcher() {
const {theme, dayScheme, setDayScheme} = useTheme()
const items = Object.keys(theme.colorSchemes).map(scheme => ({text: scheme.replace(/_/g, ' '), key: scheme}))
const items = Object.keys(theme.colorSchemes).map(scheme => ({name: scheme.replace(/_/g, ' '), key: scheme}))
const selectedItem = React.useMemo(() => items.find(item => item.key === dayScheme), [items, dayScheme])
const itemsKeys = items.map(item => item.key)
const [selectedIndex, setSelectedIndex] = React.useState(itemsKeys.indexOf(dayScheme))

return (
<DropdownMenu
renderAnchor={({children, ...anchorProps}) => (
<DropdownButton {...anchorProps} sx={{variant: 'small'}}>
{children}
</DropdownButton>
)}
items={items}
selectedItem={selectedItem}
onChange={item => {
setDayScheme(item.key)
}}
/>
<ActionMenu>
<ActionMenu.Button aria-label="Select field type">{selectedItem?.name}</ActionMenu.Button>
<ActionMenu.Overlay width="medium">
<ActionList selectionVariant="single">
{items.map((type, index) => (
<ActionList.Item
key={index}
selected={index === selectedIndex}
onSelect={() => {
setSelectedIndex(index)
setDayScheme(items[index].key)
}}
>
{type.name}
</ActionList.Item>
))}
</ActionList>
</ActionMenu.Overlay>
</ActionMenu>
)
}

Expand Down
36 changes: 1 addition & 35 deletions src/Button/styles.ts
Expand Up @@ -2,17 +2,6 @@ import {VariantType} from './types'
import {Theme} from '../ThemeProvider'

export const TEXT_ROW_HEIGHT = '20px' // custom value off the scale
const focusOutlineStyles = {
outline: '2px solid',
outlineColor: 'accent.fg',
outlineOffset: '-2px'
}
const fallbackFocus = {
...focusOutlineStyles,
':not(:focus-visible)': {
outline: 'solid 1px transparent'
}
}

export const getVariantStyles = (variant: VariantType = 'default', theme?: Theme) => {
const style = {
Expand All @@ -23,11 +12,6 @@ export const getVariantStyles = (variant: VariantType = 'default', theme?: Theme
'&:hover:not([disabled])': {
backgroundColor: 'btn.hoverBg'
},
// focus must come before :active so that the active box shadow overrides
'&:focus:not([disabled])': {
...fallbackFocus
},
'&:focus-visible:not([disabled])': focusOutlineStyles,
'&:active:not([disabled])': {
backgroundColor: 'btn.activeBg',
borderColor: 'btn.activeBorder'
Expand All @@ -52,13 +36,10 @@ export const getVariantStyles = (variant: VariantType = 'default', theme?: Theme
color: 'btn.primary.hoverText',
backgroundColor: 'btn.primary.hoverBg'
},
// focus must come before :active so that the active box shadow overrides
'&:focus:not([disabled])': {
boxShadow: 'inset 0 0 0 3px',
...fallbackFocus
boxShadow: 'inset 0 0 0 3px'
},
'&:focus-visible:not([disabled])': {
...focusOutlineStyles,
boxShadow: 'inset 0 0 0 3px'
},
'&:active:not([disabled])': {
Expand Down Expand Up @@ -95,11 +76,6 @@ export const getVariantStyles = (variant: VariantType = 'default', theme?: Theme
color: 'btn.danger.hoverText'
}
},
// focus must come before :active so that the active box shadow overrides
'&:focus:not([disabled])': {
...fallbackFocus
},
'&:focus-visible:not([disabled])': focusOutlineStyles,
'&:active:not([disabled])': {
color: 'btn.danger.selectedText',
backgroundColor: 'btn.danger.selectedBg',
Expand Down Expand Up @@ -134,11 +110,6 @@ export const getVariantStyles = (variant: VariantType = 'default', theme?: Theme
'&:hover:not([disabled])': {
backgroundColor: 'btn.hoverBg'
},
// focus must come before :active so that the active box shadow overrides
'&:focus:not([disabled])': {
...fallbackFocus
},
'&:focus-visible:not([disabled])': focusOutlineStyles,
'&:active:not([disabled])': {
backgroundColor: 'btn.selectedBg'
},
Expand Down Expand Up @@ -168,11 +139,6 @@ export const getVariantStyles = (variant: VariantType = 'default', theme?: Theme
color: 'inherit'
}
},
// focus must come before :active so that the active box shadow overrides
'&:focus:not([disabled])': {
...fallbackFocus
},
'&:focus-visible:not([disabled])': focusOutlineStyles,
'&:active:not([disabled])': {
color: 'btn.outline.selectedText',
backgroundColor: 'btn.outline.selectedBg',
Expand Down
6 changes: 5 additions & 1 deletion src/Button/types.ts
Expand Up @@ -2,8 +2,12 @@ import React, {HTMLAttributes, ComponentPropsWithRef} from 'react'
import styled from 'styled-components'
import {IconProps} from '@primer/octicons-react'
import sx, {SxProp} from '../sx'
import getGlobalFocusStyles from '../_getGlobalFocusStyles'

export const StyledButton = styled.button<SxProp>(sx)
export const StyledButton = styled.button<SxProp>`
${getGlobalFocusStyles('-2px')};
${sx};
`

export type VariantType = 'default' | 'primary' | 'invisible' | 'danger' | 'outline'

Expand Down
2 changes: 2 additions & 0 deletions src/Checkbox.tsx
Expand Up @@ -4,6 +4,7 @@ import React, {ChangeEventHandler, InputHTMLAttributes, ReactElement, useContext
import sx, {SxProp} from './sx'
import {FormValidationStatus} from './utils/types/FormValidationStatus'
import {CheckboxGroupContext} from './CheckboxGroup'
import getGlobalFocusStyles from './_getGlobalFocusStyles'

export type CheckboxProps = {
/**
Expand Down Expand Up @@ -38,6 +39,7 @@ const StyledCheckbox = styled.input`
cursor: pointer;

${props => props.disabled && `cursor: not-allowed;`}
${getGlobalFocusStyles(0)};

${sx}
`
Expand Down
3 changes: 3 additions & 0 deletions src/Pagination/Pagination.tsx
Expand Up @@ -3,6 +3,7 @@ import styled from 'styled-components'
import Box from '../Box'
import {get} from '../constants'
import sx, {SxProp} from '../sx'
import getGlobalFocusStyles from '../_getGlobalFocusStyles'
import {buildComponentData, buildPaginationModel} from './model'

const Page = styled.a`
Expand Down Expand Up @@ -37,6 +38,8 @@ const Page = styled.a`
transition-duration: 0.1s;
}

${getGlobalFocusStyles(0)};
Copy link
Contributor

Choose a reason for hiding this comment

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

The focus ring on pages has a subtle gray border that makes it look a little "fuzzy":
CleanShot 2022-05-04 at 07 28 25@2x

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is another thing I was keeping from Primer CSS.


&:active {
border-color: ${get('colors.border.muted')};
}
Expand Down
2 changes: 2 additions & 0 deletions src/Radio.tsx
Expand Up @@ -3,6 +3,7 @@ import React, {ChangeEventHandler, InputHTMLAttributes, ReactElement, useContext
import sx, {SxProp} from './sx'
import {FormValidationStatus} from './utils/types/FormValidationStatus'
import {RadioGroupContext} from './RadioGroup'
import getGlobalFocusStyles from './_getGlobalFocusStyles'

export type RadioProps = {
/**
Expand Down Expand Up @@ -41,6 +42,7 @@ const StyledRadio = styled.input`
cursor: pointer;

${props => props.disabled && `cursor: not-allowed;`}
${getGlobalFocusStyles(0)};
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to make the focus ring a circle for radios?
CleanShot 2022-05-04 at 07 27 38@2x

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is, but I'm matching what we have in Primer CSS.


${sx}
`
Expand Down
2 changes: 1 addition & 1 deletion src/SubNav.tsx
Expand Up @@ -96,7 +96,7 @@ const SubNavLink = styled.a.attrs<StyledSubNavLinkProps>(props => ({
&:focus {
text-decoration: none;
background-color: ${get('colors.canvas.subtle')};
transition: 0.2s ease;
transition: background-color 0.2s ease;

.SubNav-octicon {
color: ${get('colors.fg.muted')};
Expand Down
3 changes: 3 additions & 0 deletions src/TabNav.tsx
Expand Up @@ -5,6 +5,7 @@ import styled from 'styled-components'
import {get} from './constants'
import sx, {SxProp} from './sx'
import {ComponentProps} from './utils/types'
import getGlobalFocusStyles from './_getGlobalFocusStyles'

const ITEM_CLASS = 'TabNav-item'
const SELECTED_CLASS = 'selected'
Expand Down Expand Up @@ -49,6 +50,8 @@ const TabNavLink = styled.a.attrs<StyledTabNavLinkProps>(props => ({
border: 1px solid transparent;
border-bottom: 0;

${getGlobalFocusStyles('-6px')};

&:hover,
&:focus {
color: ${get('colors.fg.default')};
Expand Down
5 changes: 4 additions & 1 deletion src/UnderlineNav.tsx
Expand Up @@ -5,6 +5,7 @@ import styled from 'styled-components'
import {get} from './constants'
import sx, {SxProp} from './sx'
import {ComponentProps} from './utils/types'
import getGlobalFocusStyles from './_getGlobalFocusStyles'

const ITEM_CLASS = 'PRC-UnderlineNav-item'
const SELECTED_CLASS = 'PRC-selected'
Expand Down Expand Up @@ -86,7 +87,7 @@ const UnderlineNavLink = styled.a.attrs<StyledUnderlineNavLinkProps>(props => ({
color: ${get('colors.fg.default')};
text-decoration: none;
border-bottom-color: ${get('colors.neutral.muted')};
transition: 0.2s ease;
transition: border-bottom-color 0.2s ease;

.PRC-UnderlineNav-octicon {
color: ${get('colors.fg.muted')};
Expand All @@ -102,6 +103,8 @@ const UnderlineNavLink = styled.a.attrs<StyledUnderlineNavLinkProps>(props => ({
}
}

${getGlobalFocusStyles('-8px')};

${sx};
`

Expand Down
48 changes: 10 additions & 38 deletions src/_TextInputWrapper.tsx
Expand Up @@ -65,20 +65,22 @@ export type StyledWrapperProps = {
const textInputBasePadding = '12px'
export const textInputHorizPadding = textInputBasePadding

// TODO: figure out how to type a themed CSS function (e.g.: css`color: blue;`)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const renderFocusStyles = (hasTrailingAction: boolean, isInputFocused: boolean, focusStyles: any) => {
const renderFocusStyles = (hasTrailingAction: boolean, isInputFocused: boolean) => {
if (hasTrailingAction) {
return (
isInputFocused &&
css`
${focusStyles}
border-color: ${get('colors.accent.fg')};
outline: none;
box-shadow: inset 0 0 0 1px ${get('colors.accent.fg')};
`
)
}
return css`
&:focus-within {
${focusStyles}
border-color: ${get('colors.accent.fg')};
outline: none;
box-shadow: inset 0 0 0 1px ${get('colors.accent.fg')};
}
`
}
Expand Down Expand Up @@ -110,15 +112,7 @@ export const TextInputBaseWrapper = styled.span<StyledBaseWrapperProps>`
color: ${get('colors.fg.subtle')};
}

${props =>
renderFocusStyles(
Boolean(props.hasTrailingAction),
Boolean(props.isInputFocused),
css`
border-color: ${get('colors.accent.emphasis')};
box-shadow: ${get('shadows.primer.shadow.focus')};
`
)}
${props => renderFocusStyles(Boolean(props.hasTrailingAction), Boolean(props.isInputFocused))}

> textarea {
padding: ${textInputBasePadding};
Expand Down Expand Up @@ -154,29 +148,14 @@ export const TextInputBaseWrapper = styled.span<StyledBaseWrapperProps>`
props.validationStatus === 'error' &&
css`
border-color: ${get('colors.danger.emphasis')};
${renderFocusStyles(
Boolean(props.hasTrailingAction),
Boolean(props.isInputFocused),
css`
border-color: ${get('colors.danger.emphasis')};
box-shadow: ${get('shadows.btn.danger.focusShadow')};
`
)}
${renderFocusStyles(Boolean(props.hasTrailingAction), Boolean(props.isInputFocused))}
`}


${props =>
props.validationStatus === 'success' &&
css`
border-color: ${get('colors.success.emphasis')};
${renderFocusStyles(
Boolean(props.hasTrailingAction),
Boolean(props.isInputFocused),
css`
border-color: ${get('colors.success.emphasis')};
box-shadow: 0 0 0 3px ${get('colors.success.muted')};
`
)}
`}

${props =>
Expand Down Expand Up @@ -230,14 +209,7 @@ const TextInputWrapper = styled(TextInputBaseWrapper)<StyledWrapperProps>`
props.validationStatus === 'warning' &&
css`
border-color: ${get('colors.attention.emphasis')};
${renderFocusStyles(
Boolean(props.hasTrailingAction),
Boolean(props.isInputFocused),
css`
border-color: ${get('colors.attention.emphasis')};
box-shadow: 0 0 0 3px ${get('colors.attention.muted')};
`
)}
${renderFocusStyles(Boolean(props.hasTrailingAction), Boolean(props.isInputFocused))}
`}

${sx};
Expand Down
16 changes: 16 additions & 0 deletions src/__tests__/Pagination/__snapshots__/Pagination.test.tsx.snap
Expand Up @@ -43,6 +43,22 @@ exports[`Pagination renders consistently 1`] = `
transition-duration: 0.1s;
}

.c2:focus:not(:disabled) {
box-shadow: none;
outline: 2px solid #0969da;
outline-offset: 0;
}

.c2:focus:not(:disabled):not(:focus-visible) {
outline: solid 1px transparent;
}

.c2:focus-visible:not(:disabled) {
box-shadow: none;
outline: 2px solid #0969da;
outline-offset: 0;
}

.c2:active {
border-color: hsla(210,18%,87%,1);
}
Expand Down