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

feat(@theme-ui/components) Add a Stack component based on Flex layout #850

Closed

Conversation

janhoogeveen
Copy link

@janhoogeveen janhoogeveen commented Apr 16, 2020

This PR is an attempt at solving #556

image

  • Working component
  • Finish documentation
  • Add tests

Prior art
I've taken inspiration from https://github.com/siddharthkp/react-ui/blob/master/packages/react-ui/src/components/stack/index.js

Technical approach
This solution uses Flexbox to create stack layouts. I was afraid that by using css selectors you wouldn't be able to override margins anymore, but turns out you can quite easily work your way around it.

For example, you can use a negative value in your child component to negate the gap property:

image

Tradeoffs
Flex components are more complex than a simple grid component.

Alternative approach
This is an alternative, more powerful version of #809

@janhoogeveen janhoogeveen changed the title Feature/stack flex feat(@theme-ui/components) Add a Stack component based on Flex layout Apr 16, 2020
@janhoogeveen janhoogeveen marked this pull request as ready for review May 13, 2020 21:16
@janhoogeveen
Copy link
Author

Documentation is done, the component seems to work as expected and I've added tests.

All yours @jxnblk!

@jxnblk
Copy link
Member

jxnblk commented May 15, 2020

@janhoogeveen sweet! thanks for working on this! I'll try to give this a review over the next few days


export const merge = (...objs) => {
return objs.reduce(function(merged, currentValue) {
return mergeTwo(merged, currentValue)

Choose a reason for hiding this comment

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

possibly jumping in w/o enough context, but where is mergeTwo coming from in this case?

@hasparus hasparus mentioned this pull request Jun 23, 2020
32 tasks
Comment on lines +48 to +51
styles['> *:not(:last-child)'] = direction.map(d => createSpace(d, space))
} else {
styles.flexDirection = direction === 'vertical' ? 'column' : 'row'
styles['> *:not(:last-child)'] = createSpace(direction, space)

Choose a reason for hiding this comment

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

I think it would be better to use the lobotomized owl selector (> * + *) instead here so it would not override any margin defined on the children.

Here's an example.

Copy link
Contributor

@joe-bell joe-bell Jul 4, 2020

Choose a reason for hiding this comment

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

I disagree, I think adding any complex selectors (either this current implementation or the lobotomized owl) here is dangerous to be honest.

A lot of Theme UI is based on the Box primitive and anything like this is going to lead to specificity issues

Copy link

@TheMightyPenguin TheMightyPenguin Aug 2, 2020

Choose a reason for hiding this comment

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

Something simimlar to what Braid does could be done, they add a bottom spacing to each item (using padding), and add a negative margin using the same spacing to the container.

Reference:

And you can inspect an example on their playroom

With this approach no selectors would be needed!

Copy link
Contributor

Choose a reason for hiding this comment

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

This is the approach I'm using in raam too and works rather nicely (although it needs a tidy up)

@siddharthkp
Copy link

whoops, saw my repo in the description as inspiration

please don't take the stack implementation from react-ui, it's doesn't follow the spec proposal (yet)

further reading if you're curious: https://sid.st/unpolished/flex-gap-polyfill/

@joe-bell
Copy link
Contributor

joe-bell commented Jul 4, 2020

Although I really appreciate the work put into this, using > * selectors feels out of place with Theme UI for me; it could cause specificity issues and may also impact SSR too.

Side note: If anyone's coming here looking for a Stack for Theme UI in the meantime, I built raam for this exact purpose.

@lachlanjc lachlanjc added the enhancement New feature or request label Dec 3, 2020
@lachlanjc lachlanjc linked an issue Dec 3, 2020 that may be closed by this pull request
@hasparus hasparus closed this Dec 14, 2020
@hasparus hasparus deleted the branch system-ui:develop December 14, 2020 08:53
@hasparus hasparus reopened this Dec 14, 2020
@vercel
Copy link

vercel bot commented Dec 14, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/systemui/theme-ui/8i79v3pt3
✅ Preview: Failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add gap prop to Flex component
9 participants