Merge pull request #12551 from obabativavmware/improve-a11y-for-copy-text-button

Include aria label for copy button
This commit is contained in:
Ben Lambert
2022-07-13 12:36:53 +02:00
committed by GitHub
5 changed files with 40 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/core-components': patch
---
Improve accessibility for CopyTextButton
+1
View File
@@ -190,6 +190,7 @@ export function CopyTextButton(props: CopyTextButtonProps): JSX.Element;
// @public
export interface CopyTextButtonProps {
'aria-label'?: string;
text: string;
tooltipDelay?: number;
tooltipText?: string;
@@ -40,3 +40,10 @@ export const LongerTooltipDelay = () => (
tooltipDelay={3000}
/>
);
export const WithAriaLabel = () => (
<CopyTextButton
text="The text to copy to clipboard"
aria-label="This is an aria label"
/>
);
@@ -55,13 +55,14 @@ const apis = [[errorApiRef, mockErrorApi] as const] as const;
describe('<CopyTextButton />', () => {
it('renders without exploding', async () => {
const { getByTitle, queryByText } = await renderInTestApp(
const { getByTitle, queryByText, getByLabelText } = await renderInTestApp(
<TestApiProvider apis={apis}>
<CopyTextButton {...props} />
</TestApiProvider>,
);
expect(getByTitle('mockTooltip')).toBeInTheDocument();
expect(queryByText('mockTooltip')).not.toBeInTheDocument();
expect(getByLabelText('Copy text')).toBeInTheDocument();
});
it('displays tooltip and copy the text on click', async () => {
@@ -99,4 +100,13 @@ describe('<CopyTextButton />', () => {
);
expect(mockErrorApi.post).toHaveBeenCalledWith(error);
});
it('aria-label', async () => {
const { getByLabelText } = await renderInTestApp(
<TestApiProvider apis={apis}>
<CopyTextButton {...props} aria-label="text for aria-label" />
</TestApiProvider>,
);
expect(getByLabelText('text for aria-label')).toBeInTheDocument();
});
});
@@ -47,6 +47,15 @@ export interface CopyTextButtonProps {
* Default: "Text copied to clipboard"
*/
tooltipText?: string;
/**
* Text to use as aria-label prop on the button
*
* @remarks
*
* Default: "Copy text"
*/
'aria-label'?: string;
}
/**
@@ -62,13 +71,18 @@ export interface CopyTextButtonProps {
*
* @example
*
* `<CopyTextButton text="My text that I want to be copied to the clipboard" />`
* ```
* <CopyTextButton
* text="My text that I want to be copied to the clipboard"
* arial-label="Accessible label for this button" />
* ```
*/
export function CopyTextButton(props: CopyTextButtonProps) {
const {
text,
tooltipDelay = 1000,
tooltipText = 'Text copied to clipboard',
'aria-label': ariaLabel = 'Copy text',
} = props;
const errorApi = useApi(errorApiRef);
const [open, setOpen] = useState(false);
@@ -96,7 +110,7 @@ export function CopyTextButton(props: CopyTextButtonProps) {
onClose={() => setOpen(false)}
open={open}
>
<IconButton onClick={handleCopyClick}>
<IconButton onClick={handleCopyClick} aria-label={ariaLabel}>
<CopyIcon />
</IconButton>
</Tooltip>