-
Notifications
You must be signed in to change notification settings - Fork 10.7k
[Accessibility] Make the rating stars accessible #53018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
670f81e
7019283
b63536f
e7f06e1
e9e77f7
dedc1ab
8702dad
3a906cc
e70bb44
95c9a3f
101d3b9
ab03a36
a0e3462
0b8c37d
2dfbba6
3f69e44
27c3541
7967f0e
982e5cc
d121e58
e72e0d0
38c6f37
eb70de5
83b3157
7e403c8
204ce0d
17dbd98
34f2b18
dc847ca
47dec94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: major | ||
| Type: enhancement | ||
|
|
||
| Improve accessibility of the rating stars |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,24 +103,41 @@ jQuery( function( $ ) { | |
| .hide() | ||
| .before( | ||
| '<p class="stars">\ | ||
| <span>\ | ||
| <a class="star-1" href="#">1</a>\ | ||
| <a class="star-2" href="#">2</a>\ | ||
| <a class="star-3" href="#">3</a>\ | ||
| <a class="star-4" href="#">4</a>\ | ||
| <a class="star-5" href="#">5</a>\ | ||
| <span role="group" aria-labeledby="comment-form-rating-label">\ | ||
| <a role="radio" tabindex="0" aria-checked="false" class="star-1" href="#">' + | ||
| wc_single_product_params.i18n_rating_options[0] + | ||
| '</a>\ | ||
| <a role="radio" tabindex="-1" aria-checked="false" class="star-2" href="#">' + | ||
| wc_single_product_params.i18n_rating_options[1] + | ||
| '</a>\ | ||
| <a role="radio" tabindex="-1" aria-checked="false" class="star-3" href="#">' + | ||
| wc_single_product_params.i18n_rating_options[2] + | ||
| '</a>\ | ||
| <a role="radio" tabindex="-1" aria-checked="false" class="star-4" href="#">' + | ||
| wc_single_product_params.i18n_rating_options[3] + | ||
| '</a>\ | ||
| <a role="radio" tabindex="-1" aria-checked="false" class="star-5" href="#">' + | ||
| wc_single_product_params.i18n_rating_options[4] + | ||
| '</a>\ | ||
| </span>\ | ||
| </p>' | ||
| ); | ||
| } ) | ||
| .on( 'click', '#respond p.stars a', function() { | ||
| var $star = $( this ), | ||
| starPos = $star.closest( 'p.stars' ).find( 'a' ).index( $star ) + 1, | ||
| $rating = $( this ).closest( '#respond' ).find( '#rating' ), | ||
| $container = $( this ).closest( '.stars' ); | ||
|
|
||
| $rating.val( $star.text() ); | ||
| $star.siblings( 'a' ).removeClass( 'active' ); | ||
| $star.addClass( 'active' ); | ||
| $rating.val( starPos ); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set the rating value by getting the star position in the DOM instead of relying on the text value. |
||
| $star.siblings( 'a' ) | ||
| .removeClass( 'active' ) | ||
| .attr( 'aria-checked', 'false' ) | ||
| .attr( 'tabindex', '-1' ); | ||
| $star | ||
| .addClass( 'active' ) | ||
| .attr( 'aria-checked', 'true' ) | ||
| .attr( 'tabindex', '0' ); | ||
|
Comment on lines
+133
to
+140
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the rating group is focused, the focus ring appears only on the selected option, just like in a normal radio group. |
||
| $container.addClass( 'selected' ); | ||
|
|
||
| return false; | ||
|
|
@@ -134,6 +151,26 @@ jQuery( function( $ ) { | |
|
|
||
| return false; | ||
| } | ||
| } ) | ||
| .on( 'keydown', '#respond p.stars a', function( e ) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add keyboard navigation with the arrow keys. |
||
| var direction = e.key; | ||
| var next = [ 'ArrowRight', 'ArrowDown' ]; | ||
| var prev = [ 'ArrowLeft', 'ArrowUp' ]; | ||
| var allDirections = next.concat( prev ); | ||
|
|
||
| if ( ! allDirections.includes( direction ) ) { | ||
| return; | ||
| } | ||
|
|
||
| e.preventDefault(); | ||
|
|
||
| if ( next.includes( direction ) ) { | ||
| $( this ).next().focus().click(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $( this ).prev().focus().click(); | ||
| } ); | ||
|
|
||
| // Init Tabs and Star Ratings | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -510,6 +510,13 @@ private static function get_script_data( $handle ) { | |
| case 'wc-single-product': | ||
| $params = array( | ||
| 'i18n_required_rating_text' => esc_attr__( 'Please select a rating', 'woocommerce' ), | ||
| 'i18n_rating_options' => array( | ||
| esc_attr__( '1 of 5 stars', 'woocommerce' ), | ||
| esc_attr__( '2 of 5 stars', 'woocommerce' ), | ||
| esc_attr__( '3 of 5 stars', 'woocommerce' ), | ||
| esc_attr__( '4 of 5 stars', 'woocommerce' ), | ||
| esc_attr__( '5 of 5 stars', 'woocommerce' ), | ||
| ), | ||
|
Comment on lines
+513
to
+519
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve how stars are announced by screen readers. |
||
| 'i18n_product_gallery_trigger_text' => esc_attr__( 'View full-screen image gallery', 'woocommerce' ), | ||
| 'review_rating_required' => wc_review_ratings_required() ? 'yes' : 'no', | ||
| 'flexslider' => apply_filters( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/ ""is an alternative text for screen readers so they don't try to announce the "\e021" content.Reference: