Skip to content

Commit

Permalink
fix(FileUploader, CodeSnippet): backport a11y fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tw15egan committed Jun 26, 2023
1 parent 48899c9 commit 6366ff0
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@
order: 1;
overflow-y: auto;
transition: max-height $duration--moderate-01 motion(standard, productive);

&:focus {
@include focus-outline('outline');

outline-offset: 0;
}
}

// expanded snippet container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@
}

.#{$prefix}--file__state-container .#{$prefix}--file-complete {
cursor: pointer;
fill: $interactive-04;

&:focus {
Expand Down Expand Up @@ -318,6 +317,9 @@
}

.#{$prefix}--file__drop-container {
@include button-reset;
@include type-style('body-short-01');

display: flex;
overflow: hidden;
height: rem(96px);
Expand Down
8 changes: 6 additions & 2 deletions packages/react/src/components/CodeSnippet/CodeSnippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,14 @@ function CodeSnippet({
<div {...rest} className={codeSnippetClasses}>
<div
ref={codeContainerRef}
role={type === 'single' ? 'textbox' : null}
tabIndex={type === 'single' && !disabled ? 0 : null}
role={type === 'single' || type === 'multi' ? 'textbox' : null}
tabIndex={
(type === 'single' || type === 'multi') && !disabled ? 0 : null
}
className={`${prefix}--snippet-container`}
aria-label={ariaLabel || copyLabel || 'code-snippet'}
aria-readonly={type === 'single' || type === 'multi' ? true : null}
aria-multiline={type === 'multi' ? true : null}
onScroll={(type === 'single' && handleScroll) || null}
{...containerStyle}>
<pre
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/components/FileUploader/FileUploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ export default class FileUploader extends React.Component {
<p className={`${prefix}--file-filename`}>{name}</p>
<span className={`${prefix}--file__state-container`}>
<Filename
name={name}
iconDescription={iconDescription}
status={filenameStatus}
onKeyDown={(evt) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import classNames from 'classnames';
import { keys, matches } from '../../internal/keyboard';
import uniqueId from '../../tools/uniqueId';
import { usePrefix } from '../../internal/usePrefix';
import { composeEventHandlers } from '../../tools/events';
import deprecate from '../../prop-types/deprecate';

function FileUploaderDropContainer({
accept,
Expand All @@ -21,22 +23,23 @@ function FileUploaderDropContainer({
multiple,
name,
onAddFiles,
onClick,
pattern,
role,
tabIndex,
...rest
}) {
const prefix = usePrefix();
const inputRef = useRef(null);
const { current: uid } = useRef(id || uniqueId());
const [isActive, setActive] = useState(false);
const labelClasses = classNames(`${prefix}--file-browse-btn`, {
[`${prefix}--file-browse-btn--disabled`]: disabled,
});
const dropareaClasses = classNames(`${prefix}--file__drop-container`, {
[`${prefix}--file__drop-container--drag-over`]: isActive,
[className]: className,
});
const dropareaClasses = classNames(
`${prefix}--file__drop-container`,
`${prefix}--file-browse-btn`,
{
[`${prefix}--file__drop-container--drag-over`]: isActive,
[`${prefix}--file-browse-btn--disabled`]: disabled,
[className]: className,
}
);

/**
* Filters the array of added files based on file type restrictions
Expand Down Expand Up @@ -72,6 +75,12 @@ function FileUploaderDropContainer({
return onAddFiles(event, { addedFiles });
}

const handleClick = () => {
if (!disabled) {
inputRef.current.click();
}
};

return (
<div
className={`${prefix}--file`}
Expand Down Expand Up @@ -102,20 +111,21 @@ function FileUploaderDropContainer({
setActive(false);
handleChange(evt);
}}>
{/* eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions */}
<label
className={labelClasses}
htmlFor={uid}
tabIndex={tabIndex || 0}
<button
type="button"
className={dropareaClasses}
onKeyDown={(evt) => {
if (matches(evt, [keys.Enter, keys.Space])) {
evt.preventDefault();
inputRef.current.click();
}
}}
onClick={composeEventHandlers([onClick, handleClick])}
{...rest}>
<div className={dropareaClasses} role={role || 'button'}>
{labelText}
</div>
{labelText}
</button>
<label htmlFor={uid} className={`${prefix}--visually-hidden`}>
{labelText}
</label>
<input
type="file"
Expand Down Expand Up @@ -179,6 +189,12 @@ FileUploaderDropContainer.propTypes = {
*/
onAddFiles: PropTypes.func,

/**
* Provide an optional function to be called when the button element
* is clicked
*/
onClick: PropTypes.func,

/**
* Provide a custom regex pattern for the acceptedTypes
*/
Expand All @@ -187,16 +203,23 @@ FileUploaderDropContainer.propTypes = {
/**
* Provide an accessibility role for the <FileUploaderButton>
*/
role: PropTypes.string,
role: deprecate(
PropTypes.number,
'The `role` prop for `FileUploaderButton` has ' +
'been deprecated since it now renders a button element by default, and has an implicit role of button.'
),

/**
* Provide a custom tabIndex value for the <FileUploaderButton>
*/
tabIndex: PropTypes.number,
tabIndex: deprecate(
PropTypes.number,
'The `tabIndex` prop for `FileUploaderButton` has ' +
'been deprecated since it now renders a button element by default.'
),
};

FileUploaderDropContainer.defaultProps = {
tabIndex: 0,
labelText: 'Add file',
multiple: false,
onAddFiles: () => {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ function FileUploaderItem({
</p>
<span className={`${prefix}--file__state-container`}>
<Filename
name={name}
iconDescription={iconDescription}
status={status}
invalid={invalid}
onKeyDown={(evt) => {
if (matches(evt, [keys.Enter, keys.Space])) {
if (status === 'edit') {
evt.preventDefault();
onDelete(evt, { uuid: id });
}
}
Expand Down
12 changes: 9 additions & 3 deletions packages/react/src/components/FileUploader/Filename.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import React from 'react';
import Loading from '../Loading';
import { usePrefix } from '../../internal/usePrefix';

function Filename({ iconDescription, status, invalid, ...rest }) {
function Filename({ iconDescription, status, invalid, name, ...rest }) {
const prefix = usePrefix();
switch (status) {
case 'uploading':
Expand All @@ -27,7 +27,7 @@ function Filename({ iconDescription, status, invalid, ...rest }) {
<>
{invalid && <WarningFilled16 className={`${prefix}--file-invalid`} />}
<button
aria-label={iconDescription}
aria-label={`${iconDescription} - ${name}`}
className={`${prefix}--file-close`}
type="button"
{...rest}>
Expand All @@ -40,7 +40,8 @@ function Filename({ iconDescription, status, invalid, ...rest }) {
<CheckmarkFilled16
aria-label={iconDescription}
className={`${prefix}--file-complete`}
{...rest}>
{...rest}
tabIndex={null}>
{iconDescription && <title>{iconDescription}</title>}
</CheckmarkFilled16>
);
Expand All @@ -60,6 +61,11 @@ Filename.propTypes = {
*/
invalid: PropTypes.bool,

/**
* Name of the uploaded file
*/
name: PropTypes.string,

/**
* Status of the file upload
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ describe('FileUploader', () => {
);

const complete = getByLabel(container, description);
expect(edit.parentNode).not.toEqual(complete.parentNode);
expect(edit).not.toEqual(complete);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('FileUploaderDropContainer', () => {
const { container } = render(
<FileUploaderDropContainer className="test" />
);
const dropArea = container.querySelector('[role="button"]');
const dropArea = container.querySelector('button');
expect(dropArea.classList.contains('test')).toBe(true);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('FileUploaderItem', () => {
/>
);

let removeFile = getByLabel(edit.container, description);
let removeFile = getByLabel(edit.container, 'test-description - edit');
Simulate.click(removeFile);
expect(onDelete).toHaveBeenCalledTimes(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ describe('Filename', () => {
const onClick = jest.fn();
const { container: edit } = render(
<Filename
name="File 1"
iconDescription="test description"
status="edit"
onClick={onClick}
/>
);

Simulate.click(edit.querySelector(`[aria-label="test description"]`));
Simulate.click(
edit.querySelector(`[aria-label="test description - File 1"]`)
);
expect(onClick).toHaveBeenCalledTimes(1);

onClick.mockReset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ $copy-btn-feedback: $background-inverse !default;
order: 1;
overflow-y: auto;
transition: max-height $duration-moderate-01 motion(standard, productive);

&:focus {
@include focus-outline('outline');

outline-offset: 0;
}
}

// expanded snippet container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@use '../../theme' as *;
@use '../../type' as *;

@use '../../utilities/button-reset';
@use '../../utilities/convert' as *;
@use '../../utilities/focus-outline' as *;
@use '../../utilities/high-contrast-mode' as *;
Expand Down Expand Up @@ -271,7 +272,6 @@
}

.#{$prefix}--file__state-container .#{$prefix}--file-complete {
cursor: pointer;
fill: $interactive;

&:focus {
Expand Down Expand Up @@ -317,6 +317,8 @@
}

.#{$prefix}--file__drop-container {
@include button-reset.reset;

display: flex;
overflow: hidden;
height: rem(96px);
Expand Down

0 comments on commit 6366ff0

Please sign in to comment.