Skip to content

Commit ce11205

Browse files
committedAug 14, 2021
feat(bar): add labelledby and describedby aria attributes support to SVG bar item component
1 parent b6e930f commit ce11205

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed
 

‎packages/bar/src/Bar.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ const InnerBar = <RawDatum extends BarDatum>({
103103
ariaDescribedBy,
104104
isFocusable = svgDefaultProps.isFocusable,
105105
barAriaLabel,
106+
barAriaLabelledBy,
107+
barAriaDescribedBy,
106108

107109
initialHiddenIds,
108110
}: InnerBarProps<RawDatum>) => {
@@ -279,6 +281,8 @@ const InnerBar = <RawDatum extends BarDatum>({
279281
tooltip,
280282
isFocusable,
281283
ariaLabel: barAriaLabel,
284+
ariaLabelledBy: barAriaLabelledBy,
285+
ariaDescribedBy: barAriaDescribedBy,
282286
}),
283287
[
284288
borderRadius,
@@ -294,6 +298,8 @@ const InnerBar = <RawDatum extends BarDatum>({
294298
tooltip,
295299
isFocusable,
296300
barAriaLabel,
301+
barAriaLabelledBy,
302+
barAriaDescribedBy,
297303
]
298304
)
299305

‎packages/bar/src/BarItem.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export const BarItem = <RawDatum extends BarDatum>({
3434

3535
isFocusable,
3636
ariaLabel,
37+
ariaLabelledBy,
38+
ariaDescribedBy,
3739
}: BarItemProps<RawDatum>) => {
3840
const theme = useTheme()
3941
const { showTooltipFromEvent, hideTooltip } = useTooltip()
@@ -81,6 +83,8 @@ export const BarItem = <RawDatum extends BarDatum>({
8183
focusable={isFocusable}
8284
tabIndex={isFocusable ? 0 : undefined}
8385
aria-label={ariaLabel ? ariaLabel() : undefined}
86+
aria-labelledby={ariaLabelledBy ? ariaLabelledBy() : undefined}
87+
aria-describedby={ariaDescribedBy ? ariaDescribedBy() : undefined}
8488
/>
8589
{shouldRenderLabel && (
8690
<animated.text

‎packages/bar/src/types.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ export interface BarItemProps<RawDatum extends BarDatum>
161161

162162
isFocusable: boolean
163163
ariaLabel?: BarSvgProps<RawDatum>['barAriaLabel']
164+
ariaLabelledBy?: BarSvgProps<RawDatum>['barAriaLabelledBy']
165+
ariaDescribedBy?: BarSvgProps<RawDatum>['barAriaDescribedBy']
164166
}
165167

166168
export type RenderBarProps<RawDatum extends BarDatum> = Omit<
@@ -263,7 +265,9 @@ export type BarSvgProps<RawDatum extends BarDatum> = Partial<BarCommonProps<RawD
263265
ariaLabelledBy?: React.AriaAttributes['aria-labelledby']
264266
ariaDescribedBy?: React.AriaAttributes['aria-describedby']
265267
isFocusable?: boolean
266-
barAriaLabel?: (...data: any) => string
268+
barAriaLabel?: (...data: any) => React.AriaAttributes['aria-label']
269+
barAriaLabelledBy?: (...data: any) => React.AriaAttributes['aria-labelledby']
270+
barAriaDescribedBy?: (...data: any) => React.AriaAttributes['aria-describedby']
267271
}>
268272

269273
export type BarCanvasProps<RawDatum extends BarDatum> = Partial<BarCommonProps<RawDatum>> &

‎packages/bar/tests/Bar.test.tsx

+9-5
Original file line numberDiff line numberDiff line change
@@ -643,20 +643,20 @@ describe('accessibility', () => {
643643
]}
644644
keys={['A', 'B']}
645645
animate={false}
646-
ariaLabel="Aria label"
646+
ariaLabel="AriaLabel"
647647
ariaLabelledBy="AriaLabelledBy"
648648
ariaDescribedBy="AriaDescribedBy"
649649
/>
650650
)
651651

652652
const svg = wrapper.find('svg')
653653

654-
expect(svg.prop('aria-label')).toBe('Aria label')
654+
expect(svg.prop('aria-label')).toBe('AriaLabel')
655655
expect(svg.prop('aria-labelledby')).toBe('AriaLabelledBy')
656656
expect(svg.prop('aria-describedby')).toBe('AriaDescribedBy')
657657
})
658658

659-
it('should add an aria-label attribute to bars', () => {
659+
it('should add an aria attributes to bars', () => {
660660
const wrapper = mount(
661661
<Bar
662662
width={500}
@@ -667,15 +667,19 @@ describe('accessibility', () => {
667667
]}
668668
keys={['A', 'B']}
669669
animate={false}
670-
barAriaLabel={() => `Bar aria label`}
670+
barAriaLabel={() => 'BarAriaLabel'}
671+
barAriaLabelledBy={() => 'BarAriaLabelledBy'}
672+
barAriaDescribedBy={() => 'BarAriaDescribedBy'}
671673
/>
672674
)
673675

674676
wrapper
675677
.find('BarItem')
676678
.find('rect')
677679
.forEach(bar => {
678-
expect(bar.prop('aria-label')).toBe('Bar aria label')
680+
expect(bar.prop('aria-label')).toBe('BarAriaLabel')
681+
expect(bar.prop('aria-labelledby')).toBe('BarAriaLabelledBy')
682+
expect(bar.prop('aria-describedby')).toBe('BarAriaDescribedBy')
679683
})
680684
})
681685
})

‎website/src/data/components/bar/props.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -602,29 +602,43 @@ const props = [
602602
key: 'ariaLabel',
603603
flavors: ['svg'],
604604
group: 'Accessibility',
605-
help: 'Main element aria-label.',
605+
help: 'Main element [aria-label](https://www.w3.org/TR/wai-aria/#aria-label).',
606606
type: 'string',
607607
},
608608
{
609609
key: 'ariaLabelledBy',
610610
flavors: ['svg'],
611611
group: 'Accessibility',
612-
help: 'Main element aria-labelledby.',
612+
help: 'Main element [aria-labelledby](https://www.w3.org/TR/wai-aria/#aria-labelledby).',
613613
type: 'string',
614614
},
615615
{
616616
key: 'ariaDescribedBy',
617617
flavors: ['svg'],
618618
group: 'Accessibility',
619-
help: 'Main element aria-describedby.',
619+
help: 'Main element [aria-describedby](https://www.w3.org/TR/wai-aria/#aria-describedby).',
620620
type: 'string',
621621
},
622622
{
623623
key: 'barAriaLabel',
624624
flavors: ['svg'],
625625
group: 'Accessibility',
626-
help: 'Aria label for bar items.',
627-
type: 'string | (bar) => string',
626+
help: '[aria-label](https://www.w3.org/TR/wai-aria/#aria-label) for bar items.',
627+
type: '(bar) => string',
628+
},
629+
{
630+
key: 'barAriaLabelledBy',
631+
flavors: ['svg'],
632+
group: 'Accessibility',
633+
help: '[aria-labelledby](https://www.w3.org/TR/wai-aria/#aria-labelledby) for bar items.',
634+
type: '(bar) => string',
635+
},
636+
{
637+
key: 'barAriaDescribedBy',
638+
flavors: ['svg'],
639+
group: 'Accessibility',
640+
help: '[aria-describedby](https://www.w3.org/TR/wai-aria/#aria-describedby) for bar items.',
641+
type: '(bar) => string',
628642
},
629643
]
630644

0 commit comments

Comments
 (0)
Please sign in to comment.