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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve a11y on some components #1045

Merged
merged 19 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/DropdownMenu/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const DropdownMenu = <T,>({

return (
<DropdownMenuContext.Provider value={contextValue}>
{/* It is used to handle clicks on the switcher control */}
{/* FIXME change to renderProp or provide component's context */}
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
<div
ref={anchorRef}
Expand Down
15 changes: 15 additions & 0 deletions src/components/Label/Label.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ $transitionTimingFunction: ease-in-out;
border: var(--border-size) solid #{$borderColor};
}

// focus on interactive label (excluding focus on addon)
&:not(#{$disabled})#{$block}_is-interactive:focus:not(
Copy link
Contributor

Choose a reason for hiding this comment

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

What about not setting is-interactive flag if disabled is true?

:has(#{$block}__addon_interactive:focus)
Copy link
Contributor

Choose a reason for hiding this comment

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

If Label is clickable and has close button this wouldn't allow to focus the Label itself

) {
box-shadow: 0 0 0 2px var(--g-color-line-focus);
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's use outline here

}

//fallback for old browsers
&:not(#{$disabled})#{$block}_is-interactive:focus:not(
:has(#{$block}__addon_interactive:focus)
):not(:focus-visible) {
box-shadow: none;
}

// hover on interactive label (excluding hover on addon)
&:not(#{$disabled})#{$block}_is-interactive:hover:not(
:has(#{$block}__addon_interactive:hover)
Expand Down Expand Up @@ -146,6 +160,7 @@ $transitionTimingFunction: ease-in-out;

&_is-interactive {
cursor: pointer;
outline: none;
}

&_theme {
Expand Down
23 changes: 21 additions & 2 deletions src/components/Label/Label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {ClipboardIcon} from '../ClipboardIcon';
import {CopyToClipboard, CopyToClipboardStatus} from '../CopyToClipboard';
import {Icon} from '../Icon';
import {block} from '../utils/cn';
import {useActionHandlers} from '../utils/useActionHandlers';

import './Label.scss';

Expand Down Expand Up @@ -86,6 +87,8 @@ export const Label = React.forwardRef<HTMLDivElement, LabelProps>(function Label
onClick,
} = props;

const actionButtonRef = React.useRef<HTMLButtonElement>(null);

const hasContent = Boolean(children !== '' && React.Children.count(children) > 0);

const typeClose = type === 'close' && hasContent;
Expand Down Expand Up @@ -122,12 +125,25 @@ export const Label = React.forwardRef<HTMLDivElement, LabelProps>(function Label
}
};

const handleClick = (event: React.MouseEvent<HTMLDivElement>) => {
/**
* Triggered only if the handler was triggered on the element itself, and not on the actionButton
* It is necessary that keyboard navigation works correctly
*/
if (!actionButtonRef.current?.contains(event.target as Node)) {
onClick?.(event);
}
};

const {onKeyDown} = useActionHandlers(handleClick);

const renderLabel = (status?: CopyToClipboardStatus) => {
let actionButton: React.ReactNode;

if (typeCopy) {
actionButton = (
<Button
ref={actionButtonRef}
size={buttonSize}
extraProps={{'aria-label': copyButtonLabel || undefined}}
{...commonActionButtonProps}
Expand All @@ -143,6 +159,7 @@ export const Label = React.forwardRef<HTMLDivElement, LabelProps>(function Label
} else if (typeClose) {
actionButton = (
<Button
ref={actionButtonRef}
onClick={onClose ? handleCloseClick : undefined}
size={buttonSize}
extraProps={{'aria-label': closeButtonLabel || undefined}}
Expand All @@ -154,10 +171,12 @@ export const Label = React.forwardRef<HTMLDivElement, LabelProps>(function Label
}

return (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
<div
ref={ref}
onClick={hasOnClick ? onClick : undefined}
role={hasOnClick ? 'role' : undefined}
Copy link
Contributor

Choose a reason for hiding this comment

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

role="button"?

tabIndex={hasOnClick ? 0 : undefined}
onClick={hasOnClick ? handleClick : undefined}
onKeyDown={hasOnClick ? onKeyDown : undefined}
className={b(
{
theme,
Expand Down
3 changes: 2 additions & 1 deletion src/components/Link/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ export const Link = React.forwardRef<HTMLElement, LinkProps>(function Link(
{...(extraProps as React.HTMLAttributes<HTMLSpanElement>)}
{...commonProps}
ref={ref as React.Ref<HTMLSpanElement>}
role="link"
// FIXME Href always should be string
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
tabIndex={0}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export const TableColumnSetup = (props: TableColumnSetupProps) => {

return (
<div className={b(null, className)}>
{/* It is used to handle clicks on the switcher control */}
{/* FIXME change to renderProp or provide component's context */}
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
<div className={b('control')} ref={refControl} onClick={handleControlClick}>
{switcher || (
Expand Down
10 changes: 0 additions & 10 deletions src/components/UserAvatar/UserAvatar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ $block: '.#{variables.$ns}user-avatar';
background-position: center;
background-size: cover;

outline: none;

&:focus {
box-shadow: 0 0 0 2px var(--g-color-line-focus);
}

&:focus:not(:focus-visible) {
box-shadow: none;
}

&_size {
&_xs {
width: 24px;
Expand Down
15 changes: 3 additions & 12 deletions src/components/UserAvatar/UserAvatar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';

import {block} from '../utils/cn';
import {useActionHandlers} from '../utils/useActionHandlers';

import {SIZES} from './constants';
import type {UserAvatarSize} from './types';
Expand Down Expand Up @@ -35,18 +34,10 @@ export const UserAvatar = React.forwardRef<HTMLDivElement, UserAvatarProps>(
setIsErrored(false);
}, [imgUrl]);

const {onKeyDown} = useActionHandlers(onClick);

return (
<div
role="button"
tabIndex={0}
onKeyDown={onKeyDown}
className={b({size}, className)}
title={title}
onClick={onClick}
ref={ref}
>
// OnClick deprecated, will be deleted
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
<div className={b({size}, className)} title={title} onClick={onClick} ref={ref}>
<img
className={b('figure')}
width={SIZES[size]}
Expand Down
5 changes: 2 additions & 3 deletions src/hooks/useOutsideClick/__tests__/Demo.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable jsx-a11y/click-events-have-key-events */
import React from 'react';

import {useOutsideClick} from '../useOutsideClick';
Expand All @@ -23,9 +22,9 @@ export const Demo = () => {
return (
<div>
<h1>{status}</h1>
<div ref={observerRef} onClick={handleClick}>
<button ref={observerRef} onClick={handleClick}>
{'Target'}
</div>
</button>
<div>{'Outside'}</div>
</div>
);
Expand Down
Loading