-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a useConstrainedTabbing hook (#27544)
- Loading branch information
1 parent
26db39a
commit cc3e478
Showing
9 changed files
with
155 additions
and
60 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
71 changes: 11 additions & 60 deletions
71
packages/components/src/higher-order/with-constrained-tabbing/index.js
This file contains 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 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 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
33 changes: 33 additions & 0 deletions
33
packages/compose/src/hooks/use-constrained-tabbing/README.md
This file contains 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,33 @@ | ||
`useConstrainedTabbing` | ||
====================== | ||
|
||
In Dialogs/modals, the tabbing must be constrained to the content of the wrapper element. To achieve this behavior you can use the `useConstrainedTabbing` hook. | ||
|
||
## Return Object Properties | ||
|
||
### `ref` | ||
|
||
- Type: `Function` | ||
|
||
A function reference that must be passed to the DOM element where constrained tabbing should be enabled. | ||
|
||
## Usage | ||
The following example allows us to drag & drop a red square around the entire viewport. | ||
|
||
```jsx | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useConstrainedTabbing } from '@wordpress/compose'; | ||
|
||
|
||
const ConstrainedTabbingExample = () => { | ||
const ref = useConstrainedTabbing() | ||
return ( | ||
<div ref={ ref }> | ||
<Button /> | ||
<Button /> | ||
</div> | ||
); | ||
}; | ||
``` |
66 changes: 66 additions & 0 deletions
66
packages/compose/src/hooks/use-constrained-tabbing/index.js
This file contains 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,66 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useCallback } from '@wordpress/element'; | ||
import { TAB } from '@wordpress/keycodes'; | ||
import { focus } from '@wordpress/dom'; | ||
|
||
/** | ||
* In Dialogs/modals, the tabbing must be constrained to the content of | ||
* the wrapper element. This hook adds the behavior to the returned ref. | ||
* | ||
* @return {Function} Element Ref. | ||
* | ||
* @example | ||
* ```js | ||
* import { useConstrainedTabbing } from '@wordpress/compose'; | ||
* | ||
* const ConstrainedTabbingExample = () => { | ||
* const constrainedTabbingRef = useConstrainedTabbing() | ||
* return ( | ||
* <div ref={ constrainedTabbingRef }> | ||
* <Button /> | ||
* <Button /> | ||
* </div> | ||
* ); | ||
* } | ||
* ``` | ||
*/ | ||
function useConstrainedTabbing() { | ||
const ref = useCallback( ( node ) => { | ||
if ( ! node ) { | ||
return; | ||
} | ||
node.addEventListener( 'keydown', ( event ) => { | ||
if ( event.keyCode !== TAB ) { | ||
return; | ||
} | ||
|
||
const tabbables = focus.tabbable.find( node ); | ||
if ( ! tabbables.length ) { | ||
return; | ||
} | ||
const firstTabbable = tabbables[ 0 ]; | ||
const lastTabbable = tabbables[ tabbables.length - 1 ]; | ||
|
||
if ( event.shiftKey && event.target === firstTabbable ) { | ||
event.preventDefault(); | ||
lastTabbable.focus(); | ||
} else if ( ! event.shiftKey && event.target === lastTabbable ) { | ||
event.preventDefault(); | ||
firstTabbable.focus(); | ||
/* | ||
* When pressing Tab and none of the tabbables has focus, the keydown | ||
* event happens on the wrapper div: move focus on the first tabbable. | ||
*/ | ||
} else if ( ! tabbables.includes( event.target ) ) { | ||
event.preventDefault(); | ||
firstTabbable.focus(); | ||
} | ||
} ); | ||
}, [] ); | ||
|
||
return ref; | ||
} | ||
|
||
export default useConstrainedTabbing; |
14 changes: 14 additions & 0 deletions
14
packages/compose/src/hooks/use-constrained-tabbing/index.native.js
This file contains 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,14 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef } from '@wordpress/element'; | ||
|
||
function useConstrainedTabbing() { | ||
const ref = useRef(); | ||
|
||
// Do nothing on mobile as tabbing is not a mobile behavior. | ||
|
||
return ref; | ||
} | ||
|
||
export default useConstrainedTabbing; |
This file contains 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 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