-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add labels to the lesson status icons (#2786)
* Add aria attributes to the lesson navigation SVGs * Add aria attributes to the lesson outline SVGs * Add screen reader text to the custom "in progress" icon * Refactor the course outline icon replacement to use new status classes * Switch to using aria-label on in progress icon to avoid CSS issue * Add doc comments to the script
- Loading branch information
Showing
4 changed files
with
152 additions
and
66 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
58 changes: 35 additions & 23 deletions
58
wp-content/themes/pub/wporg-learn-2024/src/course-outline/index.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 |
---|---|---|
@@ -1,32 +1,44 @@ | ||
/* global wporgCourseOutlineData */ | ||
/* global wporgCourseOutlineL10n */ | ||
|
||
import { Icon, drafts, lockOutline } from '@wordpress/icons'; | ||
import { renderToString } from '@wordpress/element'; | ||
|
||
document.addEventListener( 'DOMContentLoaded', () => { | ||
if ( ! wporgCourseOutlineData ) { | ||
return; | ||
} | ||
/** | ||
* Find all in progress lessons, and replace the status icon with the Gutenberg-style `drafts` icon. | ||
*/ | ||
document.querySelectorAll( '.wp-block-sensei-lms-course-outline-lesson.is-in-progress' ).forEach( ( link ) => { | ||
const statusIcon = link.querySelector( '.wp-block-sensei-lms-course-outline-lesson__status' ); | ||
if ( statusIcon ) { | ||
const iconString = renderToString( | ||
<Icon | ||
icon={ drafts } | ||
style={ { transform: 'scale(1.5)' } } | ||
aria-label={ wporgCourseOutlineL10n.inProgress } | ||
role="img" | ||
/> | ||
); | ||
|
||
wporgCourseOutlineData[ 'in-progress' ]?.forEach( ( title ) => { | ||
const lessonLinks = document.querySelectorAll( '.wp-block-sensei-lms-course-outline-lesson' ); | ||
lessonLinks.forEach( ( link ) => { | ||
const span = link.querySelector( 'span' ); | ||
if ( span && span.textContent.trim() === title ) { | ||
const statusIcon = link.querySelector( '.wp-block-sensei-lms-course-outline-lesson__status' ); | ||
if ( statusIcon ) { | ||
statusIcon.outerHTML = renderToString( <Icon icon={ drafts } transform={ 'scale(1.5)' } /> ); | ||
} | ||
} | ||
} ); | ||
// Remove the `aria-hidden` attribute from the icon, as it has a readable label. | ||
statusIcon.outerHTML = iconString.replace( ' aria-hidden="true"', '' ); | ||
} | ||
} ); | ||
wporgCourseOutlineData.locked?.forEach( ( title ) => { | ||
const lessonLinks = document.querySelectorAll( '.wp-block-sensei-lms-course-outline-lesson' ); | ||
lessonLinks.forEach( ( link ) => { | ||
const span = link.querySelector( 'span' ); | ||
if ( span && span.textContent.trim() === title ) { | ||
span.insertAdjacentHTML( 'afterend', renderToString( <Icon icon={ lockOutline } /> ) ); | ||
} | ||
} ); | ||
|
||
/** | ||
* Find all locked lessons, and inject a `lock` icon after the title. | ||
*/ | ||
document.querySelectorAll( '.wp-block-sensei-lms-course-outline-lesson.is-locked' ).forEach( ( link ) => { | ||
const span = link.querySelector( 'span' ); | ||
if ( span ) { | ||
span.insertAdjacentHTML( | ||
'afterend', | ||
renderToString( | ||
<> | ||
<Icon icon={ lockOutline } /> | ||
<span className="screen-reader-text">{ wporgCourseOutlineL10n.locked }</span> | ||
</> | ||
) | ||
); | ||
} | ||
} ); | ||
} ); |
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