Skip to content

Components: Don't mutate 'ALL_CSS_UNITS' default value in 'useCustomUnits' #70037

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

Merged
merged 3 commits into from
May 4, 2025

Conversation

Mamaduka
Copy link
Member

@Mamaduka Mamaduka commented May 1, 2025

What?

Closes #70036.

PR fixes accidental ALL_CSS_UNITS value mutation when the units argument was provided for useCustomUnits.

Why?

I think this only happens when the units argument isn't provided, and it uses the default ALL_CSS_UNITS fallback. The useCustomUnits ends up mutating shared unit references instead of copying them.

Testing Instructions

npm run test:unit -- packages/components/src/unit-control/test/utils.ts

Testing Instructions for Keyboard

Same.

@Mamaduka Mamaduka added [Type] Bug An existing feature does not function as intended [Package] Components /packages/components labels May 1, 2025
@Mamaduka Mamaduka self-assigned this May 1, 2025
@Mamaduka Mamaduka requested a review from ajitbohra as a code owner May 1, 2025 12:27
@Mamaduka
Copy link
Member Author

Mamaduka commented May 1, 2025

cc @WordPress/gutenberg-components

Copy link

github-actions bot commented May 1, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @pryley.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: pryley.

Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@Mamaduka Mamaduka requested a review from t-hamano May 1, 2025 14:05
Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! This change looks good to me.

I have confirmed that this issue occurs not only when the fallback units value ​​is used, but also when consumers provide shared values ​​to multiple components, and this PR will also resolve that issue. Is my understanding correct?

Example:

export default function Component() {
	const units = [
		{ value: 'px', label: 'pixel' },
		{ value: 'em', label: 'em' },
		{ value: 'rem', label: 'rem' },
	];
	return (
		<>
			<UnitControl
				isResetValueOnUnitChange
				units={ useCustomUnits( {
					availableUnits: [ 'px', 'em', 'rem' ],
					defaultValues: { px: 8, em: 0.5, rem: 0.5 },
					units,
				} ) }
			/>
			<UnitControl
				isResetValueOnUnitChange
				units={ useCustomUnits( {
					availableUnits: [ 'px', 'em', 'rem' ],
					defaultValues: { px: 16, em: 1, rem: 1 },
					units,
				} ) }
			/>
		</>
	);
}

@Mamaduka
Copy link
Member Author

Mamaduka commented May 3, 2025

That's correct, if units reference the same Array<Object>, mutations would happen previously.

@Mamaduka Mamaduka force-pushed the fix/use-custom-units-default-value-mutation branch from 44afb7e to bf2a79b Compare May 3, 2025 07:18
Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@Mamaduka Mamaduka merged commit 5b33c37 into trunk May 4, 2025
61 checks passed
@Mamaduka Mamaduka deleted the fix/use-custom-units-default-value-mutation branch May 4, 2025 07:03
@github-actions github-actions bot added this to the Gutenberg 20.8 milestone May 4, 2025
chriszarate pushed a commit to chriszarate/gutenberg that referenced this pull request Jul 1, 2025
…nits' (WordPress#70037)

Unlinked contributors: pryley.

Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UnitControl: useCustomUnits.defaultValues overwrites defaultValues used in previous UnitControls
2 participants