Date Picker Dialog Example
The example below includes a date input field and a button that opens a date picker that implements the dialog design pattern. The dialog contains a calendar that uses the grid pattern to present buttons that enable the user to choose a day from the calendar. Choosing a date from the calendar closes the dialog and populates the date input field. When the dialog is opened, if the input field is empty, or does not contain a valid date, then the current date is focused in the calendar. Otherwise, the focus is placed on the day in the calendar that matches the value of the date input field.
Similar examples include:
- Alert Dialog Example: A confirmation prompt that demonstrates an alert dialog.
- Modal Dialog Example: An example demonstrating multiple layers of modal dialogs with both small and large amounts of content.
- Date Picker Combobox: An editable date input combobox that opens a dialog containing a calendar grid and buttons for navigating by month and year.
Example
February 2020
Su | Mo | Tu | We | Th | Fr | Sa |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Accessibility Features
- The description of the date format is associated with the text input via
aria-describedby
, making it available to assistive technologies as an accessible description. - After a date is chosen, the accessible name of the "Choose Date" button is changed to "Change Date, DATE_STRING" where DATE_STRING is the selected date. So, when the dialog closes and focus returns to the button, screen reader users hear confirmation of the selected date.
- In the dialog, shortcut keys are assigned to the additional buttons for changing the month and year displayed in the calendar.
- The calendar heading displaying the month and year is marked up as a live region so screen reader users get feedback from the buttons and keyboard commands that change the month and year.
- Keyboard help is displayed at the bottom of the dialog. A live region is used to announce it to screen reader users when focus moves into the calendar grid.
-
To facilitate compact visual design in the calendar, the day names in the column headers are abbreviated to two characters.
However, this makes it more difficult for screen reader users to understand the day names.
So, full day names are provided to assistive technologies in the HTML
abbr
attribute on the column headers, enabling screen readers to announce the full names when users navigate the grid. - High contrast support for focus and hover styling of the controls in the dialog box use the CSS border property:
- When a button or date cell receives focus a border is added.
- When hovering over a button or date cell with a pointing device a border is added.
- By default buttons and date cells do not have a border, padding is used as a placeholder for the added border for focus and hover styling.
Keyboard Support
Choose Date Button
Key | Function |
---|---|
Space, Enter |
|
Date Picker Dialog
Key | Function |
---|---|
ESC | Closes the dialog and returns focus to the "Choose Date" button. |
Tab |
|
Shift + Tab |
|
Date Picker Dialog: Month/Year Buttons
Key | Function |
---|---|
Space, Enter |
Change the month and/or year displayed in the calendar grid. |
Date Picker Dialog: Date Grid
Key | Function |
---|---|
Space, Enter |
|
Up Arrow | Moves focus to the same day of the previous week. |
Down Arrow | Moves focus to the same day of the next week. |
Right Arrow | Moves focus to the next day. |
Left Arrow | Moves focus to the previous day. |
Home | Moves focus to the first day (e.g Sunday) of the current week. |
End | Moves focus to the last day (e.g. Saturday) of the current week. |
Page Up |
|
Shift + Page Up |
|
Page Down |
|
Shift + Page Down |
|
Date Picker Dialog: OK and Cancel Buttons
Key | Function |
---|---|
Space, Enter |
Activates the button:
|
Role, Property, State, and Tabindex Attributes
Textbox
Role | Attribute | Element | Usage |
---|---|---|---|
aria-describedby="IDREF" |
input |
Identifies the element that provides an accessible description for the textbox, enabling assistive technologies to associate the date format description with the input. |
Choose Date Button
Role | Attribute | Element | Usage |
---|---|---|---|
aria-label="String" |
button |
|
Date Picker Dialog
Role | Attribute | Element | Usage |
---|---|---|---|
dialog |
div
|
Identifies the element as a dialog . | |
aria-modal="true" |
div |
Indicates the dialog is modal. | |
aria-label="string" |
div |
Defines the accessible name for the dialog. | |
aria-live="polite" |
h2
|
|
|
aria-live="polite" |
div |
|
Date Picker Dialog: Calendar Navigation Buttons
Role | Attribute | Element | Usage |
---|---|---|---|
aria-label="String" |
button |
Defines the accessible name of the button (e.g. "Next Year"). |
Date Picker Dialog: Date Grid
Role | Attribute | Element | Usage |
---|---|---|---|
grid |
table
|
|
|
aria-labelledby="IDREF" |
table |
Identifies the element that provides the accessible name for the grid . |
|
tabindex="0"
|
td
|
|
|
tabindex="-1"
|
td
|
|
|
aria-selected="true" |
td |
|
Note: Since the names of the days of the week in the column headers are abbreviated to two characters, they may be difficult to understand when announced by a screen reader.
An alternative column header name can be provided to screen readers by applying the abbr
attribute to the th
elements.
So, each th
element includes a abbr
attribute containing the full spelling of the name of the day for that column.
Javascript and CSS Source Code
- CSS: datepicker-dialog.css
- Javascript: datepicker-dialog.js
HTML Source Code