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.
Example
Month Year
Su | Mo | Tu | We | Th | Fr | Sa |
---|---|---|---|---|---|---|
Accessibility Features
-
When a date is chosen, the accessible name of the
Choose Date
button is updated to include the selected date. So, when the dialog closes and focus returns to theChoose Date
button, screen reader users hear confirmation of the selected date in the button name. - When the month or year of the calendar grid changes as users navigate the calendar or activate the buttons for next or previous month or year, a live region enables screen readers to announce the new month and year.
- The calendar grid provides hotkeys for changing the year and month as well as support for normal grid navigation keys.
- When the dialog opens and a date in the calendar grid receives focus, a live region enables screen readers to announce keyboard instructions for navigating the calendar grid. The instructions are also visible at the bottom of the dialog box.
Keyboard Support
Choose Date Button
Key | Function |
---|---|
Space, Enter |
|
Date Picker Dialog
Key | Function |
---|---|
ESC | Closes the dialog and returns focus to the Choose Datebutton. |
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
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-labelledby="IDREF" |
div |
Refers to the heading containing the currently displayed month and year, which defines the accessible name for the dialog. | |
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). |
|
aria-live="polite" |
h2
|
|
Date Picker Dialog: Date Grid
Role | Attribute | Element | Usage |
---|---|---|---|
grid |
table
|
|
|
aria-labelledby= |
table |
Defines the accessible name for the grid using the h2 that shows the month and year of the dates displayed in the grid.
|
|
tabindex="0"
|
button
|
|
|
tabindex="-1"
|
button
|
|
|
aria-selected="true" |
button |
|
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.css
- Javascript:
HTML Source Code