-
Notifications
You must be signed in to change notification settings - Fork 520
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CounterLabel: Accessibility improvements for better screen reader ann…
…ouncing (#2604) * Accessibility improvements on CounterLabel for better screen reader announcing * use updated CounterLabel on UnderlineNav and fix tests * update snapshots Co-authored-by: Josep Martins <josepmartins@github.com>
- Loading branch information
1 parent
0468315
commit 7e9e367
Showing
10 changed files
with
251 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/react': patch | ||
--- | ||
|
||
Accessibility improvements on CounterLabel for better screen reader announcing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,48 @@ | ||
import styled from 'styled-components' | ||
import {get} from './constants' | ||
import sx, {SxProp} from './sx' | ||
import {ComponentProps} from './utils/types' | ||
import React from 'react' | ||
import Box from './Box' | ||
import {BetterSystemStyleObject, SxProp, merge} from './sx' | ||
import VisuallyHidden from './_VisuallyHidden' | ||
|
||
type StyledCounterLabelProps = { | ||
export type CounterLabelProps = { | ||
scheme?: 'primary' | 'secondary' | ||
} & SxProp | ||
|
||
const colorStyles = ({scheme, ...props}: StyledCounterLabelProps) => { | ||
return { | ||
color: | ||
scheme === 'secondary' | ||
? get('colors.fg.default')(props) | ||
: scheme === 'primary' | ||
? get('colors.fg.onEmphasis')(props) | ||
: get('colors.fg.default')(props) | ||
} | ||
const CounterLabel: React.FC<React.PropsWithChildren<CounterLabelProps>> = ({ | ||
scheme = 'secondary', | ||
sx = {}, | ||
children, | ||
...props | ||
}) => { | ||
return ( | ||
<> | ||
<Box | ||
as="span" | ||
aria-hidden="true" | ||
sx={merge<BetterSystemStyleObject>( | ||
{ | ||
display: 'inline-block', | ||
padding: '2px 5px', | ||
fontSize: 0, | ||
fontWeight: 'bold', | ||
lineHeight: 'condensedUltra', | ||
borderRadius: '20px', | ||
backgroundColor: scheme === 'primary' ? 'neutral.emphasis' : 'neutral.muted', | ||
color: scheme === 'primary' ? 'fg.onEmphasis' : 'fg.default', | ||
'&:empty': { | ||
display: 'none' | ||
} | ||
}, | ||
sx | ||
)} | ||
{...props} | ||
> | ||
{children} | ||
</Box> | ||
<VisuallyHidden> {`(${children})`}</VisuallyHidden> | ||
</> | ||
) | ||
} | ||
|
||
const bgStyles = ({scheme, ...props}: StyledCounterLabelProps) => { | ||
return { | ||
backgroundColor: | ||
scheme === 'secondary' | ||
? get('colors.neutral.muted')(props) | ||
: scheme === 'primary' | ||
? get('colors.neutral.emphasis')(props) | ||
: get('colors.neutral.muted')(props) | ||
} | ||
} | ||
|
||
const CounterLabel = styled.span<StyledCounterLabelProps>` | ||
display: inline-block; | ||
padding: 2px 5px; | ||
font-size: ${get('fontSizes.0')}; | ||
font-weight: ${get('fontWeights.bold')}; | ||
line-height: ${get('lineHeights.condensedUltra')}; | ||
border-radius: 20px; | ||
${colorStyles}; | ||
${bgStyles}; | ||
&:empty { | ||
display: none; | ||
} | ||
${sx}; | ||
` | ||
CounterLabel.displayName = 'CounterLabel' | ||
|
||
export type CounterLabelProps = ComponentProps<typeof CounterLabel> | ||
export default CounterLabel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,134 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`CounterLabel renders consistently 1`] = ` | ||
.c0 { | ||
[ | ||
.c0 { | ||
display: inline-block; | ||
padding: 2px 5px; | ||
font-size: 12px; | ||
font-weight: 600; | ||
line-height: 1; | ||
border-radius: 20px; | ||
color: #24292f; | ||
background-color: rgba(175,184,193,0.2); | ||
color: #24292f; | ||
} | ||
.c0:empty { | ||
display: none; | ||
} | ||
<span | ||
className="c0" | ||
/> | ||
aria-hidden="true" | ||
className="c0" | ||
/>, | ||
.c0 { | ||
position: absolute; | ||
width: 1px; | ||
height: 1px; | ||
padding: 0; | ||
margin: -1px; | ||
overflow: hidden; | ||
-webkit-clip: rect(0,0,0,0); | ||
clip: rect(0,0,0,0); | ||
white-space: nowrap; | ||
border-width: 0; | ||
} | ||
<span | ||
className="c0" | ||
> | ||
(undefined) | ||
</span>, | ||
] | ||
`; | ||
|
||
exports[`CounterLabel renders with secondary scheme when no "scheme" prop is provided 1`] = ` | ||
.c0 { | ||
display: inline-block; | ||
padding: 2px 5px; | ||
font-size: 12px; | ||
font-weight: bold; | ||
line-height: condensedUltra; | ||
border-radius: 20px; | ||
background-color: neutral.muted; | ||
color: fg.default; | ||
} | ||
.c0:empty { | ||
display: none; | ||
} | ||
.c1 { | ||
position: absolute; | ||
width: 1px; | ||
height: 1px; | ||
padding: 0; | ||
margin: -1px; | ||
overflow: hidden; | ||
-webkit-clip: rect(0,0,0,0); | ||
clip: rect(0,0,0,0); | ||
white-space: nowrap; | ||
border-width: 0; | ||
} | ||
<div> | ||
<span | ||
aria-hidden="true" | ||
class="c0" | ||
> | ||
1234 | ||
</span> | ||
<span | ||
class="c1" | ||
> | ||
(1234) | ||
</span> | ||
</div> | ||
`; | ||
|
||
exports[`CounterLabel respects the primary "scheme" prop 1`] = ` | ||
.c0 { | ||
display: inline-block; | ||
padding: 2px 5px; | ||
font-size: 12px; | ||
font-weight: bold; | ||
line-height: condensedUltra; | ||
border-radius: 20px; | ||
background-color: neutral.emphasis; | ||
color: fg.onEmphasis; | ||
} | ||
.c0:empty { | ||
display: none; | ||
} | ||
.c1 { | ||
position: absolute; | ||
width: 1px; | ||
height: 1px; | ||
padding: 0; | ||
margin: -1px; | ||
overflow: hidden; | ||
-webkit-clip: rect(0,0,0,0); | ||
clip: rect(0,0,0,0); | ||
white-space: nowrap; | ||
border-width: 0; | ||
} | ||
<div> | ||
<span | ||
aria-hidden="true" | ||
class="c0" | ||
> | ||
1234 | ||
</span> | ||
<span | ||
class="c1" | ||
> | ||
(1234) | ||
</span> | ||
</div> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.