This repository was archived by the owner on Jul 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 374
a11y: fix table-forms keyboard navigation #9020
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
Composer/packages/client/src/components/CellFocusZone.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| /** @jsx jsx */ | ||
| import { jsx, css } from '@emotion/core'; | ||
| import React, { useCallback } from 'react'; | ||
| import { FocusZone, FocusZoneTabbableElements, IFocusZoneProps } from 'office-ui-fabric-react/lib/FocusZone'; | ||
| import { getFocusStyle, getTheme, mergeStyles } from 'office-ui-fabric-react/lib/Styling'; | ||
|
|
||
| import { useAfterRender } from '../hooks/useAfterRender'; | ||
|
|
||
| export const formCell = css` | ||
| outline: none; | ||
| white-space: pre-wrap; | ||
| font-size: 14px; | ||
| line-height: 28px; | ||
| `; | ||
|
|
||
| export const formCellFocus = mergeStyles( | ||
| getFocusStyle(getTheme(), { | ||
| inset: -3, | ||
| }) | ||
| ); | ||
|
|
||
| /** | ||
| * CellFocusZone component props. | ||
| * Props are intently omitted as they're required for component to function correctly. | ||
| * Please manually test the component using keyboard in places it is used in case any changes made. | ||
| */ | ||
| type CellFocusZoneProps = Omit< | ||
| React.HTMLAttributes<unknown> & IFocusZoneProps, | ||
| | 'allowFocusRoot' | ||
| | 'data-is-focusable' | ||
| | 'isCircularNavigation' | ||
| | 'className' | ||
| | 'handleTabKey' | ||
| | 'shouldFocusInnerElementWhenReceivedFocus' | ||
| | 'tabIndex' | ||
| | 'onBlur' | ||
| | 'onKeyDown' | ||
| >; | ||
|
|
||
| const CellFocusZone: React.FC<CellFocusZoneProps> = (props) => { | ||
| const onAfterRender = useAfterRender(); | ||
| const focusCurrentZoneAfterRender = useCallback((focusZoneEl: any) => { | ||
| const focusZoneId = focusZoneEl?.dataset?.focuszoneId; | ||
tonyanziano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!focusZoneId) { | ||
| return; | ||
| } | ||
| // wait for render to happen before placing focus back to the focus zone | ||
| onAfterRender(() => { | ||
| const focusZone: HTMLElement | null = document.querySelector(`[data-focuszone-id=${focusZoneId}]`); | ||
| focusZone?.focus(); | ||
| }); | ||
| }, []); | ||
|
|
||
| const onCellKeyDown = useCallback((ev) => { | ||
| // ignore any of events coming from input fields | ||
| if (ev.target.localName === 'input' || ev.target.localName === 'textarea') { | ||
| ev.stopPropagation(); | ||
| return; | ||
| } | ||
|
|
||
| // enter inside cell using Enter key | ||
| if (ev.target.dataset.focuszoneId && ev.key === 'Enter') { | ||
| const input: HTMLElement | null = (ev?.target as HTMLElement)?.querySelector('a, button, input, textarea'); | ||
| input?.focus(); | ||
| return; | ||
| } | ||
|
|
||
| // handle uncaught escape key events to allow returning back to navigation between cells | ||
| if (ev.key === 'Escape') { | ||
| ev.stopPropagation(); | ||
| focusCurrentZoneAfterRender(ev.currentTarget); | ||
| } | ||
| }, []); | ||
|
|
||
| const onCellInnerBlur = useCallback((ev) => { | ||
| // this means we dont have an element to focus so we place focus back to the cell | ||
| if (!ev.relatedTarget) { | ||
| focusCurrentZoneAfterRender(ev.currentTarget); | ||
| } | ||
| }, []); | ||
|
|
||
| return ( | ||
| <FocusZone | ||
| css={formCell} | ||
| {...props} | ||
| allowFocusRoot | ||
| data-is-focusable | ||
| isCircularNavigation | ||
| className={formCellFocus} | ||
| handleTabKey={FocusZoneTabbableElements.all} | ||
| shouldFocusInnerElementWhenReceivedFocus={false} | ||
| tabIndex={0} | ||
| onBlur={onCellInnerBlur} | ||
| onKeyDown={onCellKeyDown} | ||
| > | ||
| {props.children} | ||
| </FocusZone> | ||
| ); | ||
| }; | ||
|
|
||
| export { CellFocusZone }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { useCallback, useLayoutEffect, useRef } from 'react'; | ||
|
|
||
| /** | ||
| * Run a callback function not earlier than immediate re-renders happen | ||
| * @returns onAfterRender | ||
| */ | ||
| export const useAfterRender = () => { | ||
| const timeout = useRef<NodeJS.Timeout>(); | ||
| const callback = useRef<(() => void) | null>(null); | ||
|
|
||
| useLayoutEffect(() => { | ||
| callback.current?.(); | ||
| }); | ||
|
|
||
| return useCallback((fn: () => unknown) => { | ||
| callback.current = () => { | ||
| if (timeout.current) clearTimeout(timeout.current); | ||
| timeout.current = setTimeout(() => { | ||
| callback.current = null; | ||
| fn(); | ||
| }, 0); | ||
| }; | ||
|
|
||
| callback.current?.(); | ||
| }, []); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.