Skip to content

Commit

Permalink
feat(Calendar): improve a11y (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
zamkovskaya authored Mar 26, 2024
1 parent 5400b90 commit 082db0f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
21 changes: 20 additions & 1 deletion src/components/CalendarView/hooks/useCalendarProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {ButtonProps} from '@gravity-ui/uikit';
import type {CalendarProps} from '../../Calendar/Calendar';
import {i18n} from '../i18n';

import type {CalendarState, RangeCalendarState} from './types';
import type {CalendarLayout, CalendarState, RangeCalendarState} from './types';

const buttonDisabledClassName = 'yc-button_disabled g-button_disabled';

Expand Down Expand Up @@ -52,6 +52,8 @@ export function useCalendarProps(props: CalendarProps, state: CalendarState | Ra
},
extraProps: {
'aria-disabled': modeDisabled ? 'true' : undefined,
'aria-description': getAriaDescriptionForModeButton(state.mode, state.availableModes),
'aria-live': 'polite',
},
children: title,
};
Expand Down Expand Up @@ -133,3 +135,20 @@ export function useCalendarProps(props: CalendarProps, state: CalendarState | Ra
previousButtonProps,
};
}

function getAriaDescriptionForModeButton(mode: CalendarLayout, availableModes: CalendarLayout[]) {
const nextModeIndex = availableModes.indexOf(mode) + 1;
const isModeLast = nextModeIndex === availableModes.length;
if (isModeLast) {
return undefined;
}

const ariaLabelMap: Record<CalendarLayout, string> = {
days: '',
months: i18n('Switch to months view'),
quarters: i18n('Switch to quarters view'),
years: i18n('Switch to years view'),
};
const nextMode = availableModes[nextModeIndex];
return ariaLabelMap[nextMode];
}
5 changes: 4 additions & 1 deletion src/components/CalendarView/i18n/en.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"Previous": "Previous",
"Next": "Next"
"Next": "Next",
"Switch to months view": "Switch to months view",
"Switch to quarters view": "Switch to quarters view",
"Switch to years view": "Switch to years view"
}
5 changes: 4 additions & 1 deletion src/components/CalendarView/i18n/ru.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"Previous": "Назад",
"Next": "Вперёд"
"Next": "Вперёд",
"Switch to months view": "Переключиться на просмотр по месяцам",
"Switch to quarters view": "Переключиться на просмотр по кварталам",
"Switch to years view": "Переключиться на просмотр по годам"
}

0 comments on commit 082db0f

Please sign in to comment.