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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Badge] Add customAccessibilityLabel prop #4028

Merged
merged 4 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f

### Enhancements

- Added `statusAndProgressLabelOverride` prop to `Badge` ([#4028](https://github.com/Shopify/polaris-react/pull/4028))

### Bug fixes

- Ensured `@charset` declaration is the first thing in our styles.css file ([#4019](https://github.com/Shopify/polaris-react/pull/4019))
Expand Down
18 changes: 13 additions & 5 deletions src/components/Badge/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export interface BadgeProps {
* @default 'medium'
*/
size?: Size;
/** Pass a custom accessibilityLabel */
statusAndProgressLabelOverride?: string;
}

const PROGRESS_LABELS: {[key in Progress]: Progress} = {
Expand All @@ -47,6 +49,7 @@ export function Badge({
status,
progress,
size = DEFAULT_SIZE,
statusAndProgressLabelOverride,
}: BadgeProps) {
const i18n = useI18n();
const withinFilter = useContext(WithinFilterContext);
Expand Down Expand Up @@ -98,12 +101,17 @@ export function Badge({
break;
}

const accessibilityLabel = i18n.translate('Polaris.Badge.progressAndStatus', {
progressLabel,
statusLabel,
});
const accessibilityLabel = statusAndProgressLabelOverride
? statusAndProgressLabelOverride
: i18n.translate('Polaris.Badge.progressAndStatus', {
progressLabel,
statusLabel,
});

let accessibilityMarkup = (progressLabel || statusLabel) && (
const hasAccessibilityLabel =
progressLabel || statusLabel || statusAndProgressLabelOverride;

let accessibilityMarkup = hasAccessibilityLabel && (
<VisuallyHidden>{accessibilityLabel}</VisuallyHidden>
);

Expand Down
14 changes: 14 additions & 0 deletions src/components/Badge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,20 @@ Use to indicate when a given task has been completed. For example, when merchant
<Badge progress="complete">Fulfilled</Badge>
```

### Badge with customAccessibilityLabel

Use when the status and progress accessibilityLabels are not appropriate to a given context.

```jsx
<Badge
status="success"
progress="complete"
customAccessibilityLabel="Status: Published. Your online store is visible."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
customAccessibilityLabel="Status: Published. Your online store is visible."
statusAndProgressLabelOverride="Status: Published. Your online store is visible."

>
Published
</Badge>
```

<!-- content-for: android -->

![Complete badge. Default badge with complete status](/public_images/components/Badge/android/complete@2x.png)
Expand Down
30 changes: 30 additions & 0 deletions src/components/Badge/tests/Badge.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,34 @@ describe('<Badge />', () => {
className: 'Pip',
});
});

it('renders with a custom accessibilityLabel when a `statusAndProgressLabelOverride` is provided', () => {
const mockAccessibilityLabel = 'mock accessibilityLabel';
const badge = mountWithApp(
<Badge
status="attention"
progress="incomplete"
statusAndProgressLabelOverride={mockAccessibilityLabel}
/>,
);

expect(badge).toContainReactComponent(VisuallyHidden, {
children: mockAccessibilityLabel,
});
});

it('does not render progress or status accessibility labels when a `statusAndProgressLabelOverride` is provided', () => {
const mockAccessibilityLabel = 'mock accessibilityLabel';
const badge = mountWithApp(
<Badge
status="attention"
progress="incomplete"
statusAndProgressLabelOverride={mockAccessibilityLabel}
/>,
);

expect(badge).not.toContainReactComponent(VisuallyHidden, {
children: 'Attention Incomplete',
});
});
});