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

Edit Post: Refactor 'MetaBoxVisibility' component #67265

Merged
merged 1 commit into from
Nov 25, 2024
Merged
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
37 changes: 14 additions & 23 deletions packages/edit-post/src/components/meta-boxes/meta-box-visibility.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';

class MetaBoxVisibility extends Component {
componentDidMount() {
this.updateDOM();
}

componentDidUpdate( prevProps ) {
if ( this.props.isVisible !== prevProps.isVisible ) {
this.updateDOM();
}
}

updateDOM() {
const { id, isVisible } = this.props;
export default function MetaBoxVisibility( { id } ) {
const isVisible = useSelect(
( select ) => {
return select( editorStore ).isEditorPanelEnabled(
`meta-box-${ id }`
);
},
[ id ]
);

useEffect( () => {
const element = document.getElementById( id );
if ( ! element ) {
return;
Expand All @@ -29,13 +26,7 @@ class MetaBoxVisibility extends Component {
} else {
element.classList.add( 'is-hidden' );
}
}
}, [ id, isVisible ] );

render() {
return null;
}
return null;
}

export default withSelect( ( select, { id } ) => ( {
isVisible: select( editorStore ).isEditorPanelEnabled( `meta-box-${ id }` ),
} ) )( MetaBoxVisibility );
Loading