-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Spacing Support: Update padding support to allow configurable sides #30607
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
Changes from all commits
9c109ce
589368e
e962dcd
5a2bf1f
d3588ea
2567acf
5d1b856
a3497f9
ac06fb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { PanelBody } from '@wordpress/components'; | ||
| import { Platform } from '@wordpress/element'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { getBlockSupport } from '@wordpress/blocks'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import InspectorControls from '../components/inspector-controls'; | ||
| import { | ||
| PaddingEdit, | ||
| hasPaddingSupport, | ||
| useIsPaddingDisabled, | ||
| } from './padding'; | ||
|
|
||
| export const SPACING_SUPPORT_KEY = 'spacing'; | ||
|
|
||
| /** | ||
| * Inspector controls for spacing support. | ||
| * | ||
| * @param {Object} props Block props. | ||
| * @return {WPElement} Inspector controls for spacing support features. | ||
| */ | ||
| export function SpacingPanel( props ) { | ||
| const isDisabled = useIsSpacingDisabled( props ); | ||
| const isSupported = hasSpacingSupport( props.name ); | ||
|
|
||
| if ( isDisabled || ! isSupported ) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <InspectorControls key="spacing"> | ||
| <PanelBody title={ __( 'Spacing' ) }> | ||
| <PaddingEdit { ...props } /> | ||
| </PanelBody> | ||
| </InspectorControls> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Determine whether there is block support for padding. | ||
| * | ||
| * @param {string} blockName Block name. | ||
| * @return {boolean} Whether there is support. | ||
| */ | ||
| export function hasSpacingSupport( blockName ) { | ||
aaronrobertshaw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if ( Platform.OS !== 'web' ) { | ||
| return false; | ||
| } | ||
|
|
||
| return hasPaddingSupport( blockName ); | ||
| } | ||
|
|
||
| /** | ||
| * Determines whether spacing support has been disabled. | ||
| * | ||
| * @param {Object} props Block properties. | ||
| * @return {boolean} If spacing support is completely disabled. | ||
| */ | ||
| const useIsSpacingDisabled = ( props = {} ) => { | ||
| const paddingDisabled = useIsPaddingDisabled( props ); | ||
|
|
||
| return paddingDisabled; | ||
| }; | ||
|
|
||
| /** | ||
| * Custom hook to retrieve which padding/margin is supported | ||
| * e.g. top, right, bottom or left. | ||
| * | ||
| * Sides are opted into by default. It is only if a specific side is set to | ||
| * false that it is omitted. | ||
| * | ||
| * @param {string} blockName Block name. | ||
| * @param {string} feature The feature custom sides relate to e.g. padding or margins. | ||
| * @return {Object} Sides supporting custom margin. | ||
| */ | ||
| export function useCustomSides( blockName, feature ) { | ||
| const support = getBlockSupport( blockName, SPACING_SUPPORT_KEY ); | ||
|
|
||
| // Skip when setting is boolean as theme isn't setting arbitrary sides. | ||
| if ( typeof support[ feature ] === 'boolean' ) { | ||
| return; | ||
| } | ||
|
|
||
| return support[ feature ]; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import { | |
| __experimentalBoxControl as BoxControl, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For another time in the future, it looks like the global styles UI could share a lot of code with the hooks (not specific to this PR). cc @nosolosw
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the topic of sharing, there could be some benefit for the block support hooks to be able to access the default theme.json style values e.g. to reset controls correctly. From memory, I don't think these values are currently available within the post editor's state like they are in the site editor. So if a RangeControl is reset it might just be to
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| PanelBody, | ||
| } from '@wordpress/components'; | ||
| import { __experimentalUseCustomSides as useCustomSides } from '@wordpress/block-editor'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
|
|
@@ -42,7 +43,13 @@ const CSS_UNITS = [ | |
| }, | ||
| ]; | ||
|
|
||
| export function useHasSpacingPanel( { supports, name } ) { | ||
| export function useHasSpacingPanel( context ) { | ||
| const hasPadding = useHasPadding( context ); | ||
|
|
||
| return hasPadding; | ||
| } | ||
|
|
||
| export function useHasPadding( { name, supports } ) { | ||
| return ( | ||
| useEditorFeature( 'spacing.customPadding', name ) && | ||
| supports.includes( 'padding' ) | ||
|
|
@@ -65,29 +72,43 @@ function useCustomUnits( { units, contextName } ) { | |
| return usedUnits.length === 0 ? false : usedUnits; | ||
| } | ||
|
|
||
| export default function SpacingPanel( { | ||
| context: { name }, | ||
| getStyle, | ||
| setStyle, | ||
| } ) { | ||
| function filterValuesBySides( values, sides ) { | ||
| if ( ! sides ) { | ||
| // If no custom side configuration all sides are opted into by default. | ||
| return values; | ||
| } | ||
|
|
||
| // Only include sides opted into within filtered values. | ||
| const filteredValues = {}; | ||
| sides.forEach( ( side ) => ( filteredValues[ side ] = values[ side ] ) ); | ||
|
|
||
| return filteredValues; | ||
| } | ||
|
|
||
| export default function SpacingPanel( { context, getStyle, setStyle } ) { | ||
| const { name } = context; | ||
| const showPaddingControl = useHasPadding( context ); | ||
| const units = useCustomUnits( { contextName: name, units: CSS_UNITS } ); | ||
|
|
||
| const paddingValues = getStyle( name, 'padding' ); | ||
| const setPaddingValues = ( { top, right, bottom, left } ) => { | ||
| setStyle( name, 'padding', { | ||
| top: top || paddingValues?.top, | ||
| right: right || paddingValues?.right, | ||
| bottom: bottom || paddingValues?.bottom, | ||
| left: left || paddingValues?.left, | ||
| } ); | ||
| const paddingSides = useCustomSides( name, 'padding' ); | ||
|
|
||
| const setPaddingValues = ( newPaddingValues ) => { | ||
| const padding = filterValuesBySides( newPaddingValues, paddingSides ); | ||
| setStyle( name, 'padding', padding ); | ||
| }; | ||
|
|
||
| return ( | ||
| <PanelBody title={ __( 'Spacing' ) }> | ||
| <BoxControl | ||
| values={ paddingValues } | ||
| onChange={ setPaddingValues } | ||
| label={ __( 'Padding' ) } | ||
| units={ units } | ||
| /> | ||
| { showPaddingControl && ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we hide the panel entirely if there's no padding control?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless I've missed something, we already do. Within The
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I was just wondering why this component (taken separately) renders an empty panel if padding is not shown (I'm sure this will change with the addition of margins though). This is not that important. |
||
| <BoxControl | ||
| values={ paddingValues } | ||
| onChange={ setPaddingValues } | ||
| label={ __( 'Padding' ) } | ||
| sides={ paddingSides } | ||
| units={ units } | ||
| /> | ||
| ) } | ||
| </PanelBody> | ||
| ); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.