Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit 3ba907b

Browse files
gaearonfacebook-github-bot
authored andcommitted
Add a preventScroll prop
Summary: Browser scrolls to native editors when they get inserted into the DOM. Draft replicates that behavior. However, that behavior is occasionally not what we want at all. As a result of this, we sometimes have "scroll wars" where different parts of the product code try to restore scroll after others change it. This is a source of bugs. This adds a first-class way to disable this behavior. I named the prop a bit generically so that if I find more such cases, I can disable them too. Reviewed By: steveluscher Differential Revision: D19568181 fbshipit-source-id: ed4db22abebbdae1b11d84e1bf6d7772a840b3da
1 parent 8c50042 commit 3ba907b

File tree

4 files changed

+13
-0
lines changed

4 files changed

+13
-0
lines changed

src/component/base/DraftEditor.react.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ class DraftEditor extends React.Component<DraftEditorProps, State> {
328328
customStyleFn,
329329
customStyleMap,
330330
editorState,
331+
preventScroll,
331332
readOnly,
332333
textAlignment,
333334
textDirectionality,
@@ -369,6 +370,7 @@ class DraftEditor extends React.Component<DraftEditorProps, State> {
369370
customStyleFn,
370371
editorKey: this._editorKey,
371372
editorState,
373+
preventScroll,
372374
textDirectionality,
373375
};
374376

src/component/base/DraftEditorProps.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ export type DraftEditorProps = {
7676
// or null if no command should be invoked.
7777
keyBindingFn: (e: SyntheticKeyboardEvent<>) => ?string,
7878

79+
// Set whether the editor should prevent scrolling into view on mount
80+
// if it happens offscreen. By default, `false` to match the native behavior.
81+
preventScroll?: boolean,
82+
7983
// Set whether the `DraftEditor` component should be editable. Useful for
8084
// temporarily disabling edit behavior or allowing `DraftEditor` rendering
8185
// to be used for consumption purposes.

src/component/contents/DraftEditorBlock.react.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type Props = {
4949
direction: BidiDirection,
5050
forceSelection: boolean,
5151
offsetKey: string,
52+
preventScroll?: boolean,
5253
selection: SelectionState,
5354
startIndent?: boolean,
5455
tree: List<any>,
@@ -96,6 +97,9 @@ class DraftEditorBlock extends React.Component<Props> {
9697
* scroll parent.
9798
*/
9899
componentDidMount(): void {
100+
if (this.props.preventScroll) {
101+
return;
102+
}
99103
const selection = this.props.selection;
100104
const endKey = selection.getEndKey();
101105
if (!selection.getHasFocus() || endKey !== this.props.block.getKey()) {

src/component/contents/DraftEditorContents-core.react.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Props = {
3636
customStyleMap?: Object,
3737
editorKey?: string,
3838
editorState: EditorState,
39+
preventScroll?: boolean,
3940
textDirectionality?: BidiDirection,
4041
};
4142

@@ -132,6 +133,7 @@ class DraftEditorContents extends React.Component<Props> {
132133
customStyleFn,
133134
editorState,
134135
editorKey,
136+
preventScroll,
135137
textDirectionality,
136138
} = this.props;
137139

@@ -175,6 +177,7 @@ class DraftEditorContents extends React.Component<Props> {
175177
direction,
176178
forceSelection,
177179
offsetKey,
180+
preventScroll,
178181
selection,
179182
tree: editorState.getBlockTree(key),
180183
};

0 commit comments

Comments
 (0)