Skip to content

Commit 0496f29

Browse files
montezumekodiakhq[bot]
authored andcommitted
fix(rich-text-input): dropdown focus bug (#1145)
* fix(rich-text-input): dropdown focus bug * chore: force build * chore: update comment
1 parent a695e6e commit 0496f29

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/components/inputs/rich-text-input/rich-text-input.visualspec.js

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { getDocument, queries, wait } from 'pptr-testing-library';
44
const { getByLabelText, getByText } = queries;
55

66
describe('RichTextInput', () => {
7+
const blur = async element => {
8+
// eslint-disable-next-line no-shadow
9+
await page.evaluate(element => {
10+
element.blur();
11+
}, element);
12+
};
713
const selectAllText = async input => {
814
// eslint-disable-next-line no-shadow
915
await page.evaluate(input => {
@@ -244,6 +250,9 @@ describe('RichTextInput', () => {
244250

245251
// next, open the Style menu
246252

253+
// blur input first to test that editor focus works correctly
254+
await blur(input);
255+
247256
const styleMenuButton = await getByLabelText(doc, 'Style');
248257
await styleMenuButton.click();
249258

@@ -285,6 +294,9 @@ describe('RichTextInput', () => {
285294

286295
// now change back to paragraph (the default)
287296

297+
// blur input first to test that editor focus works correctly
298+
await blur(input);
299+
288300
// open style menu again
289301
await styleMenuButton.click();
290302

src/components/internals/rich-text-body/rich-text-body.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,20 @@ const RichTextEditorBody = React.forwardRef((props, ref) => {
252252
);
253253
}
254254

255+
const { focus } = props.editor;
256+
257+
const onToolbarClick = React.useCallback(() => {
258+
// if clicking on a dropdown, we need to both focus the RichTextInput
259+
// and open the dropdown.
260+
// if we don't use setTimeout, our dropdown opening will be hijacked by the
261+
// editors rerendering. (a so called `race condition`)
262+
// the reason we keep this here, is because any onClick from our dropdown or our mark buttons
263+
// will propogate to here.
264+
if (!props.editor.value.selection.isFocused) {
265+
setTimeout(focus, 0);
266+
}
267+
}, [props.editor.value.selection.isFocused, focus]);
268+
255269
return (
256270
<Container
257271
css={props.styles.container}
@@ -260,13 +274,7 @@ const RichTextEditorBody = React.forwardRef((props, ref) => {
260274
isReadOnly={props.isReadOnly}
261275
isDisabled={props.isDisabled}
262276
>
263-
<Toolbar
264-
onClick={() => {
265-
if (!props.editor.value.selection.isFocused) {
266-
props.editor.focus();
267-
}
268-
}}
269-
>
277+
<Toolbar onClick={onToolbarClick}>
270278
<ToolbarMainControls>
271279
<Dropdown
272280
label={intl.formatMessage(messages.styleDropdownLabel)}

0 commit comments

Comments
 (0)