Skip to content

Commit

Permalink
feat(code_block): allow for a custom copy button aria-label (#8176)
Browse files Browse the repository at this point in the history
  • Loading branch information
weronikaolejniczak authored Nov 25, 2024
1 parent 9eb944a commit 90f7d5c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/eui/changelogs/upcoming/8176.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Accessibility**

- Updated `EuiCodeBlock` with a new `copyAriaLabel` prop, which allows setting a custom screen reader label on the copy button.
14 changes: 14 additions & 0 deletions packages/eui/src/components/code/code_block.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ describe('EuiCodeBlock', () => {
});
});

it('renders `copyAriaLabel` on the copy button', () => {
const customLabel = 'Copy this code';
const { getByTestSubject } = render(
<EuiCodeBlock isCopyable copyAriaLabel={customLabel}>
{code}
</EuiCodeBlock>
);

expect(getByTestSubject('euiCodeBlockCopy')).toHaveAttribute(
'aria-label',
customLabel
);
});

describe('overflowHeight', () => {
it('is rendered', () => {
const { container } = render(
Expand Down
9 changes: 9 additions & 0 deletions packages/eui/src/components/code/code_block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ export type EuiCodeBlockProps = EuiCodeSharedProps & {
*/
isCopyable?: boolean;

/**
* Customizes the aria-label for the copy button.
*
* @default 'Copy'
*/
copyAriaLabel?: string;

/**
* Displays line numbers.
* Optionally accepts a configuration object for setting the starting number,
Expand Down Expand Up @@ -118,6 +125,7 @@ export const EuiCodeBlock: FunctionComponent<EuiCodeBlockProps> = ({
paddingSize = 'l',
fontSize = 's',
isCopyable = false,
copyAriaLabel,
whiteSpace = 'pre-wrap',
children,
className,
Expand Down Expand Up @@ -159,6 +167,7 @@ export const EuiCodeBlock: FunctionComponent<EuiCodeBlockProps> = ({
);

const { innerTextRef, copyButton } = useCopy({
copyAriaLabel,
isCopyable,
isVirtualized,
children,
Expand Down
8 changes: 5 additions & 3 deletions packages/eui/src/components/code/code_block_copy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import { NEW_LINE_REGEX_GLOBAL } from './utils';
* Hook that returns copy-related state/logic/utils
*/
export const useCopy = ({
copyAriaLabel,
isCopyable,
isVirtualized,
children,
}: {
copyAriaLabel?: string;
isCopyable: boolean;
isVirtualized: boolean;
children: ReactNode;
Expand All @@ -41,7 +43,7 @@ export const useCopy = ({

const showCopyButton = isCopyable && textToCopy;

const copyAriaLabel = useEuiI18n('euiCodeBlockCopy.copy', 'Copy');
const copyDefaultAriaLabel = useEuiI18n('euiCodeBlockCopy.copy', 'Copy');

const copyButton = useMemo(() => {
return showCopyButton ? (
Expand All @@ -52,14 +54,14 @@ export const useCopy = ({
onClick={copy}
iconType="copyClipboard"
color="text"
aria-label={copyAriaLabel}
aria-label={copyAriaLabel || copyDefaultAriaLabel}
data-test-subj="euiCodeBlockCopy"
/>
)}
</EuiCopy>
</div>
) : null;
}, [showCopyButton, textToCopy, copyAriaLabel]);
}, [copyAriaLabel, copyDefaultAriaLabel, showCopyButton, textToCopy]);

return { innerTextRef, copyButton };
};

0 comments on commit 90f7d5c

Please sign in to comment.