'use client'
import { useEffect, useState } from 'react'
// Skip link for keyboard navigation
export function SkipToContent() {
return (
Skip to main content
)
}
// Screen reader only announcements
export function ScreenReaderAnnouncement({
message,
politeness = 'polite',
}: {
message: string
politeness?: 'polite' | 'assertive'
}) {
return (
{message}
)
}
// Focus trap for modals
export function useFocusTrap(isActive: boolean) {
useEffect(() => {
if (!isActive) return
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key !== 'Tab') return
const focusableElements = document.querySelectorAll(
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
)
const firstElement = focusableElements[0] as HTMLElement
const lastElement = focusableElements[focusableElements.length - 1] as HTMLElement
if (e.shiftKey && document.activeElement === firstElement) {
e.preventDefault()
lastElement?.focus()
} else if (!e.shiftKey && document.activeElement === lastElement) {
e.preventDefault()
firstElement?.focus()
}
}
document.addEventListener('keydown', handleKeyDown)
return () => document.removeEventListener('keydown', handleKeyDown)
}, [isActive])
}
// Keyboard shortcut handler
export function useKeyboardShortcut(
key: string,
callback: () => void,
options?: {
ctrl?: boolean
shift?: boolean
alt?: boolean
meta?: boolean
preventDefault?: boolean
}
) {
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
const isMatch =
e.key.toLowerCase() === key.toLowerCase() &&
(!options?.ctrl || e.ctrlKey) &&
(!options?.shift || e.shiftKey) &&
(!options?.alt || e.altKey) &&
(!options?.meta || e.metaKey)
if (isMatch) {
if (options?.preventDefault) {
e.preventDefault()
}
callback()
}
}
document.addEventListener('keydown', handleKeyDown)
return () => document.removeEventListener('keydown', handleKeyDown)
}, [key, callback, options])
}
// Reduce motion hook
export function useReducedMotion() {
const [reducedMotion, setReducedMotion] = useState(false)
useEffect(() => {
const mediaQuery = window.matchMedia('(prefers-reduced-motion: reduce)')
setReducedMotion(mediaQuery.matches)
const handler = (e: MediaQueryListEvent) => setReducedMotion(e.matches)
mediaQuery.addEventListener('change', handler)
return () => mediaQuery.removeEventListener('change', handler)
}, [])
return reducedMotion
}
// Visually hidden text (for screen readers)
export function VisuallyHidden({ children }: { children: React.ReactNode }) {
return {children}
}
// Focus visible ring styles
export const focusRingClasses =
'focus:outline-none focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-offset-2 focus-visible:ring-offset-background'
// Accessible icon button
interface IconButtonProps extends React.ButtonHTMLAttributes {
label: string
icon: React.ReactNode
}
export function IconButton({ label, icon, className = '', ...props }: IconButtonProps) {
return (
)
}
// Live region for dynamic content
export function LiveRegion({
children,
politeness = 'polite',
}: {
children: React.ReactNode
politeness?: 'polite' | 'assertive' | 'off'
}) {
return (
{children}
)
}
// Progress indicator with screen reader support
export function AccessibleProgress({
value,
max = 100,
label,
showValue = true,
}: {
value: number
max?: number
label: string
showValue?: boolean
}) {
const percentage = Math.round((value / max) * 100)
return (
{label}
{showValue && {percentage}%}
)
}