-
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.
Min Height: Add height control component with slider (#45875)
* Min Height: Add height control component with slider * Fix typo for rem unit Co-authored-by: Ramon <ramonjd@users.noreply.github.com> * Add some handling for switching between unit types * Add storybook example Co-authored-by: Ramon <ramonjd@users.noreply.github.com>
- Loading branch information
1 parent
2a50308
commit f592820
Showing
8 changed files
with
155 additions
and
25 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
packages/block-editor/src/components/height-control/index.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,123 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo } from '@wordpress/element'; | ||
import { | ||
BaseControl, | ||
RangeControl, | ||
Flex, | ||
FlexItem, | ||
__experimentalSpacer as Spacer, | ||
__experimentalUseCustomUnits as useCustomUnits, | ||
__experimentalUnitControl as UnitControl, | ||
__experimentalParseQuantityAndUnitFromRawValue as parseQuantityAndUnitFromRawValue, | ||
} from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useSetting from '../use-setting'; | ||
|
||
const RANGE_CONTROL_CUSTOM_SETTINGS = { | ||
px: { max: 1000, step: 1 }, | ||
'%': { max: 100, step: 1 }, | ||
vw: { max: 100, step: 1 }, | ||
vh: { max: 100, step: 1 }, | ||
em: { max: 50, step: 0.1 }, | ||
rem: { max: 50, step: 0.1 }, | ||
}; | ||
|
||
export default function HeightControl( { | ||
onChange, | ||
label = __( 'Height' ), | ||
value, | ||
} ) { | ||
const customRangeValue = parseFloat( value ); | ||
|
||
const units = useCustomUnits( { | ||
availableUnits: useSetting( 'spacing.units' ) || [ | ||
'%', | ||
'px', | ||
'em', | ||
'rem', | ||
'vh', | ||
'vw', | ||
], | ||
} ); | ||
|
||
const selectedUnit = | ||
useMemo( | ||
() => parseQuantityAndUnitFromRawValue( value ), | ||
[ value ] | ||
)[ 1 ] || | ||
units[ 0 ]?.value || | ||
'px'; | ||
|
||
const handleSliderChange = ( next ) => { | ||
onChange( [ next, selectedUnit ].join( '' ) ); | ||
}; | ||
|
||
const handleUnitChange = ( newUnit ) => { | ||
// Attempt to smooth over differences between currentUnit and newUnit. | ||
// This should slightly improve the experience of switching between unit types. | ||
const [ currentValue, currentUnit ] = | ||
parseQuantityAndUnitFromRawValue( value ); | ||
|
||
if ( [ 'em', 'rem' ].includes( newUnit ) && currentUnit === 'px' ) { | ||
// Convert pixel value to an approximate of the new unit, assuming a root size of 16px. | ||
onChange( ( currentValue / 16 ).toFixed( 2 ) + newUnit ); | ||
} else if ( | ||
[ 'em', 'rem' ].includes( currentUnit ) && | ||
newUnit === 'px' | ||
) { | ||
// Convert to pixel value assuming a root size of 16px. | ||
onChange( Math.round( currentValue * 16 ) + newUnit ); | ||
} else if ( | ||
[ 'vh', 'vw', '%' ].includes( newUnit ) && | ||
currentValue > 100 | ||
) { | ||
// When converting to `vh`, `vw`, or `%` units, cap the new value at 100. | ||
onChange( 100 + newUnit ); | ||
} | ||
}; | ||
|
||
return ( | ||
<fieldset className="block-editor-height-control"> | ||
<BaseControl.VisualLabel as="legend"> | ||
{ label } | ||
</BaseControl.VisualLabel> | ||
<Flex> | ||
<FlexItem isBlock> | ||
<UnitControl | ||
value={ value } | ||
units={ units } | ||
onChange={ onChange } | ||
onUnitChange={ handleUnitChange } | ||
min={ 0 } | ||
size={ '__unstable-large' } | ||
/> | ||
</FlexItem> | ||
<FlexItem isBlock> | ||
<Spacer marginX={ 2 } marginBottom={ 0 }> | ||
<RangeControl | ||
value={ customRangeValue } | ||
min={ 0 } | ||
max={ | ||
RANGE_CONTROL_CUSTOM_SETTINGS[ selectedUnit ] | ||
?.max ?? 100 | ||
} | ||
step={ | ||
RANGE_CONTROL_CUSTOM_SETTINGS[ selectedUnit ] | ||
?.step ?? 0.1 | ||
} | ||
withInputField={ false } | ||
onChange={ handleSliderChange } | ||
__nextHasNoMarginBottom | ||
/> | ||
</Spacer> | ||
</FlexItem> | ||
</Flex> | ||
</fieldset> | ||
); | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/block-editor/src/components/height-control/stories/index.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,21 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import HeightControl from '../'; | ||
|
||
export default { | ||
component: HeightControl, | ||
title: 'BlockEditor/HeightControl', | ||
}; | ||
|
||
const Template = ( props ) => { | ||
const [ value, setValue ] = useState(); | ||
return <HeightControl onChange={ setValue } value={ value } { ...props } />; | ||
}; | ||
|
||
export const Default = Template.bind( {} ); |
5 changes: 5 additions & 0 deletions
5
packages/block-editor/src/components/height-control/style.scss
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,5 @@ | ||
.block-editor-height-control { | ||
border: 0; | ||
margin: 0; | ||
padding: 0; | ||
} |
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
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
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
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
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