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

Full Height Alignment Toolbar: Implementation + Cover integration. #26615

Merged
merged 16 commits into from
Nov 23, 2020
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Full Height Toolbar Toolbar

Unlike the block alignment options, `Full Height Alignment` can be applied to a block in an independent way. But also, it works very well together with these block alignment options, where the combination empowers the design-layout capability.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { ToolbarGroup, ToolbarButton } from '@wordpress/components';
import { fullscreen } from '@wordpress/icons';

function BlockFullHeightAlignmentToolbar( {
isActive,
label = __( 'Toggle full height' ),
onToggle,
} ) {
return (
<ToolbarGroup>
<ToolbarButton
isActive={ isActive }
icon={ fullscreen }
label={ label }
onClick={ () => onToggle( ! isActive ) }
/>
</ToolbarGroup>
);
}

export default BlockFullHeightAlignmentToolbar;
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './font-sizes';
export { default as AlignmentToolbar } from './alignment-toolbar';
export { default as Autocomplete } from './autocomplete';
export { default as BlockAlignmentToolbar } from './block-alignment-toolbar';
export { default as __experimentalBlockFullHeightAligmentToolbar } from './block-full-height-alignment-toolbar';
export { default as __experimentalBlockAlignmentMatrixToolbar } from './block-alignment-matrix-toolbar';
export { default as BlockBreadcrumb } from './block-breadcrumb';
export { BlockContextProvider } from './block-context';
Expand Down
45 changes: 42 additions & 3 deletions packages/block-library/src/cover/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
__experimentalPanelColorGradientSettings as PanelColorGradientSettings,
__experimentalUnitControl as UnitControl,
__experimentalBlockAlignmentMatrixToolbar as BlockAlignmentMatrixToolbar,
__experimentalBlockFullHeightAligmentToolbar as FullHeightAlignment,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { withDispatch } from '@wordpress/data';
Expand Down Expand Up @@ -89,6 +90,7 @@ function CoverHeightInput( {
value = '',
} ) {
const [ temporaryInput, setTemporaryInput ] = useState( null );

const instanceId = useInstanceId( UnitControl );
const inputId = `block-cover-height-input-${ instanceId }`;
const isPx = unit === 'px';
Expand Down Expand Up @@ -264,6 +266,39 @@ function CoverEdit( {
const onSelectMedia = attributesFromMedia( setAttributes );
const isBlogUrl = isBlobURL( url );

const [ prevMinHeightValue, setPrevMinHeightValue ] = useState( minHeight );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these two state values could share the same useState call the value being an object with properties minHeight and minHeightUnit that could be directly passed to set attributes. These change also aligns with probable change we will have to do make minHeightUnit and minHeight a single attribute (a string that can contain multiple CSS valid properties).

If we don't initiate the state of previous to minHeight, minHeightUnit e.g: keep them undefined and also follow the suggestion of setting the previous values inside toggleMinFullHeight we can also probably remove the need for this condition bellow "// If there aren't previous values, take the default ones.".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice suggestion. Going to refactoring using an object.

These change also aligns with probable change we will have to do make minHeightUnit and minHeight a single attribute (a string that can contain multiple CSS valid properties).

💯 agree.
I did this approach in the Background Size implementation. The data is stored in the backgroundSize attribute (string)ready to be used in the inline style. The block takes over of parsing and converting from/to this attribute to get the data usable by the UI components.

Happy to do this in a follow-up PR.

const [ prevMinHeightUnit, setPrevMinHeightUnit ] = useState(
minHeightUnit
);
const isMinFullHeight = minHeightUnit === 'vh' && minHeight === 100;

const toggleMinFullHeight = () => {
if ( isMinFullHeight ) {
// If there aren't previous values, take the default ones.
if ( prevMinHeightUnit === 'vh' && prevMinHeightValue === 100 ) {
return setAttributes( {
minHeight: undefined,
minHeightUnit: undefined,
} );
}

// Set the previous values of height.
return setAttributes( {
minHeight: prevMinHeightValue,
minHeightUnit: prevMinHeightUnit,
} );
}

setPrevMinHeightValue( minHeight );
setPrevMinHeightUnit( minHeightUnit );

// Set full height.
return setAttributes( {
minHeight: 100,
minHeightUnit: 'vh',
} );
};

const toggleParallax = () => {
setAttributes( {
hasParallax: ! hasParallax,
Expand Down Expand Up @@ -322,6 +357,10 @@ function CoverEdit( {
const controls = (
<>
<BlockControls>
<FullHeightAlignment
isActive={ isMinFullHeight }
onToggle={ toggleMinFullHeight }
/>
{ hasBackground && (
<>
<BlockAlignmentMatrixToolbar
Expand Down Expand Up @@ -405,11 +444,11 @@ function CoverEdit( {
onChange={ ( newMinHeight ) =>
setAttributes( { minHeight: newMinHeight } )
}
onUnitChange={ ( nextUnit ) => {
onUnitChange={ ( nextUnit ) =>
setAttributes( {
minHeightUnit: nextUnit,
} );
} }
} )
}
/>
</PanelBody>
<PanelColorGradientSettings
Expand Down