Skip to content
← Writing

The button that dropped accessibilityLabel

From Kosher Kav

  • React Native
  • accessibility
  • API design

Symptom

With VoiceOver on, icon-only buttons in Kosher Kav announced as "button" and nothing else. The call sites all passed accessibilityLabel. The labels were right there in the JSX.

Why the UI couldn't show it

A dropped accessibility prop changes nothing visually. The button rendered, pressed, styled and disabled exactly as designed, so every sighted check passed. The only way to see the defect is to not look.

Diagnosis

OButton, the shared button, took a typed set of props and forwarded the ones it knew about to Pressable: onPress, disabled, style, testID. It had been written that way to keep the component's API small and explicit — a reasonable instinct. But an allow-list on a wrapper around a platform primitive means every primitive prop the wrapper's author didn't think of is silently discarded. accessibilityLabel, accessibilityHint, accessibilityState and hitSlop all fell through the same hole.

// Before
export const OButton = ({ onPress, disabled, style, testID, children }: Props) => (
  <Pressable onPress={onPress} disabled={disabled} style={style} testID={testID}>
    {children}
  </Pressable>
);

Fix

Own the props you add, spread the rest. The component's type extends PressableProps, its own additions are picked off, and everything else reaches the primitive. The API stays explicit for what OButton adds and stops pretending to know the full surface of Pressable.

type Props = PressableProps & { variant?: "primary" | "ghost" };

export const OButton = ({ variant = "primary", style, ...rest }: Props) => (
  <Pressable
    accessibilityRole="button"
    style={[styles[variant], style]}
    {...rest}
  />
);

Test

Render OButton with an accessibilityLabel and query it with getByLabelText. Before the fix the query throws; after, it finds the element. The same test file asserts accessibilityRole defaults to "button" so an icon-only child still announces as one. Beyond the test, the rule that came out of it: a wrapper around a platform primitive spreads ...rest, always. The allow-list is for components that compose several primitives and have to choose where each prop goes.