-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobile-accessibility.mdc
More file actions
183 lines (134 loc) · 5 KB
/
mobile-accessibility.mdc
File metadata and controls
183 lines (134 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
---
description: Flag accessibility anti-patterns in React Native and Flutter code. Catches missing accessibility labels, small touch targets, images without alt text, and color-only state indicators.
alwaysApply: false
globs:
- "*.ts"
- "*.tsx"
- "*.dart"
standards-version: 1.7.0
---
# Accessibility Anti-Patterns
When reviewing or writing code in a React Native or Flutter project, flag these accessibility issues:
## React Native / Expo
### 1. Missing accessibilityLabel on interactive elements
Flag `TouchableOpacity`, `Pressable`, `TouchableHighlight`, and `Button` components without `accessibilityLabel`:
```tsx
// BAD: screen reader announces nothing useful
<Pressable onPress={handleDelete}>
<Icon name="trash" />
</Pressable>
// GOOD: screen reader announces "Delete item"
<Pressable onPress={handleDelete} accessibilityLabel="Delete item" accessibilityRole="button">
<Icon name="trash" />
</Pressable>
```
Icon-only buttons are the most common offender. Every interactive element needs a label.
### 2. Missing accessibilityRole
Flag interactive elements without `accessibilityRole`. This tells screen readers what type of element it is:
- `"button"` for tappable actions
- `"link"` for navigation links
- `"checkbox"` / `"switch"` for toggles
- `"header"` for section headings
- `"search"` for search fields
- `"image"` for meaningful images
### 3. Images without accessibility
Flag `Image` and `expo-image` components that display meaningful content without `accessibilityLabel`:
```tsx
// BAD: screen reader skips or says "image"
<Image source={require('./avatar.png')} />
// GOOD: describes the image
<Image
source={require('./avatar.png')}
accessibilityLabel="User profile photo"
accessible={true}
/>
```
Decorative images that do not convey information should set `accessible={false}` to hide from screen readers.
### 4. Small touch targets
Flag interactive elements with explicit dimensions smaller than 44x44 points:
```tsx
// BAD: too small for reliable tapping
<Pressable style={{ width: 24, height: 24 }} onPress={onClose}>
<Icon name="close" size={16} />
</Pressable>
// GOOD: meets minimum touch target
<Pressable
style={{ width: 44, height: 44, alignItems: 'center', justifyContent: 'center' }}
onPress={onClose}
accessibilityLabel="Close"
>
<Icon name="close" size={16} />
</Pressable>
```
Apple HIG requires 44x44 pt minimum. Material Design requires 48x48 dp. When the visual element is smaller, add padding to reach the minimum tap area.
### 5. Color-only state indicators
Flag patterns where state is communicated only through color:
```tsx
// BAD: color blind users cannot distinguish states
<View style={{ backgroundColor: isValid ? 'green' : 'red' }} />
// GOOD: add text or icon alongside color
<View style={{ backgroundColor: isValid ? 'green' : 'red' }}>
<Text>{isValid ? 'Valid' : 'Invalid'}</Text>
</View>
```
Always pair color with text, icons, or patterns to convey meaning.
## Flutter / Dart
### 1. Missing Semantics on interactive elements
Flag `GestureDetector`, `InkWell`, and custom tappable widgets without `Semantics`:
```dart
// BAD: invisible to screen readers
GestureDetector(
onTap: handleDelete,
child: Icon(Icons.delete),
)
// GOOD: labeled for screen readers
Semantics(
button: true,
label: 'Delete item',
child: GestureDetector(
onTap: handleDelete,
child: Icon(Icons.delete),
),
)
```
`ElevatedButton`, `TextButton`, and `IconButton` have built-in semantics. Custom widgets need explicit `Semantics`.
### 2. Images without semanticLabel
Flag `Image.asset` and `Image.network` without `semanticLabel`:
```dart
// BAD: screen reader ignores or says "image"
Image.asset('assets/logo.png')
// GOOD: meaningful description
Image.asset('assets/logo.png', semanticLabel: 'Company logo')
```
For decorative images, use `excludeFromSemantics: true`.
### 3. Small tap targets
Flag widgets with explicit `SizedBox` or `Container` constraints below 48x48 dp for interactive elements:
```dart
// BAD: too small
SizedBox(
width: 24,
height: 24,
child: IconButton(icon: Icon(Icons.close), onPressed: onClose),
)
// GOOD: meets Material minimum (IconButton has 48x48 default)
IconButton(
icon: Icon(Icons.close),
onPressed: onClose,
tooltip: 'Close',
)
```
Material Design minimum is 48x48 dp. `IconButton` defaults to this.
### 4. Missing tooltip on IconButton
Flag `IconButton` without a `tooltip`:
```dart
// BAD: no tooltip means no long-press hint and poor screen reader support
IconButton(icon: Icon(Icons.share), onPressed: onShare)
// GOOD: tooltip serves as semantic label
IconButton(icon: Icon(Icons.share), onPressed: onShare, tooltip: 'Share')
```
## What to Do
When any of these patterns are detected:
- **Warn**: "`{pattern}` detected. Screen reader users cannot interact with this element."
- **Suggest** the correct accessible pattern with a code example.
- **Do not flag** patterns in test files (`*.test.ts`, `*.test.dart`, `*_test.dart`).
- **Do not flag** elements explicitly marked as decorative (`accessible={false}`, `excludeFromSemantics: true`).