-
Notifications
You must be signed in to change notification settings - Fork 48.9k
[react-interactions] Repurpose React a11y modules #16997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
27 changes: 27 additions & 0 deletions
27
packages/react-interactions/accessibility/docs/FocusContain.md
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,27 @@ | ||
# FocusContain | ||
|
||
`FocusContain` is a component that contains user-focusability to only that | ||
of the children of the component. This means focus control will not escape | ||
unless the componoent is disabled (using the `disabled` prop) or unmounted. | ||
Additionally, `FocusContain` can contain tab focus when passed a `ReactScope` | ||
using the `tabFocus` prop. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import FocusContain from 'react-interactions/accessibility/focus-contain'; | ||
import TabbableScope from 'react-interactions/accessibility/tabbable-scope'; | ||
|
||
function MyDialog(props) { | ||
return ( | ||
<FocusContain tabScope={TabbableScope} disabled={false}> | ||
<div> | ||
<h2>{props.title}<h2> | ||
<p>{props.text}</p> | ||
<Button onPress={...}>Accept</Button> | ||
<Button onPress={...}>Close</Button> | ||
</div> | ||
</FocusContain> | ||
) | ||
} | ||
``` |
60 changes: 0 additions & 60 deletions
60
packages/react-interactions/accessibility/docs/FocusControl.md
This file was deleted.
Oops, something went wrong.
74 changes: 47 additions & 27 deletions
74
packages/react-interactions/accessibility/docs/FocusManager.md
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 |
---|---|---|
@@ -1,39 +1,59 @@ | ||
# FocusManager | ||
|
||
`FocusManager` is a component that is designed to provide basic focus management | ||
control. These are the various props that `FocusManager` accepts: | ||
`FocusManager` is a module that exports a selection of helpful utility functions to be used | ||
in conjunction with the `ref` from a React Scope, such as `TabbableScope`. | ||
|
||
## Usage | ||
## Example | ||
|
||
```jsx | ||
function MyDialog(props) { | ||
import { | ||
focusFirst, | ||
focusNext, | ||
focusPrevious, | ||
getNextScope, | ||
getPreviousScope, | ||
} from 'react-interactions/accessibility/focus-manager'; | ||
|
||
function KeyboardFocusMover(props) { | ||
const scopeRef = useRef(null); | ||
|
||
useEffect(() => { | ||
const scope = scopeRef.current; | ||
|
||
if (scope) { | ||
// Focus the first tabbable DOM node in my children | ||
focusFirst(scope); | ||
// Then focus the next chilkd | ||
focusNext(scope); | ||
} | ||
}); | ||
|
||
return ( | ||
<FocusManager containFocus={true} autoFocus={true}> | ||
<div> | ||
<h2>{props.title}<h2> | ||
<p>{props.text}</p> | ||
<Button onPress={...}>Accept</Button> | ||
<Button onPress={...}>Close</Button> | ||
</div> | ||
</FocusManager> | ||
) | ||
<TabbableScope ref={scopeRef}> | ||
{props.children} | ||
</TabbableScope> | ||
); | ||
} | ||
``` | ||
|
||
### `scope` | ||
`FocusManager` accepts a custom `ReactScope`. If a custom one is not supplied, `FocusManager` | ||
will default to using `TabbableScope`. | ||
## FocusManager API | ||
|
||
### `focusFirst` | ||
|
||
Focus the first node that matches the given scope. | ||
|
||
### `focusNext` | ||
|
||
Focus the next sequential node that matches the given scope. | ||
|
||
### `focusPrevious` | ||
|
||
Focus the previous sequential node that matches the given scope. | ||
|
||
### `getNextScope` | ||
|
||
### `autoFocus` | ||
When enabled, the first host node that matches the `FocusManager` scope will be focused | ||
upon the `FocusManager` mounting. | ||
Focus the first node that matches the next sibling scope from the given scope. | ||
|
||
### `restoreFocus` | ||
When enabled, the previous host node that was focused as `FocusManager` is mounted, | ||
has its focus restored upon `FocusManager` unmounting. | ||
### `getPreviousScope` | ||
|
||
### `containFocus` | ||
This contains the user focus to only that of `FocusManager`s sub-tree. Tabbing or | ||
interacting with nodes outside the sub-tree will restore focus back into the `FocusManager`. | ||
This is useful for modals, dialogs, dropdowns and other UI elements that require | ||
a form of user-focus control that is similar to the `inert` property on the web. | ||
Focus the first node that matches the previous sibling scope from the given scope. |
4 changes: 3 additions & 1 deletion
4
packages/react-interactions/accessibility/docs/TabbableScope.md
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 |
---|---|---|
|
@@ -9,4 +9,4 @@ | |
|
||
'use strict'; | ||
|
||
module.exports = require('./src/FocusControl'); | ||
module.exports = require('./src/FocusContain'); |
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 |
---|---|---|
|
@@ -9,4 +9,4 @@ | |
|
||
'use strict'; | ||
|
||
module.exports = require('./src/FocusList'); | ||
module.exports = require('./src/FocusGroup'); |
84 changes: 84 additions & 0 deletions
84
packages/react-interactions/accessibility/src/FocusContain.js
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,84 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {ReactScope} from 'shared/ReactTypes'; | ||
import type {KeyboardEvent} from 'react-interactions/events/keyboard'; | ||
|
||
import React from 'react'; | ||
import {useFocusWithin} from 'react-interactions/events/focus'; | ||
import {useKeyboard} from 'react-interactions/events/keyboard'; | ||
import { | ||
focusPrevious, | ||
focusNext, | ||
} from 'react-interactions/accessibility/focus-manager'; | ||
|
||
type FocusContainProps = {| | ||
children: React.Node, | ||
disabled?: boolean, | ||
tabScope: ReactScope, | ||
|}; | ||
|
||
const {useLayoutEffect, useRef} = React; | ||
|
||
export default function FocusContain({ | ||
children, | ||
disabled, | ||
tabScope: TabScope, | ||
}: FocusContainProps) { | ||
const scopeRef = useRef(null); | ||
// This ensures tabbing works through the React tree (including Portals and Suspense nodes) | ||
const keyboard = useKeyboard({ | ||
onKeyDown(event: KeyboardEvent): void { | ||
if (disabled || event.key !== 'Tab') { | ||
event.continuePropagation(); | ||
return; | ||
} | ||
const scope = scopeRef.current; | ||
if (scope !== null) { | ||
if (event.shiftKey) { | ||
focusPrevious(scope, event, true); | ||
} else { | ||
focusNext(scope, event, true); | ||
} | ||
} | ||
}, | ||
}); | ||
const focusWithin = useFocusWithin({ | ||
onBlurWithin: function(event) { | ||
if (disabled) { | ||
event.continuePropagation(); | ||
return; | ||
} | ||
const lastNode = event.target; | ||
if (lastNode) { | ||
requestAnimationFrame(() => { | ||
(lastNode: any).focus(); | ||
}); | ||
} | ||
}, | ||
}); | ||
useLayoutEffect( | ||
() => { | ||
const scope = scopeRef.current; | ||
if (scope && !disabled) { | ||
const elems = scope.getScopedNodes(); | ||
if (elems && elems.indexOf(document.activeElement) === -1) { | ||
elems[0].focus(); | ||
} | ||
} | ||
}, | ||
[disabled], | ||
); | ||
|
||
return ( | ||
<TabScope ref={scopeRef} listeners={[keyboard, focusWithin]}> | ||
{children} | ||
</TabScope> | ||
); | ||
} |
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.