1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-19 14:44:39 +01:00

Fix scroll into view to scroll parent scrollables (#4369)

This commit is contained in:
Sunny Hirai 2021-07-09 15:12:13 -07:00 committed by GitHub
parent 46c8871c9c
commit c217dbb5b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
'slate-react': patch
---
Scroll when inserting new text will now scroll parent scrollables

View File

@ -216,7 +216,6 @@ export const Editable = (props: EditableProps) => {
)
scrollIntoView(leafEl, {
scrollMode: 'if-needed',
boundary: el,
})
// @ts-ignore
delete leafEl.getBoundingClientRect

View File

@ -0,0 +1,70 @@
import React, { useState, useMemo } from 'react'
import { createEditor, Descendant } from 'slate'
import { Slate, Editable, withReact } from 'slate-react'
import { withHistory } from 'slate-history'
import { css } from 'emotion'
import range from 'lodash/range'
/**
* This is an example we can use to test the scrollIntoView functionality in
* `Editable`. Keeping it here for now as we may need it to make sure it is
* working properly after adding it.
*
* If all is good, we can remove this example.
*
* Note:
* The example needs to be added to `[example].tsx` before it can be used.
*/
const ScrollIntoViewExample = () => {
return (
<div
className={css`
height: 320px;
overflow-y: scroll;
`}
>
<div
className={css`
height: 160px;
background: #e0e0e0;
`}
/>
<div
className={css`
height: 320px;
overflow-y: scroll;
`}
>
<PlainTextEditor />
</div>
<div
className={css`
height: 160px;
background: #e0e0e0;
`}
/>
</div>
)
}
const PlainTextEditor = () => {
const [value, setValue] = useState<Descendant[]>(initialValue)
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
return (
<Slate editor={editor} value={value} onChange={value => setValue(value)}>
<Editable placeholder="Enter some plain text..." />
</Slate>
)
}
const initialValue: Descendant[] = range(5).map(() => ({
type: 'paragraph',
children: [
{
text: `There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.`,
},
],
}))
export default ScrollIntoViewExample