-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor click redirect (and avoid div) (#27253)
* Refactor click redirect(and avoid div) * Remove padding if there are meta boxes * Polish * Use mouse down * Add comment
- Loading branch information
Showing
8 changed files
with
71 additions
and
56 deletions.
There are no files selected for viewing
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
50 changes: 50 additions & 0 deletions
50
packages/block-editor/src/components/use-canvas-click-redirect/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,50 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { overEvery, findLast } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect } from '@wordpress/element'; | ||
import { focus, isTextField, placeCaretAtHorizontalEdge } from '@wordpress/dom'; | ||
|
||
/** | ||
* Given an element, returns true if the element is a tabbable text field, or | ||
* false otherwise. | ||
* | ||
* @param {Element} element Element to test. | ||
* | ||
* @return {boolean} Whether element is a tabbable text field. | ||
*/ | ||
const isTabbableTextField = overEvery( [ | ||
isTextField, | ||
focus.tabbable.isTabbableIndex, | ||
] ); | ||
|
||
export function useCanvasClickRedirect( ref ) { | ||
useEffect( () => { | ||
function onMouseDown( event ) { | ||
// Only handle clicks on the canvas, not the content. | ||
if ( event.target !== ref.current ) { | ||
return; | ||
} | ||
|
||
const focusableNodes = focus.focusable.find( ref.current ); | ||
const target = findLast( focusableNodes, isTabbableTextField ); | ||
|
||
if ( ! target ) { | ||
return; | ||
} | ||
|
||
placeCaretAtHorizontalEdge( target, true ); | ||
event.preventDefault(); | ||
} | ||
|
||
ref.current.addEventListener( 'mousedown', onMouseDown ); | ||
|
||
return () => { | ||
ref.current.addEventListener( 'mousedown', onMouseDown ); | ||
}; | ||
}, [] ); | ||
} |
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 was deleted.
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
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