Skip to content

Commit

Permalink
Fix: Can't type space if RichText component is inside button/summar…
Browse files Browse the repository at this point in the history
…y in Firefox (#50540)

* Can't type space if a contenteditable element is inside button/summary in Firefox

commit

* Continue event processing only if within a specific element
  • Loading branch information
t-hamano authored May 26, 2023
1 parent fbc67a1 commit ce1ef42
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ function RichTextWrapper(
disableLineBreaks,
onSplitAtEnd,
} ),
useFirefoxCompat(),
useFirefoxCompat( { value, onChange } ),
anchorRef,
] ) }
contentEditable={ true }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
/**
* WordPress dependencies
*/
import { useRef } from '@wordpress/element';
import { useRefEffect } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { SPACE } from '@wordpress/keycodes';
import { insert } from '@wordpress/rich-text';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';

export function useFirefoxCompat() {
export function useFirefoxCompat( props ) {
const propsRef = useRef( props );
propsRef.current = props;

const { isMultiSelecting } = useSelect( blockEditorStore );
return useRefEffect( ( element ) => {
function onFocus() {
Expand All @@ -31,9 +37,29 @@ export function useFirefoxCompat() {
}
}

// If a contenteditable element is inside a button/summary element,
// it is not possible to type a space in Firefox. Therefore, cancel
// the default event and insert a space explicitly.
// See: https://bugzilla.mozilla.org/show_bug.cgi?id=1822860
function onKeyDown( event ) {
if ( event.keyCode !== SPACE ) {
return;
}

if ( element.closest( 'button, summary' ) === null ) {
return;
}

const { value, onChange } = propsRef.current;
onChange( insert( value, ' ' ) );
event.preventDefault();
}

element.addEventListener( 'focus', onFocus );
element.addEventListener( 'keydown', onKeyDown );
return () => {
element.removeEventListener( 'focus', onFocus );
element.removeEventListener( 'keydown', onKeyDown );
};
}, [] );
}

1 comment on commit ce1ef42

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in ce1ef42.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5087121480
📝 Reported issues:

Please sign in to comment.