Skip to content

Commit

Permalink
Merge pull request #3 from AdmitHub/tabbable-skin-tones-2
Browse files Browse the repository at this point in the history
Make skin tone menu tabbable
  • Loading branch information
neckenth authored Jan 10, 2023
2 parents db60c6d + 855258e commit f96c2e5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/components/Layout/Relative.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ type Props = Readonly<{
children: React.ReactNode;
className?: string;
style?: React.CSSProperties;
tabIndex?: number;
onKeyDown?: (event: React.KeyboardEvent<HTMLDivElement>) => void;
}>;

export default function Relative({ children, className, style }: Props) {
export default function Relative({ children, className, style, tabIndex, onKeyDown }: Props) {
return (
<div style={{ ...style, position: 'relative' }} className={className}>
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
<div style={{ ...style, position: 'relative' }} className={className} tabIndex={tabIndex} onKeyDown={onKeyDown}>
{children}
</div>
);
Expand Down
28 changes: 26 additions & 2 deletions src/components/header/SkinTonePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import skinToneVariations, {
import { setSkinTone } from '../../dataUtils/skinTone';
import { useCloseAllOpenToggles } from '../../hooks/useCloseAllOpenToggles';
import { useFocusSearchInput } from '../../hooks/useFocus';
import { KeyboardEvents } from '../../hooks/useKeyboardNavigation';
import { SkinTones } from '../../types/exposedTypes';
import Absolute from '../Layout/Absolute';
import Relative from '../Layout/Relative';
Expand Down Expand Up @@ -66,8 +67,18 @@ export function SkinTonePicker({
? { flexBasis: expandedSize, height: expandedSize }
: { flexBasis: expandedSize }
}
tabIndex={0}
onKeyDown={(event: React.KeyboardEvent<HTMLDivElement>) => {
const { key } = event;
if (key === KeyboardEvents.Enter) {
if (!isOpen) {
setIsOpen(true)
}
closeAllOpenToggles();
}
}}
>
<div className="epr-skin-tone-select" ref={SkinTonePickerRef}>
<div className="epr-skin-tone-select" ref={SkinTonePickerRef} >
{skinToneVariations.map((skinToneVariation, i) => {
const active = skinToneVariation === activeSkinTone;
return (
Expand All @@ -90,11 +101,24 @@ export function SkinTonePicker({
}
closeAllOpenToggles();
}}
// when tabbed onto the SkinTonePicker, allow Enter to open and close the fan of colors
onKeyDown={(event) => {
const { key } = event;
if (key === KeyboardEvents.Enter) {
if (isOpen) {
setActiveSkinTone(skinToneVariation);
focusSearchInput();
} else {
setIsOpen(true);
}
closeAllOpenToggles();
}
}}
tabIndex={isOpen ? 0 : -1}
key={skinToneVariation}
className={clsx(`epr-tone-${skinToneVariation}`, 'epr-tone', {
[ClassNames.active]: active
})}
tabIndex={isOpen ? 0 : -1}
aria-pressed={active}
aria-label={`Skin tone ${skinTonesNamed[skinToneVariation as SkinTones]
}`}
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useKeyboardNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
useIsSkinToneInSearch
} from './useShouldShowSkinTonePicker';

enum KeyboardEvents {
export enum KeyboardEvents {
ArrowDown = 'ArrowDown',
ArrowUp = 'ArrowUp',
ArrowLeft = 'ArrowLeft',
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export {
SkinTonePickerLocation
} from './types/exposedTypes';

export interface Props extends PickerConfig {}
export interface Props extends PickerConfig { }

export default function EmojiPicker(props: Props) {
return (
Expand Down

0 comments on commit f96c2e5

Please sign in to comment.