Skip to content

Commit

Permalink
fix: hide placeholder when composing (#4352)
Browse files Browse the repository at this point in the history
  • Loading branch information
hueyhe authored Aug 5, 2021
1 parent bde6e80 commit 4b373dc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-moles-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

Do not display placeholder when composing
10 changes: 8 additions & 2 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useMemo, useCallback } from 'react'
import React, { useEffect, useRef, useMemo, useCallback, useState } from 'react'
import {
Editor,
Element,
Expand Down Expand Up @@ -112,6 +112,8 @@ export const Editable = (props: EditableProps) => {
...attributes
} = props
const editor = useSlate()
// Rerender editor when composition status changed
const [isComposing, setIsComposing] = useState(false)
const ref = useRef<HTMLDivElement>(null)

// Update internal state on each render.
Expand Down Expand Up @@ -370,6 +372,7 @@ export const Editable = (props: EditableProps) => {
// then we will abort because we're still composing and the selection
// won't be updated properly.
// https://www.w3.org/TR/input-events-2/
state.isComposing && setIsComposing(false)
state.isComposing = false
}

Expand Down Expand Up @@ -481,7 +484,8 @@ export const Editable = (props: EditableProps) => {
placeholder &&
editor.children.length === 1 &&
Array.from(Node.texts(editor)).length === 1 &&
Node.string(editor) === ''
Node.string(editor) === '' &&
!isComposing
) {
const start = Editor.start(editor, [])
decorations.push({
Expand Down Expand Up @@ -648,6 +652,7 @@ export const Editable = (props: EditableProps) => {
hasEditableTarget(editor, event.target) &&
!isEventHandled(event, attributes.onCompositionEnd)
) {
state.isComposing && setIsComposing(false)
state.isComposing = false

// COMPAT: In Chrome, `beforeinput` events for compositions
Expand All @@ -667,6 +672,7 @@ export const Editable = (props: EditableProps) => {
hasEditableTarget(editor, event.target) &&
!isEventHandled(event, attributes.onCompositionUpdate)
) {
!state.isComposing && setIsComposing(true)
state.isComposing = true
}
},
Expand Down

0 comments on commit 4b373dc

Please sign in to comment.