-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Replace hardcoded CSS_UNITS and inherit values from theme.json #31822
Conversation
Size Change: -71 B (0%) Total Size: 1.62 MB
ℹ️ View Unchanged
|
* | ||
* @return {Array} Filtered units based on settings. | ||
*/ | ||
export const useCustomUnits = ( { units, availableUnits, defaultValues } ) => { |
There was a problem hiding this 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 @aristath. Thinking a bit I'm not at ease with exposing this hook in the components package. Can you clarify how it's supposed to be used? Why do we need both this hook and the component with the units prop.
I guess on my mind doing something like this should be always sufficient, why it's not the case?
<UnitControl units={ useSetting( 'spacing.units' ) } />
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this was a very deep rabbit hole and I spent hours trying and iterating things to find something that works... 😅
These controls are used in lots of places, in different contexts. The default values vary depending on the context (for example the cover block's height should by default include vh
units while the layout or column-width units should not).
useSetting
will only get the value from theme.json
which is formatted like ["px", "em", "rem"]
. The control however requires a different format so useCustomUnits
needs to run in order to convert the units to [{value:"em", ...}, {...}, ...]
(that's what we were doing before too).
I suppose we could make UnitControl
understand an array of units like ["px","em","rem"]
and just make the conversion to the full array inside the control itself... But then if spacing.units
is empty it would not fallback to the default units as it does now.
Using useSetting
to get these would not work correctly unless we add hardcoded fallback in useSetting
to get defaults - both in the block-editor and the site-editor packages since useSetting
is implemented separately in each of them (which is also what triggered the experimentation on #31782).
The reason I placed it in components
inside the control's utils is because it seemed that the most suitable place for it... Do you think some other package would be a better home?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose we could make UnitControl understand an array of units like ["px","em","rem"] and just make the conversion to the full array inside the control itself... But then if spacing.units is empty it would not fallback to the default units as it does now.
I feel this might be the best way potentially but right now maybe we can just start by marking the hook as experimental?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and marked the hook as experimental for now.
It would be cool to get this ready for tomorrow's RC (5.8 feature freeze) |
9e393ca
to
21c5f14
Compare
import { __experimentalBoxControl as BoxControl } from '@wordpress/components'; | ||
import { | ||
__experimentalUseCustomUnits as useCustomUnits, | ||
__experimentalBoxControl as BoxControl, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes me wonder why these hooks (margin, padding) are not using the UnitControl component from the block editor components directly.
@@ -17,11 +17,12 @@ import { | |||
TextControl, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, there's already a hooked UnitControl in block-editor package, why isn't this one used here instead.
I'm going to merge this to include in 5.8 but it definitely sounds like all these components need to be consolidated. I think all of the "hooks" and "block-library" usage should rely on the Can we open an issue or follow-up about that? |
042c0d0
to
3bc0ffc
Compare
3bc0ffc
to
e1cad86
Compare
Co-authored-by: Riad Benguella <[email protected]>
Description
Currently, most controls have units hardcoded in
CSS_UNITS
constants, and many components don't use the units defined intheme.json
files.We already had a
useCustomUnits
function insidepackages/block-editor/src/components/unit-control/
, so this PR does the following:useCustomUnits
topackages/components/src/unit-control/utils.js
useCustomUnits
in@wordpress/components
CSS_UNITS
arrays, and replace them with calls touseCustomUnits
. The calls check fortheme.json
settings (usinguseSetting
), and if no settings are located then fallback to an array of units (this array was extrapolated from the previousCSS_UNITS
constants).ALL_CSS_UNITS
) containing all existing CSS units. This way users can define their arrays like["px","em"]
and all labels will be properly populated - even if they decide to use unusual units likecm
,in
orpc
🤷While doing the above, I realized that
spacing.units
doesn't make sense for everything... Spacing refers to padding & margins, not widths. So In some instances (like for example Layout settings & column widths), instead of checking forspacing.units
I'm checking forlayout.units
.Overall, this PR cleans up the code a bit and feels like an improvement, increasing consistency.
How has this been tested?
Checked in an FSE theme with and without values defined in
theme.json
forsettings.spacing.units
&settings.layout.units
.Confirmed that it works as expected for layout, padding, margin, column-width, cover block, search block. Checked both the post-editor & site-editor.
Checklist:
*.native.js
files for terms that need renaming or removal).