Accessibility: screen readerอ่านว่าอะไร
บางคนใช้แอปด้วยการฟังแทนการดู ตาม VoiceOver หรือ TalkBack ผ่านหน้าสินค้าหน้าเดียว ทีละการปัด ฟังมันเปลี่ยนจาก "ปุ่ม, ปุ่ม" เป็นหน้าที่ใครก็ใช้ได้
สิ่งที่จะได้
- อธิบายได้ว่าทำไมปุ่มที่มีแต่ไอคอน VoiceOver กับ TalkBack อ่านออกมาได้แค่ว่าปุ่ม
- ใส่ label, role และ state ให้ทุก element และอะไรที่บอกด้วยสีอย่างเดียวต้องมี state ด้วย
- ทำปุ่ม − 1 + เป็น element เดียวแบบ adjustable และรวมชื่อกับราคาให้อ่านรวดเดียว
- ขยายพื้นที่กดไอคอน 24 pt เป็น 44 pt ด้วย hitSlop และให้ปุ่มโตตามตัวหนังสือ 200%
สรุป 1 นาที
กดข้อไหน ภาพข้างล่างจะเล่นขั้นนั้นให้ดู
เริ่มจากหน้าที่ไม่มีอะไรเลย ปัดขวาไปเรื่อย ๆ แล้วค่อยเปิดทีละอย่าง ดูว่าคำที่อ่านเปลี่ยนไปยังไง
ปุ่มหัวใจที่มีแต่ไอคอน ไม่มีข้อความ screen reader จะอ่านว่าอะไร
ในภาพคือหน้าสินค้าบนมือถืออย่างที่คนใช้เห็น กรอบคือจุดที่ screen reader อยู่ ตรงที่ VoiceOver หรือ TalkBack วาดไว้ ที่ฐานของมือถือคือลำดับการอ่านของหน้าจอเป็นถนน บ้านหนึ่งหลังต่อหนึ่งจุดที่ปัดไปถึง และ screen reader เดินไปตามถนนนั้น ถัดขึ้นไปคืออาคารของระบบ ซึ่งเก็บขนาดตัวหนังสือไว้ในการตั้งค่า ในขั้นที่แตะ กล่องข้อความขยายพื้นที่กดของหัวใจให้เห็น คำที่อ่านเป็นแบบจำลองที่เรียงแบบเดียว (label, state, role) VoiceOver กับ TalkBack ใช้คำและลำดับต่างกัน และต่างกันไปตามเวอร์ชัน
เทียบทุกแบบ: Accessibility บนมือถือ
หน้าสินค้าเดียวกัน screen reader ปัดผ่านพร้อมกันทุกช่อง ดูว่าแต่ละแบบต้องปัดกี่ที และกี่จุดที่บอกได้ว่ามันทำอะไร
ใช้เมื่อ ไม่มี: คนที่ใช้ screen reader ใช้หน้านี้ไม่ได้
ไม่ควรใช้เมื่อ ปุ่มไอคอนที่ไม่มี label ทุกที่
คำที่อ่านเป็นแบบจำลอง เรียงเป็น label, state, role คำจริงและลำดับต่างกันระหว่าง VoiceOver กับ TalkBack และแต่ละเวอร์ชัน · กดช่องไหนก็ได้ ภาพใหญ่ด้านบนจะเปลี่ยนเป็นแบบนั้น พร้อมบอกว่าเหมาะกับงานแบบไหน
prop ที่ screen reader อ่าน
| prop | บอกอะไร | ตัวอย่าง |
|---|---|---|
| accessibilityLabel | มันคืออะไร | Favourite |
| accessibilityRole | เป็นของประเภทไหน | button, header, adjustable, image |
| accessibilityState | สถานะตอนนี้ | selected, disabled, checked, expanded |
| accessibilityValue + accessibilityActions | ค่าที่เปลี่ยนได้ | จำนวน 2, increment / decrement |
| accessible | รวมลูกข้างในเป็นจุดเดียว | ชื่อกับราคา |
| hitSlop | ไม่ได้อ่าน แค่ขยายพื้นที่กด | ไอคอน 24 pt ให้กดได้ 44 pt |
ลองกับของจริง
เปิด VoiceOver (iOS) หรือ TalkBack (Android) ในมือถือ แล้วลองใช้หน้าของตัวเองแบบหลับตาสักนาที Accessibility Inspector ใน Xcode กับแอป Accessibility Scanner ของ Google ช่วยหา label ที่ขาดกับพื้นที่กดที่เล็กไป แล้วตั้งตัวหนังสือในเครื่องให้ใหญ่สุด ดูว่ามีคำไหนโดนตัด
ในโค้ดจริง
// What a screen reader needs from a product card: a label for every icon, roles and states it can say, one adjustable
// quantity instead of − 1 +, the name and price read together, a 44 pt touch area, and text that may grow.
import { useState } from "react";
import { Image, Pressable, StyleSheet, Text, View } from "react-native";
const SIZES = [{ id: "S", soldOut: false }, { id: "M", soldOut: true }, { id: "L", soldOut: false }];
export function ProductCard({ onAdd }: { onAdd: (qty: number, size: string) => void }) {
const [fav, setFav] = useState(false);
const [qty, setQty] = useState(1);
const [size, setSize] = useState("S");
return (
<View style={styles.card}>
{/* A meaningful image gets a label; a decorative one would get accessible={false}. */}
<Image accessible accessibilityLabel="Blue trail running shoe" accessibilityRole="image" source={{ uri: "https://example.com/shoe.jpg" }} style={styles.photo} />
<View style={styles.row}>
{/* accessible on the container: name and price are one stop, read together. */}
<View accessible style={styles.grow}>
<Text accessibilityRole="header" style={styles.title}>Trail runner</Text>
<Text>฿2,490</Text>
</View>
{/* An icon-only button: the label says what it does, the role says it's a button, the state says it's on. */}
<Pressable
accessibilityLabel="Favourite"
accessibilityRole="button"
accessibilityState={{ selected: fav }}
hitSlop={10} // the 24 pt icon becomes a 44 pt target, nothing moves
onPress={() => setFav((f) => !f)}
>
<Text style={styles.icon}>{fav ? "♥" : "♡"}</Text>
</Pressable>
</View>
<View style={styles.row}>
{SIZES.map((s) => (
<Pressable
key={s.id}
accessibilityLabel={s.soldOut ? `Size ${s.id}, sold out` : `Size ${s.id}`}
accessibilityRole="button"
// Not colour alone: disabled is said ("dimmed" on VoiceOver), and the label says why.
accessibilityState={{ disabled: s.soldOut, selected: size === s.id }}
disabled={s.soldOut}
onPress={() => setSize(s.id)}
style={[styles.chip, s.soldOut && styles.soldOut]}
>
<Text>{s.id}</Text>
</Pressable>
))}
</View>
{/* One adjustable element: swipe up or down to change it, the new value read out. */}
<View
accessible
accessibilityLabel="Quantity"
accessibilityRole="adjustable"
accessibilityValue={{ min: 1, max: 10, now: qty }}
accessibilityActions={[{ name: "increment" }, { name: "decrement" }]}
onAccessibilityAction={(e) => {
if (e.nativeEvent.actionName === "increment") setQty((q) => Math.min(10, q + 1));
if (e.nativeEvent.actionName === "decrement") setQty((q) => Math.max(1, q - 1));
}}
style={styles.row}
>
<Pressable onPress={() => setQty((q) => Math.max(1, q - 1))} style={styles.step}><Text>−</Text></Pressable>
<Text>{qty}</Text>
<Pressable onPress={() => setQty((q) => Math.min(10, q + 1))} style={styles.step}><Text>+</Text></Pressable>
</View>
{/* No fixed height: at 200 % text the button grows instead of cutting its label. */}
<Pressable accessibilityRole="button" onPress={() => onAdd(qty, size)} style={styles.add}>
<Text maxFontSizeMultiplier={2.5} style={styles.addText}>Add to cart</Text>
</Pressable>
</View>
);
}
const styles = StyleSheet.create({
card: { gap: 12, padding: 16 },
photo: { width: "100%", aspectRatio: 1.6, borderRadius: 12 },
row: { flexDirection: "row", alignItems: "center", gap: 8 },
grow: { flex: 1 },
title: { fontSize: 20, fontWeight: "600" },
icon: { fontSize: 24 },
chip: { minWidth: 44, minHeight: 44, alignItems: "center", justifyContent: "center", borderRadius: 8, borderWidth: 1 },
soldOut: { opacity: 0.4 },
step: { minWidth: 44, minHeight: 44, alignItems: "center", justifyContent: "center" },
add: { paddingVertical: 14, borderRadius: 12, alignItems: "center", backgroundColor: "#1d5fae" },
addText: { color: "white", fontWeight: "600" },
});
tsc เทียบกับ type ที่แต่ละ library เผยแพร่เอง (react-native และ library ที่ไฟล์นี้ import) ตรวจ type อย่างเดียว ไม่ได้รันบนมือถือผลที่พิมพ์ออกมา: tsc 6.0.3 (strict): no errors against react-native 0.88.1 (type-checked only)การ์ดที่บทนี้อ่าน: label ให้ไอคอนกับรูป role กับ state (selected, disabled) จำนวนที่ปรับได้พร้อม action increment / decrement รวมชื่อกับราคาเป็นก้อน hitSlop ให้ถึง 44 pt และปุ่มที่สูงตามตัวหนังสือ ตรวจ type ด้วย tsc กับ type ที่ react-native 0.88 เผยแพร่เอง ไม่ได้รันบนมือถือหรือ screen reader จริง
คำถามที่เจอบ่อย
screen reader บนมือถือทำงานยังไง
VoiceOver บน iOS กับ TalkBack บน Android ปัดขวาเพื่อไป element ถัดไป ปัดซ้ายย้อนกลับ แตะสองทีเพื่อกด มันอ่านสิ่งที่แอปบอกผ่าน label, role, state และ value ของแต่ละ element
accessibilityLabel กับ accessibilityHint ต่างกันยังไง
label คือชื่อของมัน สั้น ๆ เช่น Favourite ถูกอ่านทุกครั้ง hint คือคำอธิบายเพิ่มว่ากดแล้วเกิดอะไร อ่านทีหลังและปิดได้ในเครื่อง ของสำคัญต้องอยู่ใน label
ทำไมใช้สีอย่างเดียวไม่พอ
คนที่มองไม่เห็นไม่รู้ว่าสีแดง และคนตาบอดสีบางคนก็แยกไม่ออก ไซซ์ที่หมดต้องมี accessibilityState disabled และ label ที่บอกว่าหมด บนจอก็ควรมีคำหรือไอคอนด้วย
ปุ่ม − 1 + ควรทำให้ screen reader ยังไง
รวมเป็น element เดียวที่ role adjustable มี accessibilityValue กับ actions increment และ decrement คนปัดขึ้นลงเพื่อเปลี่ยนค่า ได้ยินค่าใหม่ทันที ไม่ต้องปัดผ่านสามจุด
prop accessible ทำอะไร
ทำให้ View กับลูกข้างในเป็น element เดียว อ่านรวดเดียว เช่นชื่อกับราคา ระวังอย่ารวมกล่องที่ข้างในมีปุ่มให้กดแยก และระวังสะกดผิดเป็น accessibile ซึ่งไม่มีผล
ปุ่มต้องใหญ่แค่ไหน
Apple แนะนำพื้นที่กดอย่างน้อย 44 × 44 pt Material 48 × 48 dp ไอคอนเล็กกว่านั้นใส่ hitSlop เช่น 10 ทุกด้านให้ไอคอน 24 pt กดได้เต็ม 44 pt
ต้องรองรับตัวหนังสือใหญ่ยังไง
Text ของ React Native ใหญ่ตามที่ตั้งในเครื่องอยู่แล้ว สิ่งที่ต้องทำคือไม่ล็อกความสูงของปุ่มหรือแถว ให้ขยายตามเนื้อหา และลองหน้าจอที่ 200% ดูว่าไม่มีคำถูกตัด
บทต่อไป
Test แอป React Nativeระดับ: กลาง
component test หาปุ่มจาก role กับคำที่คนเห็น คือ label กับ role ชุดเดียวกับที่เพิ่งใส่ไปในบทนี้