Material-ui: Add Stack component

Created on 3 Nov 2019  路  15Comments  路  Source: mui-org/material-ui

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Sometimes we have a case when we want to give equal spacing i.e. padding/margin between child components where the children are Block components or Inline components

Currently, It is kind of possible with <Grid spacing={}/> but I have to wrap every child component with <Grid item/> inside a <Grid container/> parent. It would be nice If we can have a single parent component for giving equal spacing between children component. Though It can be a different parent component for each Block/Inline children components.

Summary 馃挕

Currently with <Grid />

  • Horizontal Spacing (Block Component)
<Grid container spacing={2} >
    <Grid item>
        <Typography>A</Typography>
    </Grid>
    <Grid item>
        <Typography>A</Typography>
    </Grid>
    <Grid item>
        <Typography>A</Typography>
    </Grid>
</Grid>
  • Vertical Spacing (Block Component)
<Grid container spacing={2} direction="column">
    <Grid item>
        <Typography>A</Typography>
    </Grid>
    <Grid item>
        <Typography>A</Typography>
    </Grid>
    <Grid item>
        <Typography>A</Typography>
    </Grid>
</Grid>

As you can see I have to wrap every <Typography /> inside a <Grid item/> component. And There is no way of distributing space between inline components.

Motivation 馃敠

What I want to achieve

  • Vertical Spacing (Block Component)
<Stack spacing={2}>
    <Typography>A</Typography>
    <Typography>A</Typography>
    <Typography>A</Typography>
</Stack>

// or with `@material-ui/system` api
<Stack spacing={{ xs: 2, sm: 3, md: 4 }}>
    <Typography>A</Typography>
    <Typography>A</Typography>
    <Typography>A</Typography>
</Stack>
  • Horizontal Spacing (Block Component)
<Stack spacing={2} direction="row">
    <Typography>A</Typography>
    <Typography>A</Typography>
    <Typography>A</Typography>
</Stack>

// or with `@material-ui/system` api
<Stack spacing={{ xs: 2, sm: 3, md: 4 }} direction="row">
    <Typography>A</Typography>
    <Typography>A</Typography>
    <Typography>A</Typography>
</Stack>
  • Inline Components
<Inline spacing={2} >
    <Typography>A</Typography>
    <Typography>A</Typography>
    <Typography>A</Typography>
</Stack>

// or with `@material-ui/system` api
<Inline spacing={{ xs: 2, sm: 3, md: 4 }} >
    <Typography>A</Typography>
    <Typography>A</Typography>
    <Typography>A</Typography>
</Stack>

I don't know If the same can be achieve from the current components set or #15561 makes this redundant. But I am certain that It can find some use cases.

Benchmark

enhancement important

Most helpful comment

So then it's a question of whether, for a relatively low cost of development, it provides sufficient utility to be worth including. I would put it in the same category as Container - something which developers can do themselves, but which solves a frequent enough pattern to be worthwhile.

All 15 comments

@vkasraj The idea is interesting, thanks for the suggestion.

Thanks! :smile:

We would leverage such components in our demos, to remove custom styles. cc @mbrookes.

I like the concept. Framer provides something similar as one of its built-in components:

image

image

Here's a rough-and-ready POC using Grid:

https://codesandbox.io/s/material-demo-xs8fq

Another example:
https://developer.microsoft.com/en-us/fabric/#/controls/web/stack

@oliviertassinari Worth pursuing in the lab?

So then it's a question of whether, for a relatively low cost of development, it provides sufficient utility to be worth including. I would put it in the same category as Container - something which developers can do themselves, but which solves a frequent enough pattern to be worthwhile.

@mbrookes Yeah, I think that its the kind of experimentation that we should try in the lab, and see the adoption. I would also propose that we dodge food with as many of our demos as possible.

Great! I am ready to test whenever it is available.

Just want to ask, Is the Stack component can be used for wrapping Inline elements or is it just for block elements? 馃

@vkasraj I think that we could use both. Would you be interested in working on such component?

@oliviertassinari Thanks for this opportunity but I am not that familiar with source code :disappointed:. It would be great If you guys work on it. :sweat_smile:

Hey
Can I tackle this component development?

@Vitao18 Feel free to give it a go :)

Once you have a proof of concept could you open a draft PR? It allows others to provide feedback easily and see the progress.

@mbrookes Was looking at it, but I don't know if he has made any progress. He asked me if I had some recommendation, from the quick look at the problem I could have:

  • Don't use the Grid component
  • Try not to use cloneElement, nor Children.map
  • Develop it using the demos use cases in mind
  • CSS grid might not be the best tool for the task
Was this page helpful?
0 / 5 - 0 ratings