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

ButtonGroup doesn't apply margin when children are different HTML elements #688

Closed
larimaza opened this issue Oct 22, 2020 · 7 comments · Fixed by #822
Closed

ButtonGroup doesn't apply margin when children are different HTML elements #688

larimaza opened this issue Oct 22, 2020 · 7 comments · Fixed by #822
Labels
🐞 bug Something isn't working as it should ⚛️ component Changes to a React component good first issue A beginner-friendly task help wanted Looking for contributions
Milestone

Comments

@larimaza
Copy link
Contributor

Component to amend

ButtonGroup

Visual

First button is an a Both buttons are button
Screen Shot 2020-10-22 at 10 45 31 Screen Shot 2020-10-22 at 10 45 45

Context

The ButtonGroup should apply margins between the buttons inside it for all cases.
However, because of selectors like this:

.css-185ogu-button-group-button-group--right > button:not(:last-of-type), .css-185ogu-button-group-button-group--right > a:not(:last-of-type) {
    margin-bottom: 16px;

...when the first button is an anchor and the other is a button, the margin is not applied, since the a is the last of its type.

The proposal would be to use the broader selector :last-child, since it's unlikely that the ButtonGroup will have another child that isn't an a or a button (and if it has, it's a wrong use of the component).

@connor-baer connor-baer changed the title ButtonGroup doesn't apply margin when one child is an anchor and the next is a button ButtonGroup doesn't apply margin when children are different HTML elements Oct 23, 2020
@connor-baer
Copy link
Member

connor-baer commented Oct 23, 2020

Thanks for the report @larimaza! This is a tricky one: Emotion puts constraints on the use of :first-child and other CSS child selectors (emotion-js/emotion#1178). Circuit UI needs to support SSR, so that's not an option.

The alternative is to iterate over the child elements with React's Children.map, clone each child, and add the styles to them explicitly. The problem here is that Emotion's css prop doesn't work with React's cloneElement function.

Another option would be to insert a spacer div between the buttons. I'm not sure what the limitations would be here 🤔

@connor-baer connor-baer added the 🐞 bug Something isn't working as it should label Oct 23, 2020
@larimaza
Copy link
Contributor Author

Tricky indeed 😬 TBH, I don't love the idea of adding a spacer div any more than I love the idea of customizing margins in usage 🙈

The other solution is also not very nice, but... We could add a margin to every child and then counter the last one's margin in the button group itself.

@connor-baer
Copy link
Member

That's better indeed. Emotion doesn't restrict the :last-child selector. Very clever! 🧠

@connor-baer connor-baer added the help wanted Looking for contributions label Dec 9, 2020
@connor-baer connor-baer added the ⚛️ component Changes to a React component label Jan 29, 2021
@connor-baer connor-baer added this to the v2.x milestone Jan 29, 2021
@connor-baer connor-baer added the good first issue A beginner-friendly task label Feb 24, 2021
@robinjimenez
Copy link
Contributor

@connor-baer I can work on work on this!
I've been checking the code and doing some tests, replacing :last-of-type with :last-child seems to do the trick for both bottom and right margins

@connor-baer
Copy link
Member

@robinjimenez Awesome! 🙌 Could you open a PR with the changes, please?

@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2021

🎉 This issue has been resolved in version 2.4.0-canary.6 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions
Copy link
Contributor

🎉 This issue has been resolved in version 2.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working as it should ⚛️ component Changes to a React component good first issue A beginner-friendly task help wanted Looking for contributions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants