forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Safari: prevent focus capturing caused by flex display (WordPress#66402)
Co-authored-by: ellatrix <ellatrix@git.wordpress.org> Co-authored-by: mcsf <mcsf@git.wordpress.org> Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
- Loading branch information
1 parent
d49bdd8
commit 0a1b5a9
Showing
4 changed files
with
49 additions
and
18 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
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
44 changes: 44 additions & 0 deletions
44
packages/rich-text/src/component/event-listeners/prevent-focus-capture.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,44 @@ | ||
/** | ||
* Prevents focus from being captured by the element when clicking _outside_ | ||
* around the element. This may happen when the parent element is flex. | ||
* @see https://github.com/WordPress/gutenberg/pull/65857 | ||
* @see https://github.com/WordPress/gutenberg/pull/66402 | ||
*/ | ||
export function preventFocusCapture() { | ||
return ( element ) => { | ||
const { ownerDocument } = element; | ||
const { defaultView } = ownerDocument; | ||
|
||
let value = null; | ||
|
||
function onPointerDown( event ) { | ||
// Abort if the event is default prevented, we will not get a pointer up event. | ||
if ( event.defaultPrevented ) { | ||
return; | ||
} | ||
if ( event.target === element ) { | ||
return; | ||
} | ||
if ( ! event.target.contains( element ) ) { | ||
return; | ||
} | ||
value = element.getAttribute( 'contenteditable' ); | ||
element.setAttribute( 'contenteditable', 'false' ); | ||
defaultView.getSelection().removeAllRanges(); | ||
} | ||
|
||
function onPointerUp() { | ||
if ( value !== null ) { | ||
element.setAttribute( 'contenteditable', value ); | ||
value = null; | ||
} | ||
} | ||
|
||
defaultView.addEventListener( 'pointerdown', onPointerDown ); | ||
defaultView.addEventListener( 'pointerup', onPointerUp ); | ||
return () => { | ||
defaultView.removeEventListener( 'pointerdown', onPointerDown ); | ||
defaultView.removeEventListener( 'pointerup', onPointerUp ); | ||
}; | ||
}; | ||
} |
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