Skip to content

Commit af9adfc

Browse files
chore: wip
1 parent ae5d727 commit af9adfc

File tree

5 files changed

+504
-126
lines changed

5 files changed

+504
-126
lines changed

packages/stx/src/a11y.ts

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
1+
/**
2+
* Accessibility (A11y) Module
3+
*
4+
* Provides directives and utilities for accessibility compliance:
5+
* - `@a11y('type', 'message')` - Add accessibility hints as HTML comments
6+
* - `@screenReader(...)@endScreenReader` - Screen reader only content
7+
* - `@ariaDescribe('id', 'description')` - Connect elements with descriptions
8+
*
9+
* Also provides automated accessibility checking via `checkA11y()` and
10+
* directory scanning via `scanA11yIssues()`.
11+
*
12+
* ## Accessibility Checks
13+
*
14+
* The automated checker validates:
15+
* - Images have alt attributes
16+
* - Interactive elements have accessible names
17+
* - Form inputs have associated labels
18+
* - Heading hierarchy is maintained
19+
* - Document has lang attribute
20+
*/
121
import type { CustomDirective, StxOptions } from './types'
222
import path from 'node:path'
3-
// Note: Using very-happy-dom for DOM parsing in tests
23+
24+
// =============================================================================
25+
// Types
26+
// =============================================================================
427

528
/**
629
* Accessibility violation found during a11y checks
@@ -20,14 +43,19 @@ export interface A11yViolation {
2043
helpUrl?: string
2144
}
2245

46+
// =============================================================================
47+
// Directive Processing
48+
// =============================================================================
49+
2350
/**
24-
* Process accessibility directives
51+
* Process accessibility directives.
52+
* Handles @a11y hints, @screenReader content, and @ariaDescribe.
2553
*/
2654
export function processA11yDirectives(
2755
template: string,
28-
context: Record<string, any>,
29-
filePath: string,
30-
options: StxOptions,
56+
_context: Record<string, any>,
57+
_filePath: string,
58+
_options: StxOptions,
3159
): string {
3260
let output = template
3361

@@ -73,8 +101,13 @@ export function processA11yDirectives(
73101
return output
74102
}
75103

104+
// =============================================================================
105+
// Utilities
106+
// =============================================================================
107+
76108
/**
77-
* Create the screen reader only style
109+
* Create the screen reader only CSS style.
110+
* Add this to your stylesheet or inject via `<style>` tag.
78111
*/
79112
export function getScreenReaderOnlyStyle(): string {
80113
return `
@@ -92,8 +125,13 @@ export function getScreenReaderOnlyStyle(): string {
92125
`.trim()
93126
}
94127

128+
// =============================================================================
129+
// Accessibility Checker
130+
// =============================================================================
131+
95132
/**
96-
* Automatically check template for accessibility issues
133+
* Automatically check template for accessibility issues.
134+
* Requires a DOM environment (e.g., happy-dom setup).
97135
*/
98136
export async function checkA11y(html: string, filePath: string): Promise<A11yViolation[]> {
99137
const violations: A11yViolation[] = []
@@ -257,8 +295,13 @@ export async function checkA11y(html: string, filePath: string): Promise<A11yVio
257295
return violations
258296
}
259297

298+
// =============================================================================
299+
// Directory Scanner
300+
// =============================================================================
301+
260302
/**
261-
* Scan a directory of stx files for accessibility issues
303+
* Scan a directory of stx files for accessibility issues.
304+
* Returns a map of file paths to their violations.
262305
*/
263306
export async function scanA11yIssues(
264307
directory: string,
@@ -309,12 +352,16 @@ export async function scanA11yIssues(
309352
return results
310353
}
311354

355+
// =============================================================================
356+
// Custom Directives
357+
// =============================================================================
358+
312359
/**
313360
* A11y directive for adding accessibility hints
314361
*/
315362
export const a11yDirective: CustomDirective = {
316363
name: 'a11y',
317-
handler: (content, params, context, filePath) => {
364+
handler: (content, params, _context, _filePath) => {
318365
if (!params.length) {
319366
return content
320367
}

0 commit comments

Comments
 (0)