Insights

Making Next.js Sitecore Projects Accessible

Enhancing accessibility in Next.js Sitecore projects

Making Next.js Sitecore Projects Accessible

Accessibility in web development ensures that websites and applications can be used by everyone, including people with disabilities. It encompasses various practices and guidelines that make digital content usable for all individuals. Adhering to accessibility standards not only improves the user experience but also helps in meeting legal requirements such as the Web Content Accessibility Guidelines (WCAG), Americans with Disabilities Act (ADA), and Section 508.

Document Structure

A well-structured HTML document is the foundation of an accessible website. Semantic HTML and proper head tags ensure that assistive technologies can accurately interpret and navigate the content. In Sitecore Next.js projects, much of this structure is managed by the Sitecore Next.js kit.

  • Semantic HTML

    Using semantic HTML tags like <header>, <main>, <footer>, and <nav> enhances both accessibility and SEO. These tags provide meaning to the content structure, allowing screen readers and other assistive technologies to navigate and interpret the page more effectively.

      <header>
        <!-- Site header content -->
      </header>
      <main id="content">
        <!-- Main content -->
      </main>
      <footer>
        <!-- Footer content -->
      </footer>
      <nav>
        <!-- Navigation content -->
      </nav>
    
  • Head Tag

The <head> tag of your HTML document plays a crucial role in accessibility.

- Setting the `lang` attribute correctly helps screen readers and other assistive technologies understand the language of the content.

    ```css
    <html lang="en">
    ```

- **Meta Charset:** Defines the character set for the document.

    ```css
    <meta charset="UTF-8">
    ```

- **Viewport Settings:** Ensures the website is responsive and accessible on different devices.

    ```css
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    ```

- **Title Tag:** Provides a concise description of the page content, which is essential for screen readers.

    ```css
    <title>Accessible Sitecore Next.js Project</title>
    ```

- **Meta Description:** Gives a brief summary of the page content, used by search engines and screen readers.

    ```css
    <meta name="description" content="This is an example of making a Sitecore Next.js project accessible.">
    ```

- **Link Rel Icons:** Ensure appropriate icons for different devices and platforms.

    ```css
    <link rel="icon" href="/favicon.ico">
    ```

These elements collectively improve the accessibility and usability of your website, making it easier for all users to navigate and understand your content.

Skip to Main Content

Providing a "Skip to main content" link allows keyboard users to bypass repetitive navigation links and jump straight to the main content. This link should be focusable and visible when focused.

<a href="#content" class="sr-only focus:not-sr-only">Skip to main content</a>

Use the sr-only class to visually hide the link but make it accessible to screen readers. When focused, it should become visible to users.

It's important to distinguish between links and buttons:

  • Links (<a>):

    • Navigation: Use <a> tags for navigation purposes.
    • External Links: For external links, add aria-label to specify that they open a new page or site. This helps screen reader users understand the link behavior.

        <a href="https://example.com" aria-label="External link to example.com - opens in new tab" target="_blank" rel="noopener noreferrer">Example</a>
      
    • Role Attribute: If an <a> tag functions as a button, add role="button" to indicate its purpose.

        <a href="#" role="button" aria-label="Read more">Read More</a>
      
  • Buttons (<button>): Use for actions.

      <button type="button" aria-label="Close modal">Close</button>
    

Make sure all interactive elements are focusable and usable via keyboard.

Lists

Lists are an essential part of structuring content on web pages. Using appropriate roles and ARIA attributes ensures they are accessible.

  • Ordered (<ol>) and Unordered (<ul>) Lists: Use these tags to create lists with a logical order or no specific order, respectively.
  • Descriptive Labels: Ensure that lists have descriptive labels when necessary. If you use

    or other elements as lists, you need to mention role="list" and role="listitem". Otherwise, you don't need to explicitly set these roles. Only use role="list" and role="listitem" if you have to.

     

      <div role="list" aria-label="List of services">
        <div role="listitem">List item 1</div>
        <div role="listitem">List item 2</div>
        <div role="listitem">List item 3</div>
      </div>
    
      <ul aria-label="List of services">
        <li>Service 1</li>
        <li >Service 2</li>
        <li >Service 3</li>
      </ul>
    

Navigation menus should be accessible via keyboard and screen readers. Ensure the following attributes are used appropriately:

  • Tab Index: Manage focus order with tabindex when needed.
  • ARIA Attributes: Use aria-expanded, aria-haspopup, and aria-controls for dropdown menus.Use aria-hidden for elements that are hidden from screen readers.
  • Keyboard Navigation: Ensure that users can navigate through the menu using the keyboard, using tab, arrow keys, and enter or space to activate links or buttons.

      <nav aria-label="Main navigation">
        <ul>
          <li role="none">
            <a href="/home" role="menuitem" aria-label="Home">Home</a>
          </li>
          <li role="none">
            <button role="menuitem" aria-expanded="false" aria-haspopup="true" aria-controls="submenu">Services</button>
            <ul id="submenu" role="menu" aria-hidden="true">
              <li role="none">
                <a href="/service1" role="menuitem" aria-label="Service 1">Service 1</a>
              </li>
              <li role="none">
                <a href="/service2" role="menuitem" aria-label="Service 2">Service 2</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
    

Images

Images play a crucial role in web content and must be accessible to all users. This includes providing descriptive alternative text and using appropriate components for image rendering in Next.js and Sitecore JSS projects. All images should have descriptive alt attributes to provide alternative text for screen readers.

<img src="example.jpg" alt="Description of the image">

Next.js Image Component

Next.js provides an Image component that offers several benefits, including automatic image optimization and enhanced accessibility features. Always provide a descriptive alt attribute for images.

import Image from 'next/image';

const Example = () => (
  <Image 
    src="/example.jpg" 
    alt="Description of the image" 
    width={500} 
    height={500} 
  />
);

Sitecore JSS Image Handling

In Sitecore JSS, images can be managed and rendered using the Image component from the @sitecore-jss/sitecore-jss-nextjs package. Ensure the alt attribute is provided, typically through the Sitecore content field.

import { Image } from '@sitecore-jss/sitecore-jss-nextjs';

const Example = (props) => (
  <Image 
    field={props.fields.image} 
    alt={props.fields.image.alt || 'Description of the image'} 
  />
);

Forms

Forms must be accessible with proper labels and ARIA attributes to ensure that all users, including those using screen readers, can understand and interact with form elements.

Labels

The most straightforward and widely supported method for labeling form elements is to use a <label> element with the for attribute, which explicitly associates the label with the input element.

<label for="name">Name</label>
<input type="text" id="name" name="name">

ARIA Attributes

When a visible <label> cannot be used, ARIA attributes like aria-label and aria-labelledby can provide accessible names for form controls.

  • aria-label

    The aria-label attribute provides an accessible name directly to an element when there is no visible label. It is read by screen readers to give context to the form control. You use aria-label when you cannot use a visible label, such as when the design does not allow for a visible label, but you still need to provide a name for the element that screen readers can announce.

      <input type="text" aria-label="Name" name="name">
    
  • aria-labelledby The aria-labelledby attribute references another element that contains the label text. This is useful when the label text is part of a more complex structure or located in a different part of the document.

      <label id="name-label">Name</label>
      <input type="text" aria-labelledby="name-label" name="name">
    

Required Fields

Use the aria-required attribute to indicate that a form field is required. This should be used alongside the required attribute to ensure full accessibility support.

<input type="text" aria-required="true" required name="name">

General Best Practices for forms accessibility:

  • Error Messages: Use aria-live regions to announce form validation errors or other dynamic changes to assistive technologies.

      //Dynamic content which updates without a page reload is generally 
      <div aria-live="assertive">
        <span id="error-message">This field is required.</span>
      </div>
    
  • Fieldsets and Legends: Group related form controls using <fieldset> and <legend> to provide additional context.

      <fieldset>
        <legend>Personal Information</legend>
        <label for="name">Name</label>
        <input type="text" id="name" name="name">
      </fieldset>
    
  • Descriptive Help Text: Provide help text or instructions using aria-describedby.

      <label for="email">Email</label>
      <input type="email" id="email" name="email" aria-describedby="email-help">
      <small id="email-help">We'll never share your email with anyone else.</small>
    

Tables

When using tables, ensure they are accessible by providing appropriate headers and summaries.

Table Headers

Use the <th> element to define headers for rows and columns. This helps screen readers understand the structure and content of the table.

<table>
  <thead>
    <tr>
      <th>Header 1</th>
      <th>Header 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data 1</td>
      <td>Data 2</td>
    </tr>
  </tbody>
</table>

Scope Attribute

Use the scope attribute to specify whether a header applies to a row or column. This improves the accessibility of the table for screen readers.

<th scope="col">Column Header</th>
<th scope="row">Row Header</th>

Table Captions

Provide a caption to describe the contents of the table. This helps all users, including those using screen readers, to understand the purpose of the table.

<table>
  <caption>Table Summary</caption>
  <thead>
    <tr>
      <th>Header 1</th>
      <th>Header 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data 1</td>
      <td>Data 2</td>
    </tr>
  </tbody>
</table>

Complex Tables

For complex tables with multiple levels of headers, use id and headers attributes to associate data cells with their corresponding headers.

<table>
  <caption>Complex Table Summary</caption>
  <thead>
    <tr>
      <th id="header1">Header 1</th>
      <th id="header2">Header 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td headers="header1">Data 1</td>
      <td headers="header2">Data 2</td>
    </tr>
  </tbody>
</table>

Additional Sections

Keyboard Navigation

Managing focus is crucial for accessibility:

  • Focus Management: Use tabindex and JavaScript to manage focus.
  • Skip Links: Provide additional skip links for navigation between sections.

Color Contrast

Ensure that text and background colors meet WCAG contrast ratio requirements. Use tools like the WebAIM Contrast Checker to verify.

Dynamic Content

Use aria-live regions to make dynamic content updates accessible.

<div aria-live="polite">Content updated</div>

Headings

Ensure that heading tags (<h1>, <h2>, <h3>, etc.) are used in a logical and hierarchical order. This helps screen readers understand the structure of the content.

  • Hierarchical Order: Start with <h1> for the main title, followed by <h2> for sections, <h3> for subsections, and so on. Avoid skipping heading levels (e.g., do not use <h2> followed directly by <h4>).

      <h1>Main Title</h1>
      <h2>Section Title</h2>
      <h3>Subsection Title</h3>
    

Accessible Widgets

For custom interactive elements (widgets), ensure they are accessible:

  • ARIA Roles and Properties: Use appropriate ARIA roles and properties to describe the behavior of the widget.
  • Keyboard Interactions: Ensure all interactive elements can be operated using a keyboard.

      <div role="dialog" aria-labelledby="dialog-title" aria-describedby="dialog-description">
        <h2 id="dialog-title">Dialog Title</h2>
        <p id="dialog-description">Dialog description goes here.</p>
      </div>
    

Accessible Media

Ensure all media content, such as audio and video, is accessible:

  • Captions and Transcripts: Provide captions for videos and transcripts for audio content.

      <video controls>
        <source src="video.mp4" type="video/mp4">
        <track kind="captions" src="captions.vtt" srclang="en" label="English">
      </video>
    
  • Audio Descriptions: Provide audio descriptions for video content to describe important visual information.

      <audio controls>
        <source src="description.mp3" type="audio/mp3">
      </audio>
    

Last Word on Making Next.js Sitecore Projects Accessible

Accessibility is a fundamental aspect of web development that ensures websites and applications can be used by everyone, including people with disabilities. By implementing accessibility best practices in your Next.js Sitecore projects, you not only enhance the user experience but also comply with important legal standards such as the Web Content Accessibility Guidelines (WCAG), Americans with Disabilities Act (ADA), and Section 508.

In this guide, we've covered essential areas including:

  • Proper document structure with semantic HTML and head tags
  • Implementing skip links for better keyboard navigation
  • Ensuring links and buttons are accessible
  • Creating accessible navigation menus and lists
  • Handling images with descriptive alt text
  • Building accessible forms with appropriate labels and ARIA attributes
  • Utilizing keyboard navigation, color contrast, and dynamic content techniques
  • Structuring headings in a logical order
  • Designing accessible tables

By following these guidelines, you can create inclusive web experiences that cater to all users, including those using assistive technologies. Accessibility is an ongoing effort, and staying informed about best practices and standards is crucial.

References:



Meet Sohrab Saboori

Senior Full-Stack Developer

💪👨‍💻🤼

Sohrab is a Senior Front-End Developer with extensive experience in React, Next.js, JavaScript, and TypeScript. Sohrab is committed to delivering outstanding digital solutions that not only meet but exceed clients' expectations. His expertise in building scalable and efficient web applications, responsive websites, and e-commerce platforms is unparalleled. Sohrab has a keen eye for detail and a passion for creating seamless user experiences. He is a problem-solver at heart and enjoys working with clients to find innovative solutions to their digital needs. When he's not coding, you can find him lifting weights at the gym, pounding the pavement on the run, exploring the great outdoors, or trying new restaurants and cuisines. Sohrab believes in a healthy and balanced lifestyle and finds that these activities help fuel his creativity and problem-solving skills.

Connect with Sohrab