Skip to content

Commit

Permalink
Can't type space if a contenteditable element is inside button/summar…
Browse files Browse the repository at this point in the history
…y in Firefox

commit
  • Loading branch information
t-hamano committed May 11, 2023
1 parent 8346327 commit c63efaf
Show file tree
Hide file tree
Showing 2 changed files with 24 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,25 @@ 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;
}

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 );
};
}, [] );
}

0 comments on commit c63efaf

Please sign in to comment.