Skip to content
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

Improve code block sections for screen readers #260

Closed
janosdebugs opened this issue Jan 13, 2024 · 11 comments
Closed

Improve code block sections for screen readers #260

janosdebugs opened this issue Jan 13, 2024 · 11 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@janosdebugs
Copy link
Contributor

Currently, the code blocks are not marked up to make them easy to navigate for screen readers. This causes them to be read in line with the rest of the text.

There are several solutions to make them stand out. The easiest is to wrap them in a <figure></figure>. More advanced options could be adding a role attribute to the block. A <figcaption></figcaption> can also be used to enhance the experience alongside the appropriate CSS rules:

<figure aria-roledescription="Code block">
    <!- This needs automation to inject the language: -->
    <figcaption class="sr-only">Language: hcl</figcaption>
    <pre>...</pre>
</figure>
@janosdebugs janosdebugs added enhancement New feature or request help wanted Extra attention is needed accepted Issues accepted for implementation labels Jan 13, 2024
@janosdebugs janosdebugs changed the title Markup code sections for screen readers Improve code block sections for screen readers Jan 13, 2024
@janosdebugs janosdebugs removed accepted Issues accepted for implementation help wanted Extra attention is needed labels Jan 13, 2024
@janosdebugs janosdebugs added good first issue Good for newcomers help wanted Extra attention is needed labels Jan 17, 2024
@SypherSP
Copy link
Contributor

Hi @janosdebugs, i would like to have a go at this issue.

Could you please help me out with the relevant part of the code that is responsible for rendering the .mdx files?

@janosdebugs
Copy link
Contributor Author

Hey @SypherSP sorry I missed this, I've assigned you. Regarding the specific implementation, in order to make the code more accessible, we should do the following (in order, happy to have separate PRs for these):

  1. Make sure that the code block has a tabindex="0" defined so that the screen reader can focus on it with keyboard navigation.
  2. The code could be wrapped in a figure to provide additional context using the figcaption. The screen reader-only functions should have a CSS markup to make it visible to screen readers and Braille users, but not to "normal" browsers.

To test this feature you can:

  1. Use the free NVDA software.
  2. Use the Android Talkback or iOS Voiceover feature.

All 3 of these require some getting used to, please familiarize yourself with them so you can test this properly. Specifically, please look at the browse vs. focus mode when using NVDA, these can be a bit confusing at first.

@SypherSP
Copy link
Contributor

<!- This needs automation to inject the language: -->

Could you explain what you mean by this.

What do you think about implementing a new component like

const FigureWithCaption: React.FC<FigureWithCaptionProps> = ({ children, caption }) => {
  return (
    <figure aria-roledescription="Code block">
    <figcaption className="sr-only" tabIndex={0}>Caption: {caption}</figcaption>
    {children}
</figure>
  );
};

which can be used in any mdx file like this

<FigureWithCaption caption="Sample code">
``hcl
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 1.0.4"
    }
  }
}
``
</FigureWithCaption>

@Yantrio
Copy link
Member

Yantrio commented Feb 27, 2024

I personally would rather have the mdx be agnostic of what is rendering it where possible.

@SypherSP
Copy link
Contributor

Okay, ill look for another approach

@janosdebugs
Copy link
Contributor Author

@Yantrio it may not be possible, we may have to create a subclass(?) of <CodeBlock> to improve this, but I agree that upstreaming the fixes would be preferable.

@SypherSP
Copy link
Contributor

Using Swizzling we can add the figure wrapper easily, but to add a caption from markdown might be a little complicated.

@janosdebugs
Copy link
Contributor Author

Let's try the figure wrapper first so we get a tabindex (if not present already). @SypherSP please test the changes with a screen reader before adding them, the code I posted is from another project and it may not work as well for this website.

@SypherSP
Copy link
Contributor

Yeah, im testing things as im adding them. Will link a pr as soon as i figure out wrapping.

@codiini
Copy link

codiini commented Jun 4, 2024

@janosdebugs I think this can be closed since the corresponding PR has been merged

@janosdebugs
Copy link
Contributor Author

Thanks @codiini !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants