Skip to content
This repository has been archived by the owner on Oct 19, 2021. It is now read-only.

Commit

Permalink
fix(code-snippet): add screen reader accessibility improvements (#2165)
Browse files Browse the repository at this point in the history
* fix(code-snippet): add alert role to copy verification

* fix(code-snippet): add aria-describedby, aria-labelledby to inline snippet

* fix(code-snippet): add unique id to aria-describedby

* fix(code-snippet): remove breaking change

* fix(codesnippet): preserve ariaLabel
  • Loading branch information
dakahn authored and asudoh committed Apr 23, 2019
1 parent 9ff509e commit bda3016
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/components/CodeSnippet/CodeSnippet-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ const props = {
inline: () => ({
light: boolean('Light variant (light)', false),
feedback: text('Feedback text (feedback)', 'Feedback Enabled 👍'),
copyLabel: text('ARIA label of the copy button (copyLabel)', 'Copy Code'),
onClick: action('onClick'),
copyLabel: text(
'ARIA label for the snippet/copy button (copyLabel)',
'copyable code snippet'
),
}),
single: () => ({
feedback: text('Feedback text (feedback)', 'Feedback Enabled 👍'),
Expand Down
24 changes: 15 additions & 9 deletions src/components/CodeSnippet/CodeSnippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Copy from '../Copy';
import CopyButton from '../CopyButton';
import Icon from '../Icon';
import { componentsX } from '../../internal/FeatureFlags';
import uid from '../../tools/uniqueId';

const { prefix } = settings;

Expand All @@ -39,11 +40,6 @@ export default class CodeSnippet extends Component {
*/
feedback: PropTypes.string,

/**
* Specify the label used for the Copy Button
*/
copyLabel: PropTypes.string,

/**
* Specify the description for the Copy Button
*/
Expand All @@ -55,6 +51,12 @@ export default class CodeSnippet extends Component {
*/
onClick: PropTypes.func,

/**
* Specify a label to be read by screen readers on the containing <textbox>
* node
*/
copyLabel: PropTypes.string,

/**
* Specify a label to be read by screen readers on the containing <textbox>
* node
Expand Down Expand Up @@ -119,14 +121,17 @@ export default class CodeSnippet extends Component {
feedback,
onClick,
ariaLabel,
copyLabel,
copyLabel, //TODO: Merge this prop to `ariaLabel` in `v11`
copyButtonDescription,
light,
showMoreText,
showLessText,
...other
} = this.props;

// a unique id generated for aria-describedby
this.uid = uid();

const codeSnippetClasses = classNames(className, {
[`${prefix}--snippet`]: true,
[`${prefix}--snippet--single`]: type === 'single',
Expand Down Expand Up @@ -172,7 +177,7 @@ export default class CodeSnippet extends Component {
role="textbox"
tabIndex={0}
className={`${prefix}--snippet-container`}
aria-label={ariaLabel ? ariaLabel : 'code-snippet'}>
aria-label={ariaLabel || copyLabel || 'code-snippet'}>
<code>
<pre
ref={codeContent => {
Expand All @@ -197,10 +202,11 @@ export default class CodeSnippet extends Component {
<Copy
{...other}
onClick={onClick}
aria-label={copyLabel || ariaLabel}
aria-describedby={this.uid}
className={codeSnippetClasses}
aria-label={copyLabel}
feedback={feedback}>
<code>{children}</code>
<code id={this.uid}>{children}</code>
</Copy>
);
}
Expand Down

0 comments on commit bda3016

Please sign in to comment.