Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The CKEditor component fails to render while using @mui/material Button component #321

Closed
Mgsy opened this issue Jul 26, 2022 · 4 comments · Fixed by #346
Closed

The CKEditor component fails to render while using @mui/material Button component #321

Mgsy opened this issue Jul 26, 2022 · 4 comments · Fixed by #346
Assignees
Labels
squad:devops Issue to be handled by the Devops team. support:2 An issue reported by a commercially licensed client. type:bug type:regression
Milestone

Comments

@Mgsy
Copy link
Member

Mgsy commented Jul 26, 2022

It seems that using a <Button> component from @mui/material affects our component. After initializing the page, the <CKEditor /> component is destroyed. 

Required libraries:

npm install @mui/material @emotion/styled @emotion/react 

Example code:

import React, { Component } from 'react';
import { Button } from "@mui/material";
import Editor from '@ckeditor/ckeditor5-build-classic';
import { CKEditor } from '@ckeditor/ckeditor5-react'

const EditorComponent = () => (
  <CKEditor
      editor={ Editor }
      onReady={ editor => {
          window.editor = editor;
              // You can store the "editor" and use when it is needed.
              console.log( 'Editor is ready to use!', editor );
          } }
        onChange={ ( event, editor ) => {
            const data = editor.getData();
            console.log( { event, editor, data } );
        } }
        onBlur={ ( event, editor ) => {
            console.log( 'Blur.', editor );
        } }
        onFocus={ ( event, editor ) => {
            console.log( 'Focus.', editor );
        } }
    />
)

class App extends Component {
    render() {
        return (
            <div className="App">
                <h2>Using CKEditor 5 from online builder in React</h2>
                <Button>Button</Button>
                <EditorComponent />
            </div>
        );
    }
}

export default App;

After running the page, only the button is visible.

@Mgsy Mgsy added type:bug squad:devops Issue to be handled by the Devops team. support:2 An issue reported by a commercially licensed client. labels Jul 26, 2022
@Mgsy Mgsy changed the title The CKEditor component fails to render while using @mui/material component The CKEditor component fails to render while using @mui/material Button component Jul 26, 2022
@CKEditorBot CKEditorBot added the status:planned Set automatically when an issue lands in the "Sprint backlog" column. We will be working on it soon. label Jul 29, 2022
@CKEditorBot CKEditorBot added status:in-progress Set automatically when an issue lands in the "In progress" column. We are working on it. and removed status:planned Set automatically when an issue lands in the "Sprint backlog" column. We will be working on it soon. labels Aug 2, 2022
@przemyslaw-zan
Copy link
Member

I have not found a reason for this, but presence of the button causes componentWillUnmount react hook to trigger, which calls _destroyEditor method in our case. This edge case can be caught by adding if ( !this.editor ) {...} after this line. We should probably throw an error here.

For a workaround, instead of an error the editor can be recursively initialized again by using this._initializeEditor(); in the aforementioned check. This does work, but causes ugly flash when the editor dissapears for a second before being initialized again.

@pomek
Copy link
Member

pomek commented Aug 4, 2022

Using conditional rendering allows rendering a button and an instance of CKEditor 5 next to each other.

import React, { Component } from 'react';
import { Button } from "@mui/material";
import Editor from '@ckeditor/ckeditor5-build-classic';
import { CKEditor } from '@ckeditor/ckeditor5-react'

const EditorComponent = ( props ) => (
	<CKEditor
		editor={ Editor }
		onReady={ editor => {
			props.parent.setState( { showButton: true } );

			window.editor = editor;
			// You can store the "editor" and use when it is needed.
			console.log( 'Editor is ready to use!', editor );

		} }
		onChange={ ( event, editor ) => {
			const data = editor.getData();
			console.log( { event, editor, data } );
		} }
		onBlur={ ( event, editor ) => {
			console.log( 'Blur.', editor );
		} }
		onFocus={ ( event, editor ) => {
			console.log( 'Focus.', editor );
		} }
	/>
);

class App extends Component {
	constructor( ...all ) {
		super( ...all );

		this.state = {
			showButton: false
		}
	}

	render() {
		return (
			<div className="App">
				<h2>Using CKEditor 5 from online builder in React</h2>
				<EditorComponent parent={ this }/>
				{ this.state.showButton ? <Button>Button</Button> : '' }
			</div>
		);
	}
}

export default App;

But still, this is a workaround rather than adequately fixing the issue.

@pomek
Copy link
Member

pomek commented Aug 4, 2022

Also, it works fine with the previous version of the component (v4.0.1).

@pomek
Copy link
Member

pomek commented Aug 4, 2022

In the Strict mode, React renders each component twice. Unfortunately, this causes the <CKEditor> component to be not rendered.

When I prepared a production build of a simple application, it worked fine.

image

@CKEditorBot CKEditorBot removed the status:in-progress Set automatically when an issue lands in the "In progress" column. We are working on it. label Aug 4, 2022
@CKEditorBot CKEditorBot added the status:in-progress Set automatically when an issue lands in the "In progress" column. We are working on it. label Nov 23, 2022
@pomek pomek assigned pomek and psmyrek and unassigned pomek Nov 23, 2022
pomek added a commit that referenced this issue Nov 23, 2022
Fix: Fixed the component initialization procedure to enforce cleanup completion before subsequent editor initialization. Closes #321. Closes #338.

Thanks to @corymharper.
@CKEditorBot CKEditorBot removed the status:in-progress Set automatically when an issue lands in the "In progress" column. We are working on it. label Nov 23, 2022
@CKEditorBot CKEditorBot added this to the iteration 59 milestone Nov 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
squad:devops Issue to be handled by the Devops team. support:2 An issue reported by a commercially licensed client. type:bug type:regression
Projects
None yet
5 participants