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

Make block style slug available to render callback #56278

Open
iandunn opened this issue Nov 17, 2023 · 1 comment
Open

Make block style slug available to render callback #56278

iandunn opened this issue Nov 17, 2023 · 1 comment
Labels
[Feature] Block API API that allows to express the block paradigm. [Feature] Block Styles Issues or PRs that are related to the `register_block_style` API [Type] Enhancement A suggestion for improvement.

Comments

@iandunn
Copy link
Member

iandunn commented Nov 17, 2023

What problem does this address?

Normally when you want to change a block based on the selected style, it's enough to just add styles to the stylesheet. The are some situations where you need to do more than that, though, like changing the Google Map styles that a map block uses. Those are stored in a JSON variable.

I don't see a way to easily access the slug of the selected style. I can parse it out of attributes.className, but that's kind of hacky. I have to use a regex, or loop through my style slugs and do a string search.

I also don't see a way to get a list of styles registered for my block, so I have to restructure my code to maintain an array of styles, and then loop through them passing them to registerBlockStyle, rather than just calling it directly.

What is your proposed solution?

Add something like attributes.style which equals the slug of the selected style. It's not technically an attribute, though.

Another idea would be to expose it via something like:

const block = useSelect(
	( select ) => {
		return select( 'core/block-editor' ).getBlock( clientId );
	},
	[ clientId ]
);
@iandunn iandunn added [Type] Enhancement A suggestion for improvement. [Feature] Block Styles Issues or PRs that are related to the `register_block_style` API labels Nov 17, 2023
@iandunn
Copy link
Member Author

iandunn commented Nov 17, 2023

It'd also be nice to expose it to the PHP render_callback for dynamic blocks, in case it affects some data that PHP passes to JS during the initial page load. I ran into that too.

@gziolo gziolo added the [Feature] Block API API that allows to express the block paradigm. label Nov 24, 2023
@gziolo gziolo mentioned this issue Nov 24, 2023
58 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm. [Feature] Block Styles Issues or PRs that are related to the `register_block_style` API [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

2 participants