Accessibility fixes for ConversationDetails and AvatarPopup

This commit is contained in:
Scott Nonnenberg 2021-10-25 12:11:19 -07:00 committed by GitHub
parent 56031336a9
commit 53bc13a401
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 203 additions and 81 deletions

View file

@ -7584,11 +7584,29 @@ button.module-image__border-overlay:focus {
flex-direction: row; flex-direction: row;
width: 100%; width: 100%;
&:hover {
@include light-theme { @include light-theme {
background-color: $color-gray-15; &:hover {
background-color: $color-gray-05;
}
} }
@include dark-theme { @include dark-theme {
&:hover {
background-color: $color-gray-60;
}
}
@include keyboard-mode {
&:hover {
background-color: inherit;
}
&:focus {
background-color: $color-gray-05;
}
}
@include dark-keyboard-mode {
&:hover {
background-color: inherit;
}
&:focus {
background-color: $color-gray-60; background-color: $color-gray-60;
} }
} }

View file

@ -134,7 +134,7 @@ export const AvatarPreview = ({
const isLoading = imageStatus === ImageStatus.Loading; const isLoading = imageStatus === ImageStatus.Loading;
const clickProps = onClick ? { role: 'button', onClick } : {}; const clickProps = onClick ? { role: 'button', onClick, tabIndex: 0 } : {};
const componentStyle = { const componentStyle = {
...style, ...style,
}; };

View file

@ -18,6 +18,8 @@ import { SampleMessageBubbles } from './SampleMessageBubbles';
import { PanelRow } from './conversation/conversation-details/PanelRow'; import { PanelRow } from './conversation/conversation-details/PanelRow';
import { getCustomColorStyle } from '../util/getCustomColorStyle'; import { getCustomColorStyle } from '../util/getCustomColorStyle';
import { useDelayedRestoreFocus } from '../hooks/useRestoreFocus';
type CustomColorDataType = { type CustomColorDataType = {
id?: string; id?: string;
value?: CustomColorType; value?: CustomColorType;
@ -84,6 +86,8 @@ export const ChatColorPicker = ({
CustomColorDataType | undefined CustomColorDataType | undefined
>(undefined); >(undefined);
const [focusRef] = useDelayedRestoreFocus();
const onSelectColor = ( const onSelectColor = (
conversationColor: ConversationColorType, conversationColor: ConversationColorType,
customColorData?: { id: string; value: CustomColorType } customColorData?: { id: string; value: CustomColorType }
@ -172,7 +176,7 @@ export const ChatColorPicker = ({
/> />
<hr /> <hr />
<div className="ChatColorPicker__bubbles"> <div className="ChatColorPicker__bubbles">
{ConversationColors.map(color => ( {ConversationColors.map((color, i) => (
<div <div
aria-label={color} aria-label={color}
className={classNames( className={classNames(
@ -190,6 +194,7 @@ export const ChatColorPicker = ({
}} }}
role="button" role="button"
tabIndex={0} tabIndex={0}
ref={i === 0 ? focusRef : undefined}
/> />
))} ))}
{Object.keys(customColors).map(colorId => { {Object.keys(customColors).map(colorId => {

View file

@ -19,14 +19,11 @@ export type PropsType = Readonly<{
value?: string | number; value?: string | number;
}>; }>;
export function Select({ export const Select = React.forwardRef(
disabled, (
moduleClassName, { disabled, moduleClassName, name, onChange, options, value }: PropsType,
name, ref: React.Ref<HTMLSelectElement>
onChange, ): JSX.Element => {
options,
value,
}: PropsType): JSX.Element {
const onSelectChange = (event: ChangeEvent<HTMLSelectElement>) => { const onSelectChange = (event: ChangeEvent<HTMLSelectElement>) => {
onChange(event.target.value); onChange(event.target.value);
}; };
@ -38,6 +35,7 @@ export function Select({
name={name} name={name}
value={value} value={value}
onChange={onSelectChange} onChange={onSelectChange}
ref={ref}
> >
{options.map( {options.map(
({ disabled: optionDisabled, text, value: optionValue }) => { ({ disabled: optionDisabled, text, value: optionValue }) => {
@ -56,4 +54,5 @@ export function Select({
</select> </select>
</div> </div>
); );
} }
);

View file

@ -88,3 +88,9 @@ story.add('On (Non-admin)', () => {
return <GroupLinkManagement {...props} />; return <GroupLinkManagement {...props} />;
}); });
story.add('Off (Non-admin) - user cannot get here', () => {
const props = createProps(undefined, false);
return <GroupLinkManagement {...props} />;
});

View file

@ -11,6 +11,8 @@ import { PanelRow } from './PanelRow';
import { PanelSection } from './PanelSection'; import { PanelSection } from './PanelSection';
import { Select } from '../../Select'; import { Select } from '../../Select';
import { useDelayedRestoreFocus } from '../../../hooks/useRestoreFocus';
const AccessControlEnum = Proto.AccessControl.AccessRequired; const AccessControlEnum = Proto.AccessControl.AccessRequired;
export type PropsType = { export type PropsType = {
@ -36,6 +38,8 @@ export const GroupLinkManagement: React.ComponentType<PropsType> = ({
throw new Error('GroupLinkManagement rendered without a conversation'); throw new Error('GroupLinkManagement rendered without a conversation');
} }
const [focusRef] = useDelayedRestoreFocus();
const createEventHandler = (handleEvent: (x: boolean) => void) => { const createEventHandler = (handleEvent: (x: boolean) => void) => {
return (value: string) => { return (value: string) => {
handleEvent(value === 'true'); handleEvent(value === 'true');
@ -72,6 +76,7 @@ export const GroupLinkManagement: React.ComponentType<PropsType> = ({
value: 'false', value: 'false',
}, },
]} ]}
ref={focusRef}
value={String(Boolean(hasGroupLink))} value={String(Boolean(hasGroupLink))}
/> />
) : null ) : null
@ -90,6 +95,7 @@ export const GroupLinkManagement: React.ComponentType<PropsType> = ({
/> />
} }
label={i18n('GroupLinkManagement--share')} label={i18n('GroupLinkManagement--share')}
ref={!isAdmin ? focusRef : undefined}
onClick={() => { onClick={() => {
if (conversation.groupLink) { if (conversation.groupLink) {
copyGroupLink(conversation.groupLink); copyGroupLink(conversation.groupLink);

View file

@ -19,7 +19,9 @@ export type Props = {
const bem = bemGenerator('ConversationDetails-panel-row'); const bem = bemGenerator('ConversationDetails-panel-row');
export const PanelRow: React.ComponentType<Props> = ({ export const PanelRow = React.forwardRef<HTMLButtonElement, Props>(
(
{
alwaysShowActions, alwaysShowActions,
className, className,
disabled, disabled,
@ -29,17 +31,25 @@ export const PanelRow: React.ComponentType<Props> = ({
right, right,
actions, actions,
onClick, onClick,
}) => { }: Props,
ref: React.Ref<HTMLButtonElement>
) => {
const content = ( const content = (
<> <>
{icon !== undefined ? <div className={bem('icon')}>{icon}</div> : null} {icon !== undefined ? <div className={bem('icon')}>{icon}</div> : null}
<div className={bem('label')}> <div className={bem('label')}>
<div>{label}</div> <div>{label}</div>
{info !== undefined ? <div className={bem('info')}>{info}</div> : null} {info !== undefined ? (
<div className={bem('info')}>{info}</div>
) : null}
</div> </div>
{right !== undefined ? <div className={bem('right')}>{right}</div> : null} {right !== undefined ? (
<div className={bem('right')}>{right}</div>
) : null}
{actions !== undefined ? ( {actions !== undefined ? (
<div className={alwaysShowActions ? '' : bem('actions')}>{actions}</div> <div className={alwaysShowActions ? '' : bem('actions')}>
{actions}
</div>
) : null} ) : null}
</> </>
); );
@ -51,6 +61,7 @@ export const PanelRow: React.ComponentType<Props> = ({
type="button" type="button"
className={classNames(bem('root', 'button'), className)} className={classNames(bem('root', 'button'), className)}
onClick={onClick} onClick={onClick}
ref={ref}
> >
{content} {content}
</button> </button>
@ -58,4 +69,5 @@ export const PanelRow: React.ComponentType<Props> = ({
} }
return <div className={classNames(bem('root'), className)}>{content}</div>; return <div className={classNames(bem('root'), className)}>{content}</div>;
}; }
);

View file

@ -45,3 +45,51 @@ export const useRestoreFocus = (): Array<CallbackType> => {
return [setFocusRef]; return [setFocusRef];
}; };
// Panels are initially rendered outside the DOM, and then added to it. We need to
// delay our attempts to set focus.
// Just like the above hook, but with a debounce.
export const useDelayedRestoreFocus = (): Array<CallbackType> => {
const toFocusRef = React.useRef<HTMLElement | null>(null);
const lastFocusedRef = React.useRef<HTMLElement | null>(null);
const setFocusRef = React.useCallback(
(toFocus: HTMLElement | null | undefined) => {
function setFocus() {
if (!toFocus) {
return;
}
// We only want to do this once.
if (toFocusRef.current) {
return;
}
toFocusRef.current = toFocus;
// Remember last-focused element, focus this new target element.
lastFocusedRef.current = document.activeElement as HTMLElement;
toFocus.focus();
}
const timeout = setTimeout(setFocus, 250);
return () => {
clearTimeout(timeout);
};
},
[]
);
React.useEffect(() => {
return () => {
// On unmount, returned focus to element focused before we set the focus
setTimeout(() => {
if (lastFocusedRef.current && lastFocusedRef.current.focus) {
lastFocusedRef.current.focus();
}
});
};
}, []);
return [setFocusRef];
};

View file

@ -13022,6 +13022,20 @@
"reasonCategory": "usageTrusted", "reasonCategory": "usageTrusted",
"updated": "2021-09-17T17:37:46.279Z" "updated": "2021-09-17T17:37:46.279Z"
}, },
{
"rule": "React-useRef",
"path": "ts/hooks/useRestoreFocus.js",
"line": " const toFocusRef = React.useRef(null);",
"reasonCategory": "usageTrusted",
"updated": "2021-10-22T00:52:39.251Z"
},
{
"rule": "React-useRef",
"path": "ts/hooks/useRestoreFocus.js",
"line": " const lastFocusedRef = React.useRef(null);",
"reasonCategory": "usageTrusted",
"updated": "2021-10-22T00:52:39.251Z"
},
{ {
"rule": "React-useRef", "rule": "React-useRef",
"path": "ts/hooks/useRestoreFocus.ts", "path": "ts/hooks/useRestoreFocus.ts",
@ -13036,6 +13050,20 @@
"reasonCategory": "usageTrusted", "reasonCategory": "usageTrusted",
"updated": "2021-07-30T16:57:33.618Z" "updated": "2021-07-30T16:57:33.618Z"
}, },
{
"rule": "React-useRef",
"path": "ts/hooks/useRestoreFocus.ts",
"line": " const toFocusRef = React.useRef<HTMLElement | null>(null);",
"reasonCategory": "usageTrusted",
"updated": "2021-10-22T00:52:39.251Z"
},
{
"rule": "React-useRef",
"path": "ts/hooks/useRestoreFocus.ts",
"line": " const lastFocusedRef = React.useRef<HTMLElement | null>(null);",
"reasonCategory": "usageTrusted",
"updated": "2021-10-22T00:52:39.251Z"
},
{ {
"rule": "jQuery-append(", "rule": "jQuery-append(",
"path": "ts/logging/debuglogs.js", "path": "ts/logging/debuglogs.js",