Skip to content

Commit

Permalink
Add react-native missing package docs (#225)
Browse files Browse the repository at this point in the history
* Add SwitchListItem doc

* move hook docs

* Add SwitchWrapper doc

* update links

* update extension

* Add useExpandable hook docs

* Add ExpandablePressable doc to packages

* use mdx for files with jsx
  • Loading branch information
JDMathew authored Aug 6, 2024
1 parent b4f3942 commit 54b8151
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 13 deletions.
106 changes: 106 additions & 0 deletions packages/react-native/docs/ExpandablePressable.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { DevOnly, Required } from '@site/src/components';

# ExpandablePressable

ExpandablePressable is the [Pressable](./Pressable.md) component that automatically handles the [expanded](https://reactnative.dev/docs/accessibility#accessibilitystate) `accessibilityState`. Under the hood it uses [useExpandable](./hooks/useExpandable.md) to achieve this.

## Usage

```tsx
import { ExpandablePressable } from 'react-native-ama';

<ExpandablePressable
accessibilityLabel="I'm ExpandablePressable!"
expanded={isExpanded}
onPress={onPress}>
<Text>I'm pressable</Text>
</ExpandablePressable>;
```

#### Example

```tsx
import { ExpandablePressable, Text } from '@react-native-ama/react-native';

const Example = () => {
const [isExpanded, setIsExpanded] = React.useState(false);

return (
<>
<ExpandablePressable
accessibilityLabel={isExpanded ? 'Less' : 'More'}
expanded={isExpanded}
onPress={() => setIsExpanded(expanded => !expanded)}>
{isExpanded ? <MiniumIcon /> : <PlusIcon />}
</ExpandablePressable>
{isExpanded ? <>{/* content goes here */}</> : null}
</>
);
};
```

# Accessibility

The hook automatically:

- Sets `accessibilityRole` to **button**
- Handles the `accessibilityState` for **expanded**

Compared to the default React Native component, this custom component:

- Forces the use of `accessibilityLabel` <DevOnly />
- Performs a [Minimum Size](../../../website/guidelines/minimum-size.md) check <DevOnly />
- Performs a [contrast checker](../../../website/guidelines/contrast.md) between its background color and its children color <DevOnly />

### accessibilityRole

The `accessibilityRole` property is used by the screen reader to announce the kind of element focused on. If the property is omitted, the user might have little to no clue what could happen if the element is triggered.

[Check here for more info](../../../website/guidelines/accessibility-role.md)

### accessibilityLabel

The `accessibilityLabel` property is the first thing announced by the screen reader when the elements gain the focus; then, it announces its role. If the property is omitted, the user might have little to no clue what could happen if the element is triggered.

[Check here for more info](../../../website/guidelines/accessibility-label.md)

### Contrast checker

The component performs a [contrast check](../../../website/guidelines/contrast.md) between its background colour and the children's foreground when in dev mode.

:::info
AMA does also perform a contrast check on disabled button, as a [poor contrast can make them hard to read](https://axesslab.com/disabled-buttons-suck/#they-are-hard-to-see).
:::

### Minimum size

The component uses the [onLayout](https://reactnative.dev/docs/layoutevent) prop to perform the [minium size check](../../../website/guidelines/minimum-size.md).

## Props

### <Required /> `accessibilityLabel`

The `accessibilityLabel` for the expandable button, this is announced by the screen reader when the element gains focus, then it announces its role. The accessibilityLabel is a required properties as if it is omitted, the user will have no context for the purpose of the button.

| Type | Default |
| ------ | --------- |
| string | undefined |

### <Required /> `expanded`

Indicates whether an expandable element is currently expanded or collapsed.

| Type | Default |
| ------- | --------- |
| boolean | undefined |

## Related guidelines

- [Accessibility Label](../../../website/guidelines/accessibility-label.md)
- [Accessibility Role](../../../website/guidelines/accessibility-role.md)
- [Contrast](../../../website/guidelines/contrast.md)
- [Minimum Size](../../../website/guidelines/minimum-size.md)

## External resources

- [Disabled buttons suck](https://axesslab.com/disabled-buttons-suck/)
93 changes: 93 additions & 0 deletions packages/react-native/docs/SwitchListItem.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { Required } from '@site/src/components';

# SwitchListItem

It is a utility component that provides a list item with a customisable label and switch control [focused on accessibility](#accessibility).

## Usage

```jsx
<SwitchListItem
label={
<Text style={styles.switchText}>I'm a switch</Text>
}
style={styles.switchListItem}
value={isSwitchOn}
onValueChange={toggleSwitch}
>
{...}
</SwitchListItem>
```
## Accessibility
The component:
- is announced as a "switch"
- handles the `accessibilityState` **checked**
- user the label text as `accessibilityLabel`
## Props
### <Required /> `labelComponent`
The custom label to use for the component.
| Type | Default |
| ----------- | --------- |
| JSX.Element | undefined |
If no accessibilityLabel is provided, the component children are used to generate the one.
### `labelPosition`
The position where to render the `label`, left or right of the switch.
| Type | Default |
| ----------------- | ------- |
| 'left' \| 'right' | 'left' |
### <Required /> `value`
The switch state
| Type | Default |
| ------- | --------- |
| boolean | undefined |
### <Required /> `onValueChange`
The callback to call when the list item is toggled
| Type | Default |
| ---------- | --------- |
| () => void | undefined |
## Customisation
By default, the component uses the [React Native switch](https://reactnative.dev/docs/switch), but this behavior can be overridden by passing a child component:
```jsx
<SwitchListItem
label={<Text style={styles.switchText}>I'm a switch</Text>}
style={styles.switchListItem}
value={isSwitchOn}
onValueChange={toggleSwitch}>
<CustomSwitch value={isSwitchOn} onValueChange={toggleSwitch} />
</SwitchListItem>
```
In this case, the custom `CustomSwitch` component is used instead of the React Native one.
:::note
The `SwitchListItem` component, to avoid the switch being individually focusable on Android, the component automatically sets the following properties for all the children:
- `accessibilityElementsHidden={true}`
- `importantForAccessibility="no"`
:::
## Related guidelines
- [Forms](../../../website/guidelines/forms.md)
32 changes: 32 additions & 0 deletions packages/react-native/docs/SwitchWrapper.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { DevOnly } from '@site/src/components';

# SwitchWrapper

The component provides built-in accessibility implementation for creating a custom switch.

## Usage

```jsx pf
import {SwitchWrapper} from 'react-native-ama';

<SwitchWrapper
accessibilityLabel={accessibilityLabel}
style={[allStyles.container, style]}
onPress={onValueChange}
checked={value}>
<CustomSwitcher checked={checked} onPress={onValueChanged}>
</SwitchWrapper>
```

## Accessibility

The component uses the [useSwitch](./hooks/useSwitch.md) hook under the hood and:

- Sets the [accessibilityRole](../../../website/guidelines/accessibility-role.md) property to **switch**
- Handled the [accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) needed by the Screen Reader
- Performs a [Minimum Size](../../../website/guidelines/minimum-size.md) check <DevOnly />
- Performs a check on the [accessibilityLabel](../../../website/guidelines/accessibility-label.md) <DevOnly />

## Related guidelines

- [Forms](../../../website/guidelines/forms.md)
70 changes: 70 additions & 0 deletions packages/react-native/docs/hooks/useExpandable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {DevOnly, Required} from '@site/src/components';

# useExpandable

This hook allows creating custom Expandable elements with the [Accessibility checks](#accessibility-checks) needed.

## Usage

```jsx
import { PressableProps, useExpandable } from 'react-native-ama';

const expandableProps =
useExpandable <
PressableProps >
{
...props,
expanded: boolean,
};
```

## Example

```jsx
import { useExpandable } from 'react-native-ama';

type CustomExpandableProps = React.PropsWithChildren<
UseExpandable<PressableProps>,
>;

export const CustomExpandable = ({
children,
...rest
}: CustomExpandableProps) => {
const expandableProps = useExpandable(rest);

return <Pressable {...expandableProps}>{children}</Pressable>;
};
```

## Accessibility checks

The hook automatically:

- Sets `accessibilityRole` to **button**
- Handles the `accessibilityState` for **expanded**

At runtime, the hook:

- Forces the use of `accessibilityLabel` <DevOnly />
- Performs a [Minimum Size](../../../../website/guidelines/minimum-size.md) check <DevOnly />
- Performs a [contrast checker](../../../../website/guidelines/contrast.md) between its background color and its children color <DevOnly />

## Props

### <Required /> `accessibilityLabel`

The `accessibilityLabel` property is the first thing announced by the screen reader when the elements gain the focus;
then, it announces its role. If the property is omitted, the user might have little to no clue what could happen if the
element is triggered.

### <Required /> `expanded`

Indicates whether an expandable element is currently expanded or collapsed.

## Related guidelines

- [Accessibility Label](../../../../website/guidelines/accessibility-label.md)
- [Accessibility Role](../../../../website/guidelines/accessibility-role.md)
- [Contrast](../../../../website/guidelines/contrast.md)
- [Minimum Size](../../../../website/guidelines/minimum-size.md)
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,32 @@ At runtime the hook:

- Forces the use of `accessibilityRole` and `accessibilityLabel` <DevOnly />
- Handles the `accessibilityState` [passed as property](../components/Pressable.mdx#accessibility-states)
- Performs a [Minimum Size](/guidelines/minimum-size.md) check <DevOnly />
- Performs a [contrast checker](/guidelines/contrast.md) between background color and its children color <DevOnly />
- Performs a [Minimum Size](../../../../website/guidelines/minimum-size.md) check <DevOnly />
- Performs a [contrast checker](../../../../website/guidelines/contrast.md) between background color and its children color <DevOnly />

### accessibilityRole

The `accessibilityRole` property is used by the screen reader to announce the kind of element focused on. If the property is omitted, the user might have little to no clue what could happen if the element is triggered.

[Check here for more info](/guidelines/accessibility-role.md)
[Check here for more info](../../../../website/guidelines/accessibility-role.md)

### accessibilityLabel

The `accessibilityLabel` property is the first thing announced by the screen reader when the elements gain the focus; then, it announces its role. If the property is omitted, the user might have little to no clue what could happen if the element is triggered.

[Check here for more info](/guidelines/accessibility-label.md)
[Check here for more info](../../../../website/guidelines/accessibility-label.md)

### Contrast checker

The component performs a [contrast check](/guidelines/contrast.md) between its background colour and the children's foreground when in dev mode.
The component performs a [contrast check](../../../../website/guidelines/contrast.md) between its background colour and the children's foreground when in dev mode.

:::info
AMA does also perform a contrast check on disabled button, as a [poor contrast can make them hard to read](https://axesslab.com/disabled-buttons-suck/#they-are-hard-to-see).
:::

### Minimum size

The component uses the [onLayout](https://reactnative.dev/docs/layoutevent) prop to perform the [minium size check](/guidelines/minimum-size.md).
The component uses the [onLayout](https://reactnative.dev/docs/layoutevent) prop to perform the [minium size check](../../../../website/guidelines/minimum-size.md).

## Example

Expand All @@ -72,7 +72,7 @@ const MyCustomPressable: React.FC<MyCustomPressableProps> = ({

## Related guidelines

- [Accessibility Label](/guidelines/accessibility-label)
- [Accessibility Role](/guidelines/accessibility-role)
- [Contrast](/guidelines/contrast)
- [Minimum Size](/guidelines/minimum-size)
- [Accessibility Label](../../../../website/guidelines/accessibility-label.md)
- [Accessibility Role](../../../../website/guidelines/accessibility-role.md)
- [Contrast](../../../../website/guidelines/contrast.md)
- [Minimum Size](../../../../website/guidelines/minimum-size.md)
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ The hook automatically:

The `accessibilityLabel` property is the first thing announced by the screen reader when the elements gain the focus; then, it announces its role. If the property is omitted, the user might have little to no clue what could happen if the element is triggered.

[Check here for more info](/guidelines/accessibility-label.md)
[Check here for more info](../../../../website/guidelines/accessibility-label.md)

### Minimum size

The component uses the [onLayout](https://reactnative.dev/docs/layoutevent) prop to perform the [minium size check](/guidelines/minimum-size).
The component uses the [onLayout](https://reactnative.dev/docs/layoutevent) prop to perform the [minium size check](../../../../website/guidelines/minimum-size.md).

## Example

Expand All @@ -55,4 +55,4 @@ export const SwitchListItem = () => {

## Related guidelines

- [Forms](/guidelines/forms)
- [Forms](../../../../website/guidelines/forms.md)

0 comments on commit 54b8151

Please sign in to comment.