GeezerVison is showing github.com — [react-interactions] Modify Scope query mechanism (#17095) · react/react@4cb399a
File treeExpand file treeCollapse file tree Open diff view settings react-interactions/accessibility
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-interactions/accessibility/docs/FocusContain.md +2-2Lines changed: 2 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -10,11 +10,11 @@ using the `tabFocus` prop. | 10 | 10 |
| 11 | 11 | ```jsx | 12 | 12 | import FocusContain from 'react-interactions/accessibility/focus-contain'; | 13 | | -import TabbableScope from 'react-interactions/accessibility/tabbable-scope'; | | 13 | +import tabbableScopeQuery from 'react-interactions/accessibility/tabbable-scope-query'; | 14 | 14 |
| 15 | 15 | function MyDialog(props) { | 16 | 16 | return ( | 17 | | - <FocusContain tabScope={TabbableScope} disabled={false}> | | 17 | + <FocusContain scopeQuery={tabbableScopeQuery} disabled={false}> | 18 | 18 | <div> | 19 | 19 | <h2>{props.title}<h2> | 20 | 20 | <p>{props.text}</p> | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-interactions/accessibility/docs/FocusManager.md +6-2Lines changed: 6 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -14,6 +14,10 @@ import { | 14 | 14 | getPreviousScope, | 15 | 15 | } from 'react-interactions/accessibility/focus-manager'; | 16 | 16 |
| | 17 | +function scopeQuery(type) { | | 18 | + return type === 'div'; | | 19 | +} | | 20 | + | 17 | 21 | function KeyboardFocusMover(props) { | 18 | 22 | const scopeRef = useRef(null); | 19 | 23 |
| @@ -22,9 +26,9 @@ function KeyboardFocusMover(props) { | 22 | 26 |
| 23 | 27 | if (scope) { | 24 | 28 | // Focus the first tabbable DOM node in my children | 25 | | - focusFirst(scope); | | 29 | + focusFirst(scopeQuery, scope); | 26 | 30 | // Then focus the next chilkd | 27 | | - focusNext(scope); | | 31 | + focusNext(scopeQuery, scope); | 28 | 32 | } | 29 | 33 | }); | 30 | 34 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-interactions/accessibility/docs/TabbableScope.md -37Lines changed: 0 additions & 37 deletions Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# TabbableScopeQuery | | 2 | + | | 3 | +`TabbableScopeQuery` is a custom scope implementation that can be used with | | 4 | +`FocusContain`, `FocusGroup`, `FocusTable` and `FocusManager` modules. | | 5 | + | | 6 | +## Usage | | 7 | + | | 8 | +```jsx | | 9 | +import tabbableScopeQuery from 'react-interactions/accessibility/tabbable-scope-query'; | | 10 | + | | 11 | +function FocusableNodeCollector(props) { | | 12 | + const scopeRef = useRef(null); | | 13 | + | | 14 | + useEffect(() => { | | 15 | + const scope = scopeRef.current; | | 16 | + | | 17 | + if (scope) { | | 18 | + const tabFocusableNodes = scope.queryAllNodes(tabbableScopeQuery); | | 19 | + if (tabFocusableNodes && props.onFocusableNodes) { | | 20 | + props.onFocusableNodes(tabFocusableNodes); | | 21 | + } | | 22 | + } | | 23 | + }); | | 24 | + | | 25 | + return ( | | 26 | + <TabbableScope ref={scopeRef}> | | 27 | + {props.children} | | 28 | + </TabbableScope> | | 29 | + ); | | 30 | +} | | 31 | +``` |
Collapse file Copy file name to clipboardExpand all lines: packages/react-interactions/accessibility/src/FocusContain.js +9-8Lines changed: 9 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | * @flow | 8 | 8 | */ | 9 | 9 |
| 10 | | -import type {ReactScope} from 'shared/ReactTypes'; | 11 | 10 | import type {KeyboardEvent} from 'react-interactions/events/keyboard'; | 12 | 11 |
| 13 | 12 | import React from 'react'; | @@ -21,15 +20,17 @@ import { | 21 | 20 | type FocusContainProps = {| | 22 | 21 | children: React.Node, | 23 | 22 | disabled?: boolean, | 24 | | - tabScope: ReactScope, | | 23 | + scopeQuery: (type: string | Object, props: Object) => boolean, | 25 | 24 | |}; | 26 | 25 |
| 27 | 26 | const {useLayoutEffect, useRef} = React; | 28 | 27 |
| | 28 | +const FocusContainScope = React.unstable_createScope(); | | 29 | + | 29 | 30 | export default function FocusContain({ | 30 | 31 | children, | 31 | 32 | disabled, | 32 | | - tabScope: TabScope, | | 33 | + scopeQuery, | 33 | 34 | }: FocusContainProps): React.Node { | 34 | 35 | const scopeRef = useRef(null); | 35 | 36 | // This ensures tabbing works through the React tree (including Portals and Suspense nodes) | @@ -42,9 +43,9 @@ export default function FocusContain({ | 42 | 43 | const scope = scopeRef.current; | 43 | 44 | if (scope !== null) { | 44 | 45 | if (event.shiftKey) { | 45 | | - focusPrevious(scope, event, true); | | 46 | + focusPrevious(scopeQuery, scope, event, true); | 46 | 47 | } else { | 47 | | - focusNext(scope, event, true); | | 48 | + focusNext(scopeQuery, scope, event, true); | 48 | 49 | } | 49 | 50 | } | 50 | 51 | }, | @@ -71,7 +72,7 @@ export default function FocusContain({ | 71 | 72 | disabled !== true && | 72 | 73 | !scope.containsNode(document.activeElement) | 73 | 74 | ) { | 74 | | - const fistElem = scope.getFirstNode(); | | 75 | + const fistElem = scope.queryFirstNode(scopeQuery); | 75 | 76 | if (fistElem !== null) { | 76 | 77 | fistElem.focus(); | 77 | 78 | } | @@ -81,8 +82,8 @@ export default function FocusContain({ | 81 | 82 | ); | 82 | 83 |
| 83 | 84 | return ( | 84 | | - <TabScope ref={scopeRef} listeners={[keyboard, focusWithin]}> | | 85 | + <FocusContainScope ref={scopeRef} listeners={[keyboard, focusWithin]}> | 85 | 86 | {children} | 86 | | - </TabScope> | | 87 | + </FocusContainScope> | 87 | 88 | ); | 88 | 89 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/react-interactions/accessibility/src/FocusGroup.js +31-29Lines changed: 31 additions & 29 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | * @flow | 8 | 8 | */ | 9 | 9 |
| 10 | | -import type {ReactScope, ReactScopeMethods} from 'shared/ReactTypes'; | | 10 | +import type {ReactScopeMethods} from 'shared/ReactTypes'; | 11 | 11 | import type {KeyboardEvent} from 'react-interactions/events/keyboard'; | 12 | 12 |
| 13 | 13 | import React from 'react'; | @@ -23,14 +23,18 @@ type FocusGroupProps = {| | 23 | 23 | children: React.Node, | 24 | 24 | portrait: boolean, | 25 | 25 | wrap?: boolean, | 26 | | - tabScope?: ReactScope, | | 26 | + tabScopeQuery?: (type: string | Object, props: Object) => boolean, | 27 | 27 | allowModifiers?: boolean, | 28 | 28 | |}; | 29 | 29 |
| 30 | 30 | const {useRef} = React; | 31 | 31 |
| 32 | | -function focusGroupItem(cell: ReactScopeMethods, event: KeyboardEvent): void { | 33 | | - const firstScopedNode = cell.getFirstNode(); | | 32 | +function focusGroupItem( | | 33 | + scopeQuery: (type: string | Object, props: Object) => boolean, | | 34 | + cell: ReactScopeMethods, | | 35 | + event: KeyboardEvent, | | 36 | +): void { | | 37 | + const firstScopedNode = cell.queryFirstNode(scopeQuery); | 34 | 38 | if (firstScopedNode !== null) { | 35 | 39 | firstScopedNode.focus(); | 36 | 40 | event.preventDefault(); | @@ -91,30 +95,25 @@ function hasModifierKey(event: KeyboardEvent): boolean { | 91 | 95 | } | 92 | 96 |
| 93 | 97 | export function createFocusGroup( | 94 | | - scope: ReactScope, | | 98 | + scopeQuery: (type: string | Object, props: Object) => boolean, | 95 | 99 | ): [(FocusGroupProps) => React.Node, (FocusItemProps) => React.Node] { | 96 | | - const TableScope = React.unstable_createScope(scope.fn); | | 100 | + const TableScope = React.unstable_createScope(); | 97 | 101 |
| 98 | 102 | function Group({ | 99 | 103 | children, | 100 | 104 | portrait, | 101 | 105 | wrap, | 102 | | - tabScope: TabScope, | | 106 | + tabScopeQuery, | 103 | 107 | allowModifiers, | 104 | 108 | }: FocusGroupProps): React.Node { | 105 | | - const tabScopeRef = useRef(null); | 106 | 109 | return ( | 107 | 110 | <TableScope | 108 | 111 | type="group" | 109 | 112 | portrait={portrait} | 110 | 113 | wrap={wrap} | 111 | | - tabScopeRef={tabScopeRef} | | 114 | + tabScopeQuery={tabScopeQuery} | 112 | 115 | allowModifiers={allowModifiers}> | 113 | | - {TabScope ? ( | 114 | | - <TabScope ref={tabScopeRef}>{children}</TabScope> | 115 | | - ) : ( | 116 | | - children | 117 | | - )} | | 116 | + {children} | 118 | 117 | </TableScope> | 119 | 118 | ); | 120 | 119 | } | @@ -132,19 +131,22 @@ export function createFocusGroup( | 132 | 131 | const key = event.key; | 133 | 132 |
| 134 | 133 | if (key === 'Tab') { | 135 | | - const tabScope = getGroupProps(currentItem).tabScopeRef.current; | 136 | | - if (tabScope) { | 137 | | - const activeNode = document.activeElement; | 138 | | - const nodes = tabScope.getAllNodes(); | 139 | | - for (let i = 0; i < nodes.length; i++) { | 140 | | - const node = nodes[i]; | 141 | | - if (node !== activeNode) { | 142 | | - setElementCanTab(node, false); | 143 | | - } else { | 144 | | - setElementCanTab(node, true); | | 134 | + const tabScopeQuery = getGroupProps(currentItem).tabScopeQuery; | | 135 | + if (tabScopeQuery) { | | 136 | + const groupScope = currentItem.getParent(); | | 137 | + if (groupScope) { | | 138 | + const activeNode = document.activeElement; | | 139 | + const nodes = groupScope.queryAllNodes(tabScopeQuery); | | 140 | + for (let i = 0; i < nodes.length; i++) { | | 141 | + const node = nodes[i]; | | 142 | + if (node !== activeNode) { | | 143 | + setElementCanTab(node, false); | | 144 | + } else { | | 145 | + setElementCanTab(node, true); | | 146 | + } | 145 | 147 | } | | 148 | + return; | 146 | 149 | } | 147 | | - return; | 148 | 150 | } | 149 | 151 | event.continuePropagation(); | 150 | 152 | return; | @@ -166,7 +168,7 @@ export function createFocusGroup( | 166 | 168 | currentItem, | 167 | 169 | ); | 168 | 170 | if (previousGroupItem) { | 169 | | - focusGroupItem(previousGroupItem, event); | | 171 | + focusGroupItem(scopeQuery, previousGroupItem, event); | 170 | 172 | return; | 171 | 173 | } | 172 | 174 | } | @@ -176,7 +178,7 @@ export function createFocusGroup( | 176 | 178 | if (portrait) { | 177 | 179 | const nextGroupItem = getNextGroupItem(group, currentItem); | 178 | 180 | if (nextGroupItem) { | 179 | | - focusGroupItem(nextGroupItem, event); | | 181 | + focusGroupItem(scopeQuery, nextGroupItem, event); | 180 | 182 | return; | 181 | 183 | } | 182 | 184 | } | @@ -189,7 +191,7 @@ export function createFocusGroup( | 189 | 191 | currentItem, | 190 | 192 | ); | 191 | 193 | if (previousGroupItem) { | 192 | | - focusGroupItem(previousGroupItem, event); | | 194 | + focusGroupItem(scopeQuery, previousGroupItem, event); | 193 | 195 | return; | 194 | 196 | } | 195 | 197 | } | @@ -199,7 +201,7 @@ export function createFocusGroup( | 199 | 201 | if (!portrait) { | 200 | 202 | const nextGroupItem = getNextGroupItem(group, currentItem); | 201 | 203 | if (nextGroupItem) { | 202 | | - focusGroupItem(nextGroupItem, event); | | 204 | + focusGroupItem(scopeQuery, nextGroupItem, event); | 203 | 205 | return; | 204 | 206 | } | 205 | 207 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-interactions/accessibility/src/FocusManager.js +12-5Lines changed: 12 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,9 +12,14 @@ import type {KeyboardEvent} from 'react-interactions/events/keyboard'; | 12 | 12 |
| 13 | 13 | import getTabbableNodes from './shared/getTabbableNodes'; | 14 | 14 |
| 15 | | -export function focusFirst(scope: ReactScopeMethods): void { | 16 | | - const [, firstTabbableElem] = getTabbableNodes(scope); | 17 | | - focusElem(firstTabbableElem); | | 15 | +export function focusFirst( | | 16 | + scopeQuery: (type: string | Object, props: Object) => boolean, | | 17 | + scope: ReactScopeMethods, | | 18 | +): void { | | 19 | + const firstNode = scope.queryFirstNode(scopeQuery); | | 20 | + if (firstNode) { | | 21 | + focusElem(firstNode); | | 22 | + } | 18 | 23 | } | 19 | 24 |
| 20 | 25 | function focusElem(elem: null | HTMLElement): void { | @@ -24,6 +29,7 @@ function focusElem(elem: null | HTMLElement): void { | 24 | 29 | } | 25 | 30 |
| 26 | 31 | export function focusNext( | | 32 | + scopeQuery: (type: string | Object, props: Object) => boolean, | 27 | 33 | scope: ReactScopeMethods, | 28 | 34 | event?: KeyboardEvent, | 29 | 35 | contain?: boolean, | @@ -34,7 +40,7 @@ export function focusNext( | 34 | 40 | lastTabbableElem, | 35 | 41 | currentIndex, | 36 | 42 | focusedElement, | 37 | | - ] = getTabbableNodes(scope); | | 43 | + ] = getTabbableNodes(scopeQuery, scope); | 38 | 44 |
| 39 | 45 | if (focusedElement === null) { | 40 | 46 | if (event) { | @@ -58,6 +64,7 @@ export function focusNext( | 58 | 64 | } | 59 | 65 |
| 60 | 66 | export function focusPrevious( | | 67 | + scopeQuery: (type: string | Object, props: Object) => boolean, | 61 | 68 | scope: ReactScopeMethods, | 62 | 69 | event?: KeyboardEvent, | 63 | 70 | contain?: boolean, | @@ -68,7 +75,7 @@ export function focusPrevious( | 68 | 75 | lastTabbableElem, | 69 | 76 | currentIndex, | 70 | 77 | focusedElement, | 71 | | - ] = getTabbableNodes(scope); | | 78 | + ] = getTabbableNodes(scopeQuery, scope); | 72 | 79 |
| 73 | 80 | if (focusedElement === null) { | 74 | 81 | if (event) { | |
|