-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Min Height: Add height control component with slider #45875
Changes from 2 commits
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 }, | ||
rem: { 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From memory, I think it's possible we can get an empty units array. Should we guard against that edge case here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call, I've added a fallback |
||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the thinking behind a default max of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, this one was just a copy+paste that I hadn't considered. From looking over some of the other possible unit types that aren't covered here, I think maybe a default max of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A default of |
||
} | ||
step={ | ||
CUSTOM_VALUE_SETTINGS[ selectedUnit ]?.steps ?? | ||
0.1 | ||
} | ||
withInputField={ false } | ||
onChange={ handleSliderChange } | ||
/> | ||
</Spacer> | ||
</FlexItem> | ||
</Flex> | ||
</fieldset> | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was it a deliberate choice to increase the step value for
em
andrem
values from other controls (0.01
)? The PR description explains the higher max values only.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, this one was also a copy + paste, but the intention of these custom step values is for it to only apply to the range control instead of the unit control. With the unit control it's important that the step value is small enough to support specific values that folks might use (so
.25
is a useful fraction of anem
orrem
value for example). However, for the range control, I think it's valuable to use a slightly larger step value for the case of a user with the slider thumb focused pressing left/right keys. I've updated the PR description to include the following:I've also renamed the constant so that it's hopefully a bit clearer that it only applies to the range control. Also, happy to go with different defaults if this feels off 🙂