npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aware-components

v0.6.1

Published

React components with built-in accessibility checks for common HTML elements.

Downloads

382

Readme

Aware Components

NPM version npm-typescript GitHub License NPM Downloads

Aware Components is an unstyled React component library focused on accessibility (a11y). It provides enhanced versions of common HTML elements like <a>, <button>, <div>, <h1>-<h6>, <img>, and more. These components are designed to automatically check for accessibility issues and display console warnings during development as needed.

The goal is to improve accessibility by ensuring proper usage of naming conventions, color contrast, ARIA labeling, and the correct nesting of elements.

Features

  • Built-in Accessibility Checks: Each component provides real-time accessibility feedback, including warnings about heading levels, contrast issues, ARIA labels, and more.
  • Common HTML Elements: Components are based on familiar HTML elements, making them easy to integrate into existing projects.
  • Customizable Warnings: Developers receive accessibility warnings during development, enabling quick fixes and improvements.
  • Standalone Accessibility Checks: In addition to components, several standalone accessibility checks are exported for broader use.
  • Expandable: While currently focused on accessibility, the library is designed to expand in the future.

Stability Warning ⚠️

Note: This is an early version of and is still under active development. Features may change, and some components may not be fully stable. Use with caution.

Usage

For optimal accessibility monitoring, wrap your entire application with the A11yProvider:

import { A11yProvider, H1, Button } from 'aware-components';

function App() {
  return (
    <A11yProvider>
      <div>
        <H1>Welcome to Aware Components!</H1>
        <Button onClick={() => alert('Hello')}>Click me</Button>
      </div>
    </A11yProvider>
  );
}

export default App;

The A11yProvider is required for checks that depend on the presence or number of components, such as ensuring proper heading levels or unique landmarks like <main>. Wrapping your app in A11yProvider allows these context-aware checks to function correctly.

Examples

Accessibility issues logged in the console

Example 1: Button with insufficient contrast and size issues

import { Button, Div } from 'aware-components';

function MyButton() {
  const [count, setCount] = React.useState(0);

  return (
    <Div>
      <Button
        style={{
          backgroundColor: '#000', // Insufficient contrast with text color
          color: '#333', // Low contrast for readability
          width: '1em', // Minimum touch target size not met
        }}
        onClick={() => setCount((count) => count + 1)}
        // Missing visible text or aria-label
      />
    </Div>
  );
}

Example 2: Incorrect use of heading elements

import { H1, H3 } from 'aware-components';

function Headings() {
  return (
    <>
      {/* Repeated <H1> elements, breaking the proper heading structure */}
      <H1>Guide to Cooking</H1>
      <H1>Basics of Cooking</H1>

      {/* <H3> used without an <H2>, skipping heading levels */}
      <H3>Choosing the Right Flour</H3>
      <H3>Essential Cooking Tools</H3>
    </>
  );
}

Example 3: Standalone Accessibility checks

In addition to the components, some standalone accessibility checks are exported.

import { isRatioOk, canHaveAriaHidden } from 'aware-components';

console.log(isRatioOk('white', 'black', 'AAA', 18)); // Checks if the contrast ratio between text and background colors meets WCAG level AA or AAA, based on text size.
console.log(canHaveAriaHidden(SomeComponent())); // Checks if the aria-hidden attribute can be applied to the component.

Mix and Match

Most components can be used alongside standard HTML elements. This flexibility allows for easy integration, but for the most comprehensive accessibility insights, consider using aware-components for all core elements in your component tree.

Example: The following mix of <Div> components and HTML <div> elements will still correctly detect and count nested divs.

import { Div } from 'aware-components';

export function Container(props: React.PropsWithChildren) {
  return (
    <Div>
      <Div>
        <div>
          <Div>
            <div>{props.children}</div>
          </Div>
        </div>
      </Div>
    </Div>
  );
}

Example: Using the <Table> Component

The <Table> component from aware-components checks for accessibility issues like missing <headers> and <scope> attributes based on the table's structure. While you can use native HTML elements like <caption> inside <Table>, accessibility checks may not detect these elements if they are wrapped in custom components.

To ensure all elements, including nested ones, are detected and provide detailed warnings, use aware-components like <Caption>, <Th>, and <Td>.

import { Table, Caption } from 'aware-components';

// This caption won't be detected, and a warning will be issued.
export function Description() {
  return <caption>Delivery slots:</caption>;
}

// This caption will be detected, and no warning will be issued.
export function Description() {
  return <Caption>Delivery slots:</Caption>;
}

export function MyTable() {
  return (
    <Table>
      <Description />
      ...
    </Table>
  );
}

By using all aware-components for table elements, you enable comprehensive accessibility checks and ensure detailed feedback on potential issues.

Current Focus

  • Ensuring accessibility by default for the most common HTML components.
  • Catching common a11y issues like:
    • Heading level misuse (e.g., too many h1 elements)
    • Color contrast issues
    • Proper use of ARIA labels
    • Nesting and structure of semantic elements

Future Plans

  • Expand to more specialized components
  • Offer deeper customization for a11y checks

Contributing

Create a branch on your fork, add commits to your fork, and open a pull request from your fork to this repository.

Changelog

To check full changelog click here

License

MIT