Skip to content

Conversation

@dchyun
Copy link
Contributor

@dchyun dchyun commented May 15, 2025

📌 Summary

PREVIEW

If merged, this PR would improve the accessibility of the CodeBlock by implementing the following features

  • New arguments for @ariaLabel, @ariaLabelledBy, and @ariaDescribedBy
  • Attaching the title and description, if present, to the code block element via aria-labelledby and aria-describedby
  • Adding screen-reader only text to indicate line highlighting

It would also update the hds-clipboard modifier to support not copying sr-only text.

🛠️ Detailed description

Accessible names

The @ariaLabel, @ariaLabelledBy, and @ariaDescribedBy arguments have been added to the CodeBlock to align its approach to the CodeEditor.

Also, the setting of these aria attributes has been aligned to the CodeEditor in the following ways.

  • If a title is present, aria-labelledby will be set to the ID of the title element
  • If a description is present, aria-describedby, will be set to the ID of the description element
  • If a custom value is set on the @ariaLabel argument, and a title is present, the title id will not be set in aria-labelledby

The docs (#2896) will be updated to match the CodeEditor and say that @ariaLabel or @ariaLabelledby must be used if a title is not present. Existing implementation of the CodeBlock should be updated to add these arguments, but this will not cause breaking changes for anyone.

Line highlighting for screen readers

Currently, there is no indication to screen reader users when line highlighting is used. Due to the fact that line numbers are also inaccessible to the screen reader (which can not be fixed since line numbers come from Prism), we can not add sr only text such as "Line 2 and 4 highlighed"

Instead the approach taken is to edit the code generated by prism to add sr-only text before and after line highlight starts indicating when highlighting starts, and when it ends. To prevent the sr-only text from being copied to the clipboard with the CopyButton, the hds-clipboard modifier has been updated to ignore sr-only text

The logic for determining where to add the sr only text mirrors the Prism plugin's code for determining highlights.

📸 Screenshots

Aria attributes for title and description

Before
Screenshot 2025-05-15 at 3 43 39 PM

After
Screenshot 2025-05-15 at 3 44 09 PM

Line highlighting screen-reader only copy

Screenshot 2025-05-15 at 3 47 33 PM

🔗 External links

Jira ticket: HDS-3349


👀 Component checklist

💬 Please consider using conventional comments when reviewing this PR.

@vercel
Copy link

vercel bot commented May 15, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
hds-showcase ✅ Ready (Inspect) Visit Preview May 22, 2025 5:50pm
hds-website ✅ Ready (Inspect) Visit Preview May 22, 2025 5:50pm

@dchyun
Copy link
Contributor Author

dchyun commented May 15, 2025

@MelSumner @shleewhite I've been working on the a11y improvements originally raised in HDS-3349.

For the accessible name and description I've aligned the Code Block to the Code Editor and attached the title + description to the code element if they are present + added some aria arguments.

My main open question is related to making line highlighting visible to the SR.

One approach would be to add some sr text saying which lines are highlighted. However, the code block line numbers come from Prism, and are not accessible to the sr. The code is read as one long block of text.

Screenshot 2025-05-15 at 3 56 32 PM

The approach I've taken currently is to add sr-only text stating when a highlight starts, and when it ends in the block of text.

Screenshot 2025-05-15 at 3 47 33 PM

This involves directly editing the code that prism js gives us, but I wanted to see if this would be a good approach to take, or if there are any other ideas we have for announcing line highlights to the screen reader.

Copy link
Contributor

@shleewhite shleewhite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the approach looks good! looking at the showcase though, seems like the "highlight start/end" strings are visible right now.

@MelSumner
Copy link
Contributor

this seems like a great approach!

Copy link
Contributor

@shleewhite shleewhite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

textToCopy = targetElement.value;
} else {
// Hide any screen reader only text from the innerText calculation
const srOnlyTexts = targetElement.querySelectorAll('.sr-only');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a case for this outside of the CodeBlock component?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't right now, but it could be useful in general. I don't think there's a use case for having sr only text and wanting it copied, which is what would happen currently.

@dchyun dchyun merged commit a92d536 into main May 23, 2025
16 checks passed
@dchyun dchyun deleted the dchyun/code-block-a11y-improvements branch May 23, 2025 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants