mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-07-31 20:40:19 +02:00
EXAMPLES: fix focus bug for hovering toolbar example (#4866)
* fix focus bug for hovering toolbar example * added test
This commit is contained in:
@@ -18,4 +18,21 @@ describe('hovering toolbar example', () => {
|
|||||||
.find('span.material-icons')
|
.find('span.material-icons')
|
||||||
.should('have.length', 3)
|
.should('have.length', 3)
|
||||||
})
|
})
|
||||||
|
it('hovering toolbar disappears', () => {
|
||||||
|
cy.get('span[data-slate-string="true"]')
|
||||||
|
.eq(0)
|
||||||
|
.type(`{selectall}`)
|
||||||
|
.get('div')
|
||||||
|
.eq(12)
|
||||||
|
.should('exist')
|
||||||
|
.get('span[data-slate-string="true"]')
|
||||||
|
.eq(0)
|
||||||
|
.type(`{selectall}`)
|
||||||
|
.get('div')
|
||||||
|
.eq(0)
|
||||||
|
.click({ force: true })
|
||||||
|
.get('div')
|
||||||
|
.eq(12)
|
||||||
|
.should('have.css', 'opacity', '0')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@@ -1,11 +1,10 @@
|
|||||||
import React, { useState, useMemo, useRef, useEffect } from 'react'
|
import React, { useState, useMemo, useRef, useEffect } from 'react'
|
||||||
import { Slate, Editable, ReactEditor, withReact, useSlate } from 'slate-react'
|
import { Slate, Editable, withReact, useSlate, useFocused } from 'slate-react'
|
||||||
import {
|
import {
|
||||||
Editor,
|
Editor,
|
||||||
Transforms,
|
Transforms,
|
||||||
Text,
|
Text,
|
||||||
createEditor,
|
createEditor,
|
||||||
Element,
|
|
||||||
Descendant,
|
Descendant,
|
||||||
Range,
|
Range,
|
||||||
} from 'slate'
|
} from 'slate'
|
||||||
@@ -76,6 +75,7 @@ const Leaf = ({ attributes, children, leaf }) => {
|
|||||||
const HoveringToolbar = () => {
|
const HoveringToolbar = () => {
|
||||||
const ref = useRef<HTMLDivElement | null>()
|
const ref = useRef<HTMLDivElement | null>()
|
||||||
const editor = useSlate()
|
const editor = useSlate()
|
||||||
|
const inFocus = useFocused()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const el = ref.current
|
const el = ref.current
|
||||||
@@ -87,7 +87,7 @@ const HoveringToolbar = () => {
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
!selection ||
|
!selection ||
|
||||||
!ReactEditor.isFocused(editor) ||
|
!inFocus ||
|
||||||
Range.isCollapsed(selection) ||
|
Range.isCollapsed(selection) ||
|
||||||
Editor.string(editor, selection) === ''
|
Editor.string(editor, selection) === ''
|
||||||
) {
|
) {
|
||||||
@@ -122,6 +122,10 @@ const HoveringToolbar = () => {
|
|||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
transition: opacity 0.75s;
|
transition: opacity 0.75s;
|
||||||
`}
|
`}
|
||||||
|
onMouseDown={e => {
|
||||||
|
// prevent toolbar from taking focus away from editor
|
||||||
|
e.preventDefault()
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<FormatButton format="bold" icon="format_bold" />
|
<FormatButton format="bold" icon="format_bold" />
|
||||||
<FormatButton format="italic" icon="format_italic" />
|
<FormatButton format="italic" icon="format_italic" />
|
||||||
@@ -137,10 +141,7 @@ const FormatButton = ({ format, icon }) => {
|
|||||||
<Button
|
<Button
|
||||||
reversed
|
reversed
|
||||||
active={isFormatActive(editor, format)}
|
active={isFormatActive(editor, format)}
|
||||||
onMouseDown={event => {
|
onClick={() => toggleFormat(editor, format)}
|
||||||
event.preventDefault()
|
|
||||||
toggleFormat(editor, format)
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
<Icon>{icon}</Icon>
|
<Icon>{icon}</Icon>
|
||||||
</Button>
|
</Button>
|
||||||
|
Reference in New Issue
Block a user