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 documentation about custom class name generation #46

Open
fabiankaegy opened this issue May 16, 2022 · 1 comment
Open

Add documentation about custom class name generation #46

fabiankaegy opened this issue May 16, 2022 · 1 comment
Assignees

Comments

@fabiankaegy
Copy link
Member

In a discussion with @iansvo we've uncovered that core only generates the has-${slug}-color etc class names when you are using theme.json older themes that use the add_theme_support will only get the custom properties defined but the themes will have to manually add the classes.

This should be documented somewhere.

@iansvo
Copy link
Contributor

iansvo commented May 16, 2022

Here's an example of some code that I've written to address this on themes that do not currently leverage theme.json.

This is a reduced example that shows the point of execution for the changes to provide full context.

<?php
add_action( 'after_setup_theme',  'theme_setup' );

function theme_setup() {
	// ...

	// add basic Brand colors to gutenberg color selector
	add_theme_support( 'editor-color-palette', color_palette() );

	// Output CSS for the color palette here
	add_action( 'wp_head',  'output_color_palette_styles', 999 );
}

/**
 * Return Theme Color Palette
 *
 * @return array Theme Color Palette
 */
function color_palette() {
	return [
		[
			'name'  => esc_html__( 'Bazinga', 'wmb' ),
			'slug'  => 'bazinga',
			'color' => 'var(--color-bazinga)',
		],
	];
}

/**
 * Add CSS for the color palette to head tag
 *
 * @return void
 */
function output_color_palette_styles() {
	$color_palette = color_palette();

	echo "<style id='color-palette-styles'>\n";

	foreach ( $color_palette as $color ) {
		$slug  = $color['slug'];
		$value = $color['color'];

		echo esc_html( ".has-background.has-{$slug}-background-color { background-color: $value; }\n" );
		echo esc_html( ".has-text-color.has-{$slug}-color { color: $value; }\n" );
	}

	echo '</style>';
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants