Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessibility - Pressable does not render with aria-checked #2403

Closed
1 task done
penx opened this issue Oct 4, 2022 · 4 comments
Closed
1 task done

Accessibility - Pressable does not render with aria-checked #2403

penx opened this issue Oct 4, 2022 · 4 comments
Labels

Comments

@penx
Copy link

penx commented Oct 4, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Describe the issue

The following code

    <Pressable
      accessibilityLabel="label"
      accessibilityRole="radio"
      accessibilityState={{ checked: true }}
    >
      Example
    </Pressable>

renders as

<div aria-label="label" role="radio" tabindex="0">Example</div>

Which includes the label and role attributes but does not include aria-checked

Expected behavior

Should render as

<div aria-label="label" role="radio" tabindex="0" aria-checked="true">Example</div>

Steps to reproduce

react-native-web@0.18.9

As per code code sandbox test case.

Test case

https://codesandbox.io/s/react-native-web-checked-1ej4me

Additional comments

No response

@penx penx added the bug label Oct 4, 2022
@penx
Copy link
Author

penx commented Oct 4, 2022

I see support for this was removed in 174ebb3 with the comment" Remove deprecated accessibility APIs", but I don't see any reference to accessibilityState being deprecated at https://reactnative.dev/docs/accessibility

@penx
Copy link
Author

penx commented Oct 4, 2022

I've also seen #2372 and have left a comment there. As react-native doesn't currently support the accessibilityChecked prop, I don't understand how to use get aria-checked="true" in a way that builds for both native and web.

@necolas
Copy link
Owner

necolas commented Oct 4, 2022

Use accessibilityChecked. See https://necolas.github.io/react-native-web/docs/accessibility/ and https://github.com/necolas/react-native-web/releases/tag/0.18.0

As react-native doesn't currently support the accessibilityChecked prop, I don't understand how to use get aria-checked="true" in a way that builds for both native and web.

Use Platform.select until RN releases support for aria-checked.

@necolas necolas closed this as completed Oct 4, 2022
@penx
Copy link
Author

penx commented Oct 5, 2022

Thanks!

I still need to figure out typing, but I think I can create a utility like this:

const getAccessibilityStateProps = (accessibilityState: {
  disabled?: boolean;
  selected?: boolean;
  checked?: boolean | "mixed";
  busy?: boolean;
  expanded?: boolean;
}) =>
  Platform.select({
    web: {
      accessibilityDisabled: accessibilityState.disabled,
      accessibilitySelected: accessibilityState.selected,
      accessibilityChecked: accessibilityState.checked,
      accessibilityBusy: accessibilityState.busy,
      accessibilityExpanded: accessibilityState.expanded
    },
    default: { accessibilityState }
  });

and have wrapped this in a little library called react-native-accessibility-props that can be used like this

import { Pressable } from "react-native";
import { getAccessibilityStateProps } from "react-native-accessibility-props";

export default function App() {
  return (
    <Pressable
      accessibilityRole="radio"
      accessibilityLabel="label"
      {...getAccessibilityStateProps({ checked: true })}
    >
      Example
    </Pressable>
  );
}

Seems to work, other than the types, which I'll think about and hopefully publish an update.

https://codesandbox.io/s/react-native-accessibility-props-0vpn1r

I'll probably add another utility for all other accessibility& props too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants