-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve accessibility for DatePicker (#793)
## Background There are some accessibility issues with the DatePicker (and consequently the DateRangePicker): - One cannot tab inside the calendars, which makes it impossible for keyboard users to change months/years using the arrow buttons. - One cannot navigate inside the calendars with screen reader at all (using ctrl+opt+arrow) in, e.g., Chrome. - When closing the calendars, the focus ends up in a totally different place than what is logical. - There is no visual focus when moving focus to today's date. - The language codes are not totally correct, which causes some screen reader text to be read in English while other in Norwegian. Other issues: - It appears a little buggy that one cannot close the calendar using the button, which is possible in [React Aria's example](https://react-spectrum.adobe.com/react-aria/useDatePicker.html). ## Solution - Make it possible to navigate with keyboard in the calendar: First, study React Aria's example to see if there are any props we are missing: - `hidden={isOutsideVisibleRange}` helps navigate in the calendar with tab. Secondly, study Entur's DatePicker, who also use the same library as us: - To be able to tab easily inside the DatePicker, we can use a `FocusLock` around the `Calendar`, since we here really do not want to end up outside of the calendar unexpectedly. - Raise this [issue](adobe/react-spectrum#4922) in the `react-spectrum` repo. They made a [workaround](adobe/react-spectrum#4931) we await release on, while the original [bug](https://bugs.chromium.org/p/chromium/issues/detail?id=1473752) is reported to Chrome. - Make sure focus is reset to the calendar button when closed. This way, the next focusable element will be the next logical element. The way this is achieved, is wrapping the `CalendarTriggerButton` in a `PopoverTrigger`, which handles this for us. When using a PopoverTrigger, the focusable child must have a `forwarded ref`. The button then also needs the date picker `ref` instead of the input. - Add visual focus to today's date in the calendar when it is focused. - Correct the language codes used in React Aria's `useState` so that everything announced by the screen reader is in the same and correct language. - For the bugginess around closing the calendar with the button: This is a little hard nut because we here are using the Popover from Chakra and not from React Aria. If we simply had used the Popover from React Aria, the open-close would handle itself the correct way. We use this Popover because we use the `PopoverAnchor`, `PopoverPopoverArrow`, an so on from Chakra, which must be wrapped in the Chakra Popover. Then, we basically have two states, one for the Popover and another for the DatePicker (via `useDatePicker's state`). The Popover listens to outside clicks, escape clicks and so on, and the Popover can fire `onOpen` and `onClose` callbacks based on this. Here, we update the DatePicker's state to open and closed. If we did not do it this way, the first focusable element within the calendar is not set correctly (the first one should be today's date). The reason why the calendar closes and opens right away when clicking the trigger button while the calendar is open, is then because the Popover notices we press outside the calendar and closes the state, while the trigger button then opens it again since the state just got closed. This is also fixed by using the PopoverTrigger. The other props, `returnFocusOnClose` etc., specified for the Popover is really then not needed since they did not work as intended anyway. - In addition to this: Since we had problems with portals for the ComboBox, add `withPortal` as an optional prop so that using a Portal or not is optional. Will then also solve #731.
- Loading branch information
Showing
9 changed files
with
137 additions
and
101 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 @@ | ||
--- | ||
"@vygruppen/spor-react": patch | ||
--- | ||
|
||
Improve accessibility for DatePicker |
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
52 changes: 29 additions & 23 deletions
52
packages/spor-react/src/datepicker/CalendarTriggerButton.tsx
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
Oops, something went wrong.