Date Picker Spin Button Example
The following example uses the Spin Button Design Pattern to implement a date picker. It includes three spin buttons: one for setting the day, a second for month, and a third for year.
Similar examples include:
- Toolbar Example: A toolbar that contains a spin button for setting font size.
- Date Picker Dialog Example: Demonstrates a dialog containing a calendar grid for choosing a date.
Example
Choose a Date
current value is Friday, June 30th, 2019
Accessibility Features
-
The three spin buttons are wrapped in a group to help assistive technology users understand that all three elements together represent a date.
The accessible name for the group includes a hidden
div
that contains a date string that reflects the current values of the three spin buttons. This enables screen reader users to easily perceive the date represented by the three buttons without having to navigate to all three buttons and remember the value of each one; it is the screen reader user equivalent to seeing the date at a glance. - The day spin button uses
aria-valuetext
to properly pronounce the day. e.g. first, second, third ... - The month spin button uses
aria-valuetext
to properly pronounce the month instead of the numeric value. e.g. January, February, ... - On hover, the up and down arrow images enlarge slightly, enlarging the effective target area for increasing or decreasing the spin button value.
- Focusing a spin button enlarges the increase and decrease buttons to make perceiving keyboard focus easier.
-
The increase and decrease buttons are not contained within the
div
with rolespinbutton
so they can be separately focusable by users of touch screen readers. However, they are excluded from the page Tab sequence withtabindex="-1"
because they are redundant with the arrow key support provided to keyboard users.
Keyboard Support
The spin buttons provide the following keyboard support described in the spin button design pattern.
Key | Function |
---|---|
Down Arrow |
|
Up Arrow |
|
Page Down |
|
Page Up |
|
Home | Decreases to minimum value. |
End | Increases to maximum value. |
Role, Property, State, and Tabindex Attributes
Role | Attribute | Element | Usage |
---|---|---|---|
group |
div |
|
|
aria-labelledby="IDREFs" |
div |
|
|
spinbutton |
div |
Identifies the div element as a spinbutton . |
|
aria-label="NAME_STRING" |
div |
Defines the accessible name for each spin button (e.g. day, monthand year). |
|
aria-valuenow="NUMBER" |
div |
|
|
aria-valuetext="DAY_NUMBER_STRING" oraria-valuetext="MONTH_STRING"
|
div |
|
|
aria-valuemin="NUMBER" |
div |
Indicates the minimum allowed value for the spin button. | |
aria-valuemax="NUMBER" |
div |
|
|
aria-hidden="true" |
|
For assistive technology users, hides the text showing the next and previous values that is displayed adjacent to the up and down arrow icons since it would otherwise add superfluous elements to the screen reader reading order that are likely to be more confusing than helpful. | |
aria-label="NAME_STRING" |
button |
Defines the accessible name for each increase and decrease button (Next Day, Previous Day, Next Month, Previous Month, Next year, and Previous Year). |
|
tabindex="-1" |
button |
Removes the decrement and increment buttons from the page Tab sequence while keeping them focusable so they can be accessed with touch-based assistive technologies. |
Javascript and CSS Source Code
- CSS: datepicker-spinbuttons.css
- Javascript:
HTML Source Code