From 3255c0dc53dcd6f8b71c286d0c443d2d4439ae29 Mon Sep 17 00:00:00 2001 From: Roy Johnson Date: Thu, 18 Jul 2024 10:47:37 -0500 Subject: [PATCH] Trap focus in note menu (#2246) * Use normal component instead of "render" call This turns out to be unrelated to the dropdown I'm using. * Trap focus in dropdown menu [DISCO-307] * Update/use defaultFocusOutline * Remove doubled focus style * Use focus indicator on ColorIndicator, not ColorPicker Adjust focus outline style Update snapshots * Add z-index so MH ColorIndicator focus outline is visible --------- Co-authored-by: staxly[bot] <35789409+staxly[bot]@users.noreply.github.com> --- src/app/components/Dropdown.tsx | 22 +- .../__snapshots__/DotMenu.spec.tsx.snap | 5 +- .../__snapshots__/Dropdown.spec.tsx.snap | 4 +- .../__snapshots__/ChapterFilter.spec.tsx.snap | 8 +- .../__snapshots__/ColorFilter.spec.tsx.snap | 33 +- .../highlights/components/ColorIndicator.tsx | 1 + .../components/SummaryPopup/ContextMenu.tsx | 12 +- .../__snapshots__/ContextMenu.spec.tsx.snap | 725 +++++++++--------- .../__snapshots__/Filters.spec.tsx.snap | 29 +- .../ColorIndicator.spec.tsx.snap | 15 +- .../__snapshots__/ColorPicker.spec.tsx.snap | 25 +- .../__snapshots__/DisplayNote.spec.tsx.snap | 2 +- .../__snapshots__/Filters.spec.tsx.snap | 28 +- src/app/developer/components/Books.tsx | 7 +- src/app/theme.ts | 6 +- 15 files changed, 484 insertions(+), 438 deletions(-) diff --git a/src/app/components/Dropdown.tsx b/src/app/components/Dropdown.tsx index a2f009e0f7..de52e7a683 100644 --- a/src/app/components/Dropdown.tsx +++ b/src/app/components/Dropdown.tsx @@ -1,3 +1,4 @@ + import { HTMLElement } from '@openstax/types/lib.dom'; import flow from 'lodash/fp/flow'; import isUndefined from 'lodash/fp/isUndefined'; @@ -5,9 +6,9 @@ import omitBy from 'lodash/fp/omitBy'; import React, { ReactNode } from 'react'; import { FormattedMessage, useIntl } from 'react-intl'; import styled, { css, keyframes } from 'styled-components/macro'; -import { useFocusLost } from '../reactUtils'; +import { useFocusLost, useTrapTabNavigation } from '../reactUtils'; import { useOnEsc } from '../reactUtils'; -import theme from '../theme'; +import theme, { defaultFocusOutline } from '../theme'; import { preventDefault } from '../utils'; import { textStyle } from './Typography/base'; @@ -165,8 +166,19 @@ const TabTransparentDropdown = styled(( `} `; +function TrappingDropdownList(props: object) { + const ref = React.useRef(null); + + useTrapTabNavigation(ref); + + return ( +
    + ); +} + + // tslint:disable-next-line:variable-name -export const DropdownList = styled.ol` +export const DropdownList = styled(TrappingDropdownList)` margin: 0; padding: 0.6rem 0; background: ${theme.color.neutral.formBackground}; @@ -198,12 +210,12 @@ export const DropdownList = styled.ol` &:hover, &:focus { background: ${theme.color.neutral.formBorder}; - outline: 0.2rem auto Highlight; - outline: 0.2rem inset -webkit-focus-ring-color; + ${defaultFocusOutline} } } `; + interface DropdownItemProps { message: string; ariaMessage?: string; diff --git a/src/app/components/__snapshots__/DotMenu.spec.tsx.snap b/src/app/components/__snapshots__/DotMenu.spec.tsx.snap index 80acb6703f..b3fb1c0479 100644 --- a/src/app/components/__snapshots__/DotMenu.spec.tsx.snap +++ b/src/app/components/__snapshots__/DotMenu.spec.tsx.snap @@ -179,7 +179,7 @@ exports[`Dropdown matches snapshot 1`] = ` .c12 li a:focus { background: #d5d5d5; outline: 0.2rem auto Highlight; - outline: 0.2rem inset -webkit-focus-ring-color; + outline: 0.2rem auto -webkit-focus-ring-color; } .c12.c12 { @@ -452,7 +452,7 @@ exports[`Dropdown matches snapshot on right align 1`] = ` .c12 li a:focus { background: #d5d5d5; outline: 0.2rem auto Highlight; - outline: 0.2rem inset -webkit-focus-ring-color; + outline: 0.2rem auto -webkit-focus-ring-color; } .c12.c12 { @@ -499,6 +499,7 @@ exports[`Dropdown matches snapshot on right align 1`] = `