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

Replace FullscreenMode from Class components to functional components. #32925

Merged
merged 6 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 20 additions & 30 deletions packages/interface/src/components/fullscreen-mode/index.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,41 @@
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';

export class FullscreenMode extends Component {
componentDidMount() {
this.isSticky = false;
this.sync();
import { useEffect } from '@wordpress/element';

const FullscreenMode = ( { isActive } ) => {
useEffect( () => {
let isSticky = false;
// `is-fullscreen-mode` is set in PHP as a body class by Gutenberg, and this causes
// `sticky-menu` to be applied by WordPress and prevents the admin menu being scrolled
// even if `is-fullscreen-mode` is then removed. Let's remove `sticky-menu` here as
// a consequence of the FullscreenMode setup
if ( document.body.classList.contains( 'sticky-menu' ) ) {
this.isSticky = true;
isSticky = true;
document.body.classList.remove( 'sticky-menu' );
}
}

componentWillUnmount() {
if ( this.isSticky ) {
document.body.classList.add( 'sticky-menu' );
}

if ( this.props.isActive ) {
document.body.classList.remove( 'is-fullscreen-mode' );
}
}

componentDidUpdate( prevProps ) {
if ( this.props.isActive !== prevProps.isActive ) {
this.sync();
}
}
return () => {
if ( isSticky ) {
document.body.classList.add( 'sticky-menu' );
}
};
}, [] );

sync() {
const { isActive } = this.props;
useEffect( () => {
if ( isActive ) {
document.body.classList.add( 'is-fullscreen-mode' );
} else {
document.body.classList.remove( 'is-fullscreen-mode' );
}
}

render() {
return null;
}
}
return () => {
if ( isActive ) {
document.body.classList.remove( 'is-fullscreen-mode' );
}
};
}, [ isActive ] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are some issues with the logic here, I see:

  • isSticky is not a state in the previous implementation and changing its value shouldn't cause an effect to run, basically it needs to be stored in a ref ( useRef )
  • We can probably combine the effects that have isActive as a dependency

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youknowriad Thank you for the review. I modified useStatus to useRef.


return null;
};
export default FullscreenMode;
43 changes: 25 additions & 18 deletions packages/interface/src/components/fullscreen-mode/test/index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,50 @@
/**
* External dependencies
*/
import { shallow } from 'enzyme';

import { act, create } from 'react-test-renderer';
/**
* Internal dependencies
*/
import { FullscreenMode } from '..';
import FullscreenMode from '..';

describe( 'FullscreenMode', () => {
it( 'fullscreen mode to be added to document body when active', () => {
shallow( <FullscreenMode isActive={ true } /> );

act( () => {
create( <FullscreenMode isActive={ true } /> );
} );
expect( document.body.classList.contains( 'is-fullscreen-mode' ) ).toBe(
true
);
} );

it( 'fullscreen mode not to be added to document body when active', () => {
shallow( <FullscreenMode isActive={ false } /> );

act( () => {
create( <FullscreenMode isActive={ false } /> );
} );
expect( document.body.classList.contains( 'is-fullscreen-mode' ) ).toBe(
false
);
} );

it( 'sticky-menu to be removed from the body class if present', () => {
document.body.classList.add( 'sticky-menu' );

shallow( <FullscreenMode isActive={ false } /> );

act( () => {
create( <FullscreenMode isActive={ false } /> );
} );
expect( document.body.classList.contains( 'sticky-menu' ) ).toBe(
false
);
} );

it( 'sticky-menu to be restored when component unmounted and originally present', () => {
document.body.classList.add( 'sticky-menu' );

const mode = shallow( <FullscreenMode isActive={ false } /> );
mode.unmount();

let mode;
act( () => {
mode = create( <FullscreenMode isActive={ false } /> );
} );
act( () => {
mode.unmount();
} );
expect( document.body.classList.contains( 'sticky-menu' ) ).toBe(
true
);
Expand All @@ -51,15 +55,18 @@ describe( 'FullscreenMode', () => {
expect( document.body.classList.contains( 'is-fullscreen-mode' ) ).toBe(
false
);

const mode = shallow( <FullscreenMode isActive /> );

let mode;
act( () => {
mode = create( <FullscreenMode isActive /> );
} );
// present after mounting with `isActive`
expect( document.body.classList.contains( 'is-fullscreen-mode' ) ).toBe(
true
);

mode.unmount();
act( () => {
mode.unmount();
} );

// removed after unmounting
expect( document.body.classList.contains( 'is-fullscreen-mode' ) ).toBe(
Expand Down