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

Add support for Modifier.flex. #1699

Merged
merged 9 commits into from
Dec 4, 2023
Merged

Add support for Modifier.flex. #1699

merged 9 commits into from
Dec 4, 2023

Conversation

colinrtwhite
Copy link
Member

Adds support for a new flex property to Row and Column. This follows the CSS spec for the flex property. It's a combination property that sets flex-grow, flex-shrink, and flex-basis all at once. Notably, it's useful for creating equivalently sized nodes along the main axis of a row/column.

Copy link
Collaborator

@swankjesse swankjesse left a comment

Choose a reason for hiding this comment

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

Awesome.

How independent are the flex modifiers in HTML? Ie. if I set flex basis does it ignore the value I set for Grow and Shrink?

If they’re fully independent then I don’t like it if flex(1.0) also sets grow and shrink, because we can also set those modifiers afterwards flex(1.0).grow(0.0) and the precedence gets really ugly.

If they’re all coupled then maybe we should have one Modifier object with 3 attributes, and offer accelerator APIs to set each of 'em independently? Like we do with margins?

…layout/compose/LayoutScopes.kt

Co-authored-by: Jake Wharton <[email protected]>
@colinrtwhite
Copy link
Member Author

colinrtwhite commented Nov 23, 2023

@swankjesse Yep, flex overwrites previous values set for flex-grow and flex-shrink. Conversely, setting Modifier.flex(1.0).grow(0.0) will overwrite part of what flex(1.0) set. This isn't super clear in the API unfortunately. I like the idea of combining them (and maybe renaming?). If we do something similar to Margin it could look like this:

data class Flex(
  val grow: Double = Unset, // Create an unset constant to avoid replacing previous values.
  val shrink: Double = Unset,
  val basis: Double = Unset,
)

fun Flex(flex: Double): Flex // Set all 3

Modifier.flex(Flex(1.0)) // Set all 3

Modifier.flex(Flex(grow = 1.0)) // Sets all 3

Also, I've been thinking maybe this property would make more sense named something like weight. Thoughts?

Unfortunately, declaring extensions directly on Modifier loses the compile-time safety we get from ColumnScope/RowScope since we can't have multiple receivers (yet), which is why I'd vote to use a class instead.

@JakeWharton
Copy link
Collaborator

Last wins is a pretty common precedence rule. It will be even more obvious once we switch to the trait nomenclature, hopefully.

In CSS I've certainly done things like border: 1px solid red; border-left-with: 5px; where I want the helper and then to customize something further.

@colinrtwhite colinrtwhite merged commit 1dcb543 into trunk Dec 4, 2023
8 checks passed
@colinrtwhite colinrtwhite deleted the colin/flex branch December 4, 2023 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants