CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-material--dom

DOM manipulation utilities for Material Components providing cross-browser compatibility, focus management, and accessibility features.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

announce.mddocs/

Accessibility Announcements

ARIA-live region announcements for screen reader accessibility. Provides a system for announcing dynamic content changes to assistive technology users.

Note: The announce module is not exported through the main package index and must be imported directly.

Import

import { announce, AnnouncerPriority, DATA_MDC_DOM_ANNOUNCE } from '@material/dom/announce';

Capabilities

Announce Function

Announces messages to screen readers via ARIA-live regions.

/**
 * Announces the given message with optional priority, defaulting to "polite"
 * @param message - The message to announce to screen readers
 * @param options - Configuration options for the announcement
 */
function announce(message: string, options?: AnnouncerMessageOptions): void;

Announcement Options

Configuration options for controlling announcement behavior.

interface AnnouncerMessageOptions {
  /**
   * The priority level for the announcement
   * @default AnnouncerPriority.POLITE
   */
  priority?: AnnouncerPriority;
  
  /**
   * The document context for the announcement
   * @default document
   */
  ownerDocument?: Document;
}

Announcement Priorities

Priority levels that control how screen readers handle announcements.

enum AnnouncerPriority {
  /**
   * Polite announcements wait for the user to finish their current activity
   * This is the default and recommended for most use cases
   */
  POLITE = 'polite',
  
  /**
   * Assertive announcements interrupt the user's current activity
   * Use sparingly for urgent messages like errors
   */
  ASSERTIVE = 'assertive'
}

Data Attribute Constant

The data attribute used to identify announcement live regions.

/**
 * Data attribute added to live region elements for identification
 */
const DATA_MDC_DOM_ANNOUNCE: 'data-mdc-dom-announce';

Usage Examples

Basic Announcements

import { announce } from '@material/dom/announce';

// Simple polite announcement (default)
announce('Form saved successfully');

// Equivalent explicit polite announcement
announce('Form saved successfully', { 
  priority: AnnouncerPriority.POLITE 
});

Urgent Announcements

import { announce, AnnouncerPriority } from '@material/dom/announce';

// Assertive announcement for errors
announce('Error: Please correct the highlighted fields', {
  priority: AnnouncerPriority.ASSERTIVE
});

// Network error announcement
announce('Connection lost. Attempting to reconnect...', {
  priority: AnnouncerPriority.ASSERTIVE
});

Document Context

import { announce } from '@material/dom/announce';

// Announce in iframe context
const iframe = document.querySelector('iframe');
const iframeDoc = iframe.contentDocument;

announce('Content loaded in iframe', {
  ownerDocument: iframeDoc
});

// Announce in modal dialog context
const modal = document.querySelector('.modal');
const modalDoc = modal.ownerDocument;

announce('Modal opened', {
  ownerDocument: modalDoc
});

Form Validation

import { announce, AnnouncerPriority } from '@material/dom/announce';

function validateForm(form: HTMLFormElement) {
  const errors = findValidationErrors(form);
  
  if (errors.length > 0) {
    const errorMessage = `${errors.length} validation error${errors.length > 1 ? 's' : ''} found`;
    announce(errorMessage, {
      priority: AnnouncerPriority.ASSERTIVE
    });
  } else {
    announce('Form is valid and ready to submit');
  }
}

Dynamic Content Updates

import { announce } from '@material/dom/announce';

class SearchResults {
  updateResults(results: any[]) {
    this.renderResults(results);
    
    // Announce results to screen readers
    const count = results.length;
    if (count === 0) {
      announce('No results found');
    } else {
      announce(`${count} result${count > 1 ? 's' : ''} found`);
    }
  }
  
  showLoadingState() {
    announce('Searching...');
  }
  
  showError(error: string) {
    announce(`Search error: ${error}`, {
      priority: AnnouncerPriority.ASSERTIVE
    });
  }
}

Progress Updates

import { announce } from '@material/dom/announce';

class FileUpload {
  updateProgress(percent: number) {
    // Update visual progress bar
    this.progressBar.style.width = `${percent}%`;
    
    // Announce progress at intervals
    if (percent % 25 === 0) {
      announce(`Upload ${percent}% complete`);
    }
  }
  
  onComplete() {
    announce('File upload completed successfully');
  }
  
  onError(error: string) {
    announce(`Upload failed: ${error}`, {
      priority: AnnouncerPriority.ASSERTIVE
    });
  }
}

Accessibility Guidelines

When to Use Announcements

Good use cases:

  • Form validation results
  • Content loading completion
  • Search result counts
  • Error messages
  • Status changes
  • Progress updates

Avoid announcements for:

  • Every user interaction
  • Redundant information already in focus
  • Overly frequent updates
  • Purely decorative changes

Priority Guidelines

POLITE (default):

  • Success messages
  • Content updates
  • Search results
  • Progress milestones
  • Non-critical status changes

ASSERTIVE (use sparingly):

  • Error messages
  • Network failures
  • Invalid form submissions
  • Time-sensitive alerts
  • Critical system messages

Message Guidelines

Write clear, concise messages:

// Good
announce('3 items added to cart');

// Too verbose  
announce('The user has successfully added three items to their shopping cart and can now proceed to checkout');

Include relevant context:

// Good
announce('Page 2 of 5 loaded');

// Missing context
announce('Page loaded');

Use consistent language:

// Consistent
announce('Form saved');
announce('Form deleted');

// Inconsistent
announce('Form has been saved successfully');
announce('Deletion complete');

Implementation Details

Live Region Management

The announcer automatically creates and manages ARIA-live regions:

  • Creates separate regions for polite and assertive announcements
  • Regions are positioned off-screen but accessible to screen readers
  • Regions are marked with aria-atomic="true" for complete message reading
  • Regions are reused across multiple announcements for efficiency

Screen Reader Compatibility

The implementation works with major screen readers:

  • NVDA: Requires brief timeout before content update
  • VoiceOver: Handles immediate content updates
  • JAWS: Compatible with both priorities
  • Dragon: Works with voice navigation

Cross-Document Support

Supports announcements in different document contexts:

  • Main document announcements
  • Iframe document announcements
  • Modal dialog document contexts
  • Multiple document management per announcer instance

Performance Considerations

  • Uses singleton pattern to minimize memory usage
  • Lazy creation of live regions
  • Automatic cleanup of disconnected regions
  • Click listener cleanup to prevent memory leaks

docs

announce.md

events.md

focus-trap.md

index.md

keyboard.md

ponyfill.md

tile.json