JSPM

@a11ytools/aria-roles

1.0.3
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 19
    • Score
      100M100P100Q63644F
    • License MIT

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

    Package Exports

    • @a11ytools/aria-roles
    • @a11ytools/aria-roles/dist/index.js

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@a11ytools/aria-roles) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    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 @a11ytools/aria-roles

    ๐Ÿ”ง Usage

    Basic Usage

    import { getAllAriaRoles, isValidAriaRole, ariaRoles } from "@a11ytools/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 "@a11ytools/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 '@a11ytools/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 '@a11ytools/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 '@a11ytools/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.