Build inclusive applications with WCAG-compliant components, keyboard navigation, screen reader support, and focus management.
# Accessibility Patterns for Google Antigravity
Creating accessible applications ensures everyone can use your software. This guide covers accessibility patterns optimized for Google Antigravity and React development.
## Accessible Component Library
Build accessible base components:
```typescript
// components/ui/Button.tsx
import { forwardRef, ButtonHTMLAttributes } from "react";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/utils";
const buttonVariants = cva(
"inline-flex items-center justify-center rounded-md font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:bg-primary/90",
destructive: "bg-destructive text-destructive-foreground hover:bg-destructive/90",
outline: "border border-input bg-background hover:bg-accent",
ghost: "hover:bg-accent hover:text-accent-foreground",
},
size: {
default: "h-10 px-4 py-2",
sm: "h-9 px-3",
lg: "h-11 px-8",
icon: "h-10 w-10",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
}
);
interface ButtonProps
extends ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
isLoading?: boolean;
}
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
({ className, variant, size, isLoading, children, disabled, ...props }, ref) => {
return (
<button
className={cn(buttonVariants({ variant, size, className }))}
ref={ref}
disabled={disabled || isLoading}
aria-busy={isLoading}
aria-disabled={disabled || isLoading}
{...props}
>
{isLoading ? (
<>
<span className="sr-only">Loading</span>
<Spinner className="mr-2 h-4 w-4 animate-spin" aria-hidden="true" />
</>
) : null}
{children}
</button>
);
}
);
Button.displayName = "Button";
```
## Focus Management
Implement proper focus management:
```typescript
// hooks/useFocusTrap.ts
import { useEffect, useRef, useCallback } from "react";
const FOCUSABLE_ELEMENTS = [
"a[href]",
"button:not([disabled])",
"input:not([disabled])",
"select:not([disabled])",
"textarea:not([disabled])",
"[tabindex]:not([tabindex="-1"])",
].join(", ");
export function useFocusTrap<T extends HTMLElement>(active: boolean = true) {
const containerRef = useRef<T>(null);
const previousFocusRef = useRef<HTMLElement | null>(null);
const getFocusableElements = useCallback(() => {
if (!containerRef.current) return [];
return Array.from(
containerRef.current.querySelectorAll<HTMLElement>(FOCUSABLE_ELEMENTS)
).filter((el) => el.offsetParent !== null);
}, []);
useEffect(() => {
if (!active) return;
previousFocusRef.current = document.activeElement as HTMLElement;
const focusableElements = getFocusableElements();
if (focusableElements.length > 0) {
focusableElements[0].focus();
}
return () => {
previousFocusRef.current?.focus();
};
}, [active, getFocusableElements]);
useEffect(() => {
if (!active) return;
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key !== "Tab") return;
const focusableElements = getFocusableElements();
if (focusableElements.length === 0) return;
const firstElement = focusableElements[0];
const lastElement = focusableElements[focusableElements.length - 1];
if (event.shiftKey && document.activeElement === firstElement) {
event.preventDefault();
lastElement.focus();
} else if (!event.shiftKey && document.activeElement === lastElement) {
event.preventDefault();
firstElement.focus();
}
};
document.addEventListener("keydown", handleKeyDown);
return () => document.removeEventListener("keydown", handleKeyDown);
}, [active, getFocusableElements]);
return containerRef;
}
// Usage in Modal
export function Modal({ isOpen, onClose, children }: ModalProps) {
const modalRef = useFocusTrap<HTMLDivElement>(isOpen);
if (!isOpen) return null;
return (
<div
role="dialog"
aria-modal="true"
aria-labelledby="modal-title"
ref={modalRef}
className="fixed inset-0 z-50 flex items-center justify-center"
>
<div className="fixed inset-0 bg-black/50" onClick={onClose} aria-hidden="true" />
<div className="relative bg-white rounded-lg p-6 max-w-md w-full">
{children}
</div>
</div>
);
}
```
## Screen Reader Announcements
Implement live region announcements:
```typescript
// components/LiveAnnouncer.tsx
"use client";
import { createContext, useContext, useState, useCallback } from "react";
interface AnnouncerContextType {
announce: (message: string, priority?: "polite" | "assertive") => void;
}
const AnnouncerContext = createContext<AnnouncerContextType | null>(null);
export function LiveAnnouncerProvider({ children }: { children: React.ReactNode }) {
const [politeMessage, setPoliteMessage] = useState("");
const [assertiveMessage, setAssertiveMessage] = useState("");
const announce = useCallback((message: string, priority: "polite" | "assertive" = "polite") => {
if (priority === "assertive") {
setAssertiveMessage("");
setTimeout(() => setAssertiveMessage(message), 100);
} else {
setPoliteMessage("");
setTimeout(() => setPoliteMessage(message), 100);
}
}, []);
return (
<AnnouncerContext.Provider value={{ announce }}>
{children}
<div
role="status"
aria-live="polite"
aria-atomic="true"
className="sr-only"
>
{politeMessage}
</div>
<div
role="alert"
aria-live="assertive"
aria-atomic="true"
className="sr-only"
>
{assertiveMessage}
</div>
</AnnouncerContext.Provider>
);
}
export function useAnnounce() {
const context = useContext(AnnouncerContext);
if (!context) {
throw new Error("useAnnounce must be used within LiveAnnouncerProvider");
}
return context.announce;
}
```
## Keyboard Navigation
Implement roving tabindex for complex widgets:
```typescript
// hooks/useRovingTabIndex.ts
import { useState, useCallback, KeyboardEvent } from "react";
export function useRovingTabIndex(itemCount: number) {
const [focusedIndex, setFocusedIndex] = useState(0);
const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
switch (event.key) {
case "ArrowDown":
case "ArrowRight":
event.preventDefault();
setFocusedIndex((prev) => (prev + 1) % itemCount);
break;
case "ArrowUp":
case "ArrowLeft":
event.preventDefault();
setFocusedIndex((prev) => (prev - 1 + itemCount) % itemCount);
break;
case "Home":
event.preventDefault();
setFocusedIndex(0);
break;
case "End":
event.preventDefault();
setFocusedIndex(itemCount - 1);
break;
}
},
[itemCount]
);
const getTabIndex = useCallback(
(index: number) => (index === focusedIndex ? 0 : -1),
[focusedIndex]
);
return { focusedIndex, handleKeyDown, getTabIndex, setFocusedIndex };
}
```
## Best Practices
When implementing accessibility in Antigravity projects, test with screen readers regularly, use semantic HTML elements, provide skip links for navigation, ensure color contrast ratios meet WCAG standards, add visible focus indicators, include alt text for all images, support reduced motion preferences, and validate with automated tools like axe-core.This Accessibility prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their accessibility implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For Accessibility projects, consider mentioning your framework version, coding style, and any specific libraries you're using.