Skip to content
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

Fix HStack and VStack alignment prop #47914

Merged
merged 7 commits into from
Mar 2, 2023
Merged

Conversation

youknowriad
Copy link
Contributor

What?

While working on #47827 I've noticed a bug in VStack component. The alignment prop was not working as expected. It turns out that there are several bugs in the alignment prop of both VStack and HStack, This PR fixes it.

Testing Instructions

1- Open the storybook of both VStack and HStack
2- Check that all the alignment options behave as expected.

@youknowriad youknowriad added [Type] Bug An existing feature does not function as intended [Feature] UI Components Impacts or related to the UI component system labels Feb 9, 2023
@youknowriad youknowriad self-assigned this Feb 9, 2023
@@ -62,7 +62,7 @@ export function useFlex( props: WordPressComponentProps< FlexProps, 'div' > ) {

const classes = useMemo( () => {
const base = css( {
alignItems: isColumn ? 'normal' : align,
alignItems: align,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the most important change. I'm not sure I understand the reason for the check originally, I checked the original PR and it's not mentioned.

@@ -11,7 +11,7 @@ import { isValueDefined } from '../utils/values';

const H_ALIGNMENTS: Alignments = {
bottom: { align: 'flex-end', justify: 'center' },
bottomLeft: { align: 'flex-start', justify: 'flex-end' },
bottomLeft: { align: 'flex-end', justify: 'flex-start' },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was buggy (inverted values)

@@ -25,13 +25,13 @@ const H_ALIGNMENTS: Alignments = {

const V_ALIGNMENTS: Alignments = {
bottom: { justify: 'flex-end', align: 'center' },
bottomLeft: { justify: 'flex-start', align: 'flex-end' },
bottomLeft: { justify: 'flex-end', align: 'flex-start' },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was buggy (inverted values)

bottomRight: { justify: 'flex-end', align: 'flex-end' },
center: { justify: 'center', align: 'center' },
edge: { justify: 'space-between', align: 'center' },
left: { justify: 'center', align: 'flex-start' },
right: { justify: 'center', align: 'flex-end' },
stretch: { justify: 'stretch' },
stretch: { align: 'stretch' },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was buggy too.

@ciampo ciampo added the [Package] Components /packages/components label Feb 9, 2023
@github-actions
Copy link

github-actions bot commented Feb 9, 2023

Flaky tests detected in 172ee45.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4265445081
📝 Reported issues:

@youknowriad
Copy link
Contributor Author

Any thoughts on this PR @ciampo

@ciampo
Copy link
Contributor

ciampo commented Feb 24, 2023

It's in my review queue — I hope to get to it by EOD.

I've been working on implementing some VizReg tests specifically on HStack and VStack (#48260) in the meantime, which will help while reviewing this PR.

Copy link
Contributor

@ciampo ciampo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright — I took a first look at the code changes! Since it's quite tricky to understand the full implications of these changes, I used the recently introduced Visual Regression tests to compare this PR against trunk.

Here's what I'm doing to test this PR with VizReg
  1. Checkout trunk
  2. Install latest version of dependencies: npm run distclean && npm ci
  3. Install PlayWright: npx playwright install
  4. Generate snapshots:
    • In one terminal window, run Storybook for VizReg: npm run storybook:e2e:dev
    • Once Storybook is up and running, generate Playwright snapshots: npm run test:e2e:storybook -- --update-snapshots
  5. Checkout this PR
  6. Compare snapshots:
    • In one terminal window, run Storybook for VizReg: npm run storybook:e2e:dev
    • Once Storybook is up and running, compare VizReg snapshots: npm run test:e2e:storybook

I also rebased on top of trunk to include the latest VizReg changes

packages/components/src/v-stack/hook.ts Outdated Show resolved Hide resolved
@@ -62,7 +62,7 @@ export function useFlex( props: WordPressComponentProps< FlexProps, 'div' > ) {

const classes = useMemo( () => {
const base = css( {
alignItems: isColumn ? 'normal' : align,
alignItems: align ?? ( isColumn ? 'normal' : 'center' ),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change definitely makes sense, but I'm not sure if the new behaviour of HStack is 100% correct either.

I was taking a look at HStack when direction="column" , and I was playing around with the alignment prop:

  • on trunk, in this case changing the alignment prop doesn't seem to make any differences
  • on this PR, it seems to work as expected, except for the alignment="edge" case - the items appear to be centered, but I believed they should be stretched (especially given the description of the edge value: "Aligns content to the edges of the container.")
Looks like this change would fix it
diff --git a/packages/components/src/h-stack/utils.ts b/packages/components/src/h-stack/utils.ts
index b1413ee2dc..60151b00a2 100644
--- a/packages/components/src/h-stack/utils.ts
+++ b/packages/components/src/h-stack/utils.ts
@@ -28,7 +28,7 @@ const V_ALIGNMENTS: Alignments = {
 	bottomLeft: { justify: 'flex-end', align: 'flex-start' },
 	bottomRight: { justify: 'flex-end', align: 'flex-end' },
 	center: { justify: 'center', align: 'center' },
-	edge: { justify: 'space-between', align: 'center' },
+	edge: { justify: 'space-between', align: 'stretch' },
 	left: { justify: 'center', align: 'flex-start' },
 	right: { justify: 'center', align: 'flex-end' },
 	stretch: { align: 'stretch' },

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember properly, I based the behavior of "edge" here by comparing the behavior of "h" in HStack and applied the same behavior but in the other axis.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As much as I don't love the arbitrariness of this align prop, I agree with Riad that this new edge behavior for VStack is correct/consistent within the existing logic of the system.

In this system, I understand stretch to apply only to the cross axis, and edge to apply only to the main axis.

How about we tweak the description to make that clearer?

-	 * * `edge`: Aligns content to the edges of the container.
+	 * * `edge`: Justifies content to be evenly spread out up to the main axis edges of the container.
-	 * * `stretch`: Stretches content to the edges of the container.
+	 * * `stretch`: Stretches content to the cross axis edges of the container.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you both.

Given that the separation between breaking change and bug fix can be blurry here, I agree with Lena's proposed solution

@ciampo ciampo force-pushed the fix/vstack-hstack-alignments branch from 02d893c to 172ee45 Compare February 24, 2023 19:03
Comment on lines +49 to +57
<VStack
{ ...props }
style={ { background: '#eee', minHeight: '200px' } }
>
{ [ 'One', 'Two', 'Three', 'Four', 'Five' ].map( ( text ) => (
<View key={ text } style={ { background: '#b9f9ff' } }>
{ text }
</View>
) ) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this improvement!

@@ -62,7 +62,7 @@ export function useFlex( props: WordPressComponentProps< FlexProps, 'div' > ) {

const classes = useMemo( () => {
const base = css( {
alignItems: isColumn ? 'normal' : align,
alignItems: align ?? ( isColumn ? 'normal' : 'center' ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As much as I don't love the arbitrariness of this align prop, I agree with Riad that this new edge behavior for VStack is correct/consistent within the existing logic of the system.

In this system, I understand stretch to apply only to the cross axis, and edge to apply only to the main axis.

How about we tweak the description to make that clearer?

-	 * * `edge`: Aligns content to the edges of the container.
+	 * * `edge`: Justifies content to be evenly spread out up to the main axis edges of the container.
-	 * * `stretch`: Stretches content to the edges of the container.
+	 * * `stretch`: Stretches content to the cross axis edges of the container.

packages/components/src/v-stack/hook.ts Outdated Show resolved Hide resolved
Copy link
Member

@mirka mirka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 🚢

@youknowriad youknowriad merged commit 03ec631 into trunk Mar 2, 2023
@youknowriad youknowriad deleted the fix/vstack-hstack-alignments branch March 2, 2023 06:20
@github-actions github-actions bot added this to the Gutenberg 15.4 milestone Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] UI Components Impacts or related to the UI component system [Package] Components /packages/components [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants