Skip to content

Commit 7cc31a0

Browse files
committed
Trap focus in dropdown menu
[DISCO-307]
1 parent bf5c0b4 commit 7cc31a0

File tree

6 files changed

+388
-355
lines changed

6 files changed

+388
-355
lines changed

src/app/components/Dropdown.tsx

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
12
import { HTMLElement } from '@openstax/types/lib.dom';
23
import flow from 'lodash/fp/flow';
34
import isUndefined from 'lodash/fp/isUndefined';
45
import omitBy from 'lodash/fp/omitBy';
56
import React, { ReactNode } from 'react';
67
import { FormattedMessage, useIntl } from 'react-intl';
78
import styled, { css, keyframes } from 'styled-components/macro';
8-
import { useFocusLost } from '../reactUtils';
9+
import { useFocusLost, useTrapTabNavigation } from '../reactUtils';
910
import { useOnEsc } from '../reactUtils';
1011
import theme from '../theme';
1112
import { preventDefault } from '../utils';
@@ -165,8 +166,19 @@ const TabTransparentDropdown = styled((
165166
`}
166167
`;
167168

169+
function TrappingDropdownList(props: object) {
170+
const ref = React.useRef(null);
171+
172+
useTrapTabNavigation(ref);
173+
174+
return (
175+
<ol ref={ref} {...props} />
176+
);
177+
}
178+
179+
168180
// tslint:disable-next-line:variable-name
169-
export const DropdownList = styled.ol`
181+
export const DropdownList = styled(TrappingDropdownList)`
170182
margin: 0;
171183
padding: 0.6rem 0;
172184
background: ${theme.color.neutral.formBackground};
@@ -204,6 +216,7 @@ export const DropdownList = styled.ol`
204216
}
205217
`;
206218

219+
207220
interface DropdownItemProps {
208221
message: string;
209222
ariaMessage?: string;

src/app/components/__snapshots__/DotMenu.spec.tsx.snap

+1
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ exports[`Dropdown matches snapshot on right align 1`] = `
499499
</button>
500500
<ol
501501
className="c12"
502+
rightAlign={true}
502503
>
503504
<li>
504505
<a

src/app/content/highlights/components/ColorPicker.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ const ColorButton = styled(({className, size, style, ...props}: ColorButtonProps
6161
height: 0;
6262
width: 0;
6363
}
64+
65+
:focus-within {
66+
outline: 0.2rem auto Highlight;
67+
outline: 0.2rem inset -webkit-focus-ring-color;
68+
}
6469
`;
6570

6671
type NavKeys = 'Home' | 'End' | 'ArrowLeft' | 'ArrowRight' | ' ' | 'Enter';

src/app/content/highlights/components/SummaryPopup/ContextMenu.tsx

+7-5
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,14 @@ const ContextMenu = ({
126126
transparentTab={false}
127127
>
128128
<HighlightToggleEditContent>
129-
<ColorPicker
130-
color={color}
131-
size='small'
132-
onChange={onColorChange}
133-
/>
134129
<StyledDropdownList>
130+
<li>
131+
<ColorPicker
132+
color={color}
133+
size='small'
134+
onChange={onColorChange}
135+
/>
136+
</li>
135137
<DropdownItem
136138
data-testid='edit'
137139
ariaMessage={editMessage}

0 commit comments

Comments
 (0)