-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Flex component - add 'direction' prop. #30261
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
Conversation
| // @ts-ignore | ||
| direction={ simplifiedDirection } |
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.
TS was angry about this potentially being undefined when we have noted it as a string. But if we look at the code above, it would always be a string. So @ts-ignore for now unless there are better suggestions.
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.
simplifiedDirection is a string beucase of direction.slice( 0, -8). That's why TS is raging. I'm not sure if it is possible to force a type on a specific variable with JSDoc. simplifiedDirection as FlexDirection could fix the problem in TS.
I think you are better off with an explicit function types-wise:
/** @typedef {import('react').CSSProperties['flexDirection'] | 'row' | 'column'} FlexDirection */
^^^ put this into `/* eslint-disable jsdoc/valid-types */` above OwnProps
* @property {FlexDirection} direction Sets the flex-direction property.
^^^
and replace the type in OwnProps
/**
*
* @param {FlexDirection} direction
* @return {FlexDirection} direction
*/
function simplifyDirection( direction ) {
if ( direction === 'column-reverse' ) {
return 'column';
}
if ( direction === 'row-reverse' ) {
return 'row';
}
return direction;
}then you can use it like
const simplifiedDirection = simplifyDirection( direction );and you can remove the @ts-ignore
|
Size Change: +4.68 kB (0%) Total Size: 1.42 MB
ℹ️ View Unchanged
|
I'm not quite a fan of this. 🤔 If we want to support flex-direction's Otherwise, we might as well keep |
|
Given the conflict with |
I think this makes sense, to clarify: So would that mean use |
|
I was thinking something like this: function Flex( { direction, isReversed } ) {
const directionArray = direction.split( '-' );
let actualDirection = direction[0];
if ( isReversed && directionArray.length === 0 ) {
actualDirection += '-reverse';
}
return <div style={ { flexDirection: actualDirection } } />;
} |
|
Sorry for closing, hit the wrong button! |
Im confused, in that example if |
|
Going off @sarayourfriend's comment about deprecating direction overrides isReversed: or or something else? |
@Addison-Stavlo let's ignore my error of not considering the difference between Currently the component only support For this reason, I'm inclined to either ignore Don't have a strong opinion either way. I would conceptually like that |
Yeah, that might make the most sense. 🤔 |
|
To me deprecation means this: function useDirection({ isReversed, direction }) {
if ( typeof isReversed !== 'undefined' ) {
deprecated( 'Flex isReversed', { alternative: 'Flex direction="row-reverse"' } );
if ( isReversed ) {
return 'row-reverse';
}
return 'row';
}
return direction;
}And then the component just does: function Flex( props ) {
const direction = useDirection( props ):
const { otherProp } = props;
// ...That way the component can ignore |
|
Thanks @sarayourfriend! That clarifies the idea a bit for sure. 😁 This seems to be working as expected with 117dcc7 .
|
|
Works well for me! How about adding some stories? |
|
Duh, looks like #31297 superseded this PR. |
|
Yeah, I'm happy with that 😁 . Now that it is merged il close this out. |

Description
I noticed this neat
Flexcomponent and went to use it in a vertical/column direction. It seems that currently the vertical orientation (flex-direction: column) is not supported. This PR attempts to add this functionality.This is slightly odd since the currently existing
isReversedfunction sets theflex-directioncss property by assuming row. Lets consider deprecatingisReversedin favor ofdirection. If theisReversedprop is present, a deprecation warning is thrown and the component has the same behavior as it previously did. Otherwise, we go with the newdirectionprop defaulting to 'row'.How has this been tested?
Test the component with the deprecated
isReversedprop and ensure their are no regressions.Test the component with the new
directionprop and ensure settings work as expected.Screenshots
Types of changes
Checklist: