-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a block to enhance the sensei progress bars by adding aria labels
See #2482
- Loading branch information
1 parent
34e706f
commit 1152c4b
Showing
5 changed files
with
70 additions
and
2 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
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
4 changes: 4 additions & 0 deletions
4
wp-content/themes/pub/wporg-learn-2024/src/sensei-progress-bar/block.json
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,4 @@ | ||
{ | ||
"name": "wporg-learn/sensei-progress-bar", | ||
"viewScript": "file:./view.js" | ||
} |
36 changes: 36 additions & 0 deletions
36
wp-content/themes/pub/wporg-learn-2024/src/sensei-progress-bar/index.php
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,36 @@ | ||
<?php | ||
/** | ||
* Adds aria labels to the Sensei progress bars. | ||
*/ | ||
|
||
namespace WordPressdotorg\Theme\Learn_2024\Sensei_Progress_Bar; | ||
|
||
defined( 'WPINC' ) || die(); | ||
|
||
/** | ||
* Actions and filters. | ||
*/ | ||
add_action( 'init', __NAMESPACE__ . '\init' ); | ||
|
||
/** | ||
* Add the scripts to update the Sensei progress bar block. | ||
* | ||
* The dependencies are autogenerated in block.json, and can be read with | ||
* `wp_json_file_decode` & `register_block_script_handle. | ||
*/ | ||
function init() { | ||
$metadata_file = dirname( dirname( __DIR__ ) ) . '/build/sensei-progress-bar/block.json'; | ||
$metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); | ||
$metadata['file'] = $metadata_file; | ||
|
||
$script_handle = register_block_script_handle( $metadata, 'viewScript', 0 ); | ||
|
||
// Enqueue the assets only when the Sensei progress bar block is on the page. | ||
add_action( | ||
'render_block_sensei-lms/course-progress', | ||
function( $block_content ) use ( $script_handle ) { | ||
wp_enqueue_script( $script_handle ); | ||
return $block_content; | ||
} | ||
); | ||
} |
27 changes: 27 additions & 0 deletions
27
wp-content/themes/pub/wporg-learn-2024/src/sensei-progress-bar/view.js
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,27 @@ | ||
function init() { | ||
/** | ||
* Enhance the accessibility of the course progress bars. | ||
* | ||
* This function adds an `aria-labelledby` attribute to each progress bar | ||
* to associate it with the label element above | ||
*/ | ||
function enhanceCourseProgressAccessibility() { | ||
document.querySelectorAll( '.sensei-progress-bar__bar' ).forEach( ( progressBar, index ) => { | ||
const wrapper = progressBar.closest( '.sensei-block-wrapper' ); | ||
|
||
if ( wrapper ) { | ||
const labelElement = wrapper.querySelector( '.sensei-progress-bar__label' ); | ||
|
||
if ( labelElement ) { | ||
const id = `sensei-progress-bar__label-${ index }`; | ||
labelElement.id = id; | ||
progressBar.setAttribute( 'aria-labelledby', id ); | ||
} | ||
} | ||
} ); | ||
} | ||
|
||
enhanceCourseProgressAccessibility(); | ||
} | ||
|
||
document.addEventListener( 'DOMContentLoaded', init ); |