-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add spacing panel to global styles sidebar
- Loading branch information
Showing
2 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
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
63 changes: 63 additions & 0 deletions
63
packages/edit-site/src/components/sidebar/spacing-panel.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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { | ||
__experimentalBoxControl as BoxControl, | ||
PanelBody, | ||
} from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useEditorFeature } from '../editor/utils'; | ||
|
||
export function useHasSpacingPanel( { supports } ) { | ||
return supports.includes( 'paddingBottom' ); | ||
} | ||
|
||
function filterUnitsWithSettings( settings = [], units = [] ) { | ||
return units.filter( ( unit ) => { | ||
return settings.includes( unit.value ); | ||
} ); | ||
} | ||
|
||
function useCustomUnits( units ) { | ||
const availableUnits = useEditorFeature( 'spacing.units' ); | ||
const usedUnits = filterUnitsWithSettings( | ||
! availableUnits ? [] : availableUnits, | ||
units | ||
); | ||
|
||
return usedUnits.length === 0 ? false : usedUnits; | ||
} | ||
|
||
export default function SpacingPanel( { | ||
context: { name }, | ||
getStyleProperty, | ||
setStyleProperty, | ||
} ) { | ||
const units = useCustomUnits(); | ||
const paddingValues = { | ||
top: getStyleProperty( name, 'paddingTop' ), | ||
right: getStyleProperty( name, 'paddingRight' ), | ||
bottom: getStyleProperty( name, 'paddingBottom' ), | ||
left: getStyleProperty( name, 'paddingLeft' ), | ||
}; | ||
const setPaddingValues = ( { top, right, bottom, left } ) => { | ||
setStyleProperty( name, 'paddingTop', top ); | ||
setStyleProperty( name, 'paddingRight', right ); | ||
setStyleProperty( name, 'paddingBottom', bottom ); | ||
setStyleProperty( name, 'paddingLeft', left ); | ||
}; | ||
return ( | ||
<PanelBody title={ __( 'Spacing' ) }> | ||
<BoxControl | ||
values={ paddingValues } | ||
onChange={ setPaddingValues } | ||
label={ __( 'Padding' ) } | ||
units={ units } | ||
/> | ||
</PanelBody> | ||
); | ||
} |