React-draft-wysiwyg: Hide ToolBar - Performance

Created on 19 Sep 2017  路  19Comments  路  Source: jpuri/react-draft-wysiwyg

Hi

Just some thoughts about the Hide toolbar feature. Instead of visually hide the toolbar through CSS, Do you think you can completely stop rendering the Toolbar if HideToolbar props is passed. This can improve the performance of rendering.

Great library 馃挴

Most helpful comment

i figured it out with the idea of @Hereward , and came up to the final solution which is given below.

`import React, { Component } from "react";

import { EditorState } from "draft-js";

import { Editor } from "react-draft-wysiwyg";
import "../../../../node_modules/react-draft-wysiwyg/dist/react-draft-wysiwyg.css";

class TextEditor extends Component {
constructor(props) {
super(props);

this.state = {
  editorState: EditorState.createEmpty(),
  hideToolbar: true
};

}

onEditorStateChange = editorState => {
this.setState({
editorState
});
};

onContentStateChange = contentState => {
this.setState({
contentState
});
};

render() {
const { editorState } = this.state;

return (
  <Editor
    editorState={editorState}
    toolbarOnFocus={!this.state.hideToolbar}
    toolbarHidden={this.state.hideToolbar}
    wrapperClassName="demo-wrapper"
    editorClassName="demo-editor"
    toolbarClassName="toolbar-class"
    onFocus={() => {
      this.setState({ hideToolbar: false });
    }}
    onBlur={() => {
      this.setState({ hideToolbar: true });
    }}
    onEditorStateChange={this.onEditorStateChange}
    onContentStateChange={this.onContentStateChange}
    placeholder={"  Write Here"}
    editorStyle={{
      backgroundColor: "#fff",
      padding: 0,
      borderWidth: 0,
      borderColor: "transparent",
      height: 60
    }}
  />
);

}
}

export default TextEditor;
`

All 19 comments

Hey @yangyu0311ugroop:

Not rendering the toolbar at all was my initial though also for this case, but it was creating troubles like this: https://github.com/jpuri/react-draft-wysiwyg/issues/389. Thus I changed it to use CSS properties.

I am glad you liked the lib 馃槃

A hidden toolbar has visibility: hidden. Wouldn't it be better to have display: none ?

Thats is a good idea @DennisKo , I will make this change.

I am finding that using display: none in this case may prevent users from using this property for toolbar.
visibility:hidden works well in this case as floating toolbars are usually positioned absolutely. And even if they are not we do not want change in their visibility to change page layout.

Maybe give the option to toggle between display:none and visibility:hidden then. I guess both have valid use cases..

I just noticed that the visibility: hidden is getting set inline. Maybe introduce a class for that like editorToolbar-hidden

visibility: hidden causes that toolbar is not visible, but there is still blank space occupied by the toolbar and not usable by the editor text. also clicking in this blank area does not fire focus

I'm facing a similar issue. I want to completely hide the toolbar area, but instead there is still a blank space.

I would like a display:none option. Although a simple wrapper component fixes this issue, it could be a nice addition.

screen shot 2018-01-15 at 9 37 05 pm

Please look at example above here: https://jpuri.github.io/react-draft-wysiwyg/#/demo. It does not takes any space.

I'm sorry but it definitely takes space when toolbarHidden is enabled

I am working to fix this issue for toolbarHidden property. For toolbarOnFocus property I would suggest using absolute styling like I mentioned above.

After a couple attempts, a work-around to this is by adding a class to the toolbar with a display of none.

.hide-toolbar { display:none !important; }

And then
toolbarClassName='hide-toolbar'

Hope this helps till the issue is resolved.
@jpuri thanks, this is awesome!

My solution in css :
.rdw-editor-toolbar[aria-hidden="true"] { display: none !important; }

I'm trying to find a solution to this problem. Is absolute positioning the recommended approach now? The other suggestion mentioned above eg. .hide-toolbar { display:none !important; } does not work because then the toolbar is always hidden (lol).

I tried the absolute positioning approach but it unfortunately creates a different problem as the toolbar now sits on top of everything.

There needs to be a way to set display:none / display block so that the toolbar will go into the page layout and push the rest of the html down. Visibility:hidden just leaves a big blank space.

This should be pretty simple? Is it necessary to hack the code?

In your demo the problem where the absolutely positioned toolbar sits on top of (and obscures) the underlying content can clearly be seen.

I came up with this solution (using a state variable which is toggled by the onFocus and OnBlur callbacks):

toolbarClassName={hideToolbar ? classes.toolbarHidden : classes.toolbar}
onFocus={() => {this.setState({hideToolbar: false});}}
onBlur={() => {this.setState({hideToolbar: true});}}

i figured it out with the idea of @Hereward , and came up to the final solution which is given below.

`import React, { Component } from "react";

import { EditorState } from "draft-js";

import { Editor } from "react-draft-wysiwyg";
import "../../../../node_modules/react-draft-wysiwyg/dist/react-draft-wysiwyg.css";

class TextEditor extends Component {
constructor(props) {
super(props);

this.state = {
  editorState: EditorState.createEmpty(),
  hideToolbar: true
};

}

onEditorStateChange = editorState => {
this.setState({
editorState
});
};

onContentStateChange = contentState => {
this.setState({
contentState
});
};

render() {
const { editorState } = this.state;

return (
  <Editor
    editorState={editorState}
    toolbarOnFocus={!this.state.hideToolbar}
    toolbarHidden={this.state.hideToolbar}
    wrapperClassName="demo-wrapper"
    editorClassName="demo-editor"
    toolbarClassName="toolbar-class"
    onFocus={() => {
      this.setState({ hideToolbar: false });
    }}
    onBlur={() => {
      this.setState({ hideToolbar: true });
    }}
    onEditorStateChange={this.onEditorStateChange}
    onContentStateChange={this.onContentStateChange}
    placeholder={"  Write Here"}
    editorStyle={{
      backgroundColor: "#fff",
      padding: 0,
      borderWidth: 0,
      borderColor: "transparent",
      height: 60
    }}
  />
);

}
}

export default TextEditor;
`

@astrosam21 great solution ! Thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gyarasu picture gyarasu  路  4Comments

trongbang86 picture trongbang86  路  3Comments

ananddodia picture ananddodia  路  4Comments

Pixelatex picture Pixelatex  路  3Comments

jpuri picture jpuri  路  4Comments