Skip to content
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

Add layout content size controls to global styles #42309

Merged
merged 7 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 107 additions & 1 deletion packages/edit-site/src/components/global-styles/dimensions-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import {
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
__experimentalBoxControl as BoxControl,
__experimentalHStack as HStack,
__experimentalUnitControl as UnitControl,
__experimentalUseCustomUnits as useCustomUnits,
__experimentalView as View,
} from '@wordpress/components';
import { __experimentalUseCustomSides as useCustomSides } from '@wordpress/block-editor';
import { Icon, positionCenter, stretchWide } from '@wordpress/icons';

/**
* Internal dependencies
Expand All @@ -19,11 +22,27 @@ import { getSupportedGlobalStylesPanels, useSetting, useStyle } from './hooks';
const AXIAL_SIDES = [ 'horizontal', 'vertical' ];

export function useHasDimensionsPanel( name ) {
const hasContentSize = useHasContentSize( name );
const hasWideSize = useHasWideSize( name );
const hasPadding = useHasPadding( name );
const hasMargin = useHasMargin( name );
const hasGap = useHasGap( name );

return hasPadding || hasMargin || hasGap;
return hasContentSize || hasWideSize || hasPadding || hasMargin || hasGap;
}

function useHasContentSize( name ) {
const supports = getSupportedGlobalStylesPanels( name );
const [ settings ] = useSetting( 'layout.contentSize', name );

return settings && supports.includes( 'contentSize' );
}

function useHasWideSize( name ) {
const supports = getSupportedGlobalStylesPanels( name );
const [ settings ] = useSetting( 'layout.wideSize', name );

return settings && supports.includes( 'wideSize' );
}

function useHasPadding( name ) {
Expand Down Expand Up @@ -86,6 +105,8 @@ function splitStyleValue( value ) {
}

export default function DimensionsPanel( { name } ) {
const showContentSizeControl = useHasContentSize( name );
const showWideSizeControl = useHasWideSize( name );
const showPaddingControl = useHasPadding( name );
const showMarginControl = useHasMargin( name );
const showGapControl = useHasGap( name );
Expand All @@ -99,6 +120,34 @@ export default function DimensionsPanel( { name } ) {
],
} );

const [ contentSizeValue, setContentSizeValue ] = useSetting(
'layout.contentSize',
name
);

const [ userSetContentSizeValue ] = useSetting(
'layout.contentSize',
name,
'user'
);

const hasUserSetContentSizeValue = () => !! userSetContentSizeValue;
const resetContentSizeValue = () => setContentSizeValue( '' );

const [ wideSizeValue, setWideSizeValue ] = useSetting(
'layout.wideSize',
name
);

const [ userSetWideSizeValue ] = useSetting(
'layout.wideSize',
name,
'user'
);

const hasUserSetWideSizeValue = () => !! userSetWideSizeValue;
const resetWideSizeValue = () => setWideSizeValue( '' );

const [ rawPadding, setRawPadding ] = useStyle( 'spacing.padding', name );
const paddingValues = splitStyleValue( rawPadding );
const paddingSides = useCustomSides( name, 'padding' );
Expand Down Expand Up @@ -137,10 +186,67 @@ export default function DimensionsPanel( { name } ) {
resetPaddingValue();
resetMarginValue();
resetGapValue();
resetContentSizeValue();
resetWideSizeValue();
};

return (
<ToolsPanel label={ __( 'Dimensions' ) } resetAll={ resetAll }>
{ ( showContentSizeControl || showWideSizeControl ) && (
<span className="span-columns">
{ __( 'Set the width of the main content area.' ) }
</span>
) }
{ showContentSizeControl && (
<ToolsPanelItem
className="single-column"
label={ __( 'Content size' ) }
hasValue={ hasUserSetContentSizeValue }
onDeselect={ resetContentSizeValue }
isShownByDefault={ true }
>
<HStack alignment="flex-end">
<UnitControl
label={ __( 'Content' ) }
labelPosition="top"
__unstableInputWidth="80px"
value={ contentSizeValue || '' }
onChange={ ( nextContentSize ) => {
setContentSizeValue( nextContentSize );
} }
units={ units }
/>
<View>
<Icon icon={ positionCenter } />
</View>
</HStack>
</ToolsPanelItem>
) }
{ showWideSizeControl && (
<ToolsPanelItem
className="single-column"
label={ __( 'Wide size' ) }
hasValue={ hasUserSetWideSizeValue }
onDeselect={ resetWideSizeValue }
isShownByDefault={ true }
>
<HStack alignment="flex-end">
<UnitControl
label={ __( 'Wide' ) }
labelPosition="top"
__unstableInputWidth="80px"
value={ wideSizeValue || '' }
onChange={ ( nextWideSize ) => {
setWideSizeValue( nextWideSize );
} }
units={ units }
/>
<View>
<Icon icon={ stretchWide } />
</View>
</HStack>
</ToolsPanelItem>
) }
{ showPaddingControl && (
<ToolsPanelItem
hasValue={ hasPaddingValue }
Expand Down
2 changes: 2 additions & 0 deletions packages/edit-site/src/components/global-styles/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ const ROOT_BLOCK_SUPPORTS = [
'textDecoration',
'textTransform',
'padding',
'contentSize',
'wideSize',
];

export function getSupportedGlobalStylesPanels( name ) {
Expand Down
4 changes: 4 additions & 0 deletions packages/edit-site/src/components/sidebar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
grid-column: span 1;
}

.edit-site-global-styles-sidebar .components-tools-panel .span-columns {
grid-column: 1 / -1;
}

.edit-site-global-styles-sidebar__blocks-group {
padding-top: $grid-unit-30;
border-top: $border-width solid $gray-200;
Expand Down