-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Min Height: Add height control component with slider #45875
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 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /** | ||
| * 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 CUSTOM_VALUE_SETTINGS = { | ||
| px: { max: 1000, steps: 1 }, | ||
| '%': { max: 100, steps: 1 }, | ||
| vw: { max: 100, steps: 1 }, | ||
| vh: { max: 100, steps: 1 }, | ||
| em: { max: 50, steps: 0.1 }, | ||
| rm: { max: 50, steps: 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; | ||
|
||
|
|
||
| const handleSliderChange = ( next ) => { | ||
| onChange( [ next, selectedUnit ].join( '' ) ); | ||
| }; | ||
|
|
||
| return ( | ||
| <fieldset className="component-height-control"> | ||
| <BaseControl.VisualLabel as="legend"> | ||
| { label } | ||
| </BaseControl.VisualLabel> | ||
| <Flex> | ||
| <FlexItem isBlock> | ||
| <UnitControl | ||
| value={ value } | ||
| units={ units } | ||
| onChange={ onChange } | ||
| min={ 0 } | ||
| size={ '__unstable-large' } | ||
| /> | ||
| </FlexItem> | ||
| <FlexItem isBlock> | ||
| <Spacer marginX={ 2 } marginBottom={ 0 }> | ||
| <RangeControl | ||
| value={ customRangeValue } | ||
| min={ 0 } | ||
| max={ | ||
| CUSTOM_VALUE_SETTINGS[ selectedUnit ]?.max ?? 10 | ||
|
||
| } | ||
| step={ | ||
| CUSTOM_VALUE_SETTINGS[ selectedUnit ]?.steps ?? | ||
| 0.1 | ||
| } | ||
| withInputField={ false } | ||
| onChange={ handleSliderChange } | ||
| /> | ||
| </Spacer> | ||
| </FlexItem> | ||
| </Flex> | ||
| </fieldset> | ||
| ); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.