-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
BoxControl: Add support for grouped directions (vertical and horizontal controls) #32610
Changes from 5 commits
54e917b
8aea636
f6c6e35
43c68cb
40f5a5f
785ed95
f6a8372
90352e7
0f95bd0
1f933f0
a940bc7
4af702e
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,103 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { noop } from 'lodash'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import UnitControl from './unit-control'; | ||
import { LABELS } from './utils'; | ||
import { Layout } from './styles/box-control-styles'; | ||
|
||
const groupedSides = [ 'vertical', 'horizontal' ]; | ||
|
||
export default function VerticalHorizontalInputControls( { | ||
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. It seems we could call this 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, I like that name! Thanks for the suggestion, I can put up a small PR to rename it 👍 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. Great, thanks! |
||
onChange = noop, | ||
andrewserong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
onFocus = noop, | ||
onHoverOn = noop, | ||
onHoverOff = noop, | ||
values, | ||
sides, | ||
...props | ||
} ) { | ||
const createHandleOnFocus = ( side ) => ( event ) => { | ||
onFocus( event, { side } ); | ||
}; | ||
|
||
const createHandleOnHoverOn = ( side ) => () => { | ||
if ( side === 'vertical' ) { | ||
onHoverOn( { | ||
top: true, | ||
bottom: true, | ||
} ); | ||
} else { | ||
onHoverOn( { | ||
left: true, | ||
right: true, | ||
} ); | ||
} | ||
}; | ||
|
||
const createHandleOnHoverOff = ( side ) => () => { | ||
if ( side === 'vertical' ) { | ||
onHoverOff( { | ||
top: false, | ||
bottom: false, | ||
} ); | ||
} else { | ||
onHoverOff( { | ||
left: false, | ||
right: false, | ||
} ); | ||
} | ||
}; | ||
|
||
const createHandleOnChange = ( side ) => ( next ) => { | ||
const nextValues = { ...values }; | ||
|
||
if ( side === 'vertical' ) { | ||
nextValues.top = next; | ||
nextValues.bottom = next; | ||
} | ||
if ( side === 'horizontal' ) { | ||
andrewserong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
nextValues.left = next; | ||
nextValues.right = next; | ||
} | ||
|
||
onChange( nextValues ); | ||
}; | ||
|
||
// Filter sides if custom configuration provided, maintaining default order. | ||
const filteredSides = sides?.length | ||
? groupedSides.filter( ( side ) => sides.includes( side ) ) | ||
: groupedSides; | ||
|
||
const first = filteredSides[ 0 ]; | ||
const last = filteredSides[ filteredSides.length - 1 ]; | ||
const only = first === last && first; | ||
andrewserong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return ( | ||
<Layout | ||
gap={ 0 } | ||
align="top" | ||
className="component-box-control__vertical-horizontal-input-controls" | ||
> | ||
{ filteredSides.map( ( side ) => ( | ||
<UnitControl | ||
{ ...props } | ||
isFirst={ first === side } | ||
isLast={ last === side } | ||
isOnly={ only === side } | ||
value={ 'vertical' === side ? values.top : values.left } | ||
onChange={ createHandleOnChange( side ) } | ||
onFocus={ createHandleOnFocus( side ) } | ||
onHoverOn={ createHandleOnHoverOn( side ) } | ||
onHoverOff={ createHandleOnHoverOff( side ) } | ||
label={ LABELS[ side ] } | ||
key={ `box-control-${ side }` } | ||
andrewserong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/> | ||
) ) } | ||
</Layout> | ||
); | ||
} |
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.
Nit: For this to be more semantic english, should we use
has
instead ofis
here givendirections
is plural?An even more descriptive prop name might be like
splitOnAxis
or something like that. Grouped directions could mean corners are grouped together for example.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.
Thanks, I love some good naming tips, the
is
never sit right with the plural for me. I actually quite likesplitOnAxis
, so I've updated this PR to use that instead. Happy to rename again if you think there's a better option.