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

Add page-template/post-template value to body class #8162

Closed
iamzozo opened this issue Jul 24, 2018 · 6 comments
Closed

Add page-template/post-template value to body class #8162

iamzozo opened this issue Jul 24, 2018 · 6 comments
Labels
[Status] Duplicate Used to indicate that a current issue matches an existing one and can be closed [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes. [Type] Question Questions about the design or development of the editor.

Comments

@iamzozo
Copy link

iamzozo commented Jul 24, 2018

When switching page or post template, it would be good to also add this value to the body class on the admin screen.

I'm working on a theme, and I want to modify the editor layout (widths) and styles for the block content (typo), according to the theme.

@websevendev
Copy link

Related: #7810 (I don't think they're adding it)

I did it manually by rendering this component into an invisible div I added on Gutenberg page:

class PageTemplateUpdater extends React.Component {
	componentDidUpdate(prevProps) {
		if(prevProps.template) {
			$('body').removeClass('page-template-' + prevProps.template.replace('.php', ''));
		}
		if(this.props.template) {
			$('body').addClass('page-template-' + this.props.template.replace('.php', ''));
		}
	}

	render() {
		return null;
	}
}

const withPageTemplate = wp.compose.createHigherOrderComponent(
	wp.data.withSelect(
		select => {
			const {
				getEditedPostAttribute,
			} = select('core/editor');

			return {
				template: getEditedPostAttribute('template'),
			};
		}
	),
	'withPageTemplate'
);

export default withPageTemplate(PageTemplateUpdater);

@designsimply designsimply added [Type] Question Questions about the design or development of the editor. [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes. [Status] Duplicate Used to indicate that a current issue matches an existing one and can be closed labels Jul 24, 2018
@designsimply
Copy link
Member

Closing as a duplicate of #7810. Thank you so much for the code example @websevendev!

@strarsis
Copy link
Contributor

strarsis commented Mar 4, 2019

@websevendev: I am importing your code in the editor JavaScript file that is enqueued for Gutenberg.
But no class is assigned to the body element.
And is it also possible to add a frontpage/home class so the front page can be selected by the theme styles?

@websevendev
Copy link

@strarsis As I said, I render this component manually.

The aforementioned code is in, for example, /PageTemplateWatcher/index.js, and in my "main" js file I import it:

import PageTemplateWatcher from './PageTemplateWatcher';

and render it:

document.addEventListener('DOMContentLoaded', () => {
	ReactDOM.render(<PageTemplateWatcher />, document.getElementById('my-root'));
});

And the root is added by PHP, but you could dynamically create it as well:

add_action('admin_footer', function() {
	?>
	<div id="my-root"></div>
	<?php
});

@strarsis
Copy link
Contributor

strarsis commented Mar 4, 2019

@websevendev: Thanks! Eslint complains about missing props validation.
How to add props validation when using Gutenberg factories(?) for components?

3:16  error  'template' is missing in props validation          react/prop-types
4:55  error  'template' is missing in props validation          react/prop-types
4:64  error  'template.replace' is missing in props validation  react/prop-types
6:17  error  'template' is missing in props validation          react/prop-types
7:53  error  'template' is missing in props validation          react/prop-types
7:62  error  'template.replace' is missing in props validation  react/prop-types

Edit: Right, I have to add the classes in theme using the admin_body_class filter.

/**
 * Admin body template class
 */
function admin_body_template_class( $str_classes ) {
    global $post;
    $template_slug = get_page_template_slug( $post );
    $classes   = explode(' ', $str_classes);
    $classes[] = $template_slug;
    $new_str_classes = join(' ', $classes);
    return $new_str_classes;
}
add_filter( 'admin_body_class', 'admin_body_template_class' );

IMHO it would be great if this code is added to Gutenberg core or at least
an extension that can be readily loaded by themes that need to select specific templates/pages/posts.

@strarsis
Copy link
Contributor

Based on the code here, I made a little tutorial (sage9 based theme): https://discourse.roots.io/t/gutenberg-body-class-elements-by-template/15310

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Status] Duplicate Used to indicate that a current issue matches an existing one and can be closed [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes. [Type] Question Questions about the design or development of the editor.
Projects
None yet
Development

No branches or pull requests

4 participants