-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace hardcoded CSS_UNITS and inherit values from theme.json (#31822)
Co-authored-by: Riad Benguella <benguella@gmail.com>
- Loading branch information
Showing
20 changed files
with
299 additions
and
311 deletions.
There are no files selected for viewing
47 changes: 14 additions & 33 deletions
47
packages/block-editor/src/components/unit-control/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,27 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __experimentalUnitControl as BaseUnitControl } from '@wordpress/components'; | ||
import { | ||
__experimentalUseCustomUnits as useCustomUnits, | ||
__experimentalUnitControl as BaseUnitControl, | ||
} from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useSetting from '../use-setting'; | ||
|
||
export default function UnitControl( { units: unitsProp, ...props } ) { | ||
const units = useCustomUnits( unitsProp ); | ||
|
||
return <BaseUnitControl units={ units } { ...props } />; | ||
} | ||
|
||
/** | ||
* Filters available units based on values defined by settings. | ||
* | ||
* @param {Array} settings Collection of preferred units. | ||
* @param {Array} units Collection of available units. | ||
* | ||
* @return {Array} Filtered units based on settings. | ||
*/ | ||
function filterUnitsWithSettings( settings = [], units = [] ) { | ||
return units.filter( ( unit ) => { | ||
return settings.includes( unit.value ); | ||
const units = useCustomUnits( { | ||
availableUnits: useSetting( 'spacing.units' ) || [ | ||
'%', | ||
'px', | ||
'em', | ||
'rem', | ||
'vw', | ||
], | ||
units: unitsProp, | ||
} ); | ||
} | ||
|
||
/** | ||
* Custom hook to retrieve and consolidate units setting from add_theme_support(). | ||
* | ||
* @param {Array} units Collection of available units. | ||
* | ||
* @return {Array} Filtered units based on settings. | ||
*/ | ||
export function useCustomUnits( units ) { | ||
const availableUnits = useSetting( 'spacing.units' ); | ||
const usedUnits = filterUnitsWithSettings( | ||
! availableUnits ? [] : availableUnits, | ||
units | ||
); | ||
|
||
return usedUnits.length === 0 ? false : usedUnits; | ||
return <BaseUnitControl units={ units } { ...props } />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.