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

feat: Add some accessibility updates to BarChart component #444

Merged
merged 6 commits into from
Jul 24, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/BarChart/CustomTooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const CustomTooltip: React.FC<CustomToolTipProps> = (
}));
}
return (
<Paper elevation={1} sx={{ p: 2 }}>
<Paper aria-live="assertive" elevation={1} sx={{ p: 2 }} role="status">
<Typography sx={{ color: tooltipColor, mb: 0.5 }}>{label}</Typography>

{showValue &&
Expand Down
3 changes: 3 additions & 0 deletions src/components/BarChart/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export default {
args: {
barKeys: defaultBarKeys,
data: defaultData,
description:
'A bar chart displaying the volumes per US state in 2019, 2020, and 2021',
title: 'Volumes for US States',
},
argTypes: Playground(
{
Expand Down
17 changes: 15 additions & 2 deletions src/components/BarChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { useTheme, useThemeProps } from '@mui/material/styles';
import { TypographyProps } from '@mui/material/Typography';
import { isFunction } from 'lodash';
import { isFunction, kebabCase, toLower } from 'lodash';
import numeral from 'numeral';
import React, { useMemo } from 'react';
import {
Expand Down Expand Up @@ -63,6 +63,7 @@ export interface BarChartProps {
customizeBarFillColor?: (index: number, key: string) => string | undefined;
customizedAxisTickProps?: CustomizedAxisTickProps;
data: Array<DataProps>;
description?: string;
height?: number | ((calculatedHeight: number) => number | undefined);
labelListProps?: LabelListProps<ILabelListData>;
maxHeight?: number | string;
Expand All @@ -72,6 +73,7 @@ export interface BarChartProps {
subLabelProps?: LabelListProps<ILabelListData>;
subLabelWidth?: number;
subLabels?: Array<string>;
title?: string;
tooltipBarId?: string;
tooltipProps?: CustomToolTipProps;
UnhoveredTooltipComponent?: React.JSX.Element;
Expand Down Expand Up @@ -113,6 +115,7 @@ export const BarChart: React.FC<BarChartProps> = (
customizeBarFillColor,
customizedAxisTickProps,
data,
description,
height,
labelListProps,
maxHeight,
Expand All @@ -121,6 +124,7 @@ export const BarChart: React.FC<BarChartProps> = (
subLabelProps,
subLabelWidth,
subLabels,
title,
tooltipBarId,
tooltipProps,
variant,
Expand Down Expand Up @@ -212,6 +216,7 @@ export const BarChart: React.FC<BarChartProps> = (
(subLabelWidth || maxSubLabelWidth) + parseInt(String(spacing(1)), 10);
}

const lowerCaseTitle = toLower(title);
return (
<StyledContainer
height={finalHeight}
Expand All @@ -229,9 +234,12 @@ export const BarChart: React.FC<BarChartProps> = (
{...responsiveContainerProps}
>
<ComposedChart
accessibilityLayer
barCategoryGap={DEFAULT_BAR_CATEGORY_GAP}
barGap={DEFAULT_BAR_GAP}
data={data}
desc={description}
id={`${title ? `${kebabCase(lowerCaseTitle)}-` : ''}bar-chart`}
layout="vertical"
margin={{
bottom: parseInt(String(spacing(0.6)), 10),
Expand All @@ -244,6 +252,7 @@ export const BarChart: React.FC<BarChartProps> = (
cursor: chartProps?.onClick ? 'pointer' : undefined,
fontFamily: typography.fontFamily,
}}
title={title}
{...chartProps}
>
<CartesianGrid
Expand Down Expand Up @@ -287,7 +296,7 @@ export const BarChart: React.FC<BarChartProps> = (
isAnimationActive={animate}
key={`${key}-bar`}
onAnimationStart={onAnimationStart}
onMouseLeave={(): void => {
onMouseMove={(): void => {
if (setTooltipBarId) setTooltipBarId(undefined);
setBarIdHovered(undefined);
}}
Expand All @@ -302,6 +311,7 @@ export const BarChart: React.FC<BarChartProps> = (
<LabelList
dataKey={key}
formatter={(v: number): string => numeral(v).format('0,0')}
id={`${key}-bar-label`}
onMouseLeave={(): void => {
if (setTooltipBarId) setTooltipBarId(undefined);
setBarIdHovered(undefined);
Expand All @@ -317,10 +327,12 @@ export const BarChart: React.FC<BarChartProps> = (
fill: palette.grey[700],
fontSize: typography.caption.fontSize,
}}
tabIndex={0}
{...labelListProps}
/>
{barKeys.length > 1 && (
<LabelList
id={`${key}-label`}
position="left"
style={{
fill: palette.grey[700],
Expand All @@ -337,6 +349,7 @@ export const BarChart: React.FC<BarChartProps> = (
})}

<Tooltip
accessibilityLayer
content={
<CustomTooltip barId={tooltipBarId || barIdHovered} data={data} />
}
Expand Down
Loading