-
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 useDialog hook and replace the duplicated PopoverWrapper (#27643)
- Loading branch information
1 parent
7cc1ba1
commit f9c2fa3
Showing
15 changed files
with
250 additions
and
339 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.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
`useDialog` | ||
=========== | ||
|
||
React hook to be used on a dialog wrapper to enable the following behaviors: | ||
|
||
- constrained tabbing. | ||
- focus on mount. | ||
- return focus on unmount. | ||
- focus outside. | ||
|
||
## Returned value | ||
|
||
The hooks returns an array composed of the two following values: | ||
|
||
### `ref` | ||
|
||
- Type: `Object|Function` | ||
|
||
A React ref that must be passed to the DOM element where the behavior should be attached. | ||
|
||
### `props` | ||
|
||
- Type: `Object` | ||
|
||
Extra props to apply to the wrapper. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { __experimentalUseDialog as useDialog } from '@wordpress/compose'; | ||
|
||
const MyDialog = () => { | ||
const [ ref, extraProps ] = useDialog( { | ||
onClose: () => console.log('do something to close the dialog') | ||
} ); | ||
|
||
return ( | ||
<div ref={ ref } {...extraProps}> | ||
<Button /> | ||
<Button /> | ||
</div> | ||
); | ||
}; | ||
``` |
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,68 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import mergeRefs from 'react-merge-refs'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef, useCallback, useEffect } from '@wordpress/element'; | ||
import { ESCAPE } from '@wordpress/keycodes'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useConstrainedTabbing from '../use-constrained-tabbing'; | ||
import useFocusOnMount from '../use-focus-on-mount'; | ||
import useFocusReturn from '../use-focus-return'; | ||
import useFocusOutside from '../use-focus-outside'; | ||
|
||
/** | ||
* Returns a ref and props to apply to a dialog wrapper to enable the following behaviors: | ||
* - constrained tabbing. | ||
* - focus on mount. | ||
* - return focus on unmount. | ||
* - focus outside. | ||
* | ||
* @param {Object} options Dialog Options. | ||
*/ | ||
function useDialog( options ) { | ||
const onClose = useRef(); | ||
useEffect( () => { | ||
onClose.current = options.onClose; | ||
}, [ options.onClose ] ); | ||
const constrainedTabbingRef = useConstrainedTabbing(); | ||
const focusOnMountRef = useFocusOnMount(); | ||
const focusReturnRef = useFocusReturn(); | ||
const focusOutsideProps = useFocusOutside( options.onClose ); | ||
const closeOnEscapeRef = useCallback( ( node ) => { | ||
if ( ! node ) { | ||
return; | ||
} | ||
|
||
node.addEventListener( 'keydown', ( event ) => { | ||
// Close on escape | ||
if ( event.keyCode === ESCAPE && onClose.current ) { | ||
event.stopPropagation(); | ||
onClose.current(); | ||
} | ||
} ); | ||
|
||
node.addEventListener( 'mousedown', ( event ) => { | ||
// I'm not really certain what this is for but it matches the previous behavior. | ||
event.stopPropagation(); | ||
} ); | ||
}, [] ); | ||
|
||
return [ | ||
mergeRefs( [ | ||
constrainedTabbingRef, | ||
focusOnMountRef, | ||
focusReturnRef, | ||
closeOnEscapeRef, | ||
] ), | ||
focusOutsideProps, | ||
]; | ||
} | ||
|
||
export default useDialog; |
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
Oops, something went wrong.