Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@a11y-tools/aria-roles

a11ytools269MIT1.0.0TypeScript support: included

A utility for fetching valid ARIA roles dynamically, validating roles, and providing type-safe access to ARIA role names.

aria, aria roles, aria utilities, accessibility, a11y, web accessibility, aria attributes, assistive technology, typescript, type-safe

readme

aria-roles

aria-roles is a lightweight utility library that provides a reliable way to fetch valid ARIA roles dynamically. It simplifies accessibility development by preventing hardcoded role values and enabling validation for better UI accessibility compliance.

🚀 Features

  • 📜 Retrieve a complete, up-to-date list of all valid ARIA roles
  • ✅ Validate whether a given role is a recognized ARIA role
  • 🔒 Type-safe access to ARIA role names
  • ⚡ Lightweight, fast, and dependency-free
  • 🔍 Designed for use in accessibility tooling, testing, and frontend frameworks

📦 Installation

npm install @a11y-tools/aria-roles

🔧 Usage

Basic Usage

import { getAllAriaRoles, isValidAriaRole, ariaRoles } from "@a11y-tools/aria-roles";

// Get all valid ARIA roles
console.log(getAllAriaRoles()); // Returns an array of valid ARIA roles

// Validate a role
console.log(isValidAriaRole("button")); // true
console.log(isValidAriaRole("fake-role")); // false

// Type-safe access to role names
element.setAttribute('role', ariaRoles.button);
element.setAttribute('role', ariaRoles.tabpanel);

TypeScript Usage

import { AriaRole, ariaRoles, isValidAriaRole } from "@a11y-tools/aria-roles";

// Type-safe role definition
const myRole: AriaRole = ariaRoles.button;

// Type narrowing with isValidAriaRole
function setRole(element: HTMLElement, role: string) {
  if (isValidAriaRole(role)) {
    // TypeScript knows 'role' is of type AriaRole here
    element.setAttribute('role', role);
  } else {
    console.warn(`Invalid ARIA role: ${role}`);
  }
}

React Example

import React from 'react';
import { ariaRoles } from '@a11y-tools/aria-roles';

function AccessibleButton({ children, onClick }) {
  return (
    <div 
      role={ariaRoles.button} 
      onClick={onClick}
      tabIndex={0}
      onKeyDown={(e) => {
        if (e.key === 'Enter' || e.key === ' ') {
          onClick();
        }
      }}
    >
      {children}
    </div>
  );
}

Vue Example

<template>
  <div 
    :role="ariaRoles.button"
    @click="onClick"
    tabindex="0"
    @keydown="handleKeyDown"
  >
    <slot></slot>
  </div>
</template>

<script>
import { ariaRoles } from '@a11y-tools/aria-roles';

export default {
  setup() {
    const handleKeyDown = (e) => {
      if (e.key === 'Enter' || e.key === ' ') {
        onClick();
      }
    };

    return {
      ariaRoles,
      handleKeyDown
    };
  },
  methods: {
    onClick() {
      // Handle click
    }
  }
}
</script>

🧪 Testing Example

import { render, screen } from '@testing-library/react';
import { ariaRoles, isValidAriaRole } from '@a11y-tools/aria-roles';

// React Testing Library - use ariaRoles for type-safe queries
test('Component uses correct ARIA roles', () => {
  render(<MyComponent />);

  // Query elements by their roles using ariaRoles
  const button = screen.getByRole(ariaRoles.button);
  const navigation = screen.getByRole(ariaRoles.navigation);

  // Verify elements have proper attributes
  expect(button).toHaveAttribute('aria-pressed', 'false');
  expect(navigation).toBeInTheDocument();
});

// Validate all roles in a component
test('All roles are valid', () => {
  const { container } = render(<MyComponent />);

  // Check that all role attributes are valid
  container.querySelectorAll('[role]').forEach(element => {
    const role = element.getAttribute('role');
    expect(isValidAriaRole(role)).toBe(true);
  });
});

🔄 API Reference

getAllAriaRoles()

Returns an array of all valid ARIA roles as strings.

function getAllAriaRoles(): AriaRole[]

isValidAriaRole(role)

Validates if a string is a valid ARIA role. In TypeScript, this function acts as a type guard.

function isValidAriaRole(role: string): role is AriaRole

ariaRoles

An object that provides type-safe access to all ARIA role names. Each property is named after an ARIA role and returns the role name as a string.

const ariaRoles: Record<AriaRole, AriaRole>

AriaRole (TypeScript only)

A TypeScript type that represents all valid ARIA roles.

type AriaRole = 'alert' | 'alertdialog' | 'application' | /* ... */;

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📜 License

This project is licensed under the MIT License.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[1.0.2] - 2024-03-07

Changed

  • Renamed roles export to ariaRoles for better clarity and to avoid naming conflicts
  • Renamed getAriaRoles() function to getAllAriaRoles() to better describe its purpose
  • Added comprehensive JSDoc comments to improve developer experience
  • Updated README with more detailed examples

[1.0.1] - 2024-03-04

Changed

  • Fixed package configuration for proper TypeScript type definitions
  • Updated documentation with more examples

[1.0.0] - 2024-03-04

Added

  • Initial release
  • Type-safe ARIA role definitions
  • getAriaRoles() function to retrieve all valid ARIA roles
  • isValidAriaRole() function to validate role strings
  • roles object for type-safe access to role names