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

Cover: Add custom unit support for height controls #20888

Merged
merged 31 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
5ff8a7e
Add unit-control component
Mar 12, 2020
52bc8e5
Add G2 color values to color utils. Apply to UnitControl
Mar 12, 2020
b1676f5
Add numeric inputmode for UnitControl
Mar 12, 2020
c608b21
Add TODO note for NumberControl
Mar 12, 2020
cfb2f5a
Add NumberControl for better increment/decrement handling
Mar 12, 2020
8ba87a1
Refactor UnitControl and NumberControl. Add README files for components
Mar 13, 2020
c8641ea
Adjust UnitSelectControl rendering
Mar 13, 2020
9fd0b31
Improve integration of UnitControl with Cover Block
Mar 13, 2020
0c1b6b5
Adjust default minHeightUnit
Mar 13, 2020
2210d42
Fix default height for Cover block
Mar 13, 2020
125405f
Add add_theme_support for custom units
Mar 13, 2020
c411186
Adjust styles for UnitLabel
Mar 13, 2020
fc8e56d
Add unit tests for NumberControl. Update snapshot for UnitControl.
Mar 14, 2020
34a4bab
Abstract UnitControl to block-editor component
Mar 14, 2020
1dcc082
Refactor to remove getComputedSize. Set unitized height directly with…
Mar 15, 2020
50ed451
Improve reset values for UnitControl
Mar 15, 2020
8b43e60
Enable support for secondary parameters for `add_theme_support( 'disa…
Mar 17, 2020
7691e30
Add comments for unit filtering
Mar 17, 2020
a3998f3
Rename disable-custom-units to experimental-custom-units
Mar 19, 2020
2b6ea59
Provide return description for filterUnitsWithSettings util
Mar 19, 2020
34bafa0
Improve logic clarity of Cover minHeight style
Mar 19, 2020
23e79b7
Add dev notes for G2 color map
Mar 19, 2020
1b3a583
Default minHeightUnit of Cover to `px`
Mar 19, 2020
0a65b6d
Adjust colors to deep merge without mutation
Mar 20, 2020
8eaf5a6
Adjust label of Cover height control
Mar 25, 2020
455cb04
Remove unnecessary test comments from number-control test
Mar 27, 2020
94b3a85
Add tests for UnitControl
Mar 27, 2020
52882de
Update Storybook snapshots
Mar 27, 2020
95a7024
Remove default px value for minHeightUnit in Cover
Mar 30, 2020
fab4f91
Update PHP comments for line height and unit filters
Mar 30, 2020
0d8f031
Fix PHP lint issues
Mar 30, 2020
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
Prev Previous commit
Next Next commit
Add add_theme_support for custom units
  • Loading branch information
Jon Q committed Mar 30, 2020
commit 125405fd8e1b665d0f51d8777e6a53e889283e89
19 changes: 14 additions & 5 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -649,18 +649,27 @@ function gutenberg_extend_settings_block_patterns( $settings ) {

/**
* Extends block editor settings to determine whether to use custom line height controls.
* Currently experimental.
*
* @param array $settings Default editor settings.
*
* @return array Filtered editor settings.
*/
function gutenberg_extend_settings_custom_line_height( $settings ) {
$settings['__experimentalDisableCustomLineHeight'] = get_theme_support( 'disable-custom-line-height' );
return $settings;
}
add_filter( 'block_editor_settings', 'gutenberg_extend_settings_custom_line_height' );

/**
* Extends block editor settings to determine whether to use custom unit controls.
* Currently experimental.
*
* @param array $settings Default editor settings.
*
* @return array Filtered editor settings.
*/
function gutenberg_extend_settings_custom_units( $settings ) {
$settings['__experimentalDisableCustomUnits'] = get_theme_support( 'disable-custom-units' );
Copy link
Member

Choose a reason for hiding this comment

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

Should this support a second argument where the theme can say which should be the unit shown / used? Like get_theme_support( 'disable-custom-units', 'rem' ) would force rem to be used in all unit-based selectors.

Copy link
Author

Choose a reason for hiding this comment

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

Good suggestion! Let me make that adjustment ✨

return $settings;
}
add_filter( 'block_editor_settings', 'gutenberg_extend_settings_custom_units' );

/*
* Register default patterns if not registered in Core already.
*/
Expand Down
15 changes: 13 additions & 2 deletions packages/block-library/src/cover/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
__experimentalPanelColorGradientSettings as PanelColorGradientSettings,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { withDispatch } from '@wordpress/data';
import { useSelect, withDispatch } from '@wordpress/data';
import { cover as icon } from '@wordpress/icons';

/**
Expand Down Expand Up @@ -73,6 +73,15 @@ function retrieveFastAverageColor() {
return retrieveFastAverageColor.fastAverageColor;
}

export function useIsCustomUnitsDisabled() {
const isDisabled = useSelect( ( select ) => {
const { getSettings } = select( 'core/block-editor' );
return !! getSettings().__experimentalDisableCustomUnits;
}, [] );

return isDisabled;
}

const { __getComputedSize: getComputedUnitSize } = UnitControl;

function CoverHeightInput( {
Expand All @@ -85,6 +94,7 @@ function CoverHeightInput( {
const instanceId = useInstanceId( UnitControl );
const inputId = `block-cover-height-input-${ instanceId }`;
const isPx = unit === 'px';
const isCustomUnitsDisabled = useIsCustomUnitsDisabled();

const handleOnChange = ( unprocessedValue ) => {
const inputValue =
Expand All @@ -108,6 +118,7 @@ function CoverHeightInput( {

const inputValue = temporaryInput !== null ? temporaryInput : value;
const min = isPx ? COVER_MIN_HEIGHT : 0;
const units = isCustomUnitsDisabled ? false : CSS_UNITS;

return (
<BaseControl label={ __( 'Minimum height in pixels' ) } id={ inputId }>
Copy link
Member

Choose a reason for hiding this comment

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

This label is no longer entirely accurate.

Expand All @@ -120,7 +131,7 @@ function CoverHeightInput( {
step="1"
style={ { maxWidth: 80 } }
unit={ unit }
units={ CSS_UNITS }
units={ units }
value={ inputValue }
/>
</BaseControl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function UnitSelectControl( {
size = 'default',
value = 'px',
} ) {
if ( isEmpty( options ) || options.length === 1 ) {
if ( isEmpty( options ) || options.length === 1 || options === false ) {
return <UnitLabel size={ size }>{ value }</UnitLabel>;
}

Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class EditorProvider extends Component {
'templateLock',
'titlePlaceholder',
'onUpdateDefaultBlockStyles',
'__experimentalDisableCustomUnits',
'__experimentalEnableLegacyWidgetBlock',
'__experimentalBlockDirectory',
'__experimentalEnableFullSiteEditing',
Expand Down