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

Please expose registered aspect ratios in the block editor data store #67211

Closed
markhowellsmead opened this issue Nov 21, 2024 · 5 comments
Closed
Labels
[Package] Block editor /packages/block-editor [Type] Enhancement A suggestion for improvement.

Comments

@markhowellsmead
Copy link

What problem does this address?

This will allow extenders to use the registered aspect ratios in their own components / blocks. This would be useful as there is no aspect ratio component available.

const getAspectRatios = () => {
    const settings = wp.data.select('core/block-editor').getSettings();
    return settings?.aspectRatios || [];
};

const aspectRatios = useSelect(getAspectRatios, []);
@markhowellsmead markhowellsmead added the [Type] Enhancement A suggestion for improvement. label Nov 21, 2024
@skorasaurus skorasaurus added the [Package] Block editor /packages/block-editor label Nov 22, 2024
@t-hamano
Copy link
Contributor

I think you can get the default and theme settings via the useSettings hook. Does the below code help?

import { useSettings } from '@wordpress/block-editor';

function MyComponent() {
	const [ defaultRatios, themeRatios ] = useSettings(
		'dimensions.aspectRatios.default',
		'dimensions.aspectRatios.theme'
	);

	console.log( defaultRatios );
	console.log( themeRatios );

	return <div>My Component</div>;
}

@markhowellsmead
Copy link
Author

Ah, thanks Aki. That works fine when I merge the two arrays together. It's a shame that the options for useSettings aren't documented anywhere.

import { useSettings } from '@wordpress/block-editor';

const [defaultRatios, themeRatios] = useSettings('dimensions.aspectRatios.default', 'dimensions.aspectRatios.theme');

const aspectRatios = [...(defaultRatios || []), ...(themeRatios || [])];

const aspectRatioOptions = aspectRatios.map((ratio) => {
	return {
		value: ratio.ratio,
		label: ratio.name,
	};
});

@t-hamano
Copy link
Contributor

@markhowellsmead
Copy link
Author

Yes, but the available option keys aren't.

@t-hamano
Copy link
Contributor

Got it, maybe the documentation needs to be a bit more detailed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Block editor /packages/block-editor [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

3 participants